elf32-i370.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. /* i370-specific support for 32-bit ELF
  2. Copyright (C) 1994-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Support.
  4. Hacked by Linas Vepstas for i370 linas@linas.org
  5. This file is part of BFD, the Binary File Descriptor library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. /* This file is based on a preliminary PowerPC ELF ABI.
  19. But its been hacked on for the IBM 360/370 architectures.
  20. Basically, the 31bit relocation works, and just about everything
  21. else is a wild card. In particular, don't expect shared libs or
  22. dynamic loading to work ... its never been tested. */
  23. #include "sysdep.h"
  24. #include "bfd.h"
  25. #include "bfdlink.h"
  26. #include "libbfd.h"
  27. #include "elf-bfd.h"
  28. #include "elf/i370.h"
  29. static reloc_howto_type *i370_elf_howto_table[ (int)R_I370_max ];
  30. static reloc_howto_type i370_elf_howto_raw[] =
  31. {
  32. /* This reloc does nothing. */
  33. HOWTO (R_I370_NONE, /* type */
  34. 0, /* rightshift */
  35. 3, /* size (0 = byte, 1 = short, 2 = long) */
  36. 0, /* bitsize */
  37. FALSE, /* pc_relative */
  38. 0, /* bitpos */
  39. complain_overflow_dont, /* complain_on_overflow */
  40. bfd_elf_generic_reloc, /* special_function */
  41. "R_I370_NONE", /* name */
  42. FALSE, /* partial_inplace */
  43. 0, /* src_mask */
  44. 0, /* dst_mask */
  45. FALSE), /* pcrel_offset */
  46. /* A standard 31 bit relocation. */
  47. HOWTO (R_I370_ADDR31, /* type */
  48. 0, /* rightshift */
  49. 2, /* size (0 = byte, 1 = short, 2 = long) */
  50. 31, /* bitsize */
  51. FALSE, /* pc_relative */
  52. 0, /* bitpos */
  53. complain_overflow_bitfield, /* complain_on_overflow */
  54. bfd_elf_generic_reloc, /* special_function */
  55. "R_I370_ADDR31", /* name */
  56. FALSE, /* partial_inplace */
  57. 0, /* src_mask */
  58. 0x7fffffff, /* dst_mask */
  59. FALSE), /* pcrel_offset */
  60. /* A standard 32 bit relocation. */
  61. HOWTO (R_I370_ADDR32, /* type */
  62. 0, /* rightshift */
  63. 2, /* size (0 = byte, 1 = short, 2 = long) */
  64. 32, /* bitsize */
  65. FALSE, /* pc_relative */
  66. 0, /* bitpos */
  67. complain_overflow_bitfield, /* complain_on_overflow */
  68. bfd_elf_generic_reloc, /* special_function */
  69. "R_I370_ADDR32", /* name */
  70. FALSE, /* partial_inplace */
  71. 0, /* src_mask */
  72. 0xffffffff, /* dst_mask */
  73. FALSE), /* pcrel_offset */
  74. /* A standard 16 bit relocation. */
  75. HOWTO (R_I370_ADDR16, /* type */
  76. 0, /* rightshift */
  77. 1, /* size (0 = byte, 1 = short, 2 = long) */
  78. 16, /* bitsize */
  79. FALSE, /* pc_relative */
  80. 0, /* bitpos */
  81. complain_overflow_bitfield, /* complain_on_overflow */
  82. bfd_elf_generic_reloc, /* special_function */
  83. "R_I370_ADDR16", /* name */
  84. FALSE, /* partial_inplace */
  85. 0, /* src_mask */
  86. 0xffff, /* dst_mask */
  87. FALSE), /* pcrel_offset */
  88. /* 31-bit PC relative. */
  89. HOWTO (R_I370_REL31, /* type */
  90. 0, /* rightshift */
  91. 2, /* size (0 = byte, 1 = short, 2 = long) */
  92. 31, /* bitsize */
  93. TRUE, /* pc_relative */
  94. 0, /* bitpos */
  95. complain_overflow_bitfield, /* complain_on_overflow */
  96. bfd_elf_generic_reloc, /* special_function */
  97. "R_I370_REL31", /* name */
  98. FALSE, /* partial_inplace */
  99. 0, /* src_mask */
  100. 0x7fffffff, /* dst_mask */
  101. TRUE), /* pcrel_offset */
  102. /* 32-bit PC relative. */
  103. HOWTO (R_I370_REL32, /* type */
  104. 0, /* rightshift */
  105. 2, /* size (0 = byte, 1 = short, 2 = long) */
  106. 32, /* bitsize */
  107. TRUE, /* pc_relative */
  108. 0, /* bitpos */
  109. complain_overflow_bitfield, /* complain_on_overflow */
  110. bfd_elf_generic_reloc, /* special_function */
  111. "R_I370_REL32", /* name */
  112. FALSE, /* partial_inplace */
  113. 0, /* src_mask */
  114. 0xffffffff, /* dst_mask */
  115. TRUE), /* pcrel_offset */
  116. /* A standard 12 bit relocation. */
  117. HOWTO (R_I370_ADDR12, /* type */
  118. 0, /* rightshift */
  119. 1, /* size (0 = byte, 1 = short, 2 = long) */
  120. 12, /* bitsize */
  121. FALSE, /* pc_relative */
  122. 0, /* bitpos */
  123. complain_overflow_bitfield, /* complain_on_overflow */
  124. bfd_elf_generic_reloc, /* special_function */
  125. "R_I370_ADDR12", /* name */
  126. FALSE, /* partial_inplace */
  127. 0, /* src_mask */
  128. 0xfff, /* dst_mask */
  129. FALSE), /* pcrel_offset */
  130. /* 12-bit PC relative. */
  131. HOWTO (R_I370_REL12, /* type */
  132. 0, /* rightshift */
  133. 1, /* size (0 = byte, 1 = short, 2 = long) */
  134. 12, /* bitsize */
  135. TRUE, /* pc_relative */
  136. 0, /* bitpos */
  137. complain_overflow_bitfield, /* complain_on_overflow */
  138. bfd_elf_generic_reloc, /* special_function */
  139. "R_I370_REL12", /* name */
  140. FALSE, /* partial_inplace */
  141. 0, /* src_mask */
  142. 0xfff, /* dst_mask */
  143. TRUE), /* pcrel_offset */
  144. /* A standard 8 bit relocation. */
  145. HOWTO (R_I370_ADDR8, /* type */
  146. 0, /* rightshift */
  147. 0, /* size (0 = byte, 1 = short, 2 = long) */
  148. 8, /* bitsize */
  149. FALSE, /* pc_relative */
  150. 0, /* bitpos */
  151. complain_overflow_bitfield, /* complain_on_overflow */
  152. bfd_elf_generic_reloc, /* special_function */
  153. "R_I370_ADDR8", /* name */
  154. FALSE, /* partial_inplace */
  155. 0, /* src_mask */
  156. 0xff, /* dst_mask */
  157. FALSE), /* pcrel_offset */
  158. /* 8-bit PC relative. */
  159. HOWTO (R_I370_REL8, /* type */
  160. 0, /* rightshift */
  161. 0, /* size (0 = byte, 1 = short, 2 = long) */
  162. 8, /* bitsize */
  163. TRUE, /* pc_relative */
  164. 0, /* bitpos */
  165. complain_overflow_bitfield, /* complain_on_overflow */
  166. bfd_elf_generic_reloc, /* special_function */
  167. "R_I370_REL8", /* name */
  168. FALSE, /* partial_inplace */
  169. 0, /* src_mask */
  170. 0xff, /* dst_mask */
  171. TRUE), /* pcrel_offset */
  172. /* This is used only by the dynamic linker. The symbol should exist
  173. both in the object being run and in some shared library. The
  174. dynamic linker copies the data addressed by the symbol from the
  175. shared library into the object, because the object being
  176. run has to have the data at some particular address. */
  177. HOWTO (R_I370_COPY, /* type */
  178. 0, /* rightshift */
  179. 2, /* size (0 = byte, 1 = short, 2 = long) */
  180. 32, /* bitsize */
  181. FALSE, /* pc_relative */
  182. 0, /* bitpos */
  183. complain_overflow_bitfield, /* complain_on_overflow */
  184. bfd_elf_generic_reloc, /* special_function */
  185. "R_I370_COPY", /* name */
  186. FALSE, /* partial_inplace */
  187. 0, /* src_mask */
  188. 0, /* dst_mask */
  189. FALSE), /* pcrel_offset */
  190. /* Used only by the dynamic linker. When the object is run, this
  191. longword is set to the load address of the object, plus the
  192. addend. */
  193. HOWTO (R_I370_RELATIVE, /* type */
  194. 0, /* rightshift */
  195. 2, /* size (0 = byte, 1 = short, 2 = long) */
  196. 32, /* bitsize */
  197. FALSE, /* pc_relative */
  198. 0, /* bitpos */
  199. complain_overflow_bitfield, /* complain_on_overflow */
  200. bfd_elf_generic_reloc, /* special_function */
  201. "R_I370_RELATIVE", /* name */
  202. FALSE, /* partial_inplace */
  203. 0, /* src_mask */
  204. 0xffffffff, /* dst_mask */
  205. FALSE), /* pcrel_offset */
  206. };
  207. /* Initialize the i370_elf_howto_table, so that linear accesses can be done. */
  208. static void
  209. i370_elf_howto_init (void)
  210. {
  211. unsigned int i, type;
  212. for (i = 0; i < sizeof (i370_elf_howto_raw) / sizeof (i370_elf_howto_raw[0]); i++)
  213. {
  214. type = i370_elf_howto_raw[i].type;
  215. BFD_ASSERT (type < sizeof (i370_elf_howto_table) / sizeof (i370_elf_howto_table[0]));
  216. i370_elf_howto_table[type] = &i370_elf_howto_raw[i];
  217. }
  218. }
  219. static reloc_howto_type *
  220. i370_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  221. bfd_reloc_code_real_type code)
  222. {
  223. enum i370_reloc_type i370_reloc = R_I370_NONE;
  224. if (!i370_elf_howto_table[ R_I370_ADDR31 ])
  225. /* Initialize howto table if needed. */
  226. i370_elf_howto_init ();
  227. switch ((int) code)
  228. {
  229. default:
  230. return NULL;
  231. case BFD_RELOC_NONE: i370_reloc = R_I370_NONE; break;
  232. case BFD_RELOC_32: i370_reloc = R_I370_ADDR31; break;
  233. case BFD_RELOC_16: i370_reloc = R_I370_ADDR16; break;
  234. case BFD_RELOC_32_PCREL: i370_reloc = R_I370_REL31; break;
  235. case BFD_RELOC_CTOR: i370_reloc = R_I370_ADDR31; break;
  236. case BFD_RELOC_I370_D12: i370_reloc = R_I370_ADDR12; break;
  237. }
  238. return i370_elf_howto_table[ (int)i370_reloc ];
  239. };
  240. static reloc_howto_type *
  241. i370_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  242. const char *r_name)
  243. {
  244. unsigned int i;
  245. for (i = 0;
  246. i < sizeof (i370_elf_howto_raw) / sizeof (i370_elf_howto_raw[0]);
  247. i++)
  248. if (i370_elf_howto_raw[i].name != NULL
  249. && strcasecmp (i370_elf_howto_raw[i].name, r_name) == 0)
  250. return &i370_elf_howto_raw[i];
  251. return NULL;
  252. }
  253. /* The name of the dynamic interpreter. This is put in the .interp
  254. section. */
  255. #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
  256. /* Set the howto pointer for an i370 ELF reloc. */
  257. static void
  258. i370_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  259. arelent *cache_ptr,
  260. Elf_Internal_Rela *dst)
  261. {
  262. unsigned int r_type;
  263. if (!i370_elf_howto_table[ R_I370_ADDR31 ])
  264. /* Initialize howto table. */
  265. i370_elf_howto_init ();
  266. r_type = ELF32_R_TYPE (dst->r_info);
  267. if (r_type >= R_I370_max)
  268. {
  269. (*_bfd_error_handler) (_("%B: unrecognised I370 reloc number: %d"),
  270. abfd, r_type);
  271. bfd_set_error (bfd_error_bad_value);
  272. r_type = R_I370_NONE;
  273. }
  274. cache_ptr->howto = i370_elf_howto_table[r_type];
  275. }
  276. /* Hack alert -- the following several routines look generic to me ...
  277. why are we bothering with them ? */
  278. /* Function to set whether a module needs the -mrelocatable bit set. */
  279. static bfd_boolean
  280. i370_elf_set_private_flags (bfd *abfd, flagword flags)
  281. {
  282. BFD_ASSERT (!elf_flags_init (abfd)
  283. || elf_elfheader (abfd)->e_flags == flags);
  284. elf_elfheader (abfd)->e_flags = flags;
  285. elf_flags_init (abfd) = TRUE;
  286. return TRUE;
  287. }
  288. /* Merge backend specific data from an object file to the output
  289. object file when linking. */
  290. static bfd_boolean
  291. i370_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
  292. {
  293. flagword old_flags;
  294. flagword new_flags;
  295. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  296. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  297. return TRUE;
  298. new_flags = elf_elfheader (ibfd)->e_flags;
  299. old_flags = elf_elfheader (obfd)->e_flags;
  300. if (!elf_flags_init (obfd)) /* First call, no flags set. */
  301. {
  302. elf_flags_init (obfd) = TRUE;
  303. elf_elfheader (obfd)->e_flags = new_flags;
  304. }
  305. else if (new_flags == old_flags) /* Compatible flags are ok. */
  306. ;
  307. else /* Incompatible flags. */
  308. {
  309. (*_bfd_error_handler)
  310. ("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
  311. ibfd, (long) new_flags, (long) old_flags);
  312. bfd_set_error (bfd_error_bad_value);
  313. return FALSE;
  314. }
  315. return TRUE;
  316. }
  317. /* Handle an i370 specific section when reading an object file. This
  318. is called when elfcode.h finds a section with an unknown type. */
  319. /* XXX hack alert bogus This routine is mostly all junk and almost
  320. certainly does the wrong thing. Its here simply because it does
  321. just enough to allow glibc-2.1 ld.so to compile & link. */
  322. static bfd_boolean
  323. i370_elf_section_from_shdr (bfd *abfd,
  324. Elf_Internal_Shdr *hdr,
  325. const char *name,
  326. int shindex)
  327. {
  328. asection *newsect;
  329. flagword flags;
  330. if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  331. return FALSE;
  332. newsect = hdr->bfd_section;
  333. flags = bfd_get_section_flags (abfd, newsect);
  334. if (hdr->sh_type == SHT_ORDERED)
  335. flags |= SEC_SORT_ENTRIES;
  336. bfd_set_section_flags (abfd, newsect, flags);
  337. return TRUE;
  338. }
  339. /* Set up any other section flags and such that may be necessary. */
  340. /* XXX hack alert bogus This routine is mostly all junk and almost
  341. certainly does the wrong thing. Its here simply because it does
  342. just enough to allow glibc-2.1 ld.so to compile & link. */
  343. static bfd_boolean
  344. i370_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
  345. Elf_Internal_Shdr *shdr,
  346. asection *asect)
  347. {
  348. if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
  349. shdr->sh_flags |= SHF_EXCLUDE;
  350. if ((asect->flags & SEC_SORT_ENTRIES) != 0)
  351. shdr->sh_type = SHT_ORDERED;
  352. return TRUE;
  353. }
  354. /* We have to create .dynsbss and .rela.sbss here so that they get mapped
  355. to output sections (just like _bfd_elf_create_dynamic_sections has
  356. to create .dynbss and .rela.bss). */
  357. /* XXX hack alert bogus This routine is mostly all junk and almost
  358. certainly does the wrong thing. Its here simply because it does
  359. just enough to allow glibc-2.1 ld.so to compile & link. */
  360. static bfd_boolean
  361. i370_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  362. {
  363. asection *s;
  364. flagword flags;
  365. if (!_bfd_elf_create_dynamic_sections(abfd, info))
  366. return FALSE;
  367. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  368. | SEC_LINKER_CREATED);
  369. s = bfd_make_section_anyway_with_flags (abfd, ".dynsbss",
  370. SEC_ALLOC | SEC_LINKER_CREATED);
  371. if (s == NULL)
  372. return FALSE;
  373. if (! bfd_link_pic (info))
  374. {
  375. s = bfd_make_section_anyway_with_flags (abfd, ".rela.sbss",
  376. flags | SEC_READONLY);
  377. if (s == NULL
  378. || ! bfd_set_section_alignment (abfd, s, 2))
  379. return FALSE;
  380. }
  381. /* XXX beats me, seem to need a rela.text ... */
  382. s = bfd_make_section_anyway_with_flags (abfd, ".rela.text",
  383. flags | SEC_READONLY);
  384. if (s == NULL
  385. || ! bfd_set_section_alignment (abfd, s, 2))
  386. return FALSE;
  387. return TRUE;
  388. }
  389. /* Adjust a symbol defined by a dynamic object and referenced by a
  390. regular object. The current definition is in some section of the
  391. dynamic object, but we're not including those sections. We have to
  392. change the definition to something the rest of the link can
  393. understand. */
  394. /* XXX hack alert bogus This routine is mostly all junk and almost
  395. certainly does the wrong thing. Its here simply because it does
  396. just enough to allow glibc-2.1 ld.so to compile & link. */
  397. static bfd_boolean
  398. i370_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  399. struct elf_link_hash_entry *h)
  400. {
  401. bfd *dynobj = elf_hash_table (info)->dynobj;
  402. asection *s;
  403. #ifdef DEBUG
  404. fprintf (stderr, "i370_elf_adjust_dynamic_symbol called for %s\n",
  405. h->root.root.string);
  406. #endif
  407. /* Make sure we know what is going on here. */
  408. BFD_ASSERT (dynobj != NULL
  409. && (h->needs_plt
  410. || h->u.weakdef != NULL
  411. || (h->def_dynamic
  412. && h->ref_regular
  413. && !h->def_regular)));
  414. s = bfd_get_linker_section (dynobj, ".rela.text");
  415. BFD_ASSERT (s != NULL);
  416. s->size += sizeof (Elf32_External_Rela);
  417. /* If this is a weak symbol, and there is a real definition, the
  418. processor independent code will have arranged for us to see the
  419. real definition first, and we can just use the same value. */
  420. if (h->u.weakdef != NULL)
  421. {
  422. BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
  423. || h->u.weakdef->root.type == bfd_link_hash_defweak);
  424. h->root.u.def.section = h->u.weakdef->root.u.def.section;
  425. h->root.u.def.value = h->u.weakdef->root.u.def.value;
  426. return TRUE;
  427. }
  428. /* This is a reference to a symbol defined by a dynamic object which
  429. is not a function. */
  430. /* If we are creating a shared library, we must presume that the
  431. only references to the symbol are via the global offset table.
  432. For such cases we need not do anything here; the relocations will
  433. be handled correctly by relocate_section. */
  434. if (bfd_link_pic (info))
  435. return TRUE;
  436. /* We must allocate the symbol in our .dynbss section, which will
  437. become part of the .bss section of the executable. There will be
  438. an entry for this symbol in the .dynsym section. The dynamic
  439. object will contain position independent code, so all references
  440. from the dynamic object to this symbol will go through the global
  441. offset table. The dynamic linker will use the .dynsym entry to
  442. determine the address it must put in the global offset table, so
  443. both the dynamic object and the regular object will refer to the
  444. same memory location for the variable.
  445. Of course, if the symbol is sufficiently small, we must instead
  446. allocate it in .sbss. FIXME: It would be better to do this if and
  447. only if there were actually SDAREL relocs for that symbol. */
  448. if (h->size <= elf_gp_size (dynobj))
  449. s = bfd_get_linker_section (dynobj, ".dynsbss");
  450. else
  451. s = bfd_get_linker_section (dynobj, ".dynbss");
  452. BFD_ASSERT (s != NULL);
  453. /* We must generate a R_I370_COPY reloc to tell the dynamic linker to
  454. copy the initial value out of the dynamic object and into the
  455. runtime process image. We need to remember the offset into the
  456. .rela.bss section we are going to use. */
  457. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  458. {
  459. asection *srel;
  460. if (h->size <= elf_gp_size (dynobj))
  461. srel = bfd_get_linker_section (dynobj, ".rela.sbss");
  462. else
  463. srel = bfd_get_linker_section (dynobj, ".rela.bss");
  464. BFD_ASSERT (srel != NULL);
  465. srel->size += sizeof (Elf32_External_Rela);
  466. h->needs_copy = 1;
  467. }
  468. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  469. }
  470. /* Increment the index of a dynamic symbol by a given amount. Called
  471. via elf_link_hash_traverse. */
  472. /* XXX hack alert bogus This routine is mostly all junk and almost
  473. certainly does the wrong thing. Its here simply because it does
  474. just enough to allow glibc-2.1 ld.so to compile & link. */
  475. static bfd_boolean
  476. i370_elf_adjust_dynindx (struct elf_link_hash_entry *h, void * cparg)
  477. {
  478. int *cp = (int *) cparg;
  479. #ifdef DEBUG
  480. fprintf (stderr,
  481. "i370_elf_adjust_dynindx called, h->dynindx = %ld, *cp = %d\n",
  482. h->dynindx, *cp);
  483. #endif
  484. if (h->dynindx != -1)
  485. h->dynindx += *cp;
  486. return TRUE;
  487. }
  488. /* Set the sizes of the dynamic sections. */
  489. /* XXX hack alert bogus This routine is mostly all junk and almost
  490. certainly does the wrong thing. Its here simply because it does
  491. just enough to allow glibc-2.1 ld.so to compile & link. */
  492. static bfd_boolean
  493. i370_elf_size_dynamic_sections (bfd *output_bfd,
  494. struct bfd_link_info *info)
  495. {
  496. bfd *dynobj;
  497. asection *s;
  498. bfd_boolean plt;
  499. bfd_boolean relocs;
  500. bfd_boolean reltext;
  501. #ifdef DEBUG
  502. fprintf (stderr, "i370_elf_size_dynamic_sections called\n");
  503. #endif
  504. dynobj = elf_hash_table (info)->dynobj;
  505. BFD_ASSERT (dynobj != NULL);
  506. if (elf_hash_table (info)->dynamic_sections_created)
  507. {
  508. /* Set the contents of the .interp section to the interpreter. */
  509. if (bfd_link_executable (info) && !info->nointerp)
  510. {
  511. s = bfd_get_linker_section (dynobj, ".interp");
  512. BFD_ASSERT (s != NULL);
  513. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  514. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  515. }
  516. }
  517. else
  518. {
  519. /* We may have created entries in the .rela.got, .rela.sdata, and
  520. .rela.sdata2 sections. However, if we are not creating the
  521. dynamic sections, we will not actually use these entries. Reset
  522. the size of .rela.got, et al, which will cause it to get
  523. stripped from the output file below. */
  524. static char *rela_sections[] = { ".rela.got", ".rela.sdata",
  525. ".rela.sdata2", ".rela.sbss",
  526. NULL };
  527. char **p;
  528. for (p = rela_sections; *p != NULL; p++)
  529. {
  530. s = bfd_get_linker_section (dynobj, *p);
  531. if (s != NULL)
  532. s->size = 0;
  533. }
  534. }
  535. /* The check_relocs and adjust_dynamic_symbol entry points have
  536. determined the sizes of the various dynamic sections. Allocate
  537. memory for them. */
  538. plt = FALSE;
  539. relocs = FALSE;
  540. reltext = FALSE;
  541. for (s = dynobj->sections; s != NULL; s = s->next)
  542. {
  543. const char *name;
  544. if ((s->flags & SEC_LINKER_CREATED) == 0)
  545. continue;
  546. /* It's OK to base decisions on the section name, because none
  547. of the dynobj section names depend upon the input files. */
  548. name = bfd_get_section_name (dynobj, s);
  549. if (strcmp (name, ".plt") == 0)
  550. {
  551. /* Remember whether there is a PLT. */
  552. plt = s->size != 0;
  553. }
  554. else if (CONST_STRNEQ (name, ".rela"))
  555. {
  556. if (s->size != 0)
  557. {
  558. asection *target;
  559. const char *outname;
  560. /* Remember whether there are any relocation sections. */
  561. relocs = TRUE;
  562. /* If this relocation section applies to a read only
  563. section, then we probably need a DT_TEXTREL entry. */
  564. outname = bfd_get_section_name (output_bfd,
  565. s->output_section);
  566. target = bfd_get_section_by_name (output_bfd, outname + 5);
  567. if (target != NULL
  568. && (target->flags & SEC_READONLY) != 0
  569. && (target->flags & SEC_ALLOC) != 0)
  570. reltext = TRUE;
  571. /* We use the reloc_count field as a counter if we need
  572. to copy relocs into the output file. */
  573. s->reloc_count = 0;
  574. }
  575. }
  576. else if (strcmp (name, ".got") != 0
  577. && strcmp (name, ".sdata") != 0
  578. && strcmp (name, ".sdata2") != 0
  579. && strcmp (name, ".dynbss") != 0
  580. && strcmp (name, ".dynsbss") != 0)
  581. {
  582. /* It's not one of our sections, so don't allocate space. */
  583. continue;
  584. }
  585. if (s->size == 0)
  586. {
  587. /* If we don't need this section, strip it from the
  588. output file. This is mostly to handle .rela.bss and
  589. .rela.plt. We must create both sections in
  590. create_dynamic_sections, because they must be created
  591. before the linker maps input sections to output
  592. sections. The linker does that before
  593. adjust_dynamic_symbol is called, and it is that
  594. function which decides whether anything needs to go
  595. into these sections. */
  596. s->flags |= SEC_EXCLUDE;
  597. continue;
  598. }
  599. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  600. continue;
  601. /* Allocate memory for the section contents. */
  602. s->contents = bfd_zalloc (dynobj, s->size);
  603. if (s->contents == NULL)
  604. return FALSE;
  605. }
  606. if (elf_hash_table (info)->dynamic_sections_created)
  607. {
  608. /* Add some entries to the .dynamic section. We fill in the
  609. values later, in i370_elf_finish_dynamic_sections, but we
  610. must add the entries now so that we get the correct size for
  611. the .dynamic section. The DT_DEBUG entry is filled in by the
  612. dynamic linker and used by the debugger. */
  613. #define add_dynamic_entry(TAG, VAL) \
  614. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  615. if (!bfd_link_pic (info))
  616. {
  617. if (!add_dynamic_entry (DT_DEBUG, 0))
  618. return FALSE;
  619. }
  620. if (plt)
  621. {
  622. if (!add_dynamic_entry (DT_PLTGOT, 0)
  623. || !add_dynamic_entry (DT_PLTRELSZ, 0)
  624. || !add_dynamic_entry (DT_PLTREL, DT_RELA)
  625. || !add_dynamic_entry (DT_JMPREL, 0))
  626. return FALSE;
  627. }
  628. if (relocs)
  629. {
  630. if (!add_dynamic_entry (DT_RELA, 0)
  631. || !add_dynamic_entry (DT_RELASZ, 0)
  632. || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
  633. return FALSE;
  634. }
  635. if (reltext)
  636. {
  637. if (!add_dynamic_entry (DT_TEXTREL, 0))
  638. return FALSE;
  639. info->flags |= DF_TEXTREL;
  640. }
  641. }
  642. #undef add_dynamic_entry
  643. /* If we are generating a shared library, we generate a section
  644. symbol for each output section. These are local symbols, which
  645. means that they must come first in the dynamic symbol table.
  646. That means we must increment the dynamic symbol index of every
  647. other dynamic symbol.
  648. FIXME: We assume that there will never be relocations to
  649. locations in linker-created sections that do not have
  650. externally-visible names. Instead, we should work out precisely
  651. which sections relocations are targeted at. */
  652. if (bfd_link_pic (info))
  653. {
  654. int c;
  655. for (c = 0, s = output_bfd->sections; s != NULL; s = s->next)
  656. {
  657. if ((s->flags & SEC_LINKER_CREATED) != 0
  658. || (s->flags & SEC_ALLOC) == 0)
  659. {
  660. elf_section_data (s)->dynindx = -1;
  661. continue;
  662. }
  663. /* These symbols will have no names, so we don't need to
  664. fiddle with dynstr_index. */
  665. elf_section_data (s)->dynindx = c + 1;
  666. c++;
  667. }
  668. elf_link_hash_traverse (elf_hash_table (info),
  669. i370_elf_adjust_dynindx, & c);
  670. elf_hash_table (info)->dynsymcount += c;
  671. }
  672. return TRUE;
  673. }
  674. /* Look through the relocs for a section during the first phase, and
  675. allocate space in the global offset table or procedure linkage
  676. table. */
  677. /* XXX hack alert bogus This routine is mostly all junk and almost
  678. certainly does the wrong thing. Its here simply because it does
  679. just enough to allow glibc-2.1 ld.so to compile & link. */
  680. static bfd_boolean
  681. i370_elf_check_relocs (bfd *abfd,
  682. struct bfd_link_info *info,
  683. asection *sec,
  684. const Elf_Internal_Rela *relocs)
  685. {
  686. bfd *dynobj;
  687. Elf_Internal_Shdr *symtab_hdr;
  688. struct elf_link_hash_entry **sym_hashes;
  689. const Elf_Internal_Rela *rel;
  690. const Elf_Internal_Rela *rel_end;
  691. asection *sreloc;
  692. if (bfd_link_relocatable (info))
  693. return TRUE;
  694. #ifdef DEBUG
  695. _bfd_error_handler ("i370_elf_check_relocs called for section %A in %B",
  696. sec, abfd);
  697. #endif
  698. dynobj = elf_hash_table (info)->dynobj;
  699. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  700. sym_hashes = elf_sym_hashes (abfd);
  701. sreloc = NULL;
  702. rel_end = relocs + sec->reloc_count;
  703. for (rel = relocs; rel < rel_end; rel++)
  704. {
  705. unsigned long r_symndx;
  706. struct elf_link_hash_entry *h;
  707. r_symndx = ELF32_R_SYM (rel->r_info);
  708. if (r_symndx < symtab_hdr->sh_info)
  709. h = NULL;
  710. else
  711. {
  712. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  713. while (h->root.type == bfd_link_hash_indirect
  714. || h->root.type == bfd_link_hash_warning)
  715. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  716. /* PR15323, ref flags aren't set for references in the same
  717. object. */
  718. h->root.non_ir_ref = 1;
  719. }
  720. if (bfd_link_pic (info))
  721. {
  722. #ifdef DEBUG
  723. fprintf (stderr,
  724. "i370_elf_check_relocs needs to create relocation for %s\n",
  725. (h && h->root.root.string)
  726. ? h->root.root.string : "<unknown>");
  727. #endif
  728. if (sreloc == NULL)
  729. {
  730. sreloc = _bfd_elf_make_dynamic_reloc_section
  731. (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
  732. if (sreloc == NULL)
  733. return FALSE;
  734. }
  735. sreloc->size += sizeof (Elf32_External_Rela);
  736. /* FIXME: We should here do what the m68k and i386
  737. backends do: if the reloc is pc-relative, record it
  738. in case it turns out that the reloc is unnecessary
  739. because the symbol is forced local by versioning or
  740. we are linking with -Bdynamic. Fortunately this
  741. case is not frequent. */
  742. }
  743. }
  744. return TRUE;
  745. }
  746. /* Finish up the dynamic sections. */
  747. /* XXX hack alert bogus This routine is mostly all junk and almost
  748. certainly does the wrong thing. Its here simply because it does
  749. just enough to allow glibc-2.1 ld.so to compile & link. */
  750. static bfd_boolean
  751. i370_elf_finish_dynamic_sections (bfd *output_bfd,
  752. struct bfd_link_info *info)
  753. {
  754. asection *sdyn;
  755. bfd *dynobj = elf_hash_table (info)->dynobj;
  756. asection *sgot = bfd_get_linker_section (dynobj, ".got");
  757. #ifdef DEBUG
  758. fprintf (stderr, "i370_elf_finish_dynamic_sections called\n");
  759. #endif
  760. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  761. if (elf_hash_table (info)->dynamic_sections_created)
  762. {
  763. asection *splt;
  764. Elf32_External_Dyn *dyncon, *dynconend;
  765. splt = bfd_get_linker_section (dynobj, ".plt");
  766. BFD_ASSERT (splt != NULL && sdyn != NULL);
  767. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  768. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  769. for (; dyncon < dynconend; dyncon++)
  770. {
  771. Elf_Internal_Dyn dyn;
  772. const char *name;
  773. bfd_boolean size;
  774. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  775. switch (dyn.d_tag)
  776. {
  777. case DT_PLTGOT: name = ".plt"; size = FALSE; break;
  778. case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
  779. case DT_JMPREL: name = ".rela.plt"; size = FALSE; break;
  780. default: name = NULL; size = FALSE; break;
  781. }
  782. if (name != NULL)
  783. {
  784. asection *s;
  785. s = bfd_get_section_by_name (output_bfd, name);
  786. if (s == NULL)
  787. dyn.d_un.d_val = 0;
  788. else
  789. {
  790. if (! size)
  791. dyn.d_un.d_ptr = s->vma;
  792. else
  793. dyn.d_un.d_val = s->size;
  794. }
  795. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  796. }
  797. }
  798. }
  799. if (sgot && sgot->size != 0)
  800. {
  801. unsigned char *contents = sgot->contents;
  802. if (sdyn == NULL)
  803. bfd_put_32 (output_bfd, (bfd_vma) 0, contents);
  804. else
  805. bfd_put_32 (output_bfd,
  806. sdyn->output_section->vma + sdyn->output_offset,
  807. contents);
  808. elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  809. }
  810. if (bfd_link_pic (info))
  811. {
  812. asection *sdynsym;
  813. asection *s;
  814. Elf_Internal_Sym sym;
  815. int maxdindx = 0;
  816. /* Set up the section symbols for the output sections. */
  817. sdynsym = bfd_get_linker_section (dynobj, ".dynsym");
  818. BFD_ASSERT (sdynsym != NULL);
  819. sym.st_size = 0;
  820. sym.st_name = 0;
  821. sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  822. sym.st_other = 0;
  823. sym.st_target_internal = 0;
  824. for (s = output_bfd->sections; s != NULL; s = s->next)
  825. {
  826. int indx, dindx;
  827. Elf32_External_Sym *esym;
  828. sym.st_value = s->vma;
  829. indx = elf_section_data (s)->this_idx;
  830. dindx = elf_section_data (s)->dynindx;
  831. if (dindx != -1)
  832. {
  833. BFD_ASSERT(indx > 0);
  834. BFD_ASSERT(dindx > 0);
  835. if (dindx > maxdindx)
  836. maxdindx = dindx;
  837. sym.st_shndx = indx;
  838. esym = (Elf32_External_Sym *) sdynsym->contents + dindx;
  839. bfd_elf32_swap_symbol_out (output_bfd, &sym, esym, NULL);
  840. }
  841. }
  842. /* Set the sh_info field of the output .dynsym section to the
  843. index of the first global symbol. */
  844. elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
  845. maxdindx + 1;
  846. }
  847. return TRUE;
  848. }
  849. /* The RELOCATE_SECTION function is called by the ELF backend linker
  850. to handle the relocations for a section.
  851. The relocs are always passed as Rela structures; if the section
  852. actually uses Rel structures, the r_addend field will always be
  853. zero.
  854. This function is responsible for adjust the section contents as
  855. necessary, and (if using Rela relocs and generating a
  856. relocatable output file) adjusting the reloc addend as
  857. necessary.
  858. This function does not have to worry about setting the reloc
  859. address or the reloc symbol index.
  860. LOCAL_SYMS is a pointer to the swapped in local symbols.
  861. LOCAL_SECTIONS is an array giving the section in the input file
  862. corresponding to the st_shndx field of each local symbol.
  863. The global hash table entry for the global symbols can be found
  864. via elf_sym_hashes (input_bfd).
  865. When generating relocatable output, this function must handle
  866. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  867. going to be the section symbol corresponding to the output
  868. section, which means that the addend must be adjusted
  869. accordingly. */
  870. static bfd_boolean
  871. i370_elf_relocate_section (bfd *output_bfd,
  872. struct bfd_link_info *info,
  873. bfd *input_bfd,
  874. asection *input_section,
  875. bfd_byte *contents,
  876. Elf_Internal_Rela *relocs,
  877. Elf_Internal_Sym *local_syms,
  878. asection **local_sections)
  879. {
  880. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  881. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
  882. Elf_Internal_Rela *rel = relocs;
  883. Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
  884. asection *sreloc = NULL;
  885. bfd_boolean ret = TRUE;
  886. #ifdef DEBUG
  887. _bfd_error_handler ("i370_elf_relocate_section called for %B section %A, %ld relocations%s",
  888. input_bfd, input_section,
  889. (long) input_section->reloc_count,
  890. (bfd_link_relocatable (info)) ? " (relocatable)" : "");
  891. #endif
  892. if (!i370_elf_howto_table[ R_I370_ADDR31 ])
  893. /* Initialize howto table if needed. */
  894. i370_elf_howto_init ();
  895. for (; rel < relend; rel++)
  896. {
  897. enum i370_reloc_type r_type = (enum i370_reloc_type) ELF32_R_TYPE (rel->r_info);
  898. bfd_vma offset = rel->r_offset;
  899. bfd_vma addend = rel->r_addend;
  900. bfd_reloc_status_type r = bfd_reloc_other;
  901. Elf_Internal_Sym *sym = NULL;
  902. asection *sec = NULL;
  903. struct elf_link_hash_entry * h = NULL;
  904. const char *sym_name = NULL;
  905. reloc_howto_type *howto;
  906. unsigned long r_symndx;
  907. bfd_vma relocation;
  908. /* Unknown relocation handling. */
  909. if ((unsigned) r_type >= (unsigned) R_I370_max
  910. || !i370_elf_howto_table[(int)r_type])
  911. {
  912. (*_bfd_error_handler) ("%B: unknown relocation type %d",
  913. input_bfd,
  914. (int) r_type);
  915. bfd_set_error (bfd_error_bad_value);
  916. ret = FALSE;
  917. continue;
  918. }
  919. howto = i370_elf_howto_table[(int) r_type];
  920. r_symndx = ELF32_R_SYM (rel->r_info);
  921. relocation = 0;
  922. if (r_symndx < symtab_hdr->sh_info)
  923. {
  924. sym = local_syms + r_symndx;
  925. sec = local_sections[r_symndx];
  926. sym_name = "<local symbol>";
  927. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, & sec, rel);
  928. addend = rel->r_addend;
  929. }
  930. else
  931. {
  932. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  933. if (info->wrap_hash != NULL
  934. && (input_section->flags & SEC_DEBUGGING) != 0)
  935. h = ((struct elf_link_hash_entry *)
  936. unwrap_hash_lookup (info, input_bfd, &h->root));
  937. while (h->root.type == bfd_link_hash_indirect
  938. || h->root.type == bfd_link_hash_warning)
  939. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  940. sym_name = h->root.root.string;
  941. if (h->root.type == bfd_link_hash_defined
  942. || h->root.type == bfd_link_hash_defweak)
  943. {
  944. sec = h->root.u.def.section;
  945. if (bfd_link_pic (info)
  946. && ((! info->symbolic && h->dynindx != -1)
  947. || !h->def_regular)
  948. && (input_section->flags & SEC_ALLOC) != 0
  949. && (r_type == R_I370_ADDR31
  950. || r_type == R_I370_COPY
  951. || r_type == R_I370_ADDR16
  952. || r_type == R_I370_RELATIVE))
  953. /* In these cases, we don't need the relocation
  954. value. We check specially because in some
  955. obscure cases sec->output_section will be NULL. */
  956. ;
  957. else
  958. relocation = (h->root.u.def.value
  959. + sec->output_section->vma
  960. + sec->output_offset);
  961. }
  962. else if (h->root.type == bfd_link_hash_undefweak)
  963. ;
  964. else if (info->unresolved_syms_in_objects == RM_IGNORE
  965. && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  966. ;
  967. else if (!bfd_link_relocatable (info))
  968. {
  969. if ((*info->callbacks->undefined_symbol)
  970. (info, h->root.root.string, input_bfd,
  971. input_section, rel->r_offset,
  972. (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
  973. || ELF_ST_VISIBILITY (h->other))))
  974. {
  975. ret = FALSE;
  976. continue;
  977. }
  978. }
  979. }
  980. if (sec != NULL && discarded_section (sec))
  981. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  982. rel, 1, relend, howto, 0, contents);
  983. if (bfd_link_relocatable (info))
  984. continue;
  985. switch ((int) r_type)
  986. {
  987. default:
  988. (*_bfd_error_handler)
  989. ("%B: unknown relocation type %d for symbol %s",
  990. input_bfd, (int) r_type, sym_name);
  991. bfd_set_error (bfd_error_bad_value);
  992. ret = FALSE;
  993. continue;
  994. case (int) R_I370_NONE:
  995. continue;
  996. /* Relocations that may need to be propagated if this is a shared
  997. object. */
  998. case (int) R_I370_REL31:
  999. /* If these relocations are not to a named symbol, they can be
  1000. handled right here, no need to bother the dynamic linker. */
  1001. if (h == NULL
  1002. || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  1003. break;
  1004. /* Fall through. */
  1005. /* Relocations that always need to be propagated if this is a shared
  1006. object. */
  1007. case (int) R_I370_ADDR31:
  1008. case (int) R_I370_ADDR16:
  1009. if (bfd_link_pic (info)
  1010. && r_symndx != STN_UNDEF)
  1011. {
  1012. Elf_Internal_Rela outrel;
  1013. bfd_byte *loc;
  1014. int skip;
  1015. #ifdef DEBUG
  1016. fprintf (stderr,
  1017. "i370_elf_relocate_section needs to create relocation for %s\n",
  1018. (h && h->root.root.string) ? h->root.root.string : "<unknown>");
  1019. #endif
  1020. /* When generating a shared object, these relocations
  1021. are copied into the output file to be resolved at run
  1022. time. */
  1023. if (sreloc == NULL)
  1024. {
  1025. sreloc = _bfd_elf_get_dynamic_reloc_section
  1026. (input_bfd, input_section, /*rela?*/ TRUE);
  1027. if (sreloc == NULL)
  1028. return FALSE;
  1029. }
  1030. skip = 0;
  1031. outrel.r_offset =
  1032. _bfd_elf_section_offset (output_bfd, info, input_section,
  1033. rel->r_offset);
  1034. if (outrel.r_offset == (bfd_vma) -1
  1035. || outrel.r_offset == (bfd_vma) -2)
  1036. skip = (int) outrel.r_offset;
  1037. outrel.r_offset += (input_section->output_section->vma
  1038. + input_section->output_offset);
  1039. if (skip)
  1040. memset (&outrel, 0, sizeof outrel);
  1041. /* h->dynindx may be -1 if this symbol was marked to
  1042. become local. */
  1043. else if (h != NULL
  1044. && ((! info->symbolic && h->dynindx != -1)
  1045. || !h->def_regular))
  1046. {
  1047. BFD_ASSERT (h->dynindx != -1);
  1048. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  1049. outrel.r_addend = rel->r_addend;
  1050. }
  1051. else
  1052. {
  1053. if (r_type == R_I370_ADDR31)
  1054. {
  1055. outrel.r_info = ELF32_R_INFO (0, R_I370_RELATIVE);
  1056. outrel.r_addend = relocation + rel->r_addend;
  1057. }
  1058. else
  1059. {
  1060. long indx;
  1061. if (bfd_is_abs_section (sec))
  1062. indx = 0;
  1063. else if (sec == NULL || sec->owner == NULL)
  1064. {
  1065. bfd_set_error (bfd_error_bad_value);
  1066. return FALSE;
  1067. }
  1068. else
  1069. {
  1070. asection *osec;
  1071. /* We are turning this relocation into one
  1072. against a section symbol. It would be
  1073. proper to subtract the symbol's value,
  1074. osec->vma, from the emitted reloc addend,
  1075. but ld.so expects buggy relocs. */
  1076. osec = sec->output_section;
  1077. indx = elf_section_data (osec)->dynindx;
  1078. if (indx == 0)
  1079. {
  1080. struct elf_link_hash_table *htab;
  1081. htab = elf_hash_table (info);
  1082. osec = htab->text_index_section;
  1083. indx = elf_section_data (osec)->dynindx;
  1084. }
  1085. BFD_ASSERT (indx != 0);
  1086. #ifdef DEBUG
  1087. if (indx <= 0)
  1088. {
  1089. printf ("indx=%ld section=%s flags=%08x name=%s\n",
  1090. indx, osec->name, osec->flags,
  1091. h->root.root.string);
  1092. }
  1093. #endif
  1094. }
  1095. outrel.r_info = ELF32_R_INFO (indx, r_type);
  1096. outrel.r_addend = relocation + rel->r_addend;
  1097. }
  1098. }
  1099. loc = sreloc->contents;
  1100. loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
  1101. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1102. /* This reloc will be computed at runtime, so there's no
  1103. need to do anything now, unless this is a RELATIVE
  1104. reloc in an unallocated section. */
  1105. if (skip == -1
  1106. || (input_section->flags & SEC_ALLOC) != 0
  1107. || ELF32_R_TYPE (outrel.r_info) != R_I370_RELATIVE)
  1108. continue;
  1109. }
  1110. break;
  1111. case (int) R_I370_COPY:
  1112. case (int) R_I370_RELATIVE:
  1113. (*_bfd_error_handler)
  1114. ("%B: Relocation %s is not yet supported for symbol %s.",
  1115. input_bfd,
  1116. i370_elf_howto_table[(int) r_type]->name,
  1117. sym_name);
  1118. bfd_set_error (bfd_error_invalid_operation);
  1119. ret = FALSE;
  1120. continue;
  1121. }
  1122. #ifdef DEBUG
  1123. fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
  1124. howto->name,
  1125. (int)r_type,
  1126. sym_name,
  1127. r_symndx,
  1128. (long) offset,
  1129. (long) addend);
  1130. #endif
  1131. r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
  1132. offset, relocation, addend);
  1133. if (r != bfd_reloc_ok)
  1134. {
  1135. ret = FALSE;
  1136. switch (r)
  1137. {
  1138. default:
  1139. break;
  1140. case bfd_reloc_overflow:
  1141. {
  1142. const char *name;
  1143. if (h != NULL)
  1144. name = NULL;
  1145. else
  1146. {
  1147. name = bfd_elf_string_from_elf_section (input_bfd,
  1148. symtab_hdr->sh_link,
  1149. sym->st_name);
  1150. if (name == NULL)
  1151. break;
  1152. if (*name == '\0')
  1153. name = bfd_section_name (input_bfd, sec);
  1154. }
  1155. (*info->callbacks->reloc_overflow) (info,
  1156. (h ? &h->root : NULL),
  1157. name,
  1158. howto->name,
  1159. (bfd_vma) 0,
  1160. input_bfd,
  1161. input_section,
  1162. offset);
  1163. }
  1164. break;
  1165. }
  1166. }
  1167. }
  1168. #ifdef DEBUG
  1169. fprintf (stderr, "\n");
  1170. #endif
  1171. return ret;
  1172. }
  1173. #define TARGET_BIG_SYM i370_elf32_vec
  1174. #define TARGET_BIG_NAME "elf32-i370"
  1175. #define ELF_ARCH bfd_arch_i370
  1176. #define ELF_MACHINE_CODE EM_S370
  1177. #ifdef EM_I370_OLD
  1178. #define ELF_MACHINE_ALT1 EM_I370_OLD
  1179. #endif
  1180. #define ELF_MAXPAGESIZE 0x1000
  1181. #define ELF_OSABI ELFOSABI_GNU
  1182. #define elf_info_to_howto i370_elf_info_to_howto
  1183. #define elf_backend_plt_not_loaded 1
  1184. #define elf_backend_rela_normal 1
  1185. #define bfd_elf32_bfd_reloc_type_lookup i370_elf_reloc_type_lookup
  1186. #define bfd_elf32_bfd_reloc_name_lookup i370_elf_reloc_name_lookup
  1187. #define bfd_elf32_bfd_set_private_flags i370_elf_set_private_flags
  1188. #define bfd_elf32_bfd_merge_private_bfd_data i370_elf_merge_private_bfd_data
  1189. #define elf_backend_relocate_section i370_elf_relocate_section
  1190. /* Dynamic loader support is mostly broken; just enough here to be able to
  1191. link glibc's ld.so without errors. */
  1192. #define elf_backend_create_dynamic_sections i370_elf_create_dynamic_sections
  1193. #define elf_backend_size_dynamic_sections i370_elf_size_dynamic_sections
  1194. #define elf_backend_init_index_section _bfd_elf_init_1_index_section
  1195. #define elf_backend_finish_dynamic_sections i370_elf_finish_dynamic_sections
  1196. #define elf_backend_fake_sections i370_elf_fake_sections
  1197. #define elf_backend_section_from_shdr i370_elf_section_from_shdr
  1198. #define elf_backend_adjust_dynamic_symbol i370_elf_adjust_dynamic_symbol
  1199. #define elf_backend_check_relocs i370_elf_check_relocs
  1200. static int
  1201. i370_noop (void)
  1202. {
  1203. return 1;
  1204. }
  1205. #define elf_backend_finish_dynamic_symbol \
  1206. (bfd_boolean (*) \
  1207. (bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, \
  1208. Elf_Internal_Sym *)) i370_noop
  1209. #include "elf32-target.h"