This patch changes the link-order behaviour of tcc (the Tiny C Compiler [1]). Currently, the system paths are searched before the -L options, and the entire path is searched for dynamic libraries, then searched again for static ones. This means that a dynamic library will always be used, even if it is in the link order after a matching static one. This patch changes the behaviour to that of gcc, so that "-L" link paths are searched before built-in ones, and each directory in the path is searched in turn for both dynamic and static libraries. -Darren Tucker (2004-04-15). [1] http://www.tinycc.org/ --- tcc-0.9.20/tcc.c.orig 2004-04-15 12:15:11.000000000 +1000 +++ tcc-0.9.20/tcc.c 2004-04-16 11:24:09.000000000 +1000 @@ -960,6 +960,26 @@ *nb_ptr = nb; } +/* insert new entry at beginning of array */ +static void dynarray_insert(void ***ptab, int *nb_ptr, void *data) +{ + int nb; + void **pp, *last; + + dynarray_add(ptab, nb_ptr, data); + nb = *nb_ptr; + pp = *ptab; + + if (nb == 1) + return; + + /* shuffle all entries down 1 then insert last entry first */ + last = pp[--nb]; + for (; nb >= 0; --nb) + pp[nb+1] = pp[nb]; + pp[0] = last; +} + Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags) { Section *sec; @@ -9220,9 +9240,9 @@ tcc_define_symbol(s, "__WCHAR_TYPE__", "int"); /* default library paths */ - tcc_add_library_path(s, "/usr/local/lib"); - tcc_add_library_path(s, "/usr/lib"); tcc_add_library_path(s, "/lib"); + tcc_add_library_path(s, "/usr/lib"); + tcc_add_library_path(s, "/usr/local/lib"); /* no section zero */ dynarray_add((void ***)&s->sections, &s->nb_sections, NULL); @@ -9420,7 +9440,7 @@ char *pathname1; pathname1 = tcc_strdup(pathname); - dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1); + dynarray_insert((void ***)&s->library_paths, &s->nb_library_paths, pathname1); return 0; } @@ -9446,15 +9466,17 @@ char buf[1024]; int i; - /* first we look for the dynamic library if not static linking */ - if (!s->static_link) { - snprintf(buf, sizeof(buf), "lib%s.so", libraryname); - if (tcc_add_dll(s, buf, 0) == 0) - return 0; - } - - /* then we look for the static library */ + /* step through library path */ for(i = 0; i < s->nb_library_paths; i++) { + /* first try dynamic library unless we are staticly linking */ + if (!s->static_link) { + snprintf(buf, sizeof(buf), "%s/lib%s.so", + s->library_paths[i], libraryname); + if (tcc_add_file_internal(s, buf, 0) == 0) + return 0; + } + + /* then we look for the static library */ snprintf(buf, sizeof(buf), "%s/lib%s.a", s->library_paths[i], libraryname); if (tcc_add_file_internal(s, buf, 0) == 0)