ldelf.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  1. /* ELF emulation code for targets using elf.em.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libiberty.h"
  19. #include "filenames.h"
  20. #include "safe-ctype.h"
  21. #include "bfdlink.h"
  22. #include "ctf-api.h"
  23. #include "ld.h"
  24. #include "ldmain.h"
  25. #include "ldmisc.h"
  26. #include "ldexp.h"
  27. #include "ldlang.h"
  28. #include "ldfile.h"
  29. #include "ldemul.h"
  30. #include "ldbuildid.h"
  31. #include <ldgram.h>
  32. #include "elf-bfd.h"
  33. #ifdef HAVE_GLOB
  34. #include <glob.h>
  35. #endif
  36. #include "ldelf.h"
  37. struct dt_needed
  38. {
  39. bfd *by;
  40. const char *name;
  41. };
  42. /* Style of .note.gnu.build-id section. */
  43. const char *ldelf_emit_note_gnu_build_id;
  44. /* These variables are required to pass information back and forth
  45. between after_open and check_needed and stat_needed and vercheck. */
  46. static struct bfd_link_needed_list *global_needed;
  47. static lang_input_statement_type *global_found;
  48. static struct stat global_stat;
  49. static struct bfd_link_needed_list *global_vercheck_needed;
  50. static bool global_vercheck_failed;
  51. void
  52. ldelf_after_parse (void)
  53. {
  54. if (bfd_link_pie (&link_info))
  55. link_info.flags_1 |= (bfd_vma) DF_1_PIE;
  56. if (bfd_link_executable (&link_info)
  57. && link_info.nointerp)
  58. {
  59. if (link_info.dynamic_undefined_weak > 0)
  60. einfo (_("%P: warning: -z dynamic-undefined-weak ignored\n"));
  61. link_info.dynamic_undefined_weak = 0;
  62. }
  63. /* Disable DT_RELR if not building PIE nor shared library. */
  64. if (!bfd_link_pic (&link_info))
  65. link_info.enable_dt_relr = 0;
  66. /* Add 3 spare tags for DT_RELR, DT_RELRSZ and DT_RELRENT. */
  67. if (link_info.enable_dt_relr)
  68. link_info.spare_dynamic_tags += 3;
  69. after_parse_default ();
  70. if (link_info.commonpagesize > link_info.maxpagesize)
  71. {
  72. if (!link_info.commonpagesize_is_set)
  73. link_info.commonpagesize = link_info.maxpagesize;
  74. else if (!link_info.maxpagesize_is_set)
  75. link_info.maxpagesize = link_info.commonpagesize;
  76. else
  77. einfo (_("%F%P: common page size (0x%v) > maximum page size (0x%v)\n"),
  78. link_info.commonpagesize, link_info.maxpagesize);
  79. }
  80. }
  81. /* Handle the generation of DT_NEEDED tags. */
  82. bool
  83. ldelf_load_symbols (lang_input_statement_type *entry)
  84. {
  85. int link_class = 0;
  86. /* Tell the ELF linker that we don't want the output file to have a
  87. DT_NEEDED entry for this file, unless it is used to resolve
  88. references in a regular object. */
  89. if (entry->flags.add_DT_NEEDED_for_regular)
  90. link_class = DYN_AS_NEEDED;
  91. /* Tell the ELF linker that we don't want the output file to have a
  92. DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
  93. this file at all. */
  94. if (!entry->flags.add_DT_NEEDED_for_dynamic)
  95. link_class |= DYN_NO_ADD_NEEDED;
  96. if (entry->flags.just_syms
  97. && (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) != 0)
  98. einfo (_("%F%P: %pB: --just-symbols may not be used on DSO\n"),
  99. entry->the_bfd);
  100. if (link_class == 0
  101. || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0)
  102. return false;
  103. bfd_elf_set_dyn_lib_class (entry->the_bfd,
  104. (enum dynamic_lib_link_class) link_class);
  105. /* Continue on with normal load_symbols processing. */
  106. return false;
  107. }
  108. /* On Linux, it's possible to have different versions of the same
  109. shared library linked against different versions of libc. The
  110. dynamic linker somehow tags which libc version to use in
  111. /etc/ld.so.cache, and, based on the libc that it sees in the
  112. executable, chooses which version of the shared library to use.
  113. We try to do a similar check here by checking whether this shared
  114. library needs any other shared libraries which may conflict with
  115. libraries we have already included in the link. If it does, we
  116. skip it, and try to find another shared library farther on down the
  117. link path.
  118. This is called via lang_for_each_input_file.
  119. GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
  120. which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
  121. a conflicting version. */
  122. static void
  123. ldelf_vercheck (lang_input_statement_type *s)
  124. {
  125. const char *soname;
  126. struct bfd_link_needed_list *l;
  127. if (global_vercheck_failed)
  128. return;
  129. if (s->the_bfd == NULL
  130. || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
  131. return;
  132. soname = bfd_elf_get_dt_soname (s->the_bfd);
  133. if (soname == NULL)
  134. soname = lbasename (bfd_get_filename (s->the_bfd));
  135. for (l = global_vercheck_needed; l != NULL; l = l->next)
  136. {
  137. const char *suffix;
  138. if (filename_cmp (soname, l->name) == 0)
  139. {
  140. /* Probably can't happen, but it's an easy check. */
  141. continue;
  142. }
  143. if (strchr (l->name, '/') != NULL)
  144. continue;
  145. suffix = strstr (l->name, ".so.");
  146. if (suffix == NULL)
  147. continue;
  148. suffix += sizeof ".so." - 1;
  149. if (filename_ncmp (soname, l->name, suffix - l->name) == 0)
  150. {
  151. /* Here we know that S is a dynamic object FOO.SO.VER1, and
  152. the object we are considering needs a dynamic object
  153. FOO.SO.VER2, and VER1 and VER2 are different. This
  154. appears to be a version mismatch, so we tell the caller
  155. to try a different version of this library. */
  156. global_vercheck_failed = true;
  157. return;
  158. }
  159. }
  160. }
  161. /* See if an input file matches a DT_NEEDED entry by running stat on
  162. the file. */
  163. static void
  164. ldelf_stat_needed (lang_input_statement_type *s)
  165. {
  166. struct stat st;
  167. const char *suffix;
  168. const char *soname;
  169. if (global_found != NULL)
  170. return;
  171. if (s->the_bfd == NULL)
  172. return;
  173. /* If this input file was an as-needed entry, and wasn't found to be
  174. needed at the stage it was linked, then don't say we have loaded it. */
  175. if ((bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
  176. return;
  177. if (bfd_stat (s->the_bfd, &st) != 0)
  178. {
  179. einfo (_("%P: %pB: bfd_stat failed: %E\n"), s->the_bfd);
  180. return;
  181. }
  182. /* Some operating systems, e.g. Windows, do not provide a meaningful
  183. st_ino; they always set it to zero. (Windows does provide a
  184. meaningful st_dev.) Do not indicate a duplicate library in that
  185. case. While there is no guarantee that a system that provides
  186. meaningful inode numbers will never set st_ino to zero, this is
  187. merely an optimization, so we do not need to worry about false
  188. negatives. */
  189. if (st.st_dev == global_stat.st_dev
  190. && st.st_ino == global_stat.st_ino
  191. && st.st_ino != 0)
  192. {
  193. global_found = s;
  194. return;
  195. }
  196. /* We issue a warning if it looks like we are including two
  197. different versions of the same shared library. For example,
  198. there may be a problem if -lc picks up libc.so.6 but some other
  199. shared library has a DT_NEEDED entry of libc.so.5. This is a
  200. heuristic test, and it will only work if the name looks like
  201. NAME.so.VERSION. FIXME: Depending on file names is error-prone.
  202. If we really want to issue warnings about mixing version numbers
  203. of shared libraries, we need to find a better way. */
  204. if (strchr (global_needed->name, '/') != NULL)
  205. return;
  206. suffix = strstr (global_needed->name, ".so.");
  207. if (suffix == NULL)
  208. return;
  209. suffix += sizeof ".so." - 1;
  210. soname = bfd_elf_get_dt_soname (s->the_bfd);
  211. if (soname == NULL)
  212. soname = lbasename (s->filename);
  213. if (filename_ncmp (soname, global_needed->name,
  214. suffix - global_needed->name) == 0)
  215. einfo (_("%P: warning: %s, needed by %pB, may conflict with %s\n"),
  216. global_needed->name, global_needed->by, soname);
  217. }
  218. /* This function is called for each possible name for a dynamic object
  219. named by a DT_NEEDED entry. The FORCE parameter indicates whether
  220. to skip the check for a conflicting version. */
  221. static bool
  222. ldelf_try_needed (struct dt_needed *needed, int force, int is_linux)
  223. {
  224. bfd *abfd;
  225. const char *name = needed->name;
  226. const char *soname;
  227. int link_class;
  228. abfd = bfd_openr (name, bfd_get_target (link_info.output_bfd));
  229. if (abfd == NULL)
  230. {
  231. if (verbose)
  232. info_msg (_("attempt to open %s failed\n"), name);
  233. return false;
  234. }
  235. track_dependency_files (name);
  236. /* Linker needs to decompress sections. */
  237. abfd->flags |= BFD_DECOMPRESS;
  238. if (! bfd_check_format (abfd, bfd_object))
  239. {
  240. bfd_close (abfd);
  241. return false;
  242. }
  243. if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
  244. {
  245. bfd_close (abfd);
  246. return false;
  247. }
  248. /* For DT_NEEDED, they have to match. */
  249. if (abfd->xvec != link_info.output_bfd->xvec)
  250. {
  251. bfd_close (abfd);
  252. return false;
  253. }
  254. /* Check whether this object would include any conflicting library
  255. versions. If FORCE is set, then we skip this check; we use this
  256. the second time around, if we couldn't find any compatible
  257. instance of the shared library. */
  258. if (!force)
  259. {
  260. struct bfd_link_needed_list *needs;
  261. if (! bfd_elf_get_bfd_needed_list (abfd, &needs))
  262. einfo (_("%F%P: %pB: bfd_elf_get_bfd_needed_list failed: %E\n"), abfd);
  263. if (needs != NULL)
  264. {
  265. global_vercheck_needed = needs;
  266. global_vercheck_failed = false;
  267. lang_for_each_input_file (ldelf_vercheck);
  268. if (global_vercheck_failed)
  269. {
  270. bfd_close (abfd);
  271. /* Return FALSE to force the caller to move on to try
  272. another file on the search path. */
  273. return false;
  274. }
  275. /* But wait! It gets much worse. On Linux, if a shared
  276. library does not use libc at all, we are supposed to skip
  277. it the first time around in case we encounter a shared
  278. library later on with the same name which does use the
  279. version of libc that we want. This is much too horrible
  280. to use on any system other than Linux. */
  281. if (is_linux)
  282. {
  283. struct bfd_link_needed_list *l;
  284. for (l = needs; l != NULL; l = l->next)
  285. if (startswith (l->name, "libc.so"))
  286. break;
  287. if (l == NULL)
  288. {
  289. bfd_close (abfd);
  290. return false;
  291. }
  292. }
  293. }
  294. }
  295. /* We've found a dynamic object matching the DT_NEEDED entry. */
  296. /* We have already checked that there is no other input file of the
  297. same name. We must now check again that we are not including the
  298. same file twice. We need to do this because on many systems
  299. libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
  300. reference libc.so.1. If we have already included libc.so, we
  301. don't want to include libc.so.1 if they are the same file, and we
  302. can only check that using stat. */
  303. if (bfd_stat (abfd, &global_stat) != 0)
  304. einfo (_("%F%P: %pB: bfd_stat failed: %E\n"), abfd);
  305. /* First strip off everything before the last '/'. */
  306. soname = lbasename (bfd_get_filename (abfd));
  307. if (verbose)
  308. info_msg (_("found %s at %s\n"), soname, name);
  309. global_found = NULL;
  310. lang_for_each_input_file (ldelf_stat_needed);
  311. if (global_found != NULL)
  312. {
  313. /* Return TRUE to indicate that we found the file, even though
  314. we aren't going to do anything with it. */
  315. return true;
  316. }
  317. /* Specify the soname to use. */
  318. bfd_elf_set_dt_needed_name (abfd, soname);
  319. /* Tell the ELF linker that we don't want the output file to have a
  320. DT_NEEDED entry for this file, unless it is used to resolve
  321. references in a regular object. */
  322. link_class = DYN_DT_NEEDED;
  323. /* Tell the ELF linker that we don't want the output file to have a
  324. DT_NEEDED entry for this file at all if the entry is from a file
  325. with DYN_NO_ADD_NEEDED. */
  326. if (needed->by != NULL
  327. && (bfd_elf_get_dyn_lib_class (needed->by) & DYN_NO_ADD_NEEDED) != 0)
  328. link_class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED;
  329. bfd_elf_set_dyn_lib_class (abfd, (enum dynamic_lib_link_class) link_class);
  330. *link_info.input_bfds_tail = abfd;
  331. link_info.input_bfds_tail = &abfd->link.next;
  332. /* Add this file into the symbol table. */
  333. if (! bfd_link_add_symbols (abfd, &link_info))
  334. einfo (_("%F%P: %pB: error adding symbols: %E\n"), abfd);
  335. return true;
  336. }
  337. /* Search for a needed file in a path. */
  338. static bool
  339. ldelf_search_needed (const char *path, struct dt_needed *n, int force,
  340. int is_linux, int elfsize)
  341. {
  342. const char *s;
  343. const char *name = n->name;
  344. size_t len;
  345. struct dt_needed needed;
  346. if (name[0] == '/')
  347. return ldelf_try_needed (n, force, is_linux);
  348. if (path == NULL || *path == '\0')
  349. return false;
  350. needed.by = n->by;
  351. needed.name = n->name;
  352. len = strlen (name);
  353. while (1)
  354. {
  355. unsigned offset = 0;
  356. char * var;
  357. char *filename, *sset;
  358. s = strchr (path, config.rpath_separator);
  359. if (s == NULL)
  360. s = path + strlen (path);
  361. #if HAVE_DOS_BASED_FILE_SYSTEM
  362. /* Assume a match on the second char is part of drive specifier. */
  363. else if (config.rpath_separator == ':'
  364. && s == path + 1
  365. && ISALPHA (*path))
  366. {
  367. s = strchr (s + 1, config.rpath_separator);
  368. if (s == NULL)
  369. s = path + strlen (path);
  370. }
  371. #endif
  372. filename = (char *) xmalloc (s - path + len + 2);
  373. if (s == path)
  374. sset = filename;
  375. else
  376. {
  377. memcpy (filename, path, s - path);
  378. filename[s - path] = '/';
  379. sset = filename + (s - path) + 1;
  380. }
  381. strcpy (sset, name);
  382. /* PR 20535: Support the same pseudo-environment variables that
  383. are supported by ld.so. Namely, $ORIGIN, $LIB and $PLATFORM.
  384. Since there can be more than one occurrence of these tokens in
  385. the path we loop until no more are found. Since we might not
  386. be able to substitute some of the tokens we maintain an offset
  387. into the filename for where we should begin our scan. */
  388. while ((var = strchr (filename + offset, '$')) != NULL)
  389. {
  390. /* The ld.so manual page does not say, but I am going to assume that
  391. these tokens are terminated by a directory separator character
  392. (/) or the end of the string. There is also an implication that
  393. $ORIGIN should only be used at the start of a path, but that is
  394. not enforced here.
  395. The ld.so manual page also states that it allows ${ORIGIN},
  396. ${LIB} and ${PLATFORM}, so these are supported as well.
  397. FIXME: The code could be a lot cleverer about allocating space
  398. for the processed string. */
  399. char * end = strchr (var, '/');
  400. const char *replacement = NULL;
  401. char * v = var + 1;
  402. char * freeme = NULL;
  403. unsigned flen = strlen (filename);
  404. if (end != NULL)
  405. /* Temporarily terminate the filename at the end of the token. */
  406. * end = 0;
  407. if (*v == '{')
  408. ++ v;
  409. switch (*v++)
  410. {
  411. case 'O':
  412. if (strcmp (v, "RIGIN") == 0 || strcmp (v, "RIGIN}") == 0)
  413. {
  414. /* ORIGIN - replace with the full path to the directory
  415. containing the program or shared object. */
  416. if (needed.by == NULL)
  417. {
  418. if (link_info.output_bfd == NULL)
  419. {
  420. break;
  421. }
  422. else
  423. replacement = bfd_get_filename (link_info.output_bfd);
  424. }
  425. else
  426. replacement = bfd_get_filename (needed.by);
  427. if (replacement)
  428. {
  429. char * slash;
  430. if (replacement[0] == '/')
  431. freeme = xstrdup (replacement);
  432. else
  433. {
  434. char * current_dir = getpwd ();
  435. freeme = xmalloc (strlen (replacement)
  436. + strlen (current_dir) + 2);
  437. sprintf (freeme, "%s/%s", current_dir, replacement);
  438. }
  439. replacement = freeme;
  440. if ((slash = strrchr (replacement, '/')) != NULL)
  441. * slash = 0;
  442. }
  443. }
  444. break;
  445. case 'L':
  446. if (strcmp (v, "IB") == 0 || strcmp (v, "IB}") == 0)
  447. {
  448. /* LIB - replace with "lib" in 32-bit environments
  449. and "lib64" in 64-bit environments. */
  450. switch (elfsize)
  451. {
  452. case 32: replacement = "lib"; break;
  453. case 64: replacement = "lib64"; break;
  454. default:
  455. abort ();
  456. }
  457. }
  458. break;
  459. case 'P':
  460. /* Supporting $PLATFORM in a cross-hosted environment is not
  461. possible. Supporting it in a native environment involves
  462. loading the <sys/auxv.h> header file which loads the
  463. system <elf.h> header file, which conflicts with the
  464. "include/elf/mips.h" header file. */
  465. /* Fall through. */
  466. default:
  467. break;
  468. }
  469. if (replacement)
  470. {
  471. char * filename2 = xmalloc (flen + strlen (replacement));
  472. if (end)
  473. {
  474. sprintf (filename2, "%.*s%s/%s",
  475. (int)(var - filename), filename,
  476. replacement, end + 1);
  477. offset = (var - filename) + 1 + strlen (replacement);
  478. }
  479. else
  480. {
  481. sprintf (filename2, "%.*s%s",
  482. (int)(var - filename), filename,
  483. replacement);
  484. offset = var - filename + strlen (replacement);
  485. }
  486. free (filename);
  487. filename = filename2;
  488. /* There is no need to restore the path separator (when
  489. end != NULL) as we have replaced the entire string. */
  490. }
  491. else
  492. {
  493. if (verbose)
  494. /* We only issue an "unrecognised" message in verbose mode
  495. as the $<foo> token might be a legitimate component of
  496. a path name in the target's file system. */
  497. info_msg (_("unrecognised or unsupported token "
  498. "'%s' in search path\n"), var);
  499. if (end)
  500. /* Restore the path separator. */
  501. * end = '/';
  502. /* PR 20784: Make sure that we resume the scan *after*
  503. the token that we could not replace. */
  504. offset = (var + 1) - filename;
  505. }
  506. free (freeme);
  507. }
  508. needed.name = filename;
  509. if (ldelf_try_needed (&needed, force, is_linux))
  510. return true;
  511. free (filename);
  512. if (*s == '\0')
  513. break;
  514. path = s + 1;
  515. }
  516. return false;
  517. }
  518. /* Prefix the sysroot to absolute paths in PATH, a string containing
  519. paths separated by config.rpath_separator. If running on a DOS
  520. file system, paths containing a drive spec won't have the sysroot
  521. prefix added, unless the sysroot also specifies the same drive. */
  522. static const char *
  523. ldelf_add_sysroot (const char *path)
  524. {
  525. size_t len, extra;
  526. const char *p;
  527. char *ret, *q;
  528. int dos_drive_sysroot = HAS_DRIVE_SPEC (ld_sysroot);
  529. len = strlen (ld_sysroot);
  530. for (extra = 0, p = path; ; )
  531. {
  532. int dos_drive = HAS_DRIVE_SPEC (p);
  533. if (dos_drive)
  534. p += 2;
  535. if (IS_DIR_SEPARATOR (*p)
  536. && (!dos_drive
  537. || (dos_drive_sysroot
  538. && ld_sysroot[0] == p[-2])))
  539. {
  540. if (dos_drive && dos_drive_sysroot)
  541. extra += len - 2;
  542. else
  543. extra += len;
  544. }
  545. p = strchr (p, config.rpath_separator);
  546. if (!p)
  547. break;
  548. ++p;
  549. }
  550. ret = xmalloc (strlen (path) + extra + 1);
  551. for (q = ret, p = path; ; )
  552. {
  553. const char *end;
  554. int dos_drive = HAS_DRIVE_SPEC (p);
  555. if (dos_drive)
  556. {
  557. *q++ = *p++;
  558. *q++ = *p++;
  559. }
  560. if (IS_DIR_SEPARATOR (*p)
  561. && (!dos_drive
  562. || (dos_drive_sysroot
  563. && ld_sysroot[0] == p[-2])))
  564. {
  565. if (dos_drive && dos_drive_sysroot)
  566. {
  567. strcpy (q, ld_sysroot + 2);
  568. q += len - 2;
  569. }
  570. else
  571. {
  572. strcpy (q, ld_sysroot);
  573. q += len;
  574. }
  575. }
  576. end = strchr (p, config.rpath_separator);
  577. if (end)
  578. {
  579. size_t n = end - p + 1;
  580. strncpy (q, p, n);
  581. q += n;
  582. p += n;
  583. }
  584. else
  585. {
  586. strcpy (q, p);
  587. break;
  588. }
  589. }
  590. return ret;
  591. }
  592. /* Read the system search path the FreeBSD way rather than the Linux way. */
  593. #ifdef HAVE_ELF_HINTS_H
  594. #include <elf-hints.h>
  595. #else
  596. #include "elf-hints-local.h"
  597. #endif
  598. static bool
  599. ldelf_check_ld_elf_hints (const struct bfd_link_needed_list *l, int force,
  600. int elfsize)
  601. {
  602. static bool initialized;
  603. static const char *ld_elf_hints;
  604. struct dt_needed needed;
  605. if (!initialized)
  606. {
  607. FILE *f;
  608. char *tmppath;
  609. tmppath = concat (ld_sysroot, _PATH_ELF_HINTS, (const char *) NULL);
  610. f = fopen (tmppath, FOPEN_RB);
  611. free (tmppath);
  612. if (f != NULL)
  613. {
  614. struct elfhints_hdr hdr;
  615. if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr)
  616. && hdr.magic == ELFHINTS_MAGIC
  617. && hdr.version == 1)
  618. {
  619. if (fseek (f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
  620. {
  621. char *b;
  622. b = xmalloc (hdr.dirlistlen + 1);
  623. if (fread (b, 1, hdr.dirlistlen + 1, f) ==
  624. hdr.dirlistlen + 1)
  625. ld_elf_hints = ldelf_add_sysroot (b);
  626. free (b);
  627. }
  628. }
  629. fclose (f);
  630. }
  631. initialized = true;
  632. }
  633. if (ld_elf_hints == NULL)
  634. return false;
  635. needed.by = l->by;
  636. needed.name = l->name;
  637. return ldelf_search_needed (ld_elf_hints, &needed, force, false, elfsize);
  638. }
  639. /* For a native linker, check the file /etc/ld.so.conf for directories
  640. in which we may find shared libraries. /etc/ld.so.conf is really
  641. only meaningful on Linux. */
  642. struct ldelf_ld_so_conf
  643. {
  644. char *path;
  645. size_t len, alloc;
  646. };
  647. static bool
  648. ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf *, const char *);
  649. static void
  650. ldelf_parse_ld_so_conf_include (struct ldelf_ld_so_conf *info,
  651. const char *filename,
  652. const char *pattern)
  653. {
  654. char *newp = NULL;
  655. #ifdef HAVE_GLOB
  656. glob_t gl;
  657. #endif
  658. if (pattern[0] != '/')
  659. {
  660. char *p = strrchr (filename, '/');
  661. size_t patlen = strlen (pattern) + 1;
  662. newp = xmalloc (p - filename + 1 + patlen);
  663. memcpy (newp, filename, p - filename + 1);
  664. memcpy (newp + (p - filename + 1), pattern, patlen);
  665. pattern = newp;
  666. }
  667. #ifdef HAVE_GLOB
  668. if (glob (pattern, 0, NULL, &gl) == 0)
  669. {
  670. size_t i;
  671. for (i = 0; i < gl.gl_pathc; ++i)
  672. ldelf_parse_ld_so_conf (info, gl.gl_pathv[i]);
  673. globfree (&gl);
  674. }
  675. #else
  676. /* If we do not have glob, treat the pattern as a literal filename. */
  677. ldelf_parse_ld_so_conf (info, pattern);
  678. #endif
  679. free (newp);
  680. }
  681. static bool
  682. ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf *info, const char *filename)
  683. {
  684. FILE *f = fopen (filename, FOPEN_RT);
  685. char *line;
  686. size_t linelen;
  687. if (f == NULL)
  688. return false;
  689. linelen = 256;
  690. line = xmalloc (linelen);
  691. do
  692. {
  693. char *p = line, *q;
  694. /* Normally this would use getline(3), but we need to be portable. */
  695. while ((q = fgets (p, linelen - (p - line), f)) != NULL
  696. && strlen (q) == linelen - (p - line) - 1
  697. && line[linelen - 2] != '\n')
  698. {
  699. line = xrealloc (line, 2 * linelen);
  700. p = line + linelen - 1;
  701. linelen += linelen;
  702. }
  703. if (q == NULL && p == line)
  704. break;
  705. p = strchr (line, '\n');
  706. if (p)
  707. *p = '\0';
  708. /* Because the file format does not know any form of quoting we
  709. can search forward for the next '#' character and if found
  710. make it terminating the line. */
  711. p = strchr (line, '#');
  712. if (p)
  713. *p = '\0';
  714. /* Remove leading whitespace. NUL is no whitespace character. */
  715. p = line;
  716. while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
  717. ++p;
  718. /* If the line is blank it is ignored. */
  719. if (p[0] == '\0')
  720. continue;
  721. if (startswith (p, "include") && (p[7] == ' ' || p[7] == '\t'))
  722. {
  723. char *dir, c;
  724. p += 8;
  725. do
  726. {
  727. while (*p == ' ' || *p == '\t')
  728. ++p;
  729. if (*p == '\0')
  730. break;
  731. dir = p;
  732. while (*p != ' ' && *p != '\t' && *p)
  733. ++p;
  734. c = *p;
  735. *p++ = '\0';
  736. if (dir[0] != '\0')
  737. ldelf_parse_ld_so_conf_include (info, filename, dir);
  738. }
  739. while (c != '\0');
  740. }
  741. else
  742. {
  743. char *dir = p;
  744. while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f'
  745. && *p != '\r' && *p != '\v')
  746. ++p;
  747. while (p != dir && p[-1] == '/')
  748. --p;
  749. if (info->path == NULL)
  750. {
  751. info->alloc = p - dir + 1 + 256;
  752. info->path = xmalloc (info->alloc);
  753. info->len = 0;
  754. }
  755. else
  756. {
  757. if (info->len + 1 + (p - dir) >= info->alloc)
  758. {
  759. info->alloc += p - dir + 256;
  760. info->path = xrealloc (info->path, info->alloc);
  761. }
  762. info->path[info->len++] = config.rpath_separator;
  763. }
  764. memcpy (info->path + info->len, dir, p - dir);
  765. info->len += p - dir;
  766. info->path[info->len] = '\0';
  767. }
  768. }
  769. while (! feof (f));
  770. free (line);
  771. fclose (f);
  772. return true;
  773. }
  774. static bool
  775. ldelf_check_ld_so_conf (const struct bfd_link_needed_list *l, int force,
  776. int elfsize, const char *prefix)
  777. {
  778. static bool initialized;
  779. static const char *ld_so_conf;
  780. struct dt_needed needed;
  781. if (! initialized)
  782. {
  783. char *tmppath;
  784. struct ldelf_ld_so_conf info;
  785. info.path = NULL;
  786. info.len = info.alloc = 0;
  787. tmppath = concat (ld_sysroot, prefix, "/etc/ld.so.conf",
  788. (const char *) NULL);
  789. if (!ldelf_parse_ld_so_conf (&info, tmppath))
  790. {
  791. free (tmppath);
  792. tmppath = concat (ld_sysroot, "/etc/ld.so.conf",
  793. (const char *) NULL);
  794. ldelf_parse_ld_so_conf (&info, tmppath);
  795. }
  796. free (tmppath);
  797. if (info.path)
  798. {
  799. ld_so_conf = ldelf_add_sysroot (info.path);
  800. free (info.path);
  801. }
  802. initialized = true;
  803. }
  804. if (ld_so_conf == NULL)
  805. return false;
  806. needed.by = l->by;
  807. needed.name = l->name;
  808. return ldelf_search_needed (ld_so_conf, &needed, force, true, elfsize);
  809. }
  810. /* See if an input file matches a DT_NEEDED entry by name. */
  811. static void
  812. ldelf_check_needed (lang_input_statement_type *s)
  813. {
  814. const char *soname;
  815. /* Stop looking if we've found a loaded lib. */
  816. if (global_found != NULL
  817. && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
  818. & DYN_AS_NEEDED) == 0)
  819. return;
  820. if (s->filename == NULL || s->the_bfd == NULL)
  821. return;
  822. /* Don't look for a second non-loaded as-needed lib. */
  823. if (global_found != NULL
  824. && (bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
  825. return;
  826. if (filename_cmp (s->filename, global_needed->name) == 0)
  827. {
  828. global_found = s;
  829. return;
  830. }
  831. if (s->flags.search_dirs)
  832. {
  833. const char *f = strrchr (s->filename, '/');
  834. if (f != NULL
  835. && filename_cmp (f + 1, global_needed->name) == 0)
  836. {
  837. global_found = s;
  838. return;
  839. }
  840. }
  841. soname = bfd_elf_get_dt_soname (s->the_bfd);
  842. if (soname != NULL
  843. && filename_cmp (soname, global_needed->name) == 0)
  844. {
  845. global_found = s;
  846. return;
  847. }
  848. }
  849. static void
  850. ldelf_handle_dt_needed (struct elf_link_hash_table *htab,
  851. int use_libpath, int native, int is_linux,
  852. int is_freebsd, int elfsize, const char *prefix)
  853. {
  854. struct bfd_link_needed_list *needed, *l;
  855. bfd *abfd;
  856. bfd **save_input_bfd_tail;
  857. /* Get the list of files which appear in DT_NEEDED entries in
  858. dynamic objects included in the link (often there will be none).
  859. For each such file, we want to track down the corresponding
  860. library, and include the symbol table in the link. This is what
  861. the runtime dynamic linker will do. Tracking the files down here
  862. permits one dynamic object to include another without requiring
  863. special action by the person doing the link. Note that the
  864. needed list can actually grow while we are stepping through this
  865. loop. */
  866. save_input_bfd_tail = link_info.input_bfds_tail;
  867. needed = bfd_elf_get_needed_list (link_info.output_bfd, &link_info);
  868. for (l = needed; l != NULL; l = l->next)
  869. {
  870. struct bfd_link_needed_list *ll;
  871. struct dt_needed n, nn;
  872. int force;
  873. /* If the lib that needs this one was --as-needed and wasn't
  874. found to be needed, then this lib isn't needed either. */
  875. if (l->by != NULL
  876. && (bfd_elf_get_dyn_lib_class (l->by) & DYN_AS_NEEDED) != 0)
  877. continue;
  878. /* Skip the lib if --no-copy-dt-needed-entries and when we are
  879. handling DT_NEEDED entries or --allow-shlib-undefined is in
  880. effect. */
  881. if (l->by != NULL
  882. && (htab->handling_dt_needed
  883. || link_info.unresolved_syms_in_shared_libs == RM_IGNORE)
  884. && (bfd_elf_get_dyn_lib_class (l->by) & DYN_NO_ADD_NEEDED) != 0)
  885. continue;
  886. /* If we've already seen this file, skip it. */
  887. for (ll = needed; ll != l; ll = ll->next)
  888. if ((ll->by == NULL
  889. || (bfd_elf_get_dyn_lib_class (ll->by) & DYN_AS_NEEDED) == 0)
  890. && strcmp (ll->name, l->name) == 0)
  891. break;
  892. if (ll != l)
  893. continue;
  894. /* See if this file was included in the link explicitly. */
  895. global_needed = l;
  896. global_found = NULL;
  897. lang_for_each_input_file (ldelf_check_needed);
  898. if (global_found != NULL
  899. && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
  900. & DYN_AS_NEEDED) == 0)
  901. continue;
  902. n.by = l->by;
  903. n.name = l->name;
  904. nn.by = l->by;
  905. if (verbose)
  906. info_msg (_("%s needed by %pB\n"), l->name, l->by);
  907. /* As-needed libs specified on the command line (or linker script)
  908. take priority over libs found in search dirs. */
  909. if (global_found != NULL)
  910. {
  911. nn.name = global_found->filename;
  912. if (ldelf_try_needed (&nn, true, is_linux))
  913. continue;
  914. }
  915. /* We need to find this file and include the symbol table. We
  916. want to search for the file in the same way that the dynamic
  917. linker will search. That means that we want to use
  918. rpath_link, rpath, then the environment variable
  919. LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
  920. entries (native only), then the linker script LIB_SEARCH_DIRS.
  921. We do not search using the -L arguments.
  922. We search twice. The first time, we skip objects which may
  923. introduce version mismatches. The second time, we force
  924. their use. See ldelf_vercheck comment. */
  925. for (force = 0; force < 2; force++)
  926. {
  927. size_t len;
  928. search_dirs_type *search;
  929. const char *path;
  930. struct bfd_link_needed_list *rp;
  931. int found;
  932. if (ldelf_search_needed (command_line.rpath_link, &n, force,
  933. is_linux, elfsize))
  934. break;
  935. if (use_libpath)
  936. {
  937. path = command_line.rpath;
  938. if (path)
  939. {
  940. path = ldelf_add_sysroot (path);
  941. found = ldelf_search_needed (path, &n, force,
  942. is_linux, elfsize);
  943. free ((char *) path);
  944. if (found)
  945. break;
  946. }
  947. }
  948. if (native)
  949. {
  950. if (command_line.rpath_link == NULL
  951. && command_line.rpath == NULL)
  952. {
  953. path = (const char *) getenv ("LD_RUN_PATH");
  954. if (path
  955. && ldelf_search_needed (path, &n, force,
  956. is_linux, elfsize))
  957. break;
  958. }
  959. path = (const char *) getenv ("LD_LIBRARY_PATH");
  960. if (path
  961. && ldelf_search_needed (path, &n, force,
  962. is_linux, elfsize))
  963. break;
  964. }
  965. if (use_libpath)
  966. {
  967. found = 0;
  968. rp = bfd_elf_get_runpath_list (link_info.output_bfd, &link_info);
  969. for (; !found && rp != NULL; rp = rp->next)
  970. {
  971. path = ldelf_add_sysroot (rp->name);
  972. found = (rp->by == l->by
  973. && ldelf_search_needed (path, &n, force,
  974. is_linux, elfsize));
  975. free ((char *) path);
  976. }
  977. if (found)
  978. break;
  979. if (is_freebsd
  980. && ldelf_check_ld_elf_hints (l, force, elfsize))
  981. break;
  982. if (is_linux
  983. && ldelf_check_ld_so_conf (l, force, elfsize, prefix))
  984. break;
  985. }
  986. len = strlen (l->name);
  987. for (search = search_head; search != NULL; search = search->next)
  988. {
  989. char *filename;
  990. if (search->cmdline)
  991. continue;
  992. filename = (char *) xmalloc (strlen (search->name) + len + 2);
  993. sprintf (filename, "%s/%s", search->name, l->name);
  994. nn.name = filename;
  995. if (ldelf_try_needed (&nn, force, is_linux))
  996. break;
  997. free (filename);
  998. }
  999. if (search != NULL)
  1000. break;
  1001. }
  1002. if (force < 2)
  1003. continue;
  1004. einfo (_("%P: warning: %s, needed by %pB, not found "
  1005. "(try using -rpath or -rpath-link)\n"),
  1006. l->name, l->by);
  1007. }
  1008. /* Don't add DT_NEEDED when loading shared objects from DT_NEEDED for
  1009. plugin symbol resolution while handling DT_NEEDED entries. */
  1010. if (!htab->handling_dt_needed)
  1011. for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
  1012. if (bfd_get_format (abfd) == bfd_object
  1013. && ((abfd->flags) & DYNAMIC) != 0
  1014. && bfd_get_flavour (abfd) == bfd_target_elf_flavour
  1015. && (elf_dyn_lib_class (abfd) & (DYN_AS_NEEDED | DYN_NO_NEEDED)) == 0
  1016. && elf_dt_name (abfd) != NULL)
  1017. {
  1018. if (bfd_elf_add_dt_needed_tag (abfd, &link_info) < 0)
  1019. einfo (_("%F%P: failed to add DT_NEEDED dynamic tag\n"));
  1020. }
  1021. link_info.input_bfds_tail = save_input_bfd_tail;
  1022. *save_input_bfd_tail = NULL;
  1023. }
  1024. /* This is called before calling plugin 'all symbols read' hook. */
  1025. void
  1026. ldelf_before_plugin_all_symbols_read (int use_libpath, int native,
  1027. int is_linux, int is_freebsd,
  1028. int elfsize, const char *prefix)
  1029. {
  1030. struct elf_link_hash_table *htab = elf_hash_table (&link_info);
  1031. if (!is_elf_hash_table (&htab->root))
  1032. return;
  1033. htab->handling_dt_needed = true;
  1034. ldelf_handle_dt_needed (htab, use_libpath, native, is_linux,
  1035. is_freebsd, elfsize, prefix);
  1036. htab->handling_dt_needed = false;
  1037. }
  1038. /* This is called after all the input files have been opened and all
  1039. symbols have been loaded. */
  1040. void
  1041. ldelf_after_open (int use_libpath, int native, int is_linux, int is_freebsd,
  1042. int elfsize, const char *prefix)
  1043. {
  1044. struct elf_link_hash_table *htab;
  1045. asection *s;
  1046. bfd *abfd;
  1047. after_open_default ();
  1048. htab = elf_hash_table (&link_info);
  1049. if (!is_elf_hash_table (&htab->root))
  1050. return;
  1051. if (command_line.out_implib_filename)
  1052. {
  1053. unlink_if_ordinary (command_line.out_implib_filename);
  1054. link_info.out_implib_bfd
  1055. = bfd_openw (command_line.out_implib_filename,
  1056. bfd_get_target (link_info.output_bfd));
  1057. if (link_info.out_implib_bfd == NULL)
  1058. {
  1059. einfo (_("%F%P: %s: can't open for writing: %E\n"),
  1060. command_line.out_implib_filename);
  1061. }
  1062. }
  1063. if (ldelf_emit_note_gnu_build_id != NULL)
  1064. {
  1065. /* Find an ELF input. */
  1066. for (abfd = link_info.input_bfds;
  1067. abfd != (bfd *) NULL; abfd = abfd->link.next)
  1068. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  1069. && bfd_count_sections (abfd) != 0
  1070. && !bfd_input_just_syms (abfd))
  1071. break;
  1072. /* PR 10555: If there are no ELF input files do not try to
  1073. create a .note.gnu-build-id section. */
  1074. if (abfd == NULL
  1075. || !ldelf_setup_build_id (abfd))
  1076. {
  1077. free ((char *) ldelf_emit_note_gnu_build_id);
  1078. ldelf_emit_note_gnu_build_id = NULL;
  1079. }
  1080. }
  1081. get_elf_backend_data (link_info.output_bfd)->setup_gnu_properties (&link_info);
  1082. /* Do not allow executable files to be used as inputs to the link. */
  1083. for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
  1084. {
  1085. /* Discard input .note.gnu.build-id sections. */
  1086. s = bfd_get_section_by_name (abfd, ".note.gnu.build-id");
  1087. while (s != NULL)
  1088. {
  1089. if (s != elf_tdata (link_info.output_bfd)->o->build_id.sec)
  1090. s->flags |= SEC_EXCLUDE;
  1091. s = bfd_get_next_section_by_name (NULL, s);
  1092. }
  1093. if (abfd->xvec->flavour == bfd_target_elf_flavour
  1094. && !bfd_input_just_syms (abfd)
  1095. && elf_tdata (abfd) != NULL
  1096. /* FIXME: Maybe check for other non-supportable types as well ? */
  1097. && (elf_tdata (abfd)->elf_header->e_type == ET_EXEC
  1098. || (elf_tdata (abfd)->elf_header->e_type == ET_DYN
  1099. && elf_tdata (abfd)->is_pie)))
  1100. einfo (_("%F%P: cannot use executable file '%pB' as input to a link\n"),
  1101. abfd);
  1102. }
  1103. if (bfd_link_relocatable (&link_info))
  1104. {
  1105. if (link_info.execstack == !link_info.noexecstack)
  1106. {
  1107. /* PR ld/16744: If "-z [no]execstack" has been specified on the
  1108. command line and we are perfoming a relocatable link then no
  1109. PT_GNU_STACK segment will be created and so the
  1110. linkinfo.[no]execstack values set in _handle_option() will have no
  1111. effect. Instead we create a .note.GNU-stack section in much the
  1112. same way as the assembler does with its --[no]execstack option. */
  1113. flagword flags = SEC_READONLY | (link_info.execstack ? SEC_CODE : 0);
  1114. (void) bfd_make_section_with_flags (link_info.input_bfds,
  1115. ".note.GNU-stack", flags);
  1116. }
  1117. return;
  1118. }
  1119. if (!link_info.traditional_format)
  1120. {
  1121. bfd *elfbfd = NULL;
  1122. bool warn_eh_frame = false;
  1123. int seen_type = 0;
  1124. for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
  1125. {
  1126. int type = 0;
  1127. if (bfd_input_just_syms (abfd))
  1128. continue;
  1129. for (s = abfd->sections; s && type < COMPACT_EH_HDR; s = s->next)
  1130. {
  1131. const char *name = bfd_section_name (s);
  1132. if (bfd_is_abs_section (s->output_section))
  1133. continue;
  1134. if (startswith (name, ".eh_frame_entry"))
  1135. type = COMPACT_EH_HDR;
  1136. else if (strcmp (name, ".eh_frame") == 0 && s->size > 8)
  1137. type = DWARF2_EH_HDR;
  1138. }
  1139. if (type != 0)
  1140. {
  1141. if (seen_type == 0)
  1142. {
  1143. seen_type = type;
  1144. }
  1145. else if (seen_type != type)
  1146. {
  1147. einfo (_("%F%P: compact frame descriptions incompatible with"
  1148. " DWARF2 .eh_frame from %pB\n"),
  1149. type == DWARF2_EH_HDR ? abfd : elfbfd);
  1150. break;
  1151. }
  1152. if (!elfbfd
  1153. && (type == COMPACT_EH_HDR
  1154. || link_info.eh_frame_hdr_type != 0))
  1155. {
  1156. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  1157. elfbfd = abfd;
  1158. warn_eh_frame = true;
  1159. }
  1160. }
  1161. if (seen_type == COMPACT_EH_HDR)
  1162. link_info.eh_frame_hdr_type = COMPACT_EH_HDR;
  1163. }
  1164. if (elfbfd)
  1165. {
  1166. const struct elf_backend_data *bed;
  1167. bed = get_elf_backend_data (elfbfd);
  1168. s = bfd_make_section_with_flags (elfbfd, ".eh_frame_hdr",
  1169. bed->dynamic_sec_flags
  1170. | SEC_READONLY);
  1171. if (s != NULL
  1172. && bfd_set_section_alignment (s, 2))
  1173. {
  1174. htab->eh_info.hdr_sec = s;
  1175. warn_eh_frame = false;
  1176. }
  1177. }
  1178. if (warn_eh_frame)
  1179. einfo (_("%P: warning: cannot create .eh_frame_hdr section,"
  1180. " --eh-frame-hdr ignored\n"));
  1181. }
  1182. if (link_info.eh_frame_hdr_type == COMPACT_EH_HDR)
  1183. if (!bfd_elf_parse_eh_frame_entries (NULL, &link_info))
  1184. einfo (_("%F%P: failed to parse EH frame entries\n"));
  1185. ldelf_handle_dt_needed (htab, use_libpath, native, is_linux,
  1186. is_freebsd, elfsize, prefix);
  1187. }
  1188. static bfd_size_type
  1189. id_note_section_size (bfd *abfd ATTRIBUTE_UNUSED)
  1190. {
  1191. const char *style = ldelf_emit_note_gnu_build_id;
  1192. bfd_size_type size;
  1193. bfd_size_type build_id_size;
  1194. size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
  1195. size = (size + 3) & -(bfd_size_type) 4;
  1196. build_id_size = compute_build_id_size (style);
  1197. if (build_id_size)
  1198. size += build_id_size;
  1199. else
  1200. size = 0;
  1201. return size;
  1202. }
  1203. static bool
  1204. write_build_id (bfd *abfd)
  1205. {
  1206. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1207. struct elf_obj_tdata *t = elf_tdata (abfd);
  1208. const char *style;
  1209. asection *asec;
  1210. Elf_Internal_Shdr *i_shdr;
  1211. unsigned char *contents, *id_bits;
  1212. bfd_size_type size;
  1213. file_ptr position;
  1214. Elf_External_Note *e_note;
  1215. style = t->o->build_id.style;
  1216. asec = t->o->build_id.sec;
  1217. if (bfd_is_abs_section (asec->output_section))
  1218. {
  1219. einfo (_("%P: warning: .note.gnu.build-id section discarded,"
  1220. " --build-id ignored\n"));
  1221. return true;
  1222. }
  1223. i_shdr = &elf_section_data (asec->output_section)->this_hdr;
  1224. if (i_shdr->contents == NULL)
  1225. {
  1226. if (asec->contents == NULL)
  1227. asec->contents = (unsigned char *) xmalloc (asec->size);
  1228. contents = asec->contents;
  1229. }
  1230. else
  1231. contents = i_shdr->contents + asec->output_offset;
  1232. e_note = (Elf_External_Note *) contents;
  1233. size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
  1234. size = (size + 3) & -(bfd_size_type) 4;
  1235. id_bits = contents + size;
  1236. size = asec->size - size;
  1237. /* Clear the build ID field. */
  1238. memset (id_bits, 0, size);
  1239. bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz);
  1240. bfd_h_put_32 (abfd, size, &e_note->descsz);
  1241. bfd_h_put_32 (abfd, NT_GNU_BUILD_ID, &e_note->type);
  1242. memcpy (e_note->name, "GNU", sizeof "GNU");
  1243. generate_build_id (abfd, style, bed->s->checksum_contents, id_bits, size);
  1244. position = i_shdr->sh_offset + asec->output_offset;
  1245. size = asec->size;
  1246. return (bfd_seek (abfd, position, SEEK_SET) == 0
  1247. && bfd_bwrite (contents, size, abfd) == size);
  1248. }
  1249. /* Make .note.gnu.build-id section, and set up elf_tdata->build_id. */
  1250. bool
  1251. ldelf_setup_build_id (bfd *ibfd)
  1252. {
  1253. asection *s;
  1254. bfd_size_type size;
  1255. flagword flags;
  1256. size = id_note_section_size (ibfd);
  1257. if (size == 0)
  1258. {
  1259. einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
  1260. return false;
  1261. }
  1262. flags = (SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
  1263. | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
  1264. s = bfd_make_section_anyway_with_flags (ibfd, ".note.gnu.build-id",
  1265. flags);
  1266. if (s != NULL && bfd_set_section_alignment (s, 2))
  1267. {
  1268. struct elf_obj_tdata *t = elf_tdata (link_info.output_bfd);
  1269. t->o->build_id.after_write_object_contents = &write_build_id;
  1270. t->o->build_id.style = ldelf_emit_note_gnu_build_id;
  1271. t->o->build_id.sec = s;
  1272. elf_section_type (s) = SHT_NOTE;
  1273. s->size = size;
  1274. return true;
  1275. }
  1276. einfo (_("%P: warning: cannot create .note.gnu.build-id section,"
  1277. " --build-id ignored\n"));
  1278. return false;
  1279. }
  1280. /* Look through an expression for an assignment statement. */
  1281. static void
  1282. ldelf_find_exp_assignment (etree_type *exp)
  1283. {
  1284. bool provide = false;
  1285. switch (exp->type.node_class)
  1286. {
  1287. case etree_provide:
  1288. case etree_provided:
  1289. provide = true;
  1290. /* Fallthru */
  1291. case etree_assign:
  1292. /* We call record_link_assignment even if the symbol is defined.
  1293. This is because if it is defined by a dynamic object, we
  1294. actually want to use the value defined by the linker script,
  1295. not the value from the dynamic object (because we are setting
  1296. symbols like etext). If the symbol is defined by a regular
  1297. object, then, as it happens, calling record_link_assignment
  1298. will do no harm. */
  1299. if (strcmp (exp->assign.dst, ".") != 0)
  1300. {
  1301. if (!bfd_elf_record_link_assignment (link_info.output_bfd,
  1302. &link_info,
  1303. exp->assign.dst, provide,
  1304. exp->assign.hidden))
  1305. einfo (_("%F%P: failed to record assignment to %s: %E\n"),
  1306. exp->assign.dst);
  1307. }
  1308. ldelf_find_exp_assignment (exp->assign.src);
  1309. break;
  1310. case etree_binary:
  1311. ldelf_find_exp_assignment (exp->binary.lhs);
  1312. ldelf_find_exp_assignment (exp->binary.rhs);
  1313. break;
  1314. case etree_trinary:
  1315. ldelf_find_exp_assignment (exp->trinary.cond);
  1316. ldelf_find_exp_assignment (exp->trinary.lhs);
  1317. ldelf_find_exp_assignment (exp->trinary.rhs);
  1318. break;
  1319. case etree_unary:
  1320. ldelf_find_exp_assignment (exp->unary.child);
  1321. break;
  1322. default:
  1323. break;
  1324. }
  1325. }
  1326. /* This is called by the before_allocation routine via
  1327. lang_for_each_statement. It locates any assignment statements, and
  1328. tells the ELF backend about them, in case they are assignments to
  1329. symbols which are referred to by dynamic objects. */
  1330. static void
  1331. ldelf_find_statement_assignment (lang_statement_union_type *s)
  1332. {
  1333. if (s->header.type == lang_assignment_statement_enum)
  1334. ldelf_find_exp_assignment (s->assignment_statement.exp);
  1335. }
  1336. /* Used by before_allocation and handle_option. */
  1337. void
  1338. ldelf_append_to_separated_string (char **to, char *op_arg)
  1339. {
  1340. if (*to == NULL)
  1341. *to = xstrdup (op_arg);
  1342. else
  1343. {
  1344. size_t to_len = strlen (*to);
  1345. size_t op_arg_len = strlen (op_arg);
  1346. char *buf;
  1347. char *cp = *to;
  1348. /* First see whether OPTARG is already in the path. */
  1349. do
  1350. {
  1351. if (strncmp (op_arg, cp, op_arg_len) == 0
  1352. && (cp[op_arg_len] == 0
  1353. || cp[op_arg_len] == config.rpath_separator))
  1354. /* We found it. */
  1355. break;
  1356. /* Not yet found. */
  1357. cp = strchr (cp, config.rpath_separator);
  1358. if (cp != NULL)
  1359. ++cp;
  1360. }
  1361. while (cp != NULL);
  1362. if (cp == NULL)
  1363. {
  1364. buf = xmalloc (to_len + op_arg_len + 2);
  1365. sprintf (buf, "%s%c%s", *to,
  1366. config.rpath_separator, op_arg);
  1367. free (*to);
  1368. *to = buf;
  1369. }
  1370. }
  1371. }
  1372. /* This is called after the sections have been attached to output
  1373. sections, but before any sizes or addresses have been set. */
  1374. void
  1375. ldelf_before_allocation (char *audit, char *depaudit,
  1376. const char *default_interpreter_name)
  1377. {
  1378. const char *rpath;
  1379. asection *sinterp;
  1380. bfd *abfd;
  1381. struct bfd_link_hash_entry *ehdr_start = NULL;
  1382. unsigned char ehdr_start_save_type = 0;
  1383. char ehdr_start_save_u[sizeof ehdr_start->u
  1384. - sizeof ehdr_start->u.def.next] = "";
  1385. if (is_elf_hash_table (link_info.hash))
  1386. {
  1387. _bfd_elf_tls_setup (link_info.output_bfd, &link_info);
  1388. /* Make __ehdr_start hidden if it has been referenced, to
  1389. prevent the symbol from being dynamic. */
  1390. if (!bfd_link_relocatable (&link_info))
  1391. {
  1392. struct elf_link_hash_table *htab = elf_hash_table (&link_info);
  1393. struct elf_link_hash_entry *h
  1394. = elf_link_hash_lookup (htab, "__ehdr_start", false, false, true);
  1395. /* Only adjust the export class if the symbol was referenced
  1396. and not defined, otherwise leave it alone. */
  1397. if (h != NULL
  1398. && (h->root.type == bfd_link_hash_new
  1399. || h->root.type == bfd_link_hash_undefined
  1400. || h->root.type == bfd_link_hash_undefweak
  1401. || h->root.type == bfd_link_hash_common))
  1402. {
  1403. /* Don't leave the symbol undefined. Undefined hidden
  1404. symbols typically won't have dynamic relocations, but
  1405. we most likely will need dynamic relocations for
  1406. __ehdr_start if we are building a PIE or shared
  1407. library. */
  1408. ehdr_start = &h->root;
  1409. ehdr_start_save_type = ehdr_start->type;
  1410. memcpy (ehdr_start_save_u,
  1411. (char *) &ehdr_start->u + sizeof ehdr_start->u.def.next,
  1412. sizeof ehdr_start_save_u);
  1413. ehdr_start->type = bfd_link_hash_defined;
  1414. /* It will be converted to section-relative later. */
  1415. ehdr_start->u.def.section = bfd_abs_section_ptr;
  1416. ehdr_start->u.def.value = 0;
  1417. }
  1418. }
  1419. /* If we are going to make any variable assignments, we need to
  1420. let the ELF backend know about them in case the variables are
  1421. referred to by dynamic objects. */
  1422. lang_for_each_statement (ldelf_find_statement_assignment);
  1423. }
  1424. /* Let the ELF backend work out the sizes of any sections required
  1425. by dynamic linking. */
  1426. rpath = command_line.rpath;
  1427. if (rpath == NULL)
  1428. rpath = (const char *) getenv ("LD_RUN_PATH");
  1429. for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
  1430. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  1431. {
  1432. const char *audit_libs = elf_dt_audit (abfd);
  1433. /* If the input bfd contains an audit entry, we need to add it as
  1434. a dep audit entry. */
  1435. if (audit_libs && *audit_libs != '\0')
  1436. {
  1437. char *cp = xstrdup (audit_libs);
  1438. do
  1439. {
  1440. int more = 0;
  1441. char *cp2 = strchr (cp, config.rpath_separator);
  1442. if (cp2)
  1443. {
  1444. *cp2 = '\0';
  1445. more = 1;
  1446. }
  1447. if (cp != NULL && *cp != '\0')
  1448. ldelf_append_to_separated_string (&depaudit, cp);
  1449. cp = more ? ++cp2 : NULL;
  1450. }
  1451. while (cp != NULL);
  1452. }
  1453. }
  1454. if (! (bfd_elf_size_dynamic_sections
  1455. (link_info.output_bfd, command_line.soname, rpath,
  1456. command_line.filter_shlib, audit, depaudit,
  1457. (const char * const *) command_line.auxiliary_filters,
  1458. &link_info, &sinterp)))
  1459. einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
  1460. if (sinterp != NULL)
  1461. {
  1462. /* Let the user override the dynamic linker we are using. */
  1463. if (command_line.interpreter != NULL)
  1464. default_interpreter_name = command_line.interpreter;
  1465. if (default_interpreter_name != NULL)
  1466. {
  1467. sinterp->contents = (bfd_byte *) default_interpreter_name;
  1468. sinterp->size = strlen ((char *) sinterp->contents) + 1;
  1469. }
  1470. }
  1471. /* Look for any sections named .gnu.warning. As a GNU extensions,
  1472. we treat such sections as containing warning messages. We print
  1473. out the warning message, and then zero out the section size so
  1474. that it does not get copied into the output file. */
  1475. {
  1476. LANG_FOR_EACH_INPUT_STATEMENT (is)
  1477. {
  1478. asection *s;
  1479. bfd_size_type sz;
  1480. char *msg;
  1481. if (is->flags.just_syms)
  1482. continue;
  1483. s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
  1484. if (s == NULL)
  1485. continue;
  1486. sz = s->size;
  1487. msg = (char *) xmalloc ((size_t) (sz + 1));
  1488. if (! bfd_get_section_contents (is->the_bfd, s, msg,
  1489. (file_ptr) 0, sz))
  1490. einfo (_("%F%P: %pB: can't read contents of section .gnu.warning: %E\n"),
  1491. is->the_bfd);
  1492. msg[sz] = '\0';
  1493. (*link_info.callbacks->warning) (&link_info, msg,
  1494. (const char *) NULL, is->the_bfd,
  1495. (asection *) NULL, (bfd_vma) 0);
  1496. free (msg);
  1497. /* Clobber the section size, so that we don't waste space
  1498. copying the warning into the output file. If we've already
  1499. sized the output section, adjust its size. The adjustment
  1500. is on rawsize because targets that size sections early will
  1501. have called lang_reset_memory_regions after sizing. */
  1502. if (s->output_section != NULL
  1503. && s->output_section->rawsize >= s->size)
  1504. s->output_section->rawsize -= s->size;
  1505. s->size = 0;
  1506. /* Also set SEC_EXCLUDE, so that local symbols defined in the
  1507. warning section don't get copied to the output. */
  1508. s->flags |= SEC_EXCLUDE | SEC_KEEP;
  1509. }
  1510. }
  1511. before_allocation_default ();
  1512. if (!bfd_elf_size_dynsym_hash_dynstr (link_info.output_bfd, &link_info))
  1513. einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
  1514. if (ehdr_start != NULL)
  1515. {
  1516. /* If we twiddled __ehdr_start to defined earlier, put it back
  1517. as it was. */
  1518. ehdr_start->type = ehdr_start_save_type;
  1519. memcpy ((char *) &ehdr_start->u + sizeof ehdr_start->u.def.next,
  1520. ehdr_start_save_u,
  1521. sizeof ehdr_start_save_u);
  1522. }
  1523. }
  1524. /* Try to open a dynamic archive. This is where we know that ELF
  1525. dynamic libraries have an extension of .so (or .sl on oddball systems
  1526. like hpux). */
  1527. bool
  1528. ldelf_open_dynamic_archive (const char *arch, search_dirs_type *search,
  1529. lang_input_statement_type *entry)
  1530. {
  1531. const char *filename;
  1532. char *string;
  1533. size_t len;
  1534. bool opened = false;
  1535. if (! entry->flags.maybe_archive)
  1536. return false;
  1537. filename = entry->filename;
  1538. len = strlen (search->name) + strlen (filename);
  1539. if (entry->flags.full_name_provided)
  1540. {
  1541. len += sizeof "/";
  1542. string = (char *) xmalloc (len);
  1543. sprintf (string, "%s/%s", search->name, filename);
  1544. }
  1545. else
  1546. {
  1547. size_t xlen = 0;
  1548. len += strlen (arch) + sizeof "/lib.so";
  1549. #ifdef EXTRA_SHLIB_EXTENSION
  1550. xlen = (strlen (EXTRA_SHLIB_EXTENSION) > 3
  1551. ? strlen (EXTRA_SHLIB_EXTENSION) - 3
  1552. : 0);
  1553. #endif
  1554. string = (char *) xmalloc (len + xlen);
  1555. sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
  1556. #ifdef EXTRA_SHLIB_EXTENSION
  1557. /* Try the .so extension first. If that fails build a new filename
  1558. using EXTRA_SHLIB_EXTENSION. */
  1559. opened = ldfile_try_open_bfd (string, entry);
  1560. if (!opened)
  1561. strcpy (string + len - 4, EXTRA_SHLIB_EXTENSION);
  1562. #endif
  1563. }
  1564. if (!opened && !ldfile_try_open_bfd (string, entry))
  1565. {
  1566. free (string);
  1567. return false;
  1568. }
  1569. entry->filename = string;
  1570. /* We have found a dynamic object to include in the link. The ELF
  1571. backend linker will create a DT_NEEDED entry in the .dynamic
  1572. section naming this file. If this file includes a DT_SONAME
  1573. entry, it will be used. Otherwise, the ELF linker will just use
  1574. the name of the file. For an archive found by searching, like
  1575. this one, the DT_NEEDED entry should consist of just the name of
  1576. the file, without the path information used to find it. Note
  1577. that we only need to do this if we have a dynamic object; an
  1578. archive will never be referenced by a DT_NEEDED entry.
  1579. FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
  1580. very pretty. I haven't been able to think of anything that is
  1581. pretty, though. */
  1582. if (bfd_check_format (entry->the_bfd, bfd_object)
  1583. && (entry->the_bfd->flags & DYNAMIC) != 0)
  1584. {
  1585. ASSERT (entry->flags.maybe_archive && entry->flags.search_dirs);
  1586. /* Rather than duplicating the logic above. Just use the
  1587. filename we recorded earlier. */
  1588. if (!entry->flags.full_name_provided)
  1589. filename = lbasename (entry->filename);
  1590. bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
  1591. }
  1592. return true;
  1593. }
  1594. /* A variant of lang_output_section_find used by place_orphan. */
  1595. static lang_output_section_statement_type *
  1596. output_rel_find (int isdyn, int rela)
  1597. {
  1598. lang_output_section_statement_type *lookup;
  1599. lang_output_section_statement_type *last = NULL;
  1600. lang_output_section_statement_type *last_alloc = NULL;
  1601. lang_output_section_statement_type *last_ro_alloc = NULL;
  1602. lang_output_section_statement_type *last_rel = NULL;
  1603. lang_output_section_statement_type *last_rel_alloc = NULL;
  1604. for (lookup = (void *) lang_os_list.head;
  1605. lookup != NULL;
  1606. lookup = lookup->next)
  1607. {
  1608. if (lookup->constraint >= 0
  1609. && startswith (lookup->name, ".rel"))
  1610. {
  1611. int lookrela = lookup->name[4] == 'a';
  1612. /* .rel.dyn must come before all other reloc sections, to suit
  1613. GNU ld.so. */
  1614. if (isdyn)
  1615. break;
  1616. /* Don't place after .rel.plt as doing so results in wrong
  1617. dynamic tags. */
  1618. if (strcmp (".plt", lookup->name + 4 + lookrela) == 0)
  1619. break;
  1620. if (rela == lookrela || last_rel == NULL)
  1621. last_rel = lookup;
  1622. if ((rela == lookrela || last_rel_alloc == NULL)
  1623. && lookup->bfd_section != NULL
  1624. && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
  1625. last_rel_alloc = lookup;
  1626. }
  1627. last = lookup;
  1628. if (lookup->bfd_section != NULL
  1629. && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
  1630. {
  1631. last_alloc = lookup;
  1632. if ((lookup->bfd_section->flags & SEC_READONLY) != 0)
  1633. last_ro_alloc = lookup;
  1634. }
  1635. }
  1636. if (last_rel_alloc)
  1637. return last_rel_alloc;
  1638. if (last_rel)
  1639. return last_rel;
  1640. if (last_ro_alloc)
  1641. return last_ro_alloc;
  1642. if (last_alloc)
  1643. return last_alloc;
  1644. return last;
  1645. }
  1646. /* Return whether IN is suitable to be part of OUT. */
  1647. static bool
  1648. elf_orphan_compatible (asection *in, asection *out)
  1649. {
  1650. /* Non-zero sh_info implies a section with SHF_INFO_LINK with
  1651. unknown semantics for the generic linker, or a SHT_REL/SHT_RELA
  1652. section where sh_info specifies a symbol table. (We won't see
  1653. SHT_GROUP, SHT_SYMTAB or SHT_DYNSYM sections here.) We clearly
  1654. can't merge SHT_REL/SHT_RELA using differing symbol tables, and
  1655. shouldn't merge sections with differing unknown semantics. */
  1656. if (elf_section_data (out)->this_hdr.sh_info
  1657. != elf_section_data (in)->this_hdr.sh_info)
  1658. return false;
  1659. /* We can't merge with a member of an output section group or merge
  1660. two sections with differing SHF_EXCLUDE or other processor and OS
  1661. specific flags when doing a relocatable link. */
  1662. if (bfd_link_relocatable (&link_info)
  1663. && (elf_next_in_group (out) != NULL
  1664. || ((elf_section_flags (out) ^ elf_section_flags (in))
  1665. & (SHF_MASKPROC | SHF_MASKOS)) != 0))
  1666. return false;
  1667. return _bfd_elf_match_sections_by_type (link_info.output_bfd, out,
  1668. in->owner, in);
  1669. }
  1670. /* Place an orphan section. We use this to put random SHF_ALLOC
  1671. sections in the right segment. */
  1672. lang_output_section_statement_type *
  1673. ldelf_place_orphan (asection *s, const char *secname, int constraint)
  1674. {
  1675. static struct orphan_save hold[] =
  1676. {
  1677. { ".text",
  1678. SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
  1679. 0, 0, 0, 0 },
  1680. { ".rodata",
  1681. SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
  1682. 0, 0, 0, 0 },
  1683. { ".tdata",
  1684. SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_THREAD_LOCAL,
  1685. 0, 0, 0, 0 },
  1686. { ".data",
  1687. SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
  1688. 0, 0, 0, 0 },
  1689. { ".bss",
  1690. SEC_ALLOC,
  1691. 0, 0, 0, 0 },
  1692. { 0,
  1693. SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
  1694. 0, 0, 0, 0 },
  1695. { ".interp",
  1696. SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
  1697. 0, 0, 0, 0 },
  1698. { ".sdata",
  1699. SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_SMALL_DATA,
  1700. 0, 0, 0, 0 },
  1701. { ".comment",
  1702. SEC_HAS_CONTENTS,
  1703. 0, 0, 0, 0 },
  1704. };
  1705. enum orphan_save_index
  1706. {
  1707. orphan_text = 0,
  1708. orphan_rodata,
  1709. orphan_tdata,
  1710. orphan_data,
  1711. orphan_bss,
  1712. orphan_rel,
  1713. orphan_interp,
  1714. orphan_sdata,
  1715. orphan_nonalloc
  1716. };
  1717. static int orphan_init_done = 0;
  1718. struct orphan_save *place;
  1719. lang_output_section_statement_type *after;
  1720. lang_output_section_statement_type *os;
  1721. lang_output_section_statement_type *match_by_name = NULL;
  1722. int isdyn = 0;
  1723. int elfinput = s->owner->xvec->flavour == bfd_target_elf_flavour;
  1724. int elfoutput = link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour;
  1725. unsigned int sh_type = elfinput ? elf_section_type (s) : SHT_NULL;
  1726. flagword flags;
  1727. asection *nexts;
  1728. if (!bfd_link_relocatable (&link_info)
  1729. && link_info.combreloc
  1730. && (s->flags & SEC_ALLOC))
  1731. {
  1732. if (elfinput)
  1733. switch (sh_type)
  1734. {
  1735. case SHT_RELA:
  1736. secname = ".rela.dyn";
  1737. isdyn = 1;
  1738. break;
  1739. case SHT_REL:
  1740. secname = ".rel.dyn";
  1741. isdyn = 1;
  1742. break;
  1743. default:
  1744. break;
  1745. }
  1746. else if (startswith (secname, ".rel"))
  1747. {
  1748. secname = secname[4] == 'a' ? ".rela.dyn" : ".rel.dyn";
  1749. isdyn = 1;
  1750. }
  1751. }
  1752. if (!bfd_link_relocatable (&link_info)
  1753. && elfinput
  1754. && elfoutput
  1755. && (s->flags & SEC_ALLOC) != 0
  1756. && (elf_tdata (s->owner)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
  1757. && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
  1758. {
  1759. /* Find the output mbind section with the same type, attributes
  1760. and sh_info field. */
  1761. for (os = (void *) lang_os_list.head;
  1762. os != NULL;
  1763. os = os->next)
  1764. if (os->bfd_section != NULL
  1765. && !bfd_is_abs_section (os->bfd_section)
  1766. && (elf_section_flags (os->bfd_section) & SHF_GNU_MBIND) != 0
  1767. && ((s->flags & (SEC_ALLOC
  1768. | SEC_LOAD
  1769. | SEC_HAS_CONTENTS
  1770. | SEC_READONLY
  1771. | SEC_CODE))
  1772. == (os->bfd_section->flags & (SEC_ALLOC
  1773. | SEC_LOAD
  1774. | SEC_HAS_CONTENTS
  1775. | SEC_READONLY
  1776. | SEC_CODE)))
  1777. && (elf_section_data (os->bfd_section)->this_hdr.sh_info
  1778. == elf_section_data (s)->this_hdr.sh_info))
  1779. {
  1780. lang_add_section (&os->children, s, NULL, NULL, os);
  1781. return os;
  1782. }
  1783. /* Create the output mbind section with the ".mbind." prefix
  1784. in section name. */
  1785. if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
  1786. secname = ".mbind.bss";
  1787. else if ((s->flags & SEC_READONLY) == 0)
  1788. secname = ".mbind.data";
  1789. else if ((s->flags & SEC_CODE) == 0)
  1790. secname = ".mbind.rodata";
  1791. else
  1792. secname = ".mbind.text";
  1793. elf_tdata (link_info.output_bfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
  1794. }
  1795. /* Look through the script to see where to place this section. The
  1796. script includes entries added by previous lang_insert_orphan
  1797. calls, so this loop puts multiple compatible orphans of the same
  1798. name into a single output section. */
  1799. if (constraint == 0)
  1800. for (os = lang_output_section_find (secname);
  1801. os != NULL;
  1802. os = next_matching_output_section_statement (os, 0))
  1803. {
  1804. /* If we don't match an existing output section, tell
  1805. lang_insert_orphan to create a new output section. */
  1806. constraint = SPECIAL;
  1807. /* Check to see if we already have an output section statement
  1808. with this name, and its bfd section has compatible flags.
  1809. If the section already exists but does not have any flags
  1810. set, then it has been created by the linker, possibly as a
  1811. result of a --section-start command line switch. */
  1812. if (os->bfd_section != NULL
  1813. && (os->bfd_section->flags == 0
  1814. || (((s->flags ^ os->bfd_section->flags)
  1815. & (SEC_LOAD | SEC_ALLOC)) == 0
  1816. && (!elfinput
  1817. || !elfoutput
  1818. || elf_orphan_compatible (s, os->bfd_section)))))
  1819. {
  1820. lang_add_section (&os->children, s, NULL, NULL, os);
  1821. return os;
  1822. }
  1823. /* Save unused output sections in case we can match them
  1824. against orphans later. */
  1825. if (os->bfd_section == NULL)
  1826. match_by_name = os;
  1827. }
  1828. /* If we didn't match an active output section, see if we matched an
  1829. unused one and use that. */
  1830. if (match_by_name)
  1831. {
  1832. lang_add_section (&match_by_name->children, s, NULL, NULL, match_by_name);
  1833. return match_by_name;
  1834. }
  1835. if (!orphan_init_done)
  1836. {
  1837. struct orphan_save *ho;
  1838. for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
  1839. if (ho->name != NULL)
  1840. {
  1841. ho->os = lang_output_section_find (ho->name);
  1842. if (ho->os != NULL && ho->os->flags == 0)
  1843. ho->os->flags = ho->flags;
  1844. }
  1845. orphan_init_done = 1;
  1846. }
  1847. /* If this is a final link, then always put .gnu.warning.SYMBOL
  1848. sections into the .text section to get them out of the way. */
  1849. if (bfd_link_executable (&link_info)
  1850. && startswith (s->name, ".gnu.warning.")
  1851. && hold[orphan_text].os != NULL)
  1852. {
  1853. os = hold[orphan_text].os;
  1854. lang_add_section (&os->children, s, NULL, NULL, os);
  1855. return os;
  1856. }
  1857. flags = s->flags;
  1858. if (!bfd_link_relocatable (&link_info))
  1859. {
  1860. nexts = s;
  1861. while ((nexts = bfd_get_next_section_by_name (nexts->owner, nexts))
  1862. != NULL)
  1863. if (nexts->output_section == NULL
  1864. && (nexts->flags & SEC_EXCLUDE) == 0
  1865. && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0
  1866. && (nexts->owner->flags & DYNAMIC) == 0
  1867. && !bfd_input_just_syms (nexts->owner)
  1868. && _bfd_elf_match_sections_by_type (nexts->owner, nexts,
  1869. s->owner, s))
  1870. flags = (((flags ^ SEC_READONLY)
  1871. | (nexts->flags ^ SEC_READONLY))
  1872. ^ SEC_READONLY);
  1873. }
  1874. /* Decide which segment the section should go in based on the
  1875. section name and section flags. We put loadable .note sections
  1876. right after the .interp section, so that the PT_NOTE segment is
  1877. stored right after the program headers where the OS can read it
  1878. in the first page. */
  1879. place = NULL;
  1880. if ((flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
  1881. place = &hold[orphan_nonalloc];
  1882. else if ((flags & SEC_ALLOC) == 0)
  1883. ;
  1884. else if ((flags & SEC_LOAD) != 0
  1885. && (elfinput
  1886. ? sh_type == SHT_NOTE
  1887. : startswith (secname, ".note")))
  1888. place = &hold[orphan_interp];
  1889. else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0)
  1890. place = &hold[orphan_bss];
  1891. else if ((flags & SEC_SMALL_DATA) != 0)
  1892. place = &hold[orphan_sdata];
  1893. else if ((flags & SEC_THREAD_LOCAL) != 0)
  1894. place = &hold[orphan_tdata];
  1895. else if ((flags & SEC_READONLY) == 0)
  1896. place = &hold[orphan_data];
  1897. else if ((flags & SEC_LOAD) != 0
  1898. && (elfinput
  1899. ? sh_type == SHT_RELA || sh_type == SHT_REL
  1900. : startswith (secname, ".rel")))
  1901. place = &hold[orphan_rel];
  1902. else if ((flags & SEC_CODE) == 0)
  1903. place = &hold[orphan_rodata];
  1904. else
  1905. place = &hold[orphan_text];
  1906. after = NULL;
  1907. if (place != NULL)
  1908. {
  1909. if (place->os == NULL)
  1910. {
  1911. if (place->name != NULL)
  1912. place->os = lang_output_section_find (place->name);
  1913. else
  1914. {
  1915. int rela = elfinput ? sh_type == SHT_RELA : secname[4] == 'a';
  1916. place->os = output_rel_find (isdyn, rela);
  1917. }
  1918. }
  1919. after = place->os;
  1920. if (after == NULL)
  1921. after
  1922. = lang_output_section_find_by_flags (s, flags, &place->os,
  1923. _bfd_elf_match_sections_by_type);
  1924. if (after == NULL)
  1925. /* *ABS* is always the first output section statement. */
  1926. after = (void *) lang_os_list.head;
  1927. }
  1928. return lang_insert_orphan (s, secname, constraint, after, place, NULL, NULL);
  1929. }
  1930. void
  1931. ldelf_before_place_orphans (void)
  1932. {
  1933. bfd *abfd;
  1934. for (abfd = link_info.input_bfds;
  1935. abfd != (bfd *) NULL; abfd = abfd->link.next)
  1936. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  1937. && bfd_count_sections (abfd) != 0
  1938. && !bfd_input_just_syms (abfd))
  1939. {
  1940. asection *isec;
  1941. for (isec = abfd->sections; isec != NULL; isec = isec->next)
  1942. {
  1943. /* Discard a section if any of its linked-to section has
  1944. been discarded. */
  1945. asection *linked_to_sec;
  1946. for (linked_to_sec = elf_linked_to_section (isec);
  1947. linked_to_sec != NULL && !linked_to_sec->linker_mark;
  1948. linked_to_sec = elf_linked_to_section (linked_to_sec))
  1949. {
  1950. if (discarded_section (linked_to_sec))
  1951. {
  1952. isec->output_section = bfd_abs_section_ptr;
  1953. isec->flags |= SEC_EXCLUDE;
  1954. break;
  1955. }
  1956. linked_to_sec->linker_mark = 1;
  1957. }
  1958. for (linked_to_sec = elf_linked_to_section (isec);
  1959. linked_to_sec != NULL && linked_to_sec->linker_mark;
  1960. linked_to_sec = elf_linked_to_section (linked_to_sec))
  1961. linked_to_sec->linker_mark = 0;
  1962. }
  1963. }
  1964. }
  1965. void
  1966. ldelf_set_output_arch (void)
  1967. {
  1968. set_output_arch_default ();
  1969. if (link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour)
  1970. elf_link_info (link_info.output_bfd) = &link_info;
  1971. }