elf-eh-frame.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569
  1. /* .eh_frame section optimization.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. Written by Jakub Jelinek <jakub@redhat.com>.
  4. This file is part of BFD, the Binary File Descriptor library.
  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 "libbfd.h"
  20. #include "elf-bfd.h"
  21. #include "dwarf2.h"
  22. #define EH_FRAME_HDR_SIZE 8
  23. struct cie
  24. {
  25. unsigned int length;
  26. unsigned int hash;
  27. unsigned char version;
  28. unsigned char local_personality;
  29. char augmentation[20];
  30. bfd_vma code_align;
  31. bfd_signed_vma data_align;
  32. bfd_vma ra_column;
  33. bfd_vma augmentation_size;
  34. union {
  35. struct elf_link_hash_entry *h;
  36. struct {
  37. unsigned int bfd_id;
  38. unsigned int index;
  39. } sym;
  40. unsigned int reloc_index;
  41. } personality;
  42. struct eh_cie_fde *cie_inf;
  43. unsigned char per_encoding;
  44. unsigned char lsda_encoding;
  45. unsigned char fde_encoding;
  46. unsigned char initial_insn_length;
  47. unsigned char can_make_lsda_relative;
  48. unsigned char initial_instructions[50];
  49. };
  50. /* If *ITER hasn't reached END yet, read the next byte into *RESULT and
  51. move onto the next byte. Return true on success. */
  52. static inline bool
  53. read_byte (bfd_byte **iter, bfd_byte *end, unsigned char *result)
  54. {
  55. if (*iter >= end)
  56. return false;
  57. *result = *((*iter)++);
  58. return true;
  59. }
  60. /* Move *ITER over LENGTH bytes, or up to END, whichever is closer.
  61. Return true it was possible to move LENGTH bytes. */
  62. static inline bool
  63. skip_bytes (bfd_byte **iter, bfd_byte *end, bfd_size_type length)
  64. {
  65. if ((bfd_size_type) (end - *iter) < length)
  66. {
  67. *iter = end;
  68. return false;
  69. }
  70. *iter += length;
  71. return true;
  72. }
  73. /* Move *ITER over an leb128, stopping at END. Return true if the end
  74. of the leb128 was found. */
  75. static bool
  76. skip_leb128 (bfd_byte **iter, bfd_byte *end)
  77. {
  78. unsigned char byte;
  79. do
  80. if (!read_byte (iter, end, &byte))
  81. return false;
  82. while (byte & 0x80);
  83. return true;
  84. }
  85. /* Like skip_leb128, but treat the leb128 as an unsigned value and
  86. store it in *VALUE. */
  87. static bool
  88. read_uleb128 (bfd_byte **iter, bfd_byte *end, bfd_vma *value)
  89. {
  90. bfd_byte *start, *p;
  91. start = *iter;
  92. if (!skip_leb128 (iter, end))
  93. return false;
  94. p = *iter;
  95. *value = *--p;
  96. while (p > start)
  97. *value = (*value << 7) | (*--p & 0x7f);
  98. return true;
  99. }
  100. /* Like read_uleb128, but for signed values. */
  101. static bool
  102. read_sleb128 (bfd_byte **iter, bfd_byte *end, bfd_signed_vma *value)
  103. {
  104. bfd_byte *start, *p;
  105. start = *iter;
  106. if (!skip_leb128 (iter, end))
  107. return false;
  108. p = *iter;
  109. *value = ((*--p & 0x7f) ^ 0x40) - 0x40;
  110. while (p > start)
  111. *value = (*value << 7) | (*--p & 0x7f);
  112. return true;
  113. }
  114. /* Return 0 if either encoding is variable width, or not yet known to bfd. */
  115. static
  116. int get_DW_EH_PE_width (int encoding, int ptr_size)
  117. {
  118. /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
  119. was added to bfd. */
  120. if ((encoding & 0x60) == 0x60)
  121. return 0;
  122. switch (encoding & 7)
  123. {
  124. case DW_EH_PE_udata2: return 2;
  125. case DW_EH_PE_udata4: return 4;
  126. case DW_EH_PE_udata8: return 8;
  127. case DW_EH_PE_absptr: return ptr_size;
  128. default:
  129. break;
  130. }
  131. return 0;
  132. }
  133. #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0)
  134. /* Read a width sized value from memory. */
  135. static bfd_vma
  136. read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed)
  137. {
  138. bfd_vma value;
  139. switch (width)
  140. {
  141. case 2:
  142. if (is_signed)
  143. value = bfd_get_signed_16 (abfd, buf);
  144. else
  145. value = bfd_get_16 (abfd, buf);
  146. break;
  147. case 4:
  148. if (is_signed)
  149. value = bfd_get_signed_32 (abfd, buf);
  150. else
  151. value = bfd_get_32 (abfd, buf);
  152. break;
  153. case 8:
  154. if (is_signed)
  155. value = bfd_get_signed_64 (abfd, buf);
  156. else
  157. value = bfd_get_64 (abfd, buf);
  158. break;
  159. default:
  160. BFD_FAIL ();
  161. return 0;
  162. }
  163. return value;
  164. }
  165. /* Store a width sized value to memory. */
  166. static void
  167. write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width)
  168. {
  169. switch (width)
  170. {
  171. case 2: bfd_put_16 (abfd, value, buf); break;
  172. case 4: bfd_put_32 (abfd, value, buf); break;
  173. case 8: bfd_put_64 (abfd, value, buf); break;
  174. default: BFD_FAIL ();
  175. }
  176. }
  177. /* Return one if C1 and C2 CIEs can be merged. */
  178. static int
  179. cie_eq (const void *e1, const void *e2)
  180. {
  181. const struct cie *c1 = (const struct cie *) e1;
  182. const struct cie *c2 = (const struct cie *) e2;
  183. if (c1->hash == c2->hash
  184. && c1->length == c2->length
  185. && c1->version == c2->version
  186. && c1->local_personality == c2->local_personality
  187. && strcmp (c1->augmentation, c2->augmentation) == 0
  188. && strcmp (c1->augmentation, "eh") != 0
  189. && c1->code_align == c2->code_align
  190. && c1->data_align == c2->data_align
  191. && c1->ra_column == c2->ra_column
  192. && c1->augmentation_size == c2->augmentation_size
  193. && memcmp (&c1->personality, &c2->personality,
  194. sizeof (c1->personality)) == 0
  195. && (c1->cie_inf->u.cie.u.sec->output_section
  196. == c2->cie_inf->u.cie.u.sec->output_section)
  197. && c1->per_encoding == c2->per_encoding
  198. && c1->lsda_encoding == c2->lsda_encoding
  199. && c1->fde_encoding == c2->fde_encoding
  200. && c1->initial_insn_length == c2->initial_insn_length
  201. && c1->initial_insn_length <= sizeof (c1->initial_instructions)
  202. && memcmp (c1->initial_instructions,
  203. c2->initial_instructions,
  204. c1->initial_insn_length) == 0)
  205. return 1;
  206. return 0;
  207. }
  208. static hashval_t
  209. cie_hash (const void *e)
  210. {
  211. const struct cie *c = (const struct cie *) e;
  212. return c->hash;
  213. }
  214. static hashval_t
  215. cie_compute_hash (struct cie *c)
  216. {
  217. hashval_t h = 0;
  218. size_t len;
  219. h = iterative_hash_object (c->length, h);
  220. h = iterative_hash_object (c->version, h);
  221. h = iterative_hash (c->augmentation, strlen (c->augmentation) + 1, h);
  222. h = iterative_hash_object (c->code_align, h);
  223. h = iterative_hash_object (c->data_align, h);
  224. h = iterative_hash_object (c->ra_column, h);
  225. h = iterative_hash_object (c->augmentation_size, h);
  226. h = iterative_hash_object (c->personality, h);
  227. h = iterative_hash_object (c->cie_inf->u.cie.u.sec->output_section, h);
  228. h = iterative_hash_object (c->per_encoding, h);
  229. h = iterative_hash_object (c->lsda_encoding, h);
  230. h = iterative_hash_object (c->fde_encoding, h);
  231. h = iterative_hash_object (c->initial_insn_length, h);
  232. len = c->initial_insn_length;
  233. if (len > sizeof (c->initial_instructions))
  234. len = sizeof (c->initial_instructions);
  235. h = iterative_hash (c->initial_instructions, len, h);
  236. c->hash = h;
  237. return h;
  238. }
  239. /* Return the number of extra bytes that we'll be inserting into
  240. ENTRY's augmentation string. */
  241. static inline unsigned int
  242. extra_augmentation_string_bytes (struct eh_cie_fde *entry)
  243. {
  244. unsigned int size = 0;
  245. if (entry->cie)
  246. {
  247. if (entry->add_augmentation_size)
  248. size++;
  249. if (entry->u.cie.add_fde_encoding)
  250. size++;
  251. }
  252. return size;
  253. }
  254. /* Likewise ENTRY's augmentation data. */
  255. static inline unsigned int
  256. extra_augmentation_data_bytes (struct eh_cie_fde *entry)
  257. {
  258. unsigned int size = 0;
  259. if (entry->add_augmentation_size)
  260. size++;
  261. if (entry->cie && entry->u.cie.add_fde_encoding)
  262. size++;
  263. return size;
  264. }
  265. /* Return the size that ENTRY will have in the output. */
  266. static unsigned int
  267. size_of_output_cie_fde (struct eh_cie_fde *entry)
  268. {
  269. if (entry->removed)
  270. return 0;
  271. if (entry->size == 4)
  272. return 4;
  273. return (entry->size
  274. + extra_augmentation_string_bytes (entry)
  275. + extra_augmentation_data_bytes (entry));
  276. }
  277. /* Return the offset of the FDE or CIE after ENT. */
  278. static unsigned int
  279. next_cie_fde_offset (const struct eh_cie_fde *ent,
  280. const struct eh_cie_fde *last,
  281. const asection *sec)
  282. {
  283. while (++ent < last)
  284. {
  285. if (!ent->removed)
  286. return ent->new_offset;
  287. }
  288. return sec->size;
  289. }
  290. /* Assume that the bytes between *ITER and END are CFA instructions.
  291. Try to move *ITER past the first instruction and return true on
  292. success. ENCODED_PTR_WIDTH gives the width of pointer entries. */
  293. static bool
  294. skip_cfa_op (bfd_byte **iter, bfd_byte *end, unsigned int encoded_ptr_width)
  295. {
  296. bfd_byte op;
  297. bfd_vma length;
  298. if (!read_byte (iter, end, &op))
  299. return false;
  300. switch (op & 0xc0 ? op & 0xc0 : op)
  301. {
  302. case DW_CFA_nop:
  303. case DW_CFA_advance_loc:
  304. case DW_CFA_restore:
  305. case DW_CFA_remember_state:
  306. case DW_CFA_restore_state:
  307. case DW_CFA_GNU_window_save:
  308. /* No arguments. */
  309. return true;
  310. case DW_CFA_offset:
  311. case DW_CFA_restore_extended:
  312. case DW_CFA_undefined:
  313. case DW_CFA_same_value:
  314. case DW_CFA_def_cfa_register:
  315. case DW_CFA_def_cfa_offset:
  316. case DW_CFA_def_cfa_offset_sf:
  317. case DW_CFA_GNU_args_size:
  318. /* One leb128 argument. */
  319. return skip_leb128 (iter, end);
  320. case DW_CFA_val_offset:
  321. case DW_CFA_val_offset_sf:
  322. case DW_CFA_offset_extended:
  323. case DW_CFA_register:
  324. case DW_CFA_def_cfa:
  325. case DW_CFA_offset_extended_sf:
  326. case DW_CFA_GNU_negative_offset_extended:
  327. case DW_CFA_def_cfa_sf:
  328. /* Two leb128 arguments. */
  329. return (skip_leb128 (iter, end)
  330. && skip_leb128 (iter, end));
  331. case DW_CFA_def_cfa_expression:
  332. /* A variable-length argument. */
  333. return (read_uleb128 (iter, end, &length)
  334. && skip_bytes (iter, end, length));
  335. case DW_CFA_expression:
  336. case DW_CFA_val_expression:
  337. /* A leb128 followed by a variable-length argument. */
  338. return (skip_leb128 (iter, end)
  339. && read_uleb128 (iter, end, &length)
  340. && skip_bytes (iter, end, length));
  341. case DW_CFA_set_loc:
  342. return skip_bytes (iter, end, encoded_ptr_width);
  343. case DW_CFA_advance_loc1:
  344. return skip_bytes (iter, end, 1);
  345. case DW_CFA_advance_loc2:
  346. return skip_bytes (iter, end, 2);
  347. case DW_CFA_advance_loc4:
  348. return skip_bytes (iter, end, 4);
  349. case DW_CFA_MIPS_advance_loc8:
  350. return skip_bytes (iter, end, 8);
  351. default:
  352. return false;
  353. }
  354. }
  355. /* Try to interpret the bytes between BUF and END as CFA instructions.
  356. If every byte makes sense, return a pointer to the first DW_CFA_nop
  357. padding byte, or END if there is no padding. Return null otherwise.
  358. ENCODED_PTR_WIDTH is as for skip_cfa_op. */
  359. static bfd_byte *
  360. skip_non_nops (bfd_byte *buf, bfd_byte *end, unsigned int encoded_ptr_width,
  361. unsigned int *set_loc_count)
  362. {
  363. bfd_byte *last;
  364. last = buf;
  365. while (buf < end)
  366. if (*buf == DW_CFA_nop)
  367. buf++;
  368. else
  369. {
  370. if (*buf == DW_CFA_set_loc)
  371. ++*set_loc_count;
  372. if (!skip_cfa_op (&buf, end, encoded_ptr_width))
  373. return 0;
  374. last = buf;
  375. }
  376. return last;
  377. }
  378. /* Convert absolute encoding ENCODING into PC-relative form.
  379. SIZE is the size of a pointer. */
  380. static unsigned char
  381. make_pc_relative (unsigned char encoding, unsigned int ptr_size)
  382. {
  383. if ((encoding & 0x7f) == DW_EH_PE_absptr)
  384. switch (ptr_size)
  385. {
  386. case 2:
  387. encoding |= DW_EH_PE_sdata2;
  388. break;
  389. case 4:
  390. encoding |= DW_EH_PE_sdata4;
  391. break;
  392. case 8:
  393. encoding |= DW_EH_PE_sdata8;
  394. break;
  395. }
  396. return encoding | DW_EH_PE_pcrel;
  397. }
  398. /* Examine each .eh_frame_entry section and discard those
  399. those that are marked SEC_EXCLUDE. */
  400. static void
  401. bfd_elf_discard_eh_frame_entry (struct eh_frame_hdr_info *hdr_info)
  402. {
  403. unsigned int i;
  404. for (i = 0; i < hdr_info->array_count; i++)
  405. {
  406. if (hdr_info->u.compact.entries[i]->flags & SEC_EXCLUDE)
  407. {
  408. unsigned int j;
  409. for (j = i + 1; j < hdr_info->array_count; j++)
  410. hdr_info->u.compact.entries[j-1] = hdr_info->u.compact.entries[j];
  411. hdr_info->array_count--;
  412. hdr_info->u.compact.entries[hdr_info->array_count] = NULL;
  413. i--;
  414. }
  415. }
  416. }
  417. /* Add a .eh_frame_entry section. */
  418. static void
  419. bfd_elf_record_eh_frame_entry (struct eh_frame_hdr_info *hdr_info,
  420. asection *sec)
  421. {
  422. if (hdr_info->array_count == hdr_info->u.compact.allocated_entries)
  423. {
  424. if (hdr_info->u.compact.allocated_entries == 0)
  425. {
  426. hdr_info->frame_hdr_is_compact = true;
  427. hdr_info->u.compact.allocated_entries = 2;
  428. hdr_info->u.compact.entries =
  429. bfd_malloc (hdr_info->u.compact.allocated_entries
  430. * sizeof (hdr_info->u.compact.entries[0]));
  431. }
  432. else
  433. {
  434. hdr_info->u.compact.allocated_entries *= 2;
  435. hdr_info->u.compact.entries =
  436. bfd_realloc (hdr_info->u.compact.entries,
  437. hdr_info->u.compact.allocated_entries
  438. * sizeof (hdr_info->u.compact.entries[0]));
  439. }
  440. BFD_ASSERT (hdr_info->u.compact.entries);
  441. }
  442. hdr_info->u.compact.entries[hdr_info->array_count++] = sec;
  443. }
  444. /* Parse a .eh_frame_entry section. Figure out which text section it
  445. references. */
  446. bool
  447. _bfd_elf_parse_eh_frame_entry (struct bfd_link_info *info,
  448. asection *sec, struct elf_reloc_cookie *cookie)
  449. {
  450. struct elf_link_hash_table *htab;
  451. struct eh_frame_hdr_info *hdr_info;
  452. unsigned long r_symndx;
  453. asection *text_sec;
  454. htab = elf_hash_table (info);
  455. hdr_info = &htab->eh_info;
  456. if (sec->size == 0
  457. || sec->sec_info_type != SEC_INFO_TYPE_NONE)
  458. {
  459. return true;
  460. }
  461. if (sec->output_section && bfd_is_abs_section (sec->output_section))
  462. {
  463. /* At least one of the sections is being discarded from the
  464. link, so we should just ignore them. */
  465. return true;
  466. }
  467. if (cookie->rel == cookie->relend)
  468. return false;
  469. /* The first relocation is the function start. */
  470. r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
  471. if (r_symndx == STN_UNDEF)
  472. return false;
  473. text_sec = _bfd_elf_section_for_symbol (cookie, r_symndx, false);
  474. if (text_sec == NULL)
  475. return false;
  476. elf_section_eh_frame_entry (text_sec) = sec;
  477. if (text_sec->output_section
  478. && bfd_is_abs_section (text_sec->output_section))
  479. sec->flags |= SEC_EXCLUDE;
  480. sec->sec_info_type = SEC_INFO_TYPE_EH_FRAME_ENTRY;
  481. elf_section_data (sec)->sec_info = text_sec;
  482. bfd_elf_record_eh_frame_entry (hdr_info, sec);
  483. return true;
  484. }
  485. /* Try to parse .eh_frame section SEC, which belongs to ABFD. Store the
  486. information in the section's sec_info field on success. COOKIE
  487. describes the relocations in SEC. */
  488. void
  489. _bfd_elf_parse_eh_frame (bfd *abfd, struct bfd_link_info *info,
  490. asection *sec, struct elf_reloc_cookie *cookie)
  491. {
  492. #define REQUIRE(COND) \
  493. do \
  494. if (!(COND)) \
  495. goto free_no_table; \
  496. while (0)
  497. bfd_byte *ehbuf = NULL, *buf, *end;
  498. bfd_byte *last_fde;
  499. struct eh_cie_fde *this_inf;
  500. unsigned int hdr_length, hdr_id;
  501. unsigned int cie_count;
  502. struct cie *cie, *local_cies = NULL;
  503. struct elf_link_hash_table *htab;
  504. struct eh_frame_hdr_info *hdr_info;
  505. struct eh_frame_sec_info *sec_info = NULL;
  506. unsigned int ptr_size;
  507. unsigned int num_cies;
  508. unsigned int num_entries;
  509. elf_gc_mark_hook_fn gc_mark_hook;
  510. htab = elf_hash_table (info);
  511. hdr_info = &htab->eh_info;
  512. if (sec->size == 0
  513. || sec->sec_info_type != SEC_INFO_TYPE_NONE)
  514. {
  515. /* This file does not contain .eh_frame information. */
  516. return;
  517. }
  518. if (bfd_is_abs_section (sec->output_section))
  519. {
  520. /* At least one of the sections is being discarded from the
  521. link, so we should just ignore them. */
  522. return;
  523. }
  524. /* Read the frame unwind information from abfd. */
  525. REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf));
  526. /* If .eh_frame section size doesn't fit into int, we cannot handle
  527. it (it would need to use 64-bit .eh_frame format anyway). */
  528. REQUIRE (sec->size == (unsigned int) sec->size);
  529. ptr_size = (get_elf_backend_data (abfd)
  530. ->elf_backend_eh_frame_address_size (abfd, sec));
  531. REQUIRE (ptr_size != 0);
  532. /* Go through the section contents and work out how many FDEs and
  533. CIEs there are. */
  534. buf = ehbuf;
  535. end = ehbuf + sec->size;
  536. num_cies = 0;
  537. num_entries = 0;
  538. while (buf != end)
  539. {
  540. num_entries++;
  541. /* Read the length of the entry. */
  542. REQUIRE (skip_bytes (&buf, end, 4));
  543. hdr_length = bfd_get_32 (abfd, buf - 4);
  544. /* 64-bit .eh_frame is not supported. */
  545. REQUIRE (hdr_length != 0xffffffff);
  546. if (hdr_length == 0)
  547. break;
  548. REQUIRE (skip_bytes (&buf, end, 4));
  549. hdr_id = bfd_get_32 (abfd, buf - 4);
  550. if (hdr_id == 0)
  551. num_cies++;
  552. REQUIRE (skip_bytes (&buf, end, hdr_length - 4));
  553. }
  554. sec_info = (struct eh_frame_sec_info *)
  555. bfd_zmalloc (sizeof (struct eh_frame_sec_info)
  556. + (num_entries - 1) * sizeof (struct eh_cie_fde));
  557. REQUIRE (sec_info);
  558. /* We need to have a "struct cie" for each CIE in this section. */
  559. if (num_cies)
  560. {
  561. local_cies = (struct cie *) bfd_zmalloc (num_cies * sizeof (*local_cies));
  562. REQUIRE (local_cies);
  563. }
  564. /* FIXME: octets_per_byte. */
  565. #define ENSURE_NO_RELOCS(buf) \
  566. while (cookie->rel < cookie->relend \
  567. && (cookie->rel->r_offset \
  568. < (bfd_size_type) ((buf) - ehbuf))) \
  569. { \
  570. REQUIRE (cookie->rel->r_info == 0); \
  571. cookie->rel++; \
  572. }
  573. /* FIXME: octets_per_byte. */
  574. #define SKIP_RELOCS(buf) \
  575. while (cookie->rel < cookie->relend \
  576. && (cookie->rel->r_offset \
  577. < (bfd_size_type) ((buf) - ehbuf))) \
  578. cookie->rel++
  579. /* FIXME: octets_per_byte. */
  580. #define GET_RELOC(buf) \
  581. ((cookie->rel < cookie->relend \
  582. && (cookie->rel->r_offset \
  583. == (bfd_size_type) ((buf) - ehbuf))) \
  584. ? cookie->rel : NULL)
  585. buf = ehbuf;
  586. cie_count = 0;
  587. gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
  588. while ((bfd_size_type) (buf - ehbuf) != sec->size)
  589. {
  590. char *aug;
  591. bfd_byte *start, *insns, *insns_end;
  592. bfd_size_type length;
  593. unsigned int set_loc_count;
  594. this_inf = sec_info->entry + sec_info->count;
  595. last_fde = buf;
  596. /* Read the length of the entry. */
  597. REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4));
  598. hdr_length = bfd_get_32 (abfd, buf - 4);
  599. /* The CIE/FDE must be fully contained in this input section. */
  600. REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr_length <= sec->size);
  601. end = buf + hdr_length;
  602. this_inf->offset = last_fde - ehbuf;
  603. this_inf->size = 4 + hdr_length;
  604. this_inf->reloc_index = cookie->rel - cookie->rels;
  605. if (hdr_length == 0)
  606. {
  607. /* A zero-length CIE should only be found at the end of
  608. the section, but allow multiple terminators. */
  609. while (skip_bytes (&buf, ehbuf + sec->size, 4))
  610. REQUIRE (bfd_get_32 (abfd, buf - 4) == 0);
  611. REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size);
  612. ENSURE_NO_RELOCS (buf);
  613. sec_info->count++;
  614. break;
  615. }
  616. REQUIRE (skip_bytes (&buf, end, 4));
  617. hdr_id = bfd_get_32 (abfd, buf - 4);
  618. if (hdr_id == 0)
  619. {
  620. unsigned int initial_insn_length;
  621. /* CIE */
  622. this_inf->cie = 1;
  623. /* Point CIE to one of the section-local cie structures. */
  624. cie = local_cies + cie_count++;
  625. cie->cie_inf = this_inf;
  626. cie->length = hdr_length;
  627. start = buf;
  628. REQUIRE (read_byte (&buf, end, &cie->version));
  629. /* Cannot handle unknown versions. */
  630. REQUIRE (cie->version == 1
  631. || cie->version == 3
  632. || cie->version == 4);
  633. REQUIRE (strlen ((char *) buf) < sizeof (cie->augmentation));
  634. strcpy (cie->augmentation, (char *) buf);
  635. buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1;
  636. this_inf->u.cie.aug_str_len = buf - start - 1;
  637. ENSURE_NO_RELOCS (buf);
  638. if (buf[0] == 'e' && buf[1] == 'h')
  639. {
  640. /* GCC < 3.0 .eh_frame CIE */
  641. /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
  642. is private to each CIE, so we don't need it for anything.
  643. Just skip it. */
  644. REQUIRE (skip_bytes (&buf, end, ptr_size));
  645. SKIP_RELOCS (buf);
  646. }
  647. if (cie->version >= 4)
  648. {
  649. REQUIRE (buf + 1 < end);
  650. REQUIRE (buf[0] == ptr_size);
  651. REQUIRE (buf[1] == 0);
  652. buf += 2;
  653. }
  654. REQUIRE (read_uleb128 (&buf, end, &cie->code_align));
  655. REQUIRE (read_sleb128 (&buf, end, &cie->data_align));
  656. if (cie->version == 1)
  657. {
  658. REQUIRE (buf < end);
  659. cie->ra_column = *buf++;
  660. }
  661. else
  662. REQUIRE (read_uleb128 (&buf, end, &cie->ra_column));
  663. ENSURE_NO_RELOCS (buf);
  664. cie->lsda_encoding = DW_EH_PE_omit;
  665. cie->fde_encoding = DW_EH_PE_omit;
  666. cie->per_encoding = DW_EH_PE_omit;
  667. aug = cie->augmentation;
  668. if (aug[0] != 'e' || aug[1] != 'h')
  669. {
  670. if (*aug == 'z')
  671. {
  672. aug++;
  673. REQUIRE (read_uleb128 (&buf, end, &cie->augmentation_size));
  674. ENSURE_NO_RELOCS (buf);
  675. }
  676. while (*aug != '\0')
  677. switch (*aug++)
  678. {
  679. case 'B':
  680. break;
  681. case 'L':
  682. REQUIRE (read_byte (&buf, end, &cie->lsda_encoding));
  683. ENSURE_NO_RELOCS (buf);
  684. REQUIRE (get_DW_EH_PE_width (cie->lsda_encoding, ptr_size));
  685. break;
  686. case 'R':
  687. REQUIRE (read_byte (&buf, end, &cie->fde_encoding));
  688. ENSURE_NO_RELOCS (buf);
  689. REQUIRE (get_DW_EH_PE_width (cie->fde_encoding, ptr_size));
  690. break;
  691. case 'S':
  692. break;
  693. case 'P':
  694. {
  695. int per_width;
  696. REQUIRE (read_byte (&buf, end, &cie->per_encoding));
  697. per_width = get_DW_EH_PE_width (cie->per_encoding,
  698. ptr_size);
  699. REQUIRE (per_width);
  700. if ((cie->per_encoding & 0x70) == DW_EH_PE_aligned)
  701. {
  702. length = -(buf - ehbuf) & (per_width - 1);
  703. REQUIRE (skip_bytes (&buf, end, length));
  704. if (per_width == 8)
  705. this_inf->u.cie.per_encoding_aligned8 = 1;
  706. }
  707. this_inf->u.cie.personality_offset = buf - start;
  708. ENSURE_NO_RELOCS (buf);
  709. /* Ensure we have a reloc here. */
  710. REQUIRE (GET_RELOC (buf));
  711. cie->personality.reloc_index
  712. = cookie->rel - cookie->rels;
  713. /* Cope with MIPS-style composite relocations. */
  714. do
  715. cookie->rel++;
  716. while (GET_RELOC (buf) != NULL);
  717. REQUIRE (skip_bytes (&buf, end, per_width));
  718. }
  719. break;
  720. default:
  721. /* Unrecognized augmentation. Better bail out. */
  722. goto free_no_table;
  723. }
  724. }
  725. this_inf->u.cie.aug_data_len
  726. = buf - start - 1 - this_inf->u.cie.aug_str_len;
  727. /* For shared libraries, try to get rid of as many RELATIVE relocs
  728. as possible. */
  729. if (bfd_link_pic (info)
  730. && (get_elf_backend_data (abfd)
  731. ->elf_backend_can_make_relative_eh_frame
  732. (abfd, info, sec)))
  733. {
  734. if ((cie->fde_encoding & 0x70) == DW_EH_PE_absptr)
  735. this_inf->make_relative = 1;
  736. /* If the CIE doesn't already have an 'R' entry, it's fairly
  737. easy to add one, provided that there's no aligned data
  738. after the augmentation string. */
  739. else if (cie->fde_encoding == DW_EH_PE_omit
  740. && (cie->per_encoding & 0x70) != DW_EH_PE_aligned)
  741. {
  742. if (*cie->augmentation == 0)
  743. this_inf->add_augmentation_size = 1;
  744. this_inf->u.cie.add_fde_encoding = 1;
  745. this_inf->make_relative = 1;
  746. }
  747. if ((cie->lsda_encoding & 0x70) == DW_EH_PE_absptr)
  748. cie->can_make_lsda_relative = 1;
  749. }
  750. /* If FDE encoding was not specified, it defaults to
  751. DW_EH_absptr. */
  752. if (cie->fde_encoding == DW_EH_PE_omit)
  753. cie->fde_encoding = DW_EH_PE_absptr;
  754. initial_insn_length = end - buf;
  755. cie->initial_insn_length = initial_insn_length;
  756. memcpy (cie->initial_instructions, buf,
  757. initial_insn_length <= sizeof (cie->initial_instructions)
  758. ? initial_insn_length : sizeof (cie->initial_instructions));
  759. insns = buf;
  760. buf += initial_insn_length;
  761. ENSURE_NO_RELOCS (buf);
  762. if (!bfd_link_relocatable (info))
  763. {
  764. /* Keep info for merging cies. */
  765. this_inf->u.cie.u.full_cie = cie;
  766. this_inf->u.cie.per_encoding_relative
  767. = (cie->per_encoding & 0x70) == DW_EH_PE_pcrel;
  768. }
  769. }
  770. else
  771. {
  772. /* Find the corresponding CIE. */
  773. unsigned int cie_offset = this_inf->offset + 4 - hdr_id;
  774. for (cie = local_cies; cie < local_cies + cie_count; cie++)
  775. if (cie_offset == cie->cie_inf->offset)
  776. break;
  777. /* Ensure this FDE references one of the CIEs in this input
  778. section. */
  779. REQUIRE (cie != local_cies + cie_count);
  780. this_inf->u.fde.cie_inf = cie->cie_inf;
  781. this_inf->make_relative = cie->cie_inf->make_relative;
  782. this_inf->add_augmentation_size
  783. = cie->cie_inf->add_augmentation_size;
  784. ENSURE_NO_RELOCS (buf);
  785. if ((sec->flags & SEC_LINKER_CREATED) == 0 || cookie->rels != NULL)
  786. {
  787. asection *rsec;
  788. REQUIRE (GET_RELOC (buf));
  789. /* Chain together the FDEs for each section. */
  790. rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook,
  791. cookie, NULL);
  792. /* RSEC will be NULL if FDE was cleared out as it was belonging to
  793. a discarded SHT_GROUP. */
  794. if (rsec)
  795. {
  796. REQUIRE (rsec->owner == abfd);
  797. this_inf->u.fde.next_for_section = elf_fde_list (rsec);
  798. elf_fde_list (rsec) = this_inf;
  799. }
  800. }
  801. /* Skip the initial location and address range. */
  802. start = buf;
  803. length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
  804. REQUIRE (skip_bytes (&buf, end, 2 * length));
  805. SKIP_RELOCS (buf - length);
  806. if (!GET_RELOC (buf - length)
  807. && read_value (abfd, buf - length, length, false) == 0)
  808. {
  809. (*info->callbacks->minfo)
  810. /* xgettext:c-format */
  811. (_("discarding zero address range FDE in %pB(%pA).\n"),
  812. abfd, sec);
  813. this_inf->u.fde.cie_inf = NULL;
  814. }
  815. /* Skip the augmentation size, if present. */
  816. if (cie->augmentation[0] == 'z')
  817. REQUIRE (read_uleb128 (&buf, end, &length));
  818. else
  819. length = 0;
  820. /* Of the supported augmentation characters above, only 'L'
  821. adds augmentation data to the FDE. This code would need to
  822. be adjusted if any future augmentations do the same thing. */
  823. if (cie->lsda_encoding != DW_EH_PE_omit)
  824. {
  825. SKIP_RELOCS (buf);
  826. if (cie->can_make_lsda_relative && GET_RELOC (buf))
  827. cie->cie_inf->u.cie.make_lsda_relative = 1;
  828. this_inf->lsda_offset = buf - start;
  829. /* If there's no 'z' augmentation, we don't know where the
  830. CFA insns begin. Assume no padding. */
  831. if (cie->augmentation[0] != 'z')
  832. length = end - buf;
  833. }
  834. /* Skip over the augmentation data. */
  835. REQUIRE (skip_bytes (&buf, end, length));
  836. insns = buf;
  837. buf = last_fde + 4 + hdr_length;
  838. /* For NULL RSEC (cleared FDE belonging to a discarded section)
  839. the relocations are commonly cleared. We do not sanity check if
  840. all these relocations are cleared as (1) relocations to
  841. .gcc_except_table will remain uncleared (they will get dropped
  842. with the drop of this unused FDE) and (2) BFD already safely drops
  843. relocations of any type to .eh_frame by
  844. elf_section_ignore_discarded_relocs.
  845. TODO: The .gcc_except_table entries should be also filtered as
  846. .eh_frame entries; or GCC could rather use COMDAT for them. */
  847. SKIP_RELOCS (buf);
  848. }
  849. /* Try to interpret the CFA instructions and find the first
  850. padding nop. Shrink this_inf's size so that it doesn't
  851. include the padding. */
  852. length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
  853. set_loc_count = 0;
  854. insns_end = skip_non_nops (insns, end, length, &set_loc_count);
  855. /* If we don't understand the CFA instructions, we can't know
  856. what needs to be adjusted there. */
  857. if (insns_end == NULL
  858. /* For the time being we don't support DW_CFA_set_loc in
  859. CIE instructions. */
  860. || (set_loc_count && this_inf->cie))
  861. goto free_no_table;
  862. this_inf->size -= end - insns_end;
  863. if (insns_end != end && this_inf->cie)
  864. {
  865. cie->initial_insn_length -= end - insns_end;
  866. cie->length -= end - insns_end;
  867. }
  868. if (set_loc_count
  869. && ((cie->fde_encoding & 0x70) == DW_EH_PE_pcrel
  870. || this_inf->make_relative))
  871. {
  872. unsigned int cnt;
  873. bfd_byte *p;
  874. this_inf->set_loc = (unsigned int *)
  875. bfd_malloc ((set_loc_count + 1) * sizeof (unsigned int));
  876. REQUIRE (this_inf->set_loc);
  877. this_inf->set_loc[0] = set_loc_count;
  878. p = insns;
  879. cnt = 0;
  880. while (p < end)
  881. {
  882. if (*p == DW_CFA_set_loc)
  883. this_inf->set_loc[++cnt] = p + 1 - start;
  884. REQUIRE (skip_cfa_op (&p, end, length));
  885. }
  886. }
  887. this_inf->removed = 1;
  888. this_inf->fde_encoding = cie->fde_encoding;
  889. this_inf->lsda_encoding = cie->lsda_encoding;
  890. sec_info->count++;
  891. }
  892. BFD_ASSERT (sec_info->count == num_entries);
  893. BFD_ASSERT (cie_count == num_cies);
  894. elf_section_data (sec)->sec_info = sec_info;
  895. sec->sec_info_type = SEC_INFO_TYPE_EH_FRAME;
  896. if (!bfd_link_relocatable (info))
  897. {
  898. /* Keep info for merging cies. */
  899. sec_info->cies = local_cies;
  900. local_cies = NULL;
  901. }
  902. goto success;
  903. free_no_table:
  904. _bfd_error_handler
  905. /* xgettext:c-format */
  906. (_("error in %pB(%pA); no .eh_frame_hdr table will be created"),
  907. abfd, sec);
  908. hdr_info->u.dwarf.table = false;
  909. free (sec_info);
  910. success:
  911. free (ehbuf);
  912. free (local_cies);
  913. #undef REQUIRE
  914. }
  915. /* Order eh_frame_hdr entries by the VMA of their text section. */
  916. static int
  917. cmp_eh_frame_hdr (const void *a, const void *b)
  918. {
  919. bfd_vma text_a;
  920. bfd_vma text_b;
  921. asection *sec;
  922. sec = *(asection *const *)a;
  923. sec = (asection *) elf_section_data (sec)->sec_info;
  924. text_a = sec->output_section->vma + sec->output_offset;
  925. sec = *(asection *const *)b;
  926. sec = (asection *) elf_section_data (sec)->sec_info;
  927. text_b = sec->output_section->vma + sec->output_offset;
  928. if (text_a < text_b)
  929. return -1;
  930. return text_a > text_b;
  931. }
  932. /* Add space for a CANTUNWIND terminator to SEC if the text sections
  933. referenced by it and NEXT are not contiguous, or NEXT is NULL. */
  934. static void
  935. add_eh_frame_hdr_terminator (asection *sec,
  936. asection *next)
  937. {
  938. bfd_vma end;
  939. bfd_vma next_start;
  940. asection *text_sec;
  941. if (next)
  942. {
  943. /* See if there is a gap (presumably a text section without unwind info)
  944. between these two entries. */
  945. text_sec = (asection *) elf_section_data (sec)->sec_info;
  946. end = text_sec->output_section->vma + text_sec->output_offset
  947. + text_sec->size;
  948. text_sec = (asection *) elf_section_data (next)->sec_info;
  949. next_start = text_sec->output_section->vma + text_sec->output_offset;
  950. if (end == next_start)
  951. return;
  952. }
  953. /* Add space for a CANTUNWIND terminator. */
  954. if (!sec->rawsize)
  955. sec->rawsize = sec->size;
  956. bfd_set_section_size (sec, sec->size + 8);
  957. }
  958. /* Finish a pass over all .eh_frame_entry sections. */
  959. bool
  960. _bfd_elf_end_eh_frame_parsing (struct bfd_link_info *info)
  961. {
  962. struct eh_frame_hdr_info *hdr_info;
  963. unsigned int i;
  964. hdr_info = &elf_hash_table (info)->eh_info;
  965. if (info->eh_frame_hdr_type != COMPACT_EH_HDR
  966. || hdr_info->array_count == 0)
  967. return false;
  968. bfd_elf_discard_eh_frame_entry (hdr_info);
  969. qsort (hdr_info->u.compact.entries, hdr_info->array_count,
  970. sizeof (asection *), cmp_eh_frame_hdr);
  971. for (i = 0; i < hdr_info->array_count - 1; i++)
  972. {
  973. add_eh_frame_hdr_terminator (hdr_info->u.compact.entries[i],
  974. hdr_info->u.compact.entries[i + 1]);
  975. }
  976. /* Add a CANTUNWIND terminator after the last entry. */
  977. add_eh_frame_hdr_terminator (hdr_info->u.compact.entries[i], NULL);
  978. return true;
  979. }
  980. /* Mark all relocations against CIE or FDE ENT, which occurs in
  981. .eh_frame section SEC. COOKIE describes the relocations in SEC;
  982. its "rel" field can be changed freely. */
  983. static bool
  984. mark_entry (struct bfd_link_info *info, asection *sec,
  985. struct eh_cie_fde *ent, elf_gc_mark_hook_fn gc_mark_hook,
  986. struct elf_reloc_cookie *cookie)
  987. {
  988. /* FIXME: octets_per_byte. */
  989. for (cookie->rel = cookie->rels + ent->reloc_index;
  990. cookie->rel < cookie->relend
  991. && cookie->rel->r_offset < ent->offset + ent->size;
  992. cookie->rel++)
  993. if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, cookie))
  994. return false;
  995. return true;
  996. }
  997. /* Mark all the relocations against FDEs that relate to code in input
  998. section SEC. The FDEs belong to .eh_frame section EH_FRAME, whose
  999. relocations are described by COOKIE. */
  1000. bool
  1001. _bfd_elf_gc_mark_fdes (struct bfd_link_info *info, asection *sec,
  1002. asection *eh_frame, elf_gc_mark_hook_fn gc_mark_hook,
  1003. struct elf_reloc_cookie *cookie)
  1004. {
  1005. struct eh_cie_fde *fde, *cie;
  1006. for (fde = elf_fde_list (sec); fde; fde = fde->u.fde.next_for_section)
  1007. {
  1008. if (!mark_entry (info, eh_frame, fde, gc_mark_hook, cookie))
  1009. return false;
  1010. /* At this stage, all cie_inf fields point to local CIEs, so we
  1011. can use the same cookie to refer to them. */
  1012. cie = fde->u.fde.cie_inf;
  1013. if (cie != NULL && !cie->u.cie.gc_mark)
  1014. {
  1015. cie->u.cie.gc_mark = 1;
  1016. if (!mark_entry (info, eh_frame, cie, gc_mark_hook, cookie))
  1017. return false;
  1018. }
  1019. }
  1020. return true;
  1021. }
  1022. /* Input section SEC of ABFD is an .eh_frame section that contains the
  1023. CIE described by CIE_INF. Return a version of CIE_INF that is going
  1024. to be kept in the output, adding CIE_INF to the output if necessary.
  1025. HDR_INFO is the .eh_frame_hdr information and COOKIE describes the
  1026. relocations in REL. */
  1027. static struct eh_cie_fde *
  1028. find_merged_cie (bfd *abfd, struct bfd_link_info *info, asection *sec,
  1029. struct eh_frame_hdr_info *hdr_info,
  1030. struct elf_reloc_cookie *cookie,
  1031. struct eh_cie_fde *cie_inf)
  1032. {
  1033. unsigned long r_symndx;
  1034. struct cie *cie, *new_cie;
  1035. Elf_Internal_Rela *rel;
  1036. void **loc;
  1037. /* Use CIE_INF if we have already decided to keep it. */
  1038. if (!cie_inf->removed)
  1039. return cie_inf;
  1040. /* If we have merged CIE_INF with another CIE, use that CIE instead. */
  1041. if (cie_inf->u.cie.merged)
  1042. return cie_inf->u.cie.u.merged_with;
  1043. cie = cie_inf->u.cie.u.full_cie;
  1044. /* Assume we will need to keep CIE_INF. */
  1045. cie_inf->removed = 0;
  1046. cie_inf->u.cie.u.sec = sec;
  1047. /* If we are not merging CIEs, use CIE_INF. */
  1048. if (cie == NULL)
  1049. return cie_inf;
  1050. if (cie->per_encoding != DW_EH_PE_omit)
  1051. {
  1052. bool per_binds_local;
  1053. /* Work out the address of personality routine, or at least
  1054. enough info that we could calculate the address had we made a
  1055. final section layout. The symbol on the reloc is enough,
  1056. either the hash for a global, or (bfd id, index) pair for a
  1057. local. The assumption here is that no one uses addends on
  1058. the reloc. */
  1059. rel = cookie->rels + cie->personality.reloc_index;
  1060. memset (&cie->personality, 0, sizeof (cie->personality));
  1061. #ifdef BFD64
  1062. if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
  1063. r_symndx = ELF64_R_SYM (rel->r_info);
  1064. else
  1065. #endif
  1066. r_symndx = ELF32_R_SYM (rel->r_info);
  1067. if (r_symndx >= cookie->locsymcount
  1068. || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
  1069. {
  1070. struct elf_link_hash_entry *h;
  1071. r_symndx -= cookie->extsymoff;
  1072. h = cookie->sym_hashes[r_symndx];
  1073. while (h->root.type == bfd_link_hash_indirect
  1074. || h->root.type == bfd_link_hash_warning)
  1075. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1076. cie->personality.h = h;
  1077. per_binds_local = SYMBOL_REFERENCES_LOCAL (info, h);
  1078. }
  1079. else
  1080. {
  1081. Elf_Internal_Sym *sym;
  1082. asection *sym_sec;
  1083. sym = &cookie->locsyms[r_symndx];
  1084. sym_sec = bfd_section_from_elf_index (abfd, sym->st_shndx);
  1085. if (sym_sec == NULL)
  1086. return cie_inf;
  1087. if (sym_sec->kept_section != NULL)
  1088. sym_sec = sym_sec->kept_section;
  1089. if (sym_sec->output_section == NULL)
  1090. return cie_inf;
  1091. cie->local_personality = 1;
  1092. cie->personality.sym.bfd_id = abfd->id;
  1093. cie->personality.sym.index = r_symndx;
  1094. per_binds_local = true;
  1095. }
  1096. if (per_binds_local
  1097. && bfd_link_pic (info)
  1098. && (cie->per_encoding & 0x70) == DW_EH_PE_absptr
  1099. && (get_elf_backend_data (abfd)
  1100. ->elf_backend_can_make_relative_eh_frame (abfd, info, sec)))
  1101. {
  1102. cie_inf->u.cie.make_per_encoding_relative = 1;
  1103. cie_inf->u.cie.per_encoding_relative = 1;
  1104. }
  1105. }
  1106. /* See if we can merge this CIE with an earlier one. */
  1107. cie_compute_hash (cie);
  1108. if (hdr_info->u.dwarf.cies == NULL)
  1109. {
  1110. hdr_info->u.dwarf.cies = htab_try_create (1, cie_hash, cie_eq, free);
  1111. if (hdr_info->u.dwarf.cies == NULL)
  1112. return cie_inf;
  1113. }
  1114. loc = htab_find_slot_with_hash (hdr_info->u.dwarf.cies, cie,
  1115. cie->hash, INSERT);
  1116. if (loc == NULL)
  1117. return cie_inf;
  1118. new_cie = (struct cie *) *loc;
  1119. if (new_cie == NULL)
  1120. {
  1121. /* Keep CIE_INF and record it in the hash table. */
  1122. new_cie = (struct cie *) malloc (sizeof (struct cie));
  1123. if (new_cie == NULL)
  1124. return cie_inf;
  1125. memcpy (new_cie, cie, sizeof (struct cie));
  1126. *loc = new_cie;
  1127. }
  1128. else
  1129. {
  1130. /* Merge CIE_INF with NEW_CIE->CIE_INF. */
  1131. cie_inf->removed = 1;
  1132. cie_inf->u.cie.merged = 1;
  1133. cie_inf->u.cie.u.merged_with = new_cie->cie_inf;
  1134. if (cie_inf->u.cie.make_lsda_relative)
  1135. new_cie->cie_inf->u.cie.make_lsda_relative = 1;
  1136. }
  1137. return new_cie->cie_inf;
  1138. }
  1139. /* For a given OFFSET in SEC, return the delta to the new location
  1140. after .eh_frame editing. */
  1141. static bfd_signed_vma
  1142. offset_adjust (bfd_vma offset, const asection *sec)
  1143. {
  1144. struct eh_frame_sec_info *sec_info
  1145. = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
  1146. unsigned int lo, hi, mid;
  1147. struct eh_cie_fde *ent = NULL;
  1148. bfd_signed_vma delta;
  1149. lo = 0;
  1150. hi = sec_info->count;
  1151. if (hi == 0)
  1152. return 0;
  1153. while (lo < hi)
  1154. {
  1155. mid = (lo + hi) / 2;
  1156. ent = &sec_info->entry[mid];
  1157. if (offset < ent->offset)
  1158. hi = mid;
  1159. else if (mid + 1 >= hi)
  1160. break;
  1161. else if (offset >= ent[1].offset)
  1162. lo = mid + 1;
  1163. else
  1164. break;
  1165. }
  1166. if (!ent->removed)
  1167. delta = (bfd_vma) ent->new_offset - (bfd_vma) ent->offset;
  1168. else if (ent->cie && ent->u.cie.merged)
  1169. {
  1170. struct eh_cie_fde *cie = ent->u.cie.u.merged_with;
  1171. delta = ((bfd_vma) cie->new_offset + cie->u.cie.u.sec->output_offset
  1172. - (bfd_vma) ent->offset - sec->output_offset);
  1173. }
  1174. else
  1175. {
  1176. /* Is putting the symbol on the next entry best for a deleted
  1177. CIE/FDE? */
  1178. struct eh_cie_fde *last = sec_info->entry + sec_info->count;
  1179. delta = ((bfd_vma) next_cie_fde_offset (ent, last, sec)
  1180. - (bfd_vma) ent->offset);
  1181. return delta;
  1182. }
  1183. /* Account for editing within this CIE/FDE. */
  1184. offset -= ent->offset;
  1185. if (ent->cie)
  1186. {
  1187. unsigned int extra
  1188. = ent->add_augmentation_size + ent->u.cie.add_fde_encoding;
  1189. if (extra == 0
  1190. || offset <= 9u + ent->u.cie.aug_str_len)
  1191. return delta;
  1192. delta += extra;
  1193. if (offset <= 9u + ent->u.cie.aug_str_len + ent->u.cie.aug_data_len)
  1194. return delta;
  1195. delta += extra;
  1196. }
  1197. else
  1198. {
  1199. unsigned int ptr_size, width, extra = ent->add_augmentation_size;
  1200. if (offset <= 12 || extra == 0)
  1201. return delta;
  1202. ptr_size = (get_elf_backend_data (sec->owner)
  1203. ->elf_backend_eh_frame_address_size (sec->owner, sec));
  1204. width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
  1205. if (offset <= 8 + 2 * width)
  1206. return delta;
  1207. delta += extra;
  1208. }
  1209. return delta;
  1210. }
  1211. /* Adjust a global symbol defined in .eh_frame, so that it stays
  1212. relative to its original CIE/FDE. It is assumed that a symbol
  1213. defined at the beginning of a CIE/FDE belongs to that CIE/FDE
  1214. rather than marking the end of the previous CIE/FDE. This matters
  1215. when a CIE is merged with a previous CIE, since the symbol is
  1216. moved to the merged CIE. */
  1217. bool
  1218. _bfd_elf_adjust_eh_frame_global_symbol (struct elf_link_hash_entry *h,
  1219. void *arg ATTRIBUTE_UNUSED)
  1220. {
  1221. asection *sym_sec;
  1222. bfd_signed_vma delta;
  1223. if (h->root.type != bfd_link_hash_defined
  1224. && h->root.type != bfd_link_hash_defweak)
  1225. return true;
  1226. sym_sec = h->root.u.def.section;
  1227. if (sym_sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME
  1228. || elf_section_data (sym_sec)->sec_info == NULL)
  1229. return true;
  1230. delta = offset_adjust (h->root.u.def.value, sym_sec);
  1231. h->root.u.def.value += delta;
  1232. return true;
  1233. }
  1234. /* The same for all local symbols defined in .eh_frame. Returns true
  1235. if any symbol was changed. */
  1236. static int
  1237. adjust_eh_frame_local_symbols (const asection *sec,
  1238. struct elf_reloc_cookie *cookie)
  1239. {
  1240. int adjusted = 0;
  1241. if (cookie->locsymcount > 1)
  1242. {
  1243. unsigned int shndx = elf_section_data (sec)->this_idx;
  1244. Elf_Internal_Sym *end_sym = cookie->locsyms + cookie->locsymcount;
  1245. Elf_Internal_Sym *sym;
  1246. for (sym = cookie->locsyms + 1; sym < end_sym; ++sym)
  1247. if (sym->st_info <= ELF_ST_INFO (STB_LOCAL, STT_OBJECT)
  1248. && sym->st_shndx == shndx)
  1249. {
  1250. bfd_signed_vma delta = offset_adjust (sym->st_value, sec);
  1251. if (delta != 0)
  1252. {
  1253. adjusted = 1;
  1254. sym->st_value += delta;
  1255. }
  1256. }
  1257. }
  1258. return adjusted;
  1259. }
  1260. /* This function is called for each input file before the .eh_frame
  1261. section is relocated. It discards duplicate CIEs and FDEs for discarded
  1262. functions. The function returns TRUE iff any entries have been
  1263. deleted. */
  1264. bool
  1265. _bfd_elf_discard_section_eh_frame
  1266. (bfd *abfd, struct bfd_link_info *info, asection *sec,
  1267. bool (*reloc_symbol_deleted_p) (bfd_vma, void *),
  1268. struct elf_reloc_cookie *cookie)
  1269. {
  1270. struct eh_cie_fde *ent;
  1271. struct eh_frame_sec_info *sec_info;
  1272. struct eh_frame_hdr_info *hdr_info;
  1273. unsigned int ptr_size, offset, eh_alignment;
  1274. int changed;
  1275. if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME)
  1276. return false;
  1277. sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
  1278. if (sec_info == NULL)
  1279. return false;
  1280. ptr_size = (get_elf_backend_data (sec->owner)
  1281. ->elf_backend_eh_frame_address_size (sec->owner, sec));
  1282. hdr_info = &elf_hash_table (info)->eh_info;
  1283. for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
  1284. if (ent->size == 4)
  1285. /* There should only be one zero terminator, on the last input
  1286. file supplying .eh_frame (crtend.o). Remove any others. */
  1287. ent->removed = sec->map_head.s != NULL;
  1288. else if (!ent->cie && ent->u.fde.cie_inf != NULL)
  1289. {
  1290. bool keep;
  1291. if ((sec->flags & SEC_LINKER_CREATED) != 0 && cookie->rels == NULL)
  1292. {
  1293. unsigned int width
  1294. = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
  1295. bfd_vma value
  1296. = read_value (abfd, sec->contents + ent->offset + 8 + width,
  1297. width, get_DW_EH_PE_signed (ent->fde_encoding));
  1298. keep = value != 0;
  1299. }
  1300. else
  1301. {
  1302. cookie->rel = cookie->rels + ent->reloc_index;
  1303. /* FIXME: octets_per_byte. */
  1304. BFD_ASSERT (cookie->rel < cookie->relend
  1305. && cookie->rel->r_offset == ent->offset + 8);
  1306. keep = !(*reloc_symbol_deleted_p) (ent->offset + 8, cookie);
  1307. }
  1308. if (keep)
  1309. {
  1310. if (bfd_link_pic (info)
  1311. && (((ent->fde_encoding & 0x70) == DW_EH_PE_absptr
  1312. && ent->make_relative == 0)
  1313. || (ent->fde_encoding & 0x70) == DW_EH_PE_aligned))
  1314. {
  1315. static int num_warnings_issued = 0;
  1316. /* If a shared library uses absolute pointers
  1317. which we cannot turn into PC relative,
  1318. don't create the binary search table,
  1319. since it is affected by runtime relocations. */
  1320. hdr_info->u.dwarf.table = false;
  1321. /* Only warn if --eh-frame-hdr was specified. */
  1322. if (info->eh_frame_hdr_type != 0)
  1323. {
  1324. if (num_warnings_issued < 10)
  1325. {
  1326. _bfd_error_handler
  1327. /* xgettext:c-format */
  1328. (_("FDE encoding in %pB(%pA) prevents .eh_frame_hdr"
  1329. " table being created"), abfd, sec);
  1330. num_warnings_issued ++;
  1331. }
  1332. else if (num_warnings_issued == 10)
  1333. {
  1334. _bfd_error_handler
  1335. (_("further warnings about FDE encoding preventing .eh_frame_hdr generation dropped"));
  1336. num_warnings_issued ++;
  1337. }
  1338. }
  1339. }
  1340. ent->removed = 0;
  1341. hdr_info->u.dwarf.fde_count++;
  1342. ent->u.fde.cie_inf = find_merged_cie (abfd, info, sec, hdr_info,
  1343. cookie, ent->u.fde.cie_inf);
  1344. }
  1345. }
  1346. free (sec_info->cies);
  1347. sec_info->cies = NULL;
  1348. /* It may be that some .eh_frame input section has greater alignment
  1349. than other .eh_frame sections. In that case we run the risk of
  1350. padding with zeros before that section, which would be seen as a
  1351. zero terminator. Alignment padding must be added *inside* the
  1352. last FDE instead. For other FDEs we align according to their
  1353. encoding, in order to align FDE address range entries naturally. */
  1354. offset = 0;
  1355. changed = 0;
  1356. for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
  1357. if (!ent->removed)
  1358. {
  1359. eh_alignment = 4;
  1360. if (ent->size == 4)
  1361. ;
  1362. else if (ent->cie)
  1363. {
  1364. if (ent->u.cie.per_encoding_aligned8)
  1365. eh_alignment = 8;
  1366. }
  1367. else
  1368. {
  1369. eh_alignment = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
  1370. if (eh_alignment < 4)
  1371. eh_alignment = 4;
  1372. }
  1373. offset = (offset + eh_alignment - 1) & -eh_alignment;
  1374. ent->new_offset = offset;
  1375. if (ent->new_offset != ent->offset)
  1376. changed = 1;
  1377. offset += size_of_output_cie_fde (ent);
  1378. }
  1379. eh_alignment = 4;
  1380. offset = (offset + eh_alignment - 1) & -eh_alignment;
  1381. sec->rawsize = sec->size;
  1382. sec->size = offset;
  1383. if (sec->size != sec->rawsize)
  1384. changed = 1;
  1385. if (changed && adjust_eh_frame_local_symbols (sec, cookie))
  1386. {
  1387. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1388. symtab_hdr->contents = (unsigned char *) cookie->locsyms;
  1389. }
  1390. return changed;
  1391. }
  1392. /* This function is called for .eh_frame_hdr section after
  1393. _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
  1394. input sections. It finalizes the size of .eh_frame_hdr section. */
  1395. bool
  1396. _bfd_elf_discard_section_eh_frame_hdr (struct bfd_link_info *info)
  1397. {
  1398. struct elf_link_hash_table *htab;
  1399. struct eh_frame_hdr_info *hdr_info;
  1400. asection *sec;
  1401. htab = elf_hash_table (info);
  1402. hdr_info = &htab->eh_info;
  1403. if (!hdr_info->frame_hdr_is_compact && hdr_info->u.dwarf.cies != NULL)
  1404. {
  1405. htab_delete (hdr_info->u.dwarf.cies);
  1406. hdr_info->u.dwarf.cies = NULL;
  1407. }
  1408. sec = hdr_info->hdr_sec;
  1409. if (sec == NULL)
  1410. return false;
  1411. if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
  1412. {
  1413. /* For compact frames we only add the header. The actual table comes
  1414. from the .eh_frame_entry sections. */
  1415. sec->size = 8;
  1416. }
  1417. else
  1418. {
  1419. sec->size = EH_FRAME_HDR_SIZE;
  1420. if (hdr_info->u.dwarf.table)
  1421. sec->size += 4 + hdr_info->u.dwarf.fde_count * 8;
  1422. }
  1423. return true;
  1424. }
  1425. /* Return true if there is at least one non-empty .eh_frame section in
  1426. input files. Can only be called after ld has mapped input to
  1427. output sections, and before sections are stripped. */
  1428. bool
  1429. _bfd_elf_eh_frame_present (struct bfd_link_info *info)
  1430. {
  1431. asection *eh = bfd_get_section_by_name (info->output_bfd, ".eh_frame");
  1432. if (eh == NULL)
  1433. return false;
  1434. /* Count only sections which have at least a single CIE or FDE.
  1435. There cannot be any CIE or FDE <= 8 bytes. */
  1436. for (eh = eh->map_head.s; eh != NULL; eh = eh->map_head.s)
  1437. if (eh->size > 8)
  1438. return true;
  1439. return false;
  1440. }
  1441. /* Return true if there is at least one .eh_frame_entry section in
  1442. input files. */
  1443. bool
  1444. _bfd_elf_eh_frame_entry_present (struct bfd_link_info *info)
  1445. {
  1446. asection *o;
  1447. bfd *abfd;
  1448. for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  1449. {
  1450. for (o = abfd->sections; o; o = o->next)
  1451. {
  1452. const char *name = bfd_section_name (o);
  1453. if (strcmp (name, ".eh_frame_entry")
  1454. && !bfd_is_abs_section (o->output_section))
  1455. return true;
  1456. }
  1457. }
  1458. return false;
  1459. }
  1460. /* This function is called from size_dynamic_sections.
  1461. It needs to decide whether .eh_frame_hdr should be output or not,
  1462. because when the dynamic symbol table has been sized it is too late
  1463. to strip sections. */
  1464. bool
  1465. _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
  1466. {
  1467. struct elf_link_hash_table *htab;
  1468. struct eh_frame_hdr_info *hdr_info;
  1469. struct bfd_link_hash_entry *bh = NULL;
  1470. struct elf_link_hash_entry *h;
  1471. htab = elf_hash_table (info);
  1472. hdr_info = &htab->eh_info;
  1473. if (hdr_info->hdr_sec == NULL)
  1474. return true;
  1475. if (bfd_is_abs_section (hdr_info->hdr_sec->output_section)
  1476. || info->eh_frame_hdr_type == 0
  1477. || (info->eh_frame_hdr_type == DWARF2_EH_HDR
  1478. && !_bfd_elf_eh_frame_present (info))
  1479. || (info->eh_frame_hdr_type == COMPACT_EH_HDR
  1480. && !_bfd_elf_eh_frame_entry_present (info)))
  1481. {
  1482. hdr_info->hdr_sec->flags |= SEC_EXCLUDE;
  1483. hdr_info->hdr_sec = NULL;
  1484. return true;
  1485. }
  1486. /* Add a hidden symbol so that systems without access to PHDRs can
  1487. find the table. */
  1488. if (! (_bfd_generic_link_add_one_symbol
  1489. (info, info->output_bfd, "__GNU_EH_FRAME_HDR", BSF_LOCAL,
  1490. hdr_info->hdr_sec, 0, NULL, false, false, &bh)))
  1491. return false;
  1492. h = (struct elf_link_hash_entry *) bh;
  1493. h->def_regular = 1;
  1494. h->other = STV_HIDDEN;
  1495. get_elf_backend_data
  1496. (info->output_bfd)->elf_backend_hide_symbol (info, h, true);
  1497. if (!hdr_info->frame_hdr_is_compact)
  1498. hdr_info->u.dwarf.table = true;
  1499. return true;
  1500. }
  1501. /* Adjust an address in the .eh_frame section. Given OFFSET within
  1502. SEC, this returns the new offset in the adjusted .eh_frame section,
  1503. or -1 if the address refers to a CIE/FDE which has been removed
  1504. or to offset with dynamic relocation which is no longer needed. */
  1505. bfd_vma
  1506. _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
  1507. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1508. asection *sec,
  1509. bfd_vma offset)
  1510. {
  1511. struct eh_frame_sec_info *sec_info;
  1512. unsigned int lo, hi, mid;
  1513. if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME)
  1514. return offset;
  1515. sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
  1516. if (offset >= sec->rawsize)
  1517. return offset - sec->rawsize + sec->size;
  1518. lo = 0;
  1519. hi = sec_info->count;
  1520. mid = 0;
  1521. while (lo < hi)
  1522. {
  1523. mid = (lo + hi) / 2;
  1524. if (offset < sec_info->entry[mid].offset)
  1525. hi = mid;
  1526. else if (offset
  1527. >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
  1528. lo = mid + 1;
  1529. else
  1530. break;
  1531. }
  1532. BFD_ASSERT (lo < hi);
  1533. /* FDE or CIE was removed. */
  1534. if (sec_info->entry[mid].removed)
  1535. return (bfd_vma) -1;
  1536. /* If converting personality pointers to DW_EH_PE_pcrel, there will be
  1537. no need for run-time relocation against the personality field. */
  1538. if (sec_info->entry[mid].cie
  1539. && sec_info->entry[mid].u.cie.make_per_encoding_relative
  1540. && offset == (sec_info->entry[mid].offset + 8
  1541. + sec_info->entry[mid].u.cie.personality_offset))
  1542. return (bfd_vma) -2;
  1543. /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
  1544. relocation against FDE's initial_location field. */
  1545. if (!sec_info->entry[mid].cie
  1546. && sec_info->entry[mid].make_relative
  1547. && offset == sec_info->entry[mid].offset + 8)
  1548. return (bfd_vma) -2;
  1549. /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
  1550. for run-time relocation against LSDA field. */
  1551. if (!sec_info->entry[mid].cie
  1552. && sec_info->entry[mid].u.fde.cie_inf->u.cie.make_lsda_relative
  1553. && offset == (sec_info->entry[mid].offset + 8
  1554. + sec_info->entry[mid].lsda_offset))
  1555. return (bfd_vma) -2;
  1556. /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
  1557. relocation against DW_CFA_set_loc's arguments. */
  1558. if (sec_info->entry[mid].set_loc
  1559. && sec_info->entry[mid].make_relative
  1560. && (offset >= sec_info->entry[mid].offset + 8
  1561. + sec_info->entry[mid].set_loc[1]))
  1562. {
  1563. unsigned int cnt;
  1564. for (cnt = 1; cnt <= sec_info->entry[mid].set_loc[0]; cnt++)
  1565. if (offset == sec_info->entry[mid].offset + 8
  1566. + sec_info->entry[mid].set_loc[cnt])
  1567. return (bfd_vma) -2;
  1568. }
  1569. /* Any new augmentation bytes go before the first relocation. */
  1570. return (offset + sec_info->entry[mid].new_offset
  1571. - sec_info->entry[mid].offset
  1572. + extra_augmentation_string_bytes (sec_info->entry + mid)
  1573. + extra_augmentation_data_bytes (sec_info->entry + mid));
  1574. }
  1575. /* Write out .eh_frame_entry section. Add CANTUNWIND terminator if needed.
  1576. Also check that the contents look sane. */
  1577. bool
  1578. _bfd_elf_write_section_eh_frame_entry (bfd *abfd, struct bfd_link_info *info,
  1579. asection *sec, bfd_byte *contents)
  1580. {
  1581. const struct elf_backend_data *bed;
  1582. bfd_byte cantunwind[8];
  1583. bfd_vma addr;
  1584. bfd_vma last_addr;
  1585. bfd_vma offset;
  1586. asection *text_sec = (asection *) elf_section_data (sec)->sec_info;
  1587. if (!sec->rawsize)
  1588. sec->rawsize = sec->size;
  1589. BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME_ENTRY);
  1590. /* Check to make sure that the text section corresponding to this eh_frame_entry
  1591. section has not been excluded. In particular, mips16 stub entries will be
  1592. excluded outside of the normal process. */
  1593. if (sec->flags & SEC_EXCLUDE
  1594. || text_sec->flags & SEC_EXCLUDE)
  1595. return true;
  1596. if (!bfd_set_section_contents (abfd, sec->output_section, contents,
  1597. sec->output_offset, sec->rawsize))
  1598. return false;
  1599. last_addr = bfd_get_signed_32 (abfd, contents);
  1600. /* Check that all the entries are in order. */
  1601. for (offset = 8; offset < sec->rawsize; offset += 8)
  1602. {
  1603. addr = bfd_get_signed_32 (abfd, contents + offset) + offset;
  1604. if (addr <= last_addr)
  1605. {
  1606. /* xgettext:c-format */
  1607. _bfd_error_handler (_("%pB: %pA not in order"), sec->owner, sec);
  1608. return false;
  1609. }
  1610. last_addr = addr;
  1611. }
  1612. addr = text_sec->output_section->vma + text_sec->output_offset
  1613. + text_sec->size;
  1614. addr &= ~1;
  1615. addr -= (sec->output_section->vma + sec->output_offset + sec->rawsize);
  1616. if (addr & 1)
  1617. {
  1618. /* xgettext:c-format */
  1619. _bfd_error_handler (_("%pB: %pA invalid input section size"),
  1620. sec->owner, sec);
  1621. bfd_set_error (bfd_error_bad_value);
  1622. return false;
  1623. }
  1624. if (last_addr >= addr + sec->rawsize)
  1625. {
  1626. /* xgettext:c-format */
  1627. _bfd_error_handler (_("%pB: %pA points past end of text section"),
  1628. sec->owner, sec);
  1629. bfd_set_error (bfd_error_bad_value);
  1630. return false;
  1631. }
  1632. if (sec->size == sec->rawsize)
  1633. return true;
  1634. bed = get_elf_backend_data (abfd);
  1635. BFD_ASSERT (sec->size == sec->rawsize + 8);
  1636. BFD_ASSERT ((addr & 1) == 0);
  1637. BFD_ASSERT (bed->cant_unwind_opcode);
  1638. bfd_put_32 (abfd, addr, cantunwind);
  1639. bfd_put_32 (abfd, (*bed->cant_unwind_opcode) (info), cantunwind + 4);
  1640. return bfd_set_section_contents (abfd, sec->output_section, cantunwind,
  1641. sec->output_offset + sec->rawsize, 8);
  1642. }
  1643. /* Write out .eh_frame section. This is called with the relocated
  1644. contents. */
  1645. bool
  1646. _bfd_elf_write_section_eh_frame (bfd *abfd,
  1647. struct bfd_link_info *info,
  1648. asection *sec,
  1649. bfd_byte *contents)
  1650. {
  1651. struct eh_frame_sec_info *sec_info;
  1652. struct elf_link_hash_table *htab;
  1653. struct eh_frame_hdr_info *hdr_info;
  1654. unsigned int ptr_size;
  1655. struct eh_cie_fde *ent, *last_ent;
  1656. if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME)
  1657. /* FIXME: octets_per_byte. */
  1658. return bfd_set_section_contents (abfd, sec->output_section, contents,
  1659. sec->output_offset, sec->size);
  1660. ptr_size = (get_elf_backend_data (abfd)
  1661. ->elf_backend_eh_frame_address_size (abfd, sec));
  1662. BFD_ASSERT (ptr_size != 0);
  1663. sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
  1664. htab = elf_hash_table (info);
  1665. hdr_info = &htab->eh_info;
  1666. if (hdr_info->u.dwarf.table && hdr_info->u.dwarf.array == NULL)
  1667. {
  1668. hdr_info->frame_hdr_is_compact = false;
  1669. hdr_info->u.dwarf.array = (struct eh_frame_array_ent *)
  1670. bfd_malloc (hdr_info->u.dwarf.fde_count
  1671. * sizeof (*hdr_info->u.dwarf.array));
  1672. }
  1673. if (hdr_info->u.dwarf.array == NULL)
  1674. hdr_info = NULL;
  1675. /* The new offsets can be bigger or smaller than the original offsets.
  1676. We therefore need to make two passes over the section: one backward
  1677. pass to move entries up and one forward pass to move entries down.
  1678. The two passes won't interfere with each other because entries are
  1679. not reordered */
  1680. for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
  1681. if (!ent->removed && ent->new_offset > ent->offset)
  1682. memmove (contents + ent->new_offset, contents + ent->offset, ent->size);
  1683. for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
  1684. if (!ent->removed && ent->new_offset < ent->offset)
  1685. memmove (contents + ent->new_offset, contents + ent->offset, ent->size);
  1686. last_ent = sec_info->entry + sec_info->count;
  1687. for (ent = sec_info->entry; ent < last_ent; ++ent)
  1688. {
  1689. unsigned char *buf, *end;
  1690. unsigned int new_size;
  1691. if (ent->removed)
  1692. continue;
  1693. if (ent->size == 4)
  1694. {
  1695. /* Any terminating FDE must be at the end of the section. */
  1696. BFD_ASSERT (ent == last_ent - 1);
  1697. continue;
  1698. }
  1699. buf = contents + ent->new_offset;
  1700. end = buf + ent->size;
  1701. new_size = next_cie_fde_offset (ent, last_ent, sec) - ent->new_offset;
  1702. /* Update the size. It may be shrinked. */
  1703. bfd_put_32 (abfd, new_size - 4, buf);
  1704. /* Filling the extra bytes with DW_CFA_nops. */
  1705. if (new_size != ent->size)
  1706. memset (end, 0, new_size - ent->size);
  1707. if (ent->cie)
  1708. {
  1709. /* CIE */
  1710. if (ent->make_relative
  1711. || ent->u.cie.make_lsda_relative
  1712. || ent->u.cie.per_encoding_relative)
  1713. {
  1714. char *aug;
  1715. unsigned int version, action, extra_string, extra_data;
  1716. unsigned int per_width, per_encoding;
  1717. /* Need to find 'R' or 'L' augmentation's argument and modify
  1718. DW_EH_PE_* value. */
  1719. action = ((ent->make_relative ? 1 : 0)
  1720. | (ent->u.cie.make_lsda_relative ? 2 : 0)
  1721. | (ent->u.cie.per_encoding_relative ? 4 : 0));
  1722. extra_string = extra_augmentation_string_bytes (ent);
  1723. extra_data = extra_augmentation_data_bytes (ent);
  1724. /* Skip length, id. */
  1725. buf += 8;
  1726. version = *buf++;
  1727. aug = (char *) buf;
  1728. buf += strlen (aug) + 1;
  1729. skip_leb128 (&buf, end);
  1730. skip_leb128 (&buf, end);
  1731. if (version == 1)
  1732. skip_bytes (&buf, end, 1);
  1733. else
  1734. skip_leb128 (&buf, end);
  1735. if (*aug == 'z')
  1736. {
  1737. /* The uleb128 will always be a single byte for the kind
  1738. of augmentation strings that we're prepared to handle. */
  1739. *buf++ += extra_data;
  1740. aug++;
  1741. }
  1742. /* Make room for the new augmentation string and data bytes. */
  1743. memmove (buf + extra_string + extra_data, buf, end - buf);
  1744. memmove (aug + extra_string, aug, buf - (bfd_byte *) aug);
  1745. buf += extra_string;
  1746. end += extra_string + extra_data;
  1747. if (ent->add_augmentation_size)
  1748. {
  1749. *aug++ = 'z';
  1750. *buf++ = extra_data - 1;
  1751. }
  1752. if (ent->u.cie.add_fde_encoding)
  1753. {
  1754. BFD_ASSERT (action & 1);
  1755. *aug++ = 'R';
  1756. *buf++ = make_pc_relative (DW_EH_PE_absptr, ptr_size);
  1757. action &= ~1;
  1758. }
  1759. while (action)
  1760. switch (*aug++)
  1761. {
  1762. case 'L':
  1763. if (action & 2)
  1764. {
  1765. BFD_ASSERT (*buf == ent->lsda_encoding);
  1766. *buf = make_pc_relative (*buf, ptr_size);
  1767. action &= ~2;
  1768. }
  1769. buf++;
  1770. break;
  1771. case 'P':
  1772. if (ent->u.cie.make_per_encoding_relative)
  1773. *buf = make_pc_relative (*buf, ptr_size);
  1774. per_encoding = *buf++;
  1775. per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
  1776. BFD_ASSERT (per_width != 0);
  1777. BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
  1778. == ent->u.cie.per_encoding_relative);
  1779. if ((per_encoding & 0x70) == DW_EH_PE_aligned)
  1780. buf = (contents
  1781. + ((buf - contents + per_width - 1)
  1782. & ~((bfd_size_type) per_width - 1)));
  1783. if (action & 4)
  1784. {
  1785. bfd_vma val;
  1786. val = read_value (abfd, buf, per_width,
  1787. get_DW_EH_PE_signed (per_encoding));
  1788. if (ent->u.cie.make_per_encoding_relative)
  1789. val -= (sec->output_section->vma
  1790. + sec->output_offset
  1791. + (buf - contents));
  1792. else
  1793. {
  1794. val += (bfd_vma) ent->offset - ent->new_offset;
  1795. val -= extra_string + extra_data;
  1796. }
  1797. write_value (abfd, buf, val, per_width);
  1798. action &= ~4;
  1799. }
  1800. buf += per_width;
  1801. break;
  1802. case 'R':
  1803. if (action & 1)
  1804. {
  1805. BFD_ASSERT (*buf == ent->fde_encoding);
  1806. *buf = make_pc_relative (*buf, ptr_size);
  1807. action &= ~1;
  1808. }
  1809. buf++;
  1810. break;
  1811. case 'S':
  1812. break;
  1813. default:
  1814. BFD_FAIL ();
  1815. }
  1816. }
  1817. }
  1818. else
  1819. {
  1820. /* FDE */
  1821. bfd_vma value, address;
  1822. unsigned int width;
  1823. bfd_byte *start;
  1824. struct eh_cie_fde *cie;
  1825. /* Skip length. */
  1826. cie = ent->u.fde.cie_inf;
  1827. buf += 4;
  1828. value = ((ent->new_offset + sec->output_offset + 4)
  1829. - (cie->new_offset + cie->u.cie.u.sec->output_offset));
  1830. bfd_put_32 (abfd, value, buf);
  1831. if (bfd_link_relocatable (info))
  1832. continue;
  1833. buf += 4;
  1834. width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
  1835. value = read_value (abfd, buf, width,
  1836. get_DW_EH_PE_signed (ent->fde_encoding));
  1837. address = value;
  1838. if (value)
  1839. {
  1840. switch (ent->fde_encoding & 0x70)
  1841. {
  1842. case DW_EH_PE_textrel:
  1843. BFD_ASSERT (hdr_info == NULL);
  1844. break;
  1845. case DW_EH_PE_datarel:
  1846. {
  1847. switch (abfd->arch_info->arch)
  1848. {
  1849. case bfd_arch_ia64:
  1850. BFD_ASSERT (elf_gp (abfd) != 0);
  1851. address += elf_gp (abfd);
  1852. break;
  1853. default:
  1854. _bfd_error_handler
  1855. (_("DW_EH_PE_datarel unspecified"
  1856. " for this architecture"));
  1857. /* Fall thru */
  1858. case bfd_arch_frv:
  1859. case bfd_arch_i386:
  1860. case bfd_arch_nios2:
  1861. BFD_ASSERT (htab->hgot != NULL
  1862. && ((htab->hgot->root.type
  1863. == bfd_link_hash_defined)
  1864. || (htab->hgot->root.type
  1865. == bfd_link_hash_defweak)));
  1866. address
  1867. += (htab->hgot->root.u.def.value
  1868. + htab->hgot->root.u.def.section->output_offset
  1869. + (htab->hgot->root.u.def.section->output_section
  1870. ->vma));
  1871. break;
  1872. }
  1873. }
  1874. break;
  1875. case DW_EH_PE_pcrel:
  1876. value += (bfd_vma) ent->offset - ent->new_offset;
  1877. address += (sec->output_section->vma
  1878. + sec->output_offset
  1879. + ent->offset + 8);
  1880. break;
  1881. }
  1882. if (ent->make_relative)
  1883. value -= (sec->output_section->vma
  1884. + sec->output_offset
  1885. + ent->new_offset + 8);
  1886. write_value (abfd, buf, value, width);
  1887. }
  1888. start = buf;
  1889. if (hdr_info)
  1890. {
  1891. /* The address calculation may overflow, giving us a
  1892. value greater than 4G on a 32-bit target when
  1893. dwarf_vma is 64-bit. */
  1894. if (sizeof (address) > 4 && ptr_size == 4)
  1895. address &= 0xffffffff;
  1896. hdr_info->u.dwarf.array[hdr_info->array_count].initial_loc
  1897. = address;
  1898. hdr_info->u.dwarf.array[hdr_info->array_count].range
  1899. = read_value (abfd, buf + width, width, false);
  1900. hdr_info->u.dwarf.array[hdr_info->array_count++].fde
  1901. = (sec->output_section->vma
  1902. + sec->output_offset
  1903. + ent->new_offset);
  1904. }
  1905. if ((ent->lsda_encoding & 0x70) == DW_EH_PE_pcrel
  1906. || cie->u.cie.make_lsda_relative)
  1907. {
  1908. buf += ent->lsda_offset;
  1909. width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
  1910. value = read_value (abfd, buf, width,
  1911. get_DW_EH_PE_signed (ent->lsda_encoding));
  1912. if (value)
  1913. {
  1914. if ((ent->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
  1915. value += (bfd_vma) ent->offset - ent->new_offset;
  1916. else if (cie->u.cie.make_lsda_relative)
  1917. value -= (sec->output_section->vma
  1918. + sec->output_offset
  1919. + ent->new_offset + 8 + ent->lsda_offset);
  1920. write_value (abfd, buf, value, width);
  1921. }
  1922. }
  1923. else if (ent->add_augmentation_size)
  1924. {
  1925. /* Skip the PC and length and insert a zero byte for the
  1926. augmentation size. */
  1927. buf += width * 2;
  1928. memmove (buf + 1, buf, end - buf);
  1929. *buf = 0;
  1930. }
  1931. if (ent->set_loc)
  1932. {
  1933. /* Adjust DW_CFA_set_loc. */
  1934. unsigned int cnt;
  1935. bfd_vma new_offset;
  1936. width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
  1937. new_offset = ent->new_offset + 8
  1938. + extra_augmentation_string_bytes (ent)
  1939. + extra_augmentation_data_bytes (ent);
  1940. for (cnt = 1; cnt <= ent->set_loc[0]; cnt++)
  1941. {
  1942. buf = start + ent->set_loc[cnt];
  1943. value = read_value (abfd, buf, width,
  1944. get_DW_EH_PE_signed (ent->fde_encoding));
  1945. if (!value)
  1946. continue;
  1947. if ((ent->fde_encoding & 0x70) == DW_EH_PE_pcrel)
  1948. value += (bfd_vma) ent->offset + 8 - new_offset;
  1949. if (ent->make_relative)
  1950. value -= (sec->output_section->vma
  1951. + sec->output_offset
  1952. + new_offset + ent->set_loc[cnt]);
  1953. write_value (abfd, buf, value, width);
  1954. }
  1955. }
  1956. }
  1957. }
  1958. /* FIXME: octets_per_byte. */
  1959. return bfd_set_section_contents (abfd, sec->output_section,
  1960. contents, (file_ptr) sec->output_offset,
  1961. sec->size);
  1962. }
  1963. /* Helper function used to sort .eh_frame_hdr search table by increasing
  1964. VMA of FDE initial location. */
  1965. static int
  1966. vma_compare (const void *a, const void *b)
  1967. {
  1968. const struct eh_frame_array_ent *p = (const struct eh_frame_array_ent *) a;
  1969. const struct eh_frame_array_ent *q = (const struct eh_frame_array_ent *) b;
  1970. if (p->initial_loc > q->initial_loc)
  1971. return 1;
  1972. if (p->initial_loc < q->initial_loc)
  1973. return -1;
  1974. if (p->range > q->range)
  1975. return 1;
  1976. if (p->range < q->range)
  1977. return -1;
  1978. return 0;
  1979. }
  1980. /* Reorder .eh_frame_entry sections to match the associated text sections.
  1981. This routine is called during the final linking step, just before writing
  1982. the contents. At this stage, sections in the eh_frame_hdr_info are already
  1983. sorted in order of increasing text section address and so we simply need
  1984. to make the .eh_frame_entrys follow that same order. Note that it is
  1985. invalid for a linker script to try to force a particular order of
  1986. .eh_frame_entry sections. */
  1987. bool
  1988. _bfd_elf_fixup_eh_frame_hdr (struct bfd_link_info *info)
  1989. {
  1990. asection *sec = NULL;
  1991. asection *osec;
  1992. struct eh_frame_hdr_info *hdr_info;
  1993. unsigned int i;
  1994. bfd_vma offset;
  1995. struct bfd_link_order *p;
  1996. hdr_info = &elf_hash_table (info)->eh_info;
  1997. if (hdr_info->hdr_sec == NULL
  1998. || info->eh_frame_hdr_type != COMPACT_EH_HDR
  1999. || hdr_info->array_count == 0)
  2000. return true;
  2001. /* Change section output offsets to be in text section order. */
  2002. offset = 8;
  2003. osec = hdr_info->u.compact.entries[0]->output_section;
  2004. for (i = 0; i < hdr_info->array_count; i++)
  2005. {
  2006. sec = hdr_info->u.compact.entries[i];
  2007. if (sec->output_section != osec)
  2008. {
  2009. _bfd_error_handler
  2010. (_("invalid output section for .eh_frame_entry: %pA"),
  2011. sec->output_section);
  2012. return false;
  2013. }
  2014. sec->output_offset = offset;
  2015. offset += sec->size;
  2016. }
  2017. /* Fix the link_order to match. */
  2018. for (p = sec->output_section->map_head.link_order; p != NULL; p = p->next)
  2019. {
  2020. if (p->type != bfd_indirect_link_order)
  2021. abort();
  2022. p->offset = p->u.indirect.section->output_offset;
  2023. if (p->next != NULL)
  2024. i--;
  2025. }
  2026. if (i != 0)
  2027. {
  2028. _bfd_error_handler
  2029. (_("invalid contents in %pA section"), osec);
  2030. return false;
  2031. }
  2032. return true;
  2033. }
  2034. /* The .eh_frame_hdr format for Compact EH frames:
  2035. ubyte version (2)
  2036. ubyte eh_ref_enc (DW_EH_PE_* encoding of typinfo references)
  2037. uint32_t count (Number of entries in table)
  2038. [array from .eh_frame_entry sections] */
  2039. static bool
  2040. write_compact_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
  2041. {
  2042. struct elf_link_hash_table *htab;
  2043. struct eh_frame_hdr_info *hdr_info;
  2044. asection *sec;
  2045. const struct elf_backend_data *bed;
  2046. bfd_vma count;
  2047. bfd_byte contents[8];
  2048. unsigned int i;
  2049. htab = elf_hash_table (info);
  2050. hdr_info = &htab->eh_info;
  2051. sec = hdr_info->hdr_sec;
  2052. if (sec->size != 8)
  2053. abort();
  2054. for (i = 0; i < sizeof (contents); i++)
  2055. contents[i] = 0;
  2056. contents[0] = COMPACT_EH_HDR;
  2057. bed = get_elf_backend_data (abfd);
  2058. BFD_ASSERT (bed->compact_eh_encoding);
  2059. contents[1] = (*bed->compact_eh_encoding) (info);
  2060. count = (sec->output_section->size - 8) / 8;
  2061. bfd_put_32 (abfd, count, contents + 4);
  2062. return bfd_set_section_contents (abfd, sec->output_section, contents,
  2063. (file_ptr) sec->output_offset, sec->size);
  2064. }
  2065. /* The .eh_frame_hdr format for DWARF frames:
  2066. ubyte version (currently 1)
  2067. ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
  2068. .eh_frame section)
  2069. ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
  2070. number (or DW_EH_PE_omit if there is no
  2071. binary search table computed))
  2072. ubyte table_enc (DW_EH_PE_* encoding of binary search table,
  2073. or DW_EH_PE_omit if not present.
  2074. DW_EH_PE_datarel is using address of
  2075. .eh_frame_hdr section start as base)
  2076. [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
  2077. optionally followed by:
  2078. [encoded] fde_count (total number of FDEs in .eh_frame section)
  2079. fde_count x [encoded] initial_loc, fde
  2080. (array of encoded pairs containing
  2081. FDE initial_location field and FDE address,
  2082. sorted by increasing initial_loc). */
  2083. static bool
  2084. write_dwarf_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
  2085. {
  2086. struct elf_link_hash_table *htab;
  2087. struct eh_frame_hdr_info *hdr_info;
  2088. asection *sec;
  2089. bool retval = true;
  2090. htab = elf_hash_table (info);
  2091. hdr_info = &htab->eh_info;
  2092. sec = hdr_info->hdr_sec;
  2093. bfd_byte *contents;
  2094. asection *eh_frame_sec;
  2095. bfd_size_type size;
  2096. bfd_vma encoded_eh_frame;
  2097. size = EH_FRAME_HDR_SIZE;
  2098. if (hdr_info->u.dwarf.array
  2099. && hdr_info->array_count == hdr_info->u.dwarf.fde_count)
  2100. size += 4 + hdr_info->u.dwarf.fde_count * 8;
  2101. contents = (bfd_byte *) bfd_malloc (size);
  2102. if (contents == NULL)
  2103. return false;
  2104. eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
  2105. if (eh_frame_sec == NULL)
  2106. {
  2107. free (contents);
  2108. return false;
  2109. }
  2110. memset (contents, 0, EH_FRAME_HDR_SIZE);
  2111. /* Version. */
  2112. contents[0] = 1;
  2113. /* .eh_frame offset. */
  2114. contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
  2115. (abfd, info, eh_frame_sec, 0, sec, 4, &encoded_eh_frame);
  2116. if (hdr_info->u.dwarf.array
  2117. && hdr_info->array_count == hdr_info->u.dwarf.fde_count)
  2118. {
  2119. /* FDE count encoding. */
  2120. contents[2] = DW_EH_PE_udata4;
  2121. /* Search table encoding. */
  2122. contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
  2123. }
  2124. else
  2125. {
  2126. contents[2] = DW_EH_PE_omit;
  2127. contents[3] = DW_EH_PE_omit;
  2128. }
  2129. bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
  2130. if (contents[2] != DW_EH_PE_omit)
  2131. {
  2132. unsigned int i;
  2133. bool overlap, overflow;
  2134. bfd_put_32 (abfd, hdr_info->u.dwarf.fde_count,
  2135. contents + EH_FRAME_HDR_SIZE);
  2136. qsort (hdr_info->u.dwarf.array, hdr_info->u.dwarf.fde_count,
  2137. sizeof (*hdr_info->u.dwarf.array), vma_compare);
  2138. overlap = false;
  2139. overflow = false;
  2140. for (i = 0; i < hdr_info->u.dwarf.fde_count; i++)
  2141. {
  2142. bfd_vma val;
  2143. val = hdr_info->u.dwarf.array[i].initial_loc
  2144. - sec->output_section->vma;
  2145. val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
  2146. if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64
  2147. && (hdr_info->u.dwarf.array[i].initial_loc
  2148. != sec->output_section->vma + val))
  2149. overflow = true;
  2150. bfd_put_32 (abfd, val, contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
  2151. val = hdr_info->u.dwarf.array[i].fde - sec->output_section->vma;
  2152. val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
  2153. if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64
  2154. && (hdr_info->u.dwarf.array[i].fde
  2155. != sec->output_section->vma + val))
  2156. overflow = true;
  2157. bfd_put_32 (abfd, val, contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
  2158. if (i != 0
  2159. && (hdr_info->u.dwarf.array[i].initial_loc
  2160. < (hdr_info->u.dwarf.array[i - 1].initial_loc
  2161. + hdr_info->u.dwarf.array[i - 1].range)))
  2162. overlap = true;
  2163. }
  2164. if (overflow)
  2165. _bfd_error_handler (_(".eh_frame_hdr entry overflow"));
  2166. if (overlap)
  2167. _bfd_error_handler (_(".eh_frame_hdr refers to overlapping FDEs"));
  2168. if (overflow || overlap)
  2169. {
  2170. bfd_set_error (bfd_error_bad_value);
  2171. retval = false;
  2172. }
  2173. }
  2174. /* FIXME: octets_per_byte. */
  2175. if (!bfd_set_section_contents (abfd, sec->output_section, contents,
  2176. (file_ptr) sec->output_offset,
  2177. sec->size))
  2178. retval = false;
  2179. free (contents);
  2180. free (hdr_info->u.dwarf.array);
  2181. return retval;
  2182. }
  2183. /* Write out .eh_frame_hdr section. This must be called after
  2184. _bfd_elf_write_section_eh_frame has been called on all input
  2185. .eh_frame sections. */
  2186. bool
  2187. _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
  2188. {
  2189. struct elf_link_hash_table *htab;
  2190. struct eh_frame_hdr_info *hdr_info;
  2191. asection *sec;
  2192. htab = elf_hash_table (info);
  2193. hdr_info = &htab->eh_info;
  2194. sec = hdr_info->hdr_sec;
  2195. if (info->eh_frame_hdr_type == 0 || sec == NULL)
  2196. return true;
  2197. if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
  2198. return write_compact_eh_frame_hdr (abfd, info);
  2199. else
  2200. return write_dwarf_eh_frame_hdr (abfd, info);
  2201. }
  2202. /* Return the width of FDE addresses. This is the default implementation. */
  2203. unsigned int
  2204. _bfd_elf_eh_frame_address_size (bfd *abfd, const asection *sec ATTRIBUTE_UNUSED)
  2205. {
  2206. return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64 ? 8 : 4;
  2207. }
  2208. /* Decide whether we can use a PC-relative encoding within the given
  2209. EH frame section. This is the default implementation. */
  2210. bool
  2211. _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
  2212. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2213. asection *eh_frame_section ATTRIBUTE_UNUSED)
  2214. {
  2215. return true;
  2216. }
  2217. /* Select an encoding for the given address. Preference is given to
  2218. PC-relative addressing modes. */
  2219. bfd_byte
  2220. _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
  2221. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2222. asection *osec, bfd_vma offset,
  2223. asection *loc_sec, bfd_vma loc_offset,
  2224. bfd_vma *encoded)
  2225. {
  2226. *encoded = osec->vma + offset -
  2227. (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
  2228. return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
  2229. }