elf-eh-frame.c 67 KB

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