ldwrite.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* ldwrite.c -- write out the linked file
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. Written by Steve Chamberlain sac@cygnus.com
  4. This file is part of the GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "libiberty.h"
  21. #include "ctf-api.h"
  22. #include "safe-ctype.h"
  23. #include "ld.h"
  24. #include "ldexp.h"
  25. #include "ldlang.h"
  26. #include "ldwrite.h"
  27. #include "ldmisc.h"
  28. #include <ldgram.h>
  29. #include "ldmain.h"
  30. /* Build link_order structures for the BFD linker. */
  31. static void
  32. build_link_order (lang_statement_union_type *statement)
  33. {
  34. switch (statement->header.type)
  35. {
  36. case lang_data_statement_enum:
  37. {
  38. asection *output_section;
  39. struct bfd_link_order *link_order;
  40. bfd_vma value;
  41. output_section = statement->data_statement.output_section;
  42. ASSERT (output_section->owner == link_info.output_bfd);
  43. if (!((output_section->flags & SEC_HAS_CONTENTS) != 0
  44. || ((output_section->flags & SEC_LOAD) != 0
  45. && (output_section->flags & SEC_THREAD_LOCAL))))
  46. break;
  47. link_order = bfd_new_link_order (link_info.output_bfd, output_section);
  48. if (link_order == NULL)
  49. einfo (_("%F%P: bfd_new_link_order failed\n"));
  50. link_order->type = bfd_data_link_order;
  51. link_order->offset = statement->data_statement.output_offset;
  52. link_order->u.data.contents = (bfd_byte *) xmalloc (QUAD_SIZE);
  53. value = statement->data_statement.value;
  54. /* By convention, the bfd_put routines for an unknown
  55. endianness are big endian, so we must swap here if the
  56. input is little endian. */
  57. if (!bfd_big_endian (link_info.output_bfd)
  58. && !bfd_little_endian (link_info.output_bfd)
  59. && !link_info.big_endian)
  60. {
  61. bfd_byte buffer[8];
  62. switch (statement->data_statement.type)
  63. {
  64. case QUAD:
  65. case SQUAD:
  66. if (sizeof (bfd_vma) >= QUAD_SIZE)
  67. {
  68. bfd_putl64 (value, buffer);
  69. value = bfd_getb64 (buffer);
  70. break;
  71. }
  72. /* Fall through. */
  73. case LONG:
  74. bfd_putl32 (value, buffer);
  75. value = bfd_getb32 (buffer);
  76. break;
  77. case SHORT:
  78. bfd_putl16 (value, buffer);
  79. value = bfd_getb16 (buffer);
  80. break;
  81. case BYTE:
  82. break;
  83. default:
  84. abort ();
  85. }
  86. }
  87. ASSERT (output_section->owner == link_info.output_bfd);
  88. switch (statement->data_statement.type)
  89. {
  90. case QUAD:
  91. case SQUAD:
  92. if (sizeof (bfd_vma) >= QUAD_SIZE)
  93. bfd_put_64 (link_info.output_bfd, value,
  94. link_order->u.data.contents);
  95. else
  96. {
  97. bfd_vma high;
  98. if (statement->data_statement.type == QUAD)
  99. high = 0;
  100. else if ((value & 0x80000000) == 0)
  101. high = 0;
  102. else
  103. high = (bfd_vma) -1;
  104. bfd_put_32 (link_info.output_bfd, high,
  105. (link_order->u.data.contents
  106. + (link_info.big_endian ? 0 : 4)));
  107. bfd_put_32 (link_info.output_bfd, value,
  108. (link_order->u.data.contents
  109. + (link_info.big_endian ? 4 : 0)));
  110. }
  111. link_order->size = QUAD_SIZE;
  112. break;
  113. case LONG:
  114. bfd_put_32 (link_info.output_bfd, value,
  115. link_order->u.data.contents);
  116. link_order->size = LONG_SIZE;
  117. break;
  118. case SHORT:
  119. bfd_put_16 (link_info.output_bfd, value,
  120. link_order->u.data.contents);
  121. link_order->size = SHORT_SIZE;
  122. break;
  123. case BYTE:
  124. bfd_put_8 (link_info.output_bfd, value,
  125. link_order->u.data.contents);
  126. link_order->size = BYTE_SIZE;
  127. break;
  128. default:
  129. abort ();
  130. }
  131. link_order->u.data.size = link_order->size;
  132. }
  133. break;
  134. case lang_reloc_statement_enum:
  135. {
  136. lang_reloc_statement_type *rs;
  137. asection *output_section;
  138. struct bfd_link_order *link_order;
  139. rs = &statement->reloc_statement;
  140. output_section = rs->output_section;
  141. ASSERT (output_section->owner == link_info.output_bfd);
  142. if (!((output_section->flags & SEC_HAS_CONTENTS) != 0
  143. || ((output_section->flags & SEC_LOAD) != 0
  144. && (output_section->flags & SEC_THREAD_LOCAL))))
  145. break;
  146. link_order = bfd_new_link_order (link_info.output_bfd, output_section);
  147. if (link_order == NULL)
  148. einfo (_("%F%P: bfd_new_link_order failed\n"));
  149. link_order->offset = rs->output_offset;
  150. link_order->size = bfd_get_reloc_size (rs->howto);
  151. link_order->u.reloc.p = (struct bfd_link_order_reloc *)
  152. xmalloc (sizeof (struct bfd_link_order_reloc));
  153. link_order->u.reloc.p->reloc = rs->reloc;
  154. link_order->u.reloc.p->addend = rs->addend_value;
  155. if (rs->name == NULL)
  156. {
  157. link_order->type = bfd_section_reloc_link_order;
  158. if (rs->section->owner == link_info.output_bfd)
  159. link_order->u.reloc.p->u.section = rs->section;
  160. else
  161. {
  162. link_order->u.reloc.p->u.section = rs->section->output_section;
  163. link_order->u.reloc.p->addend += rs->section->output_offset;
  164. }
  165. }
  166. else
  167. {
  168. link_order->type = bfd_symbol_reloc_link_order;
  169. link_order->u.reloc.p->u.name = rs->name;
  170. }
  171. }
  172. break;
  173. case lang_input_section_enum:
  174. {
  175. /* Create a new link_order in the output section with this
  176. attached */
  177. asection *i = statement->input_section.section;
  178. if (i->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
  179. && (i->flags & SEC_EXCLUDE) == 0)
  180. {
  181. asection *output_section = i->output_section;
  182. struct bfd_link_order *link_order;
  183. ASSERT (output_section->owner == link_info.output_bfd);
  184. if (!((output_section->flags & SEC_HAS_CONTENTS) != 0
  185. || ((output_section->flags & SEC_LOAD) != 0
  186. && (output_section->flags & SEC_THREAD_LOCAL))))
  187. break;
  188. link_order = bfd_new_link_order (link_info.output_bfd,
  189. output_section);
  190. if (link_order == NULL)
  191. einfo (_("%F%P: bfd_new_link_order failed\n"));
  192. if ((i->flags & SEC_NEVER_LOAD) != 0
  193. && (i->flags & SEC_DEBUGGING) == 0)
  194. {
  195. /* We've got a never load section inside one which is
  196. going to be output, we'll change it into a fill. */
  197. link_order->type = bfd_data_link_order;
  198. link_order->u.data.contents = (unsigned char *) "";
  199. link_order->u.data.size = 1;
  200. }
  201. else
  202. {
  203. link_order->type = bfd_indirect_link_order;
  204. link_order->u.indirect.section = i;
  205. ASSERT (i->output_section == output_section);
  206. }
  207. link_order->size = i->size;
  208. link_order->offset = i->output_offset;
  209. }
  210. }
  211. break;
  212. case lang_padding_statement_enum:
  213. /* Make a new link_order with the right filler */
  214. {
  215. asection *output_section;
  216. struct bfd_link_order *link_order;
  217. output_section = statement->padding_statement.output_section;
  218. ASSERT (statement->padding_statement.output_section->owner
  219. == link_info.output_bfd);
  220. if (!((output_section->flags & SEC_HAS_CONTENTS) != 0
  221. || ((output_section->flags & SEC_LOAD) != 0
  222. && (output_section->flags & SEC_THREAD_LOCAL))))
  223. break;
  224. link_order = bfd_new_link_order (link_info.output_bfd,
  225. output_section);
  226. if (link_order == NULL)
  227. einfo (_("%F%P: bfd_new_link_order failed\n"));
  228. link_order->type = bfd_data_link_order;
  229. link_order->size = statement->padding_statement.size;
  230. link_order->offset = statement->padding_statement.output_offset;
  231. link_order->u.data.contents = statement->padding_statement.fill->data;
  232. link_order->u.data.size = statement->padding_statement.fill->size;
  233. }
  234. break;
  235. default:
  236. /* All the other ones fall through */
  237. break;
  238. }
  239. }
  240. /* Return true if NAME is the name of an unsplittable section. These
  241. are the stabs strings, dwarf strings. */
  242. static bool
  243. unsplittable_name (const char *name)
  244. {
  245. if (startswith (name, ".stab"))
  246. {
  247. /* There are several stab like string sections. We pattern match on
  248. ".stab...str" */
  249. unsigned len = strlen (name);
  250. if (strcmp (&name[len-3], "str") == 0)
  251. return true;
  252. }
  253. else if (strcmp (name, "$GDB_STRINGS$") == 0)
  254. return true;
  255. return false;
  256. }
  257. /* Wander around the input sections, make sure that
  258. we'll never try and create an output section with more relocs
  259. than will fit.. Do this by always assuming the worst case, and
  260. creating new output sections with all the right bits. */
  261. #define TESTIT 1
  262. static asection *
  263. clone_section (bfd *abfd, asection *s, const char *name, int *count)
  264. {
  265. char *tname;
  266. char *sname;
  267. unsigned int len;
  268. asection *n;
  269. struct bfd_link_hash_entry *h;
  270. /* Invent a section name from the section name and a dotted numeric
  271. suffix. */
  272. len = strlen (name);
  273. tname = (char *) xmalloc (len + 1);
  274. memcpy (tname, name, len + 1);
  275. /* Remove a dotted number suffix, from a previous split link. */
  276. while (len && ISDIGIT (tname[len-1]))
  277. len--;
  278. if (len > 1 && tname[len-1] == '.')
  279. /* It was a dotted number. */
  280. tname[len-1] = 0;
  281. /* We want to use the whole of the original section name for the
  282. split name, but coff can be restricted to 8 character names. */
  283. if (bfd_family_coff (abfd) && strlen (tname) > 5)
  284. {
  285. /* Some section names cannot be truncated, as the name is
  286. used to locate some other section. */
  287. if (startswith (name, ".stab")
  288. || strcmp (name, "$GDB_SYMBOLS$") == 0)
  289. {
  290. einfo (_ ("%F%P: cannot create split section name for %s\n"), name);
  291. /* Silence gcc warnings. einfo exits, so we never reach here. */
  292. return NULL;
  293. }
  294. tname[5] = 0;
  295. }
  296. if ((sname = bfd_get_unique_section_name (abfd, tname, count)) == NULL
  297. || (n = bfd_make_section_anyway (abfd, sname)) == NULL
  298. || (h = bfd_link_hash_lookup (link_info.hash,
  299. sname, true, true, false)) == NULL)
  300. {
  301. einfo (_("%F%P: clone section failed: %E\n"));
  302. /* Silence gcc warnings. einfo exits, so we never reach here. */
  303. return NULL;
  304. }
  305. free (tname);
  306. /* Set up section symbol. */
  307. h->type = bfd_link_hash_defined;
  308. h->u.def.value = 0;
  309. h->u.def.section = n;
  310. n->flags = s->flags;
  311. n->vma = s->vma;
  312. n->user_set_vma = s->user_set_vma;
  313. n->lma = s->lma;
  314. n->size = 0;
  315. n->output_offset = s->output_offset;
  316. n->output_section = n;
  317. n->orelocation = 0;
  318. n->reloc_count = 0;
  319. n->alignment_power = s->alignment_power;
  320. bfd_copy_private_section_data (abfd, s, abfd, n);
  321. return n;
  322. }
  323. #if TESTING
  324. static void
  325. ds (asection *s)
  326. {
  327. struct bfd_link_order *l = s->map_head.link_order;
  328. printf ("vma %x size %x\n", s->vma, s->size);
  329. while (l)
  330. {
  331. if (l->type == bfd_indirect_link_order)
  332. printf ("%8x %s\n", l->offset, l->u.indirect.section->owner->filename);
  333. else
  334. printf (_("%8x something else\n"), l->offset);
  335. l = l->next;
  336. }
  337. printf ("\n");
  338. }
  339. dump (char *s, asection *a1, asection *a2)
  340. {
  341. printf ("%s\n", s);
  342. ds (a1);
  343. ds (a2);
  344. }
  345. static void
  346. sanity_check (bfd *abfd)
  347. {
  348. asection *s;
  349. for (s = abfd->sections; s; s = s->next)
  350. {
  351. struct bfd_link_order *p;
  352. bfd_vma prev = 0;
  353. for (p = s->map_head.link_order; p; p = p->next)
  354. {
  355. if (p->offset > 100000)
  356. abort ();
  357. if (p->offset < prev)
  358. abort ();
  359. prev = p->offset;
  360. }
  361. }
  362. }
  363. #else
  364. #define sanity_check(a)
  365. #define dump(a, b, c)
  366. #endif
  367. static void
  368. split_sections (bfd *abfd, struct bfd_link_info *info)
  369. {
  370. asection *original_sec;
  371. int nsecs = abfd->section_count;
  372. sanity_check (abfd);
  373. /* Look through all the original sections. */
  374. for (original_sec = abfd->sections;
  375. original_sec && nsecs;
  376. original_sec = original_sec->next, nsecs--)
  377. {
  378. int count = 0;
  379. unsigned int lines = 0;
  380. unsigned int relocs = 0;
  381. bfd_size_type sec_size = 0;
  382. struct bfd_link_order *l;
  383. struct bfd_link_order *p;
  384. bfd_vma vma = original_sec->vma;
  385. asection *cursor = original_sec;
  386. /* Count up the relocations and line entries to see if anything
  387. would be too big to fit. Accumulate section size too. */
  388. for (l = NULL, p = cursor->map_head.link_order; p != NULL; p = l->next)
  389. {
  390. unsigned int thislines = 0;
  391. unsigned int thisrelocs = 0;
  392. bfd_size_type thissize = 0;
  393. if (p->type == bfd_indirect_link_order)
  394. {
  395. asection *sec;
  396. sec = p->u.indirect.section;
  397. if (info->strip == strip_none
  398. || info->strip == strip_some)
  399. thislines = sec->lineno_count;
  400. if (bfd_link_relocatable (info))
  401. thisrelocs = sec->reloc_count;
  402. thissize = sec->size;
  403. }
  404. else if (bfd_link_relocatable (info)
  405. && (p->type == bfd_section_reloc_link_order
  406. || p->type == bfd_symbol_reloc_link_order))
  407. thisrelocs++;
  408. if (l != NULL
  409. && (thisrelocs + relocs >= config.split_by_reloc
  410. || thislines + lines >= config.split_by_reloc
  411. || (thissize + sec_size >= config.split_by_file))
  412. && !unsplittable_name (cursor->name))
  413. {
  414. /* Create a new section and put this link order and the
  415. following link orders into it. */
  416. bfd_vma shift_offset;
  417. asection *n;
  418. n = clone_section (abfd, cursor, original_sec->name, &count);
  419. /* Attach the link orders to the new section and snip
  420. them off from the old section. */
  421. n->map_head.link_order = p;
  422. n->map_tail.link_order = cursor->map_tail.link_order;
  423. cursor->map_tail.link_order = l;
  424. l->next = NULL;
  425. l = p;
  426. /* Change the size of the original section and
  427. update the vma of the new one. */
  428. dump ("before snip", cursor, n);
  429. shift_offset = p->offset;
  430. n->size = cursor->size - shift_offset;
  431. cursor->size = shift_offset;
  432. vma += shift_offset;
  433. n->lma = n->vma = vma;
  434. /* Run down the chain and change the output section to
  435. the right one, update the offsets too. */
  436. do
  437. {
  438. p->offset -= shift_offset;
  439. if (p->type == bfd_indirect_link_order)
  440. {
  441. p->u.indirect.section->output_section = n;
  442. p->u.indirect.section->output_offset = p->offset;
  443. }
  444. p = p->next;
  445. }
  446. while (p);
  447. dump ("after snip", cursor, n);
  448. cursor = n;
  449. relocs = thisrelocs;
  450. lines = thislines;
  451. sec_size = thissize;
  452. }
  453. else
  454. {
  455. l = p;
  456. relocs += thisrelocs;
  457. lines += thislines;
  458. sec_size += thissize;
  459. }
  460. }
  461. }
  462. sanity_check (abfd);
  463. }
  464. /* Call BFD to write out the linked file. */
  465. void
  466. ldwrite (void)
  467. {
  468. /* Reset error indicator, which can typically something like invalid
  469. format from opening up the .o files. */
  470. bfd_set_error (bfd_error_no_error);
  471. lang_clear_os_map ();
  472. lang_for_each_statement (build_link_order);
  473. if (config.split_by_reloc != (unsigned) -1
  474. || config.split_by_file != (bfd_size_type) -1)
  475. split_sections (link_info.output_bfd, &link_info);
  476. if (!bfd_final_link (link_info.output_bfd, &link_info))
  477. {
  478. /* If there was an error recorded, print it out. Otherwise assume
  479. an appropriate error message like unknown symbol was printed
  480. out. */
  481. if (bfd_get_error () != bfd_error_no_error)
  482. einfo (_("%F%P: final link failed: %E\n"));
  483. else
  484. xexit (1);
  485. }
  486. }