elf32-pru.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  1. /* 32-bit ELF support for TI PRU.
  2. Copyright (C) 2014-2017 Free Software Foundation, Inc.
  3. Contributed by Dimitar Dimitrov <dimitar@dinux.eu>
  4. Based on elf32-nios2.c
  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 handles TI PRU ELF targets. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "libbfd.h"
  22. #include "bfdlink.h"
  23. #include "genlink.h"
  24. #include "elf-bfd.h"
  25. #include "elf/pru.h"
  26. #include "opcode/pru.h"
  27. #define SWAP_VALS(A,B) \
  28. do { \
  29. (A) ^= (B); \
  30. (B) ^= (A); \
  31. (A) ^= (B); \
  32. } while (0)
  33. /* Enable debugging printout at stdout with this variable. */
  34. static bfd_boolean debug_relax = FALSE;
  35. /* Forward declarations. */
  36. static bfd_reloc_status_type pru_elf32_pmem_relocate
  37. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  38. static bfd_reloc_status_type pru_elf32_s10_pcrel_relocate
  39. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  40. static bfd_reloc_status_type pru_elf32_u8_pcrel_relocate
  41. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  42. static bfd_reloc_status_type pru_elf32_ldi32_relocate
  43. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  44. static bfd_reloc_status_type bfd_elf_pru_diff_relocate
  45. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  46. /* Target vector. */
  47. extern const bfd_target pru_elf32_vec;
  48. /* The relocation table used for SHT_REL sections. */
  49. static reloc_howto_type elf_pru_howto_table_rel[] = {
  50. /* No relocation. */
  51. HOWTO (R_PRU_NONE, /* type */
  52. 0, /* rightshift */
  53. 0, /* size (0 = byte, 1 = short, 2 = long) */
  54. 3, /* bitsize */
  55. FALSE, /* pc_relative */
  56. 0, /* bitpos */
  57. complain_overflow_dont,/* complain_on_overflow */
  58. bfd_elf_generic_reloc, /* special_function */
  59. "R_PRU_NONE", /* name */
  60. FALSE, /* partial_inplace */
  61. 0, /* src_mask */
  62. 0, /* dst_mask */
  63. FALSE), /* pcrel_offset */
  64. HOWTO (R_PRU_16_PMEM,
  65. 2,
  66. 1, /* short */
  67. 32,
  68. FALSE,
  69. 0,
  70. complain_overflow_dont,
  71. bfd_elf_generic_reloc,
  72. "R_PRU_16_PMEM",
  73. FALSE,
  74. 0, /* src_mask */
  75. 0xffff,
  76. FALSE),
  77. HOWTO (R_PRU_U16_PMEMIMM,
  78. 2,
  79. 2,
  80. 32,
  81. FALSE,
  82. 8,
  83. complain_overflow_unsigned,
  84. pru_elf32_pmem_relocate,
  85. "R_PRU_U16_PMEMIMM",
  86. FALSE,
  87. 0, /* src_mask */
  88. 0x00ffff00,
  89. FALSE),
  90. HOWTO (R_PRU_BFD_RELOC_16,
  91. 0,
  92. 1, /* short */
  93. 16,
  94. FALSE,
  95. 0,
  96. complain_overflow_bitfield,
  97. bfd_elf_generic_reloc,
  98. "R_PRU_BFD_RELOC16",
  99. FALSE,
  100. 0, /* src_mask */
  101. 0x0000ffff,
  102. FALSE),
  103. /* 16-bit unsigned immediate relocation. */
  104. HOWTO (R_PRU_U16, /* type */
  105. 0, /* rightshift */
  106. 2, /* size (0 = byte, 1 = short, 2 = long) */
  107. 16, /* bitsize */
  108. FALSE, /* pc_relative */
  109. 8, /* bitpos */
  110. complain_overflow_unsigned, /* complain on overflow */
  111. bfd_elf_generic_reloc, /* special function */
  112. "R_PRU_U16", /* name */
  113. FALSE, /* partial_inplace */
  114. 0, /* src_mask */
  115. 0x00ffff00, /* dest_mask */
  116. FALSE), /* pcrel_offset */
  117. HOWTO (R_PRU_32_PMEM,
  118. 2,
  119. 2, /* long */
  120. 32,
  121. FALSE,
  122. 0,
  123. complain_overflow_dont,
  124. pru_elf32_pmem_relocate,
  125. "R_PRU_32_PMEM",
  126. FALSE,
  127. 0, /* src_mask */
  128. 0xffffffff,
  129. FALSE),
  130. HOWTO (R_PRU_BFD_RELOC_32,
  131. 0,
  132. 2, /* long */
  133. 32,
  134. FALSE,
  135. 0,
  136. complain_overflow_dont,
  137. bfd_elf_generic_reloc,
  138. "R_PRU_BFD_RELOC32",
  139. FALSE,
  140. 0, /* src_mask */
  141. 0xffffffff,
  142. FALSE),
  143. HOWTO (R_PRU_S10_PCREL,
  144. 2,
  145. 2,
  146. 10,
  147. TRUE,
  148. 0,
  149. complain_overflow_bitfield,
  150. pru_elf32_s10_pcrel_relocate,
  151. "R_PRU_S10_PCREL",
  152. FALSE,
  153. 0, /* src_mask */
  154. 0x060000ff,
  155. TRUE),
  156. HOWTO (R_PRU_U8_PCREL,
  157. 2,
  158. 2,
  159. 8,
  160. TRUE,
  161. 0,
  162. complain_overflow_unsigned,
  163. pru_elf32_u8_pcrel_relocate,
  164. "R_PRU_U8_PCREL",
  165. FALSE,
  166. 0, /* src_mask */
  167. 0x000000ff,
  168. TRUE),
  169. HOWTO (R_PRU_LDI32,
  170. 0, /* rightshift */
  171. 4, /* size (4 = 8bytes) */
  172. 32, /* bitsize */
  173. FALSE, /* pc_relative */
  174. 0, /* bitpos */
  175. complain_overflow_unsigned, /* complain on overflow */
  176. pru_elf32_ldi32_relocate, /* special function */
  177. "R_PRU_LDI32", /* name */
  178. FALSE, /* partial_inplace */
  179. 0, /* src_mask */
  180. 0xffffffff, /* dest_mask */
  181. FALSE), /* pcrel_offset */
  182. /* GNU-specific relocations. */
  183. HOWTO (R_PRU_GNU_BFD_RELOC_8,
  184. 0,
  185. 0, /* byte */
  186. 8,
  187. FALSE,
  188. 0,
  189. complain_overflow_bitfield,
  190. bfd_elf_generic_reloc,
  191. "R_PRU_BFD_RELOC8",
  192. FALSE,
  193. 0, /* src_mask */
  194. 0x000000ff,
  195. FALSE),
  196. HOWTO (R_PRU_GNU_DIFF8, /* type */
  197. 0, /* rightshift */
  198. 0, /* size (0 = byte, 1 = short, 2 = long) */
  199. 8, /* bitsize */
  200. FALSE, /* pc_relative */
  201. 0, /* bitpos */
  202. complain_overflow_bitfield, /* complain_on_overflow */
  203. bfd_elf_pru_diff_relocate, /* special_function */
  204. "R_PRU_DIFF8", /* name */
  205. FALSE, /* partial_inplace */
  206. 0, /* src_mask */
  207. 0xff, /* dst_mask */
  208. FALSE), /* pcrel_offset */
  209. HOWTO (R_PRU_GNU_DIFF16, /* type */
  210. 0, /* rightshift */
  211. 1, /* size (0 = byte, 1 = short, 2 = long) */
  212. 16, /* bitsize */
  213. FALSE, /* pc_relative */
  214. 0, /* bitpos */
  215. complain_overflow_bitfield, /* complain_on_overflow */
  216. bfd_elf_pru_diff_relocate,/* special_function */
  217. "R_PRU_DIFF16", /* name */
  218. FALSE, /* partial_inplace */
  219. 0, /* src_mask */
  220. 0xffff, /* dst_mask */
  221. FALSE), /* pcrel_offset */
  222. HOWTO (R_PRU_GNU_DIFF32, /* type */
  223. 0, /* rightshift */
  224. 2, /* size (0 = byte, 1 = short, 2 = long) */
  225. 32, /* bitsize */
  226. FALSE, /* pc_relative */
  227. 0, /* bitpos */
  228. complain_overflow_bitfield, /* complain_on_overflow */
  229. bfd_elf_pru_diff_relocate,/* special_function */
  230. "R_PRU_DIFF32", /* name */
  231. FALSE, /* partial_inplace */
  232. 0, /* src_mask */
  233. 0xffffffff, /* dst_mask */
  234. FALSE), /* pcrel_offset */
  235. HOWTO (R_PRU_GNU_DIFF16_PMEM, /* type */
  236. 0, /* rightshift */
  237. 1, /* size (0 = byte, 1 = short, 2 = long) */
  238. 16, /* bitsize */
  239. FALSE, /* pc_relative */
  240. 0, /* bitpos */
  241. complain_overflow_bitfield, /* complain_on_overflow */
  242. bfd_elf_pru_diff_relocate,/* special_function */
  243. "R_PRU_DIFF16_PMEM", /* name */
  244. FALSE, /* partial_inplace */
  245. 0, /* src_mask */
  246. 0xffff, /* dst_mask */
  247. FALSE), /* pcrel_offset */
  248. HOWTO (R_PRU_GNU_DIFF32_PMEM, /* type */
  249. 0, /* rightshift */
  250. 2, /* size (0 = byte, 1 = short, 2 = long) */
  251. 32, /* bitsize */
  252. FALSE, /* pc_relative */
  253. 0, /* bitpos */
  254. complain_overflow_bitfield, /* complain_on_overflow */
  255. bfd_elf_pru_diff_relocate,/* special_function */
  256. "R_PRU_DIFF32_PMEM", /* name */
  257. FALSE, /* partial_inplace */
  258. 0, /* src_mask */
  259. 0xffffffff, /* dst_mask */
  260. FALSE), /* pcrel_offset */
  261. /* Add other relocations here. */
  262. };
  263. static unsigned char elf_code_to_howto_index[R_PRU_ILLEGAL + 1];
  264. /* Return the howto for relocation RTYPE. */
  265. static reloc_howto_type *
  266. lookup_howto (unsigned int rtype)
  267. {
  268. static int initialized = 0;
  269. int i;
  270. int howto_tbl_size = (int) (sizeof (elf_pru_howto_table_rel)
  271. / sizeof (elf_pru_howto_table_rel[0]));
  272. if (!initialized)
  273. {
  274. initialized = 1;
  275. memset (elf_code_to_howto_index, 0xff,
  276. sizeof (elf_code_to_howto_index));
  277. for (i = 0; i < howto_tbl_size; i++)
  278. elf_code_to_howto_index[elf_pru_howto_table_rel[i].type] = i;
  279. }
  280. BFD_ASSERT (rtype <= R_PRU_ILLEGAL);
  281. i = elf_code_to_howto_index[rtype];
  282. if (i >= howto_tbl_size)
  283. return 0;
  284. return elf_pru_howto_table_rel + i;
  285. }
  286. /* Map for converting BFD reloc types to PRU reloc types. */
  287. struct elf_reloc_map
  288. {
  289. bfd_reloc_code_real_type bfd_val;
  290. enum elf_pru_reloc_type elf_val;
  291. };
  292. static const struct elf_reloc_map pru_reloc_map[] = {
  293. {BFD_RELOC_NONE, R_PRU_NONE},
  294. {BFD_RELOC_PRU_16_PMEM, R_PRU_16_PMEM},
  295. {BFD_RELOC_PRU_U16_PMEMIMM, R_PRU_U16_PMEMIMM},
  296. {BFD_RELOC_16, R_PRU_BFD_RELOC_16},
  297. {BFD_RELOC_PRU_U16, R_PRU_U16},
  298. {BFD_RELOC_PRU_32_PMEM, R_PRU_32_PMEM},
  299. {BFD_RELOC_32, R_PRU_BFD_RELOC_32},
  300. {BFD_RELOC_PRU_S10_PCREL, R_PRU_S10_PCREL},
  301. {BFD_RELOC_PRU_U8_PCREL, R_PRU_U8_PCREL},
  302. {BFD_RELOC_PRU_LDI32, R_PRU_LDI32},
  303. {BFD_RELOC_8, R_PRU_GNU_BFD_RELOC_8},
  304. {BFD_RELOC_PRU_GNU_DIFF8, R_PRU_GNU_DIFF8},
  305. {BFD_RELOC_PRU_GNU_DIFF16, R_PRU_GNU_DIFF16},
  306. {BFD_RELOC_PRU_GNU_DIFF32, R_PRU_GNU_DIFF32},
  307. {BFD_RELOC_PRU_GNU_DIFF16_PMEM, R_PRU_GNU_DIFF16_PMEM},
  308. {BFD_RELOC_PRU_GNU_DIFF32_PMEM, R_PRU_GNU_DIFF32_PMEM},
  309. };
  310. /* Assorted hash table functions. */
  311. /* Create an entry in a PRU ELF linker hash table. */
  312. static struct bfd_hash_entry *
  313. link_hash_newfunc (struct bfd_hash_entry *entry,
  314. struct bfd_hash_table *table, const char *string)
  315. {
  316. /* Allocate the structure if it has not already been allocated by a
  317. subclass. */
  318. if (entry == NULL)
  319. {
  320. entry = bfd_hash_allocate (table,
  321. sizeof (struct elf_link_hash_entry));
  322. if (entry == NULL)
  323. return entry;
  324. }
  325. /* Call the allocation method of the superclass. */
  326. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  327. return entry;
  328. }
  329. /* Implement bfd_elf32_bfd_reloc_type_lookup:
  330. Given a BFD reloc type, return a howto structure. */
  331. static reloc_howto_type *
  332. pru_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  333. bfd_reloc_code_real_type code)
  334. {
  335. int i;
  336. for (i = 0;
  337. i < (int) (sizeof (pru_reloc_map) / sizeof (struct elf_reloc_map));
  338. ++i)
  339. if (pru_reloc_map[i].bfd_val == code)
  340. return lookup_howto ((unsigned int) pru_reloc_map[i].elf_val);
  341. return NULL;
  342. }
  343. /* Implement bfd_elf32_bfd_reloc_name_lookup:
  344. Given a reloc name, return a howto structure. */
  345. static reloc_howto_type *
  346. pru_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  347. const char *r_name)
  348. {
  349. unsigned int i;
  350. for (i = 0;
  351. i < (sizeof (elf_pru_howto_table_rel)
  352. / sizeof (elf_pru_howto_table_rel[0]));
  353. i++)
  354. if (elf_pru_howto_table_rel[i].name
  355. && strcasecmp (elf_pru_howto_table_rel[i].name, r_name) == 0)
  356. return &elf_pru_howto_table_rel[i];
  357. return NULL;
  358. }
  359. /* Implement elf_info_to_howto:
  360. Given a ELF32 relocation, fill in a arelent structure. */
  361. static void
  362. pru_elf32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
  363. Elf_Internal_Rela *dst)
  364. {
  365. unsigned int r_type;
  366. r_type = ELF32_R_TYPE (dst->r_info);
  367. BFD_ASSERT (r_type < R_PRU_ILLEGAL);
  368. cache_ptr->howto = lookup_howto (r_type);
  369. }
  370. /* Do the relocations that require special handling. */
  371. /* Produce a word address for program memory. Linker scripts will put .text
  372. at a high offset in order to differentiate it from .data. So here we also
  373. mask the high bits of PMEM address.
  374. But why 1MB when internal Program Memory much smaller? We want to catch
  375. unintended overflows.
  376. Why not use (1<<31) as an offset and a mask? Sitara DDRAM usually resides
  377. there, and users might want to put some shared carveout memory region in
  378. their linker scripts. So 0x80000000 might be a valid .data address.
  379. Note that we still keep and pass down the original howto. This way we
  380. can reuse this function for several different relocations. */
  381. static bfd_reloc_status_type
  382. pru_elf32_do_pmem_relocate (bfd *abfd, reloc_howto_type *howto,
  383. asection *input_section,
  384. bfd_byte *data, bfd_vma offset,
  385. bfd_vma symbol_value, bfd_vma addend)
  386. {
  387. symbol_value = symbol_value + addend;
  388. addend = 0;
  389. symbol_value &= 0x3fffff;
  390. return _bfd_final_link_relocate (howto, abfd, input_section,
  391. data, offset, symbol_value, addend);
  392. }
  393. /* Direct copy of _bfd_final_link_relocate, but with special
  394. "fill-in". This copy-paste mumbo jumbo is only needed because BFD
  395. cannot deal correctly with non-contiguous bit fields. */
  396. static bfd_reloc_status_type
  397. pru_elf32_do_s10_pcrel_relocate (bfd *input_bfd, reloc_howto_type *howto,
  398. asection *input_section,
  399. bfd_byte *contents, bfd_vma address,
  400. bfd_vma relocation, bfd_vma addend)
  401. {
  402. bfd_byte *location;
  403. bfd_vma x = 0;
  404. bfd_vma qboff;
  405. bfd_reloc_status_type flag = bfd_reloc_ok;
  406. /* Sanity check the address. */
  407. if (address > bfd_get_section_limit (input_bfd, input_section))
  408. return bfd_reloc_outofrange;
  409. BFD_ASSERT (howto->pc_relative);
  410. BFD_ASSERT (howto->pcrel_offset);
  411. relocation = relocation + addend - (input_section->output_section->vma
  412. + input_section->output_offset) - address;
  413. location = contents + address;
  414. /* Get the value we are going to relocate. */
  415. BFD_ASSERT (bfd_get_reloc_size (howto) == 4);
  416. x = bfd_get_32 (input_bfd, location);
  417. qboff = GET_BROFF_SIGNED (x) << howto->rightshift;
  418. relocation += qboff;
  419. BFD_ASSERT (howto->complain_on_overflow == complain_overflow_bitfield);
  420. if (relocation > 2047 && relocation < (bfd_vma)-2048l)
  421. flag = bfd_reloc_overflow;
  422. /* Check that target address is word-aligned. */
  423. if (relocation & ((1 << howto->rightshift) - 1))
  424. flag = bfd_reloc_outofrange;
  425. relocation >>= (bfd_vma) howto->rightshift;
  426. /* Fill-in the RELOCATION to the right bits of X. */
  427. SET_BROFF_URAW (x, relocation);
  428. bfd_put_32 (input_bfd, x, location);
  429. return flag;
  430. }
  431. static bfd_reloc_status_type
  432. pru_elf32_do_u8_pcrel_relocate (bfd *abfd, reloc_howto_type *howto,
  433. asection *input_section,
  434. bfd_byte *data, bfd_vma offset,
  435. bfd_vma symbol_value, bfd_vma addend)
  436. {
  437. bfd_vma relocation;
  438. BFD_ASSERT (howto->pc_relative);
  439. BFD_ASSERT (howto->pcrel_offset);
  440. relocation = symbol_value + addend - (input_section->output_section->vma
  441. + input_section->output_offset) - offset;
  442. relocation >>= howto->rightshift;
  443. /* 0 and 1 are invalid target labels for LOOP. We cannot
  444. encode this info in HOWTO, so catch such cases here. */
  445. if (relocation < 2)
  446. return bfd_reloc_outofrange;
  447. return _bfd_final_link_relocate (howto, abfd, input_section,
  448. data, offset, symbol_value, addend);
  449. }
  450. /* Idea and code taken from elf32-d30v. */
  451. static bfd_reloc_status_type
  452. pru_elf32_do_ldi32_relocate (bfd *abfd, reloc_howto_type *howto,
  453. asection *input_section,
  454. bfd_byte *data, bfd_vma offset,
  455. bfd_vma symbol_value, bfd_vma addend)
  456. {
  457. bfd_signed_vma relocation;
  458. bfd_size_type octets = offset * bfd_octets_per_byte (abfd);
  459. bfd_byte *location;
  460. unsigned long in1, in2, num;
  461. /* A hacked-up version of _bfd_final_link_relocate() follows. */
  462. /* Sanity check the address. */
  463. if (octets + bfd_get_reloc_size (howto)
  464. > bfd_get_section_limit_octets (abfd, input_section))
  465. return bfd_reloc_outofrange;
  466. /* This function assumes that we are dealing with a basic relocation
  467. against a symbol. We want to compute the value of the symbol to
  468. relocate to. This is just VALUE, the value of the symbol, plus
  469. ADDEND, any addend associated with the reloc. */
  470. relocation = symbol_value + addend;
  471. BFD_ASSERT (!howto->pc_relative);
  472. /* A hacked-up version of _bfd_relocate_contents() follows. */
  473. location = data + offset * bfd_octets_per_byte (abfd);
  474. BFD_ASSERT (!howto->pc_relative);
  475. in1 = bfd_get_32 (abfd, location);
  476. in2 = bfd_get_32 (abfd, location + 4);
  477. /* Extract the addend - should be zero per my understanding. */
  478. num = GET_INSN_FIELD (IMM16, in1) | (GET_INSN_FIELD (IMM16, in2) << 16);
  479. BFD_ASSERT (!num);
  480. relocation += num;
  481. SET_INSN_FIELD (IMM16, in1, relocation & 0xffff);
  482. SET_INSN_FIELD (IMM16, in2, relocation >> 16);
  483. bfd_put_32 (abfd, in1, location);
  484. bfd_put_32 (abfd, in2, location + 4);
  485. return bfd_reloc_ok;
  486. }
  487. /* HOWTO handlers for relocations that require special handling. */
  488. static bfd_reloc_status_type
  489. pru_elf32_pmem_relocate (bfd *abfd, arelent *reloc_entry,
  490. asymbol *symbol, void *data,
  491. asection *input_section, bfd *output_bfd,
  492. char **error_message)
  493. {
  494. /* If this is a relocatable link (output_bfd test tells us), just
  495. call the generic function. Any adjustment will be done at final
  496. link time. */
  497. if (output_bfd != NULL)
  498. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  499. input_section, output_bfd, error_message);
  500. return pru_elf32_do_pmem_relocate (abfd, reloc_entry->howto,
  501. input_section,
  502. data, reloc_entry->address,
  503. (symbol->value
  504. + symbol->section->output_section->vma
  505. + symbol->section->output_offset),
  506. reloc_entry->addend);
  507. }
  508. static bfd_reloc_status_type
  509. pru_elf32_s10_pcrel_relocate (bfd *abfd, arelent *reloc_entry,
  510. asymbol *symbol, void *data,
  511. asection *input_section, bfd *output_bfd,
  512. char **error_message)
  513. {
  514. /* If this is a relocatable link (output_bfd test tells us), just
  515. call the generic function. Any adjustment will be done at final
  516. link time. */
  517. if (output_bfd != NULL)
  518. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  519. input_section, output_bfd, error_message);
  520. return pru_elf32_do_s10_pcrel_relocate (abfd, reloc_entry->howto,
  521. input_section, data,
  522. reloc_entry->address,
  523. (symbol->value
  524. + symbol->section->output_section->vma
  525. + symbol->section->output_offset),
  526. reloc_entry->addend);
  527. }
  528. static bfd_reloc_status_type
  529. pru_elf32_u8_pcrel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  530. void *data, asection *input_section,
  531. bfd *output_bfd,
  532. char **error_message)
  533. {
  534. /* If this is a relocatable link (output_bfd test tells us), just
  535. call the generic function. Any adjustment will be done at final
  536. link time. */
  537. if (output_bfd != NULL)
  538. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  539. input_section, output_bfd, error_message);
  540. return pru_elf32_do_u8_pcrel_relocate (abfd, reloc_entry->howto,
  541. input_section,
  542. data, reloc_entry->address,
  543. (symbol->value
  544. + symbol->section->output_section->vma
  545. + symbol->section->output_offset),
  546. reloc_entry->addend);
  547. }
  548. static bfd_reloc_status_type
  549. pru_elf32_ldi32_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  550. void *data, asection *input_section,
  551. bfd *output_bfd,
  552. char **error_message)
  553. {
  554. /* If this is a relocatable link (output_bfd test tells us), just
  555. call the generic function. Any adjustment will be done at final
  556. link time. */
  557. if (output_bfd != NULL)
  558. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  559. input_section, output_bfd, error_message);
  560. return pru_elf32_do_ldi32_relocate (abfd, reloc_entry->howto,
  561. input_section,
  562. data, reloc_entry->address,
  563. (symbol->value
  564. + symbol->section->output_section->vma
  565. + symbol->section->output_offset),
  566. reloc_entry->addend);
  567. }
  568. /* Implement elf_backend_relocate_section. */
  569. static bfd_boolean
  570. pru_elf32_relocate_section (bfd *output_bfd,
  571. struct bfd_link_info *info,
  572. bfd *input_bfd,
  573. asection *input_section,
  574. bfd_byte *contents,
  575. Elf_Internal_Rela *relocs,
  576. Elf_Internal_Sym *local_syms,
  577. asection **local_sections)
  578. {
  579. Elf_Internal_Shdr *symtab_hdr;
  580. struct elf_link_hash_entry **sym_hashes;
  581. Elf_Internal_Rela *rel;
  582. Elf_Internal_Rela *relend;
  583. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  584. sym_hashes = elf_sym_hashes (input_bfd);
  585. relend = relocs + input_section->reloc_count;
  586. for (rel = relocs; rel < relend; rel++)
  587. {
  588. reloc_howto_type *howto;
  589. unsigned long r_symndx;
  590. Elf_Internal_Sym *sym;
  591. asection *sec;
  592. struct elf_link_hash_entry *h;
  593. bfd_vma relocation;
  594. bfd_reloc_status_type r = bfd_reloc_ok;
  595. const char *name = NULL;
  596. const char* msg = (const char*) NULL;
  597. bfd_boolean unresolved_reloc;
  598. r_symndx = ELF32_R_SYM (rel->r_info);
  599. howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info));
  600. h = NULL;
  601. sym = NULL;
  602. sec = NULL;
  603. if (r_symndx < symtab_hdr->sh_info)
  604. {
  605. sym = local_syms + r_symndx;
  606. sec = local_sections[r_symndx];
  607. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  608. }
  609. else
  610. {
  611. bfd_boolean warned, ignored;
  612. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  613. r_symndx, symtab_hdr, sym_hashes,
  614. h, sec, relocation,
  615. unresolved_reloc, warned, ignored);
  616. }
  617. if (sec && discarded_section (sec))
  618. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  619. rel, 1, relend, howto, 0, contents);
  620. /* Nothing more to do unless this is a final link. */
  621. if (bfd_link_relocatable (info))
  622. continue;
  623. if (howto)
  624. {
  625. switch (howto->type)
  626. {
  627. case R_PRU_NONE:
  628. /* We don't need to find a value for this symbol. It's just a
  629. marker. */
  630. r = bfd_reloc_ok;
  631. break;
  632. case R_PRU_U16_PMEMIMM:
  633. case R_PRU_32_PMEM:
  634. case R_PRU_16_PMEM:
  635. r = pru_elf32_do_pmem_relocate (input_bfd, howto,
  636. input_section,
  637. contents, rel->r_offset,
  638. relocation, rel->r_addend);
  639. break;
  640. case R_PRU_S10_PCREL:
  641. r = pru_elf32_do_s10_pcrel_relocate (input_bfd, howto,
  642. input_section,
  643. contents,
  644. rel->r_offset,
  645. relocation,
  646. rel->r_addend);
  647. break;
  648. case R_PRU_U8_PCREL:
  649. r = pru_elf32_do_u8_pcrel_relocate (input_bfd, howto,
  650. input_section,
  651. contents,
  652. rel->r_offset,
  653. relocation,
  654. rel->r_addend);
  655. break;
  656. case R_PRU_LDI32:
  657. r = pru_elf32_do_ldi32_relocate (input_bfd, howto,
  658. input_section,
  659. contents,
  660. rel->r_offset,
  661. relocation,
  662. rel->r_addend);
  663. break;
  664. case R_PRU_GNU_DIFF8:
  665. case R_PRU_GNU_DIFF16:
  666. case R_PRU_GNU_DIFF32:
  667. case R_PRU_GNU_DIFF16_PMEM:
  668. case R_PRU_GNU_DIFF32_PMEM:
  669. /* Nothing to do here, as contents already contain the
  670. diff value. */
  671. r = bfd_reloc_ok;
  672. break;
  673. default:
  674. r = _bfd_final_link_relocate (howto, input_bfd,
  675. input_section, contents,
  676. rel->r_offset, relocation,
  677. rel->r_addend);
  678. break;
  679. }
  680. }
  681. else
  682. r = bfd_reloc_notsupported;
  683. if (r != bfd_reloc_ok)
  684. {
  685. if (h != NULL)
  686. name = h->root.root.string;
  687. else
  688. {
  689. name = bfd_elf_string_from_elf_section (input_bfd,
  690. symtab_hdr->sh_link,
  691. sym->st_name);
  692. if (name == NULL || *name == '\0')
  693. name = bfd_section_name (input_bfd, sec);
  694. }
  695. switch (r)
  696. {
  697. case bfd_reloc_overflow:
  698. (*info->callbacks->reloc_overflow) (info, NULL, name,
  699. howto->name, (bfd_vma) 0,
  700. input_bfd, input_section,
  701. rel->r_offset);
  702. break;
  703. case bfd_reloc_undefined:
  704. (*info->callbacks->undefined_symbol) (info, name, input_bfd,
  705. input_section,
  706. rel->r_offset, TRUE);
  707. break;
  708. case bfd_reloc_outofrange:
  709. if (msg == NULL)
  710. msg = _("relocation out of range");
  711. break;
  712. case bfd_reloc_notsupported:
  713. if (msg == NULL)
  714. msg = _("unsupported relocation");
  715. break;
  716. case bfd_reloc_dangerous:
  717. if (msg == NULL)
  718. msg = _("dangerous relocation");
  719. break;
  720. default:
  721. if (msg == NULL)
  722. msg = _("unknown error");
  723. break;
  724. }
  725. if (msg)
  726. {
  727. (*info->callbacks->warning) (info, msg, name, input_bfd,
  728. input_section, rel->r_offset);
  729. return FALSE;
  730. }
  731. }
  732. }
  733. return TRUE;
  734. }
  735. /* Perform a diff relocation. Nothing to do, as the difference value is
  736. already written into the section's contents. */
  737. static bfd_reloc_status_type
  738. bfd_elf_pru_diff_relocate (bfd *abfd ATTRIBUTE_UNUSED,
  739. arelent *reloc_entry ATTRIBUTE_UNUSED,
  740. asymbol *symbol ATTRIBUTE_UNUSED,
  741. void *data ATTRIBUTE_UNUSED,
  742. asection *input_section ATTRIBUTE_UNUSED,
  743. bfd *output_bfd ATTRIBUTE_UNUSED,
  744. char **error_message ATTRIBUTE_UNUSED)
  745. {
  746. return bfd_reloc_ok;
  747. }
  748. /* Returns whether the relocation type passed is a diff reloc. */
  749. static bfd_boolean
  750. elf32_pru_is_diff_reloc (Elf_Internal_Rela *irel)
  751. {
  752. return (ELF32_R_TYPE (irel->r_info) == R_PRU_GNU_DIFF8
  753. || ELF32_R_TYPE (irel->r_info) == R_PRU_GNU_DIFF16
  754. || ELF32_R_TYPE (irel->r_info) == R_PRU_GNU_DIFF32
  755. || ELF32_R_TYPE (irel->r_info) == R_PRU_GNU_DIFF16_PMEM
  756. || ELF32_R_TYPE (irel->r_info) == R_PRU_GNU_DIFF32_PMEM);
  757. }
  758. /* Reduce the diff value written in the section by count if the shrinked
  759. insn address happens to fall between the two symbols for which this
  760. diff reloc was emitted. */
  761. static void
  762. elf32_pru_adjust_diff_reloc_value (bfd *abfd,
  763. struct bfd_section *isec,
  764. Elf_Internal_Rela *irel,
  765. bfd_vma symval,
  766. bfd_vma shrinked_insn_address,
  767. int count)
  768. {
  769. unsigned char *reloc_contents = NULL;
  770. unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents;
  771. if (isec_contents == NULL)
  772. {
  773. if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents))
  774. return;
  775. elf_section_data (isec)->this_hdr.contents = isec_contents;
  776. }
  777. reloc_contents = isec_contents + irel->r_offset;
  778. /* Read value written in object file. */
  779. bfd_signed_vma x = 0;
  780. switch (ELF32_R_TYPE (irel->r_info))
  781. {
  782. case R_PRU_GNU_DIFF8:
  783. {
  784. x = bfd_get_signed_8 (abfd, reloc_contents);
  785. break;
  786. }
  787. case R_PRU_GNU_DIFF16:
  788. {
  789. x = bfd_get_signed_16 (abfd, reloc_contents);
  790. break;
  791. }
  792. case R_PRU_GNU_DIFF32:
  793. {
  794. x = bfd_get_signed_32 (abfd, reloc_contents);
  795. break;
  796. }
  797. case R_PRU_GNU_DIFF16_PMEM:
  798. {
  799. x = bfd_get_signed_16 (abfd, reloc_contents) * 4;
  800. break;
  801. }
  802. case R_PRU_GNU_DIFF32_PMEM:
  803. {
  804. x = bfd_get_signed_32 (abfd, reloc_contents) * 4;
  805. break;
  806. }
  807. default:
  808. {
  809. BFD_FAIL ();
  810. }
  811. }
  812. /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
  813. into the object file at the reloc offset. sym2's logical value is
  814. symval (<start_of_section>) + reloc addend. Compute the start and end
  815. addresses and check if the shrinked insn falls between sym1 and sym2. */
  816. bfd_vma end_address = symval + irel->r_addend;
  817. bfd_vma start_address = end_address - x;
  818. /* Shrink the absolute DIFF value (get the to labels "closer"
  819. together), because we have removed data between labels. */
  820. if (x < 0)
  821. {
  822. x += count;
  823. /* In case the signed x is negative, restore order. */
  824. SWAP_VALS (end_address, start_address);
  825. }
  826. else
  827. {
  828. x -= count;
  829. }
  830. /* Reduce the diff value by count bytes and write it back into section
  831. contents. */
  832. if (shrinked_insn_address >= start_address
  833. && shrinked_insn_address <= end_address)
  834. {
  835. switch (ELF32_R_TYPE (irel->r_info))
  836. {
  837. case R_PRU_GNU_DIFF8:
  838. {
  839. bfd_put_signed_8 (abfd, x & 0xFF, reloc_contents);
  840. break;
  841. }
  842. case R_PRU_GNU_DIFF16:
  843. {
  844. bfd_put_signed_16 (abfd, x & 0xFFFF, reloc_contents);
  845. break;
  846. }
  847. case R_PRU_GNU_DIFF32:
  848. {
  849. bfd_put_signed_32 (abfd, x & 0xFFFFFFFF, reloc_contents);
  850. break;
  851. }
  852. case R_PRU_GNU_DIFF16_PMEM:
  853. {
  854. bfd_put_signed_16 (abfd, (x / 4) & 0xFFFF, reloc_contents);
  855. break;
  856. }
  857. case R_PRU_GNU_DIFF32_PMEM:
  858. {
  859. bfd_put_signed_32 (abfd, (x / 4) & 0xFFFFFFFF, reloc_contents);
  860. break;
  861. }
  862. default:
  863. {
  864. BFD_FAIL ();
  865. }
  866. }
  867. }
  868. }
  869. /* Delete some bytes from a section while changing the size of an instruction.
  870. The parameter "addr" denotes the section-relative offset pointing just
  871. behind the shrinked instruction. "addr+count" point at the first
  872. byte just behind the original unshrinked instruction.
  873. Idea copied from the AVR port. */
  874. static bfd_boolean
  875. pru_elf_relax_delete_bytes (bfd *abfd,
  876. asection *sec,
  877. bfd_vma addr,
  878. int count)
  879. {
  880. Elf_Internal_Shdr *symtab_hdr;
  881. unsigned int sec_shndx;
  882. bfd_byte *contents;
  883. Elf_Internal_Rela *irel, *irelend;
  884. Elf_Internal_Sym *isym;
  885. Elf_Internal_Sym *isymbuf = NULL;
  886. bfd_vma toaddr;
  887. struct elf_link_hash_entry **sym_hashes;
  888. struct elf_link_hash_entry **end_hashes;
  889. unsigned int symcount;
  890. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  891. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  892. contents = elf_section_data (sec)->this_hdr.contents;
  893. toaddr = sec->size;
  894. irel = elf_section_data (sec)->relocs;
  895. irelend = irel + sec->reloc_count;
  896. /* Actually delete the bytes. */
  897. if (toaddr - addr - count > 0)
  898. memmove (contents + addr, contents + addr + count,
  899. (size_t) (toaddr - addr - count));
  900. sec->size -= count;
  901. /* Adjust all the reloc addresses. */
  902. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  903. {
  904. bfd_vma old_reloc_address;
  905. old_reloc_address = (sec->output_section->vma
  906. + sec->output_offset + irel->r_offset);
  907. /* Get the new reloc address. */
  908. if ((irel->r_offset > addr
  909. && irel->r_offset < toaddr))
  910. {
  911. if (debug_relax)
  912. printf ("Relocation at address 0x%x needs to be moved.\n"
  913. "Old section offset: 0x%x, New section offset: 0x%x \n",
  914. (unsigned int) old_reloc_address,
  915. (unsigned int) irel->r_offset,
  916. (unsigned int) ((irel->r_offset) - count));
  917. irel->r_offset -= count;
  918. }
  919. }
  920. /* The reloc's own addresses are now ok. However, we need to readjust
  921. the reloc's addend, i.e. the reloc's value if two conditions are met:
  922. 1.) the reloc is relative to a symbol in this section that
  923. is located in front of the shrinked instruction
  924. 2.) symbol plus addend end up behind the shrinked instruction.
  925. The most common case where this happens are relocs relative to
  926. the section-start symbol.
  927. This step needs to be done for all of the sections of the bfd. */
  928. {
  929. struct bfd_section *isec;
  930. for (isec = abfd->sections; isec; isec = isec->next)
  931. {
  932. bfd_vma symval;
  933. bfd_vma shrinked_insn_address;
  934. if (isec->reloc_count == 0)
  935. continue;
  936. shrinked_insn_address = (sec->output_section->vma
  937. + sec->output_offset + addr - count);
  938. irel = elf_section_data (isec)->relocs;
  939. /* PR 12161: Read in the relocs for this section if necessary. */
  940. if (irel == NULL)
  941. irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
  942. for (irelend = irel + isec->reloc_count;
  943. irel < irelend;
  944. irel++)
  945. {
  946. /* Read this BFD's local symbols if we haven't done
  947. so already. */
  948. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  949. {
  950. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  951. if (isymbuf == NULL)
  952. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  953. symtab_hdr->sh_info, 0,
  954. NULL, NULL, NULL);
  955. if (isymbuf == NULL)
  956. return FALSE;
  957. }
  958. /* Get the value of the symbol referred to by the reloc. */
  959. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  960. {
  961. /* A local symbol. */
  962. asection *sym_sec;
  963. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  964. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  965. symval = isym->st_value;
  966. /* If the reloc is absolute, it will not have
  967. a symbol or section associated with it. */
  968. if (sym_sec == sec)
  969. {
  970. symval += sym_sec->output_section->vma
  971. + sym_sec->output_offset;
  972. if (debug_relax)
  973. printf ("Checking if the relocation's "
  974. "addend needs corrections.\n"
  975. "Address of anchor symbol: 0x%x \n"
  976. "Address of relocation target: 0x%x \n"
  977. "Address of relaxed insn: 0x%x \n",
  978. (unsigned int) symval,
  979. (unsigned int) (symval + irel->r_addend),
  980. (unsigned int) shrinked_insn_address);
  981. /* Shrink the special DIFF relocations. */
  982. if (elf32_pru_is_diff_reloc (irel))
  983. {
  984. elf32_pru_adjust_diff_reloc_value (abfd, isec, irel,
  985. symval,
  986. shrinked_insn_address,
  987. count);
  988. }
  989. /* Fix the addend, if it is affected. */
  990. if (symval <= shrinked_insn_address
  991. && (symval + irel->r_addend) > shrinked_insn_address)
  992. {
  993. irel->r_addend -= count;
  994. if (debug_relax)
  995. printf ("Relocation's addend needed to be fixed \n");
  996. }
  997. }
  998. /* else...Reference symbol is absolute.
  999. No adjustment needed. */
  1000. }
  1001. /* else...Reference symbol is extern. No need for adjusting
  1002. the addend. */
  1003. }
  1004. }
  1005. }
  1006. /* Adjust the local symbols defined in this section. */
  1007. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1008. /* Fix PR 9841, there may be no local symbols. */
  1009. if (isym != NULL)
  1010. {
  1011. Elf_Internal_Sym *isymend;
  1012. isymend = isym + symtab_hdr->sh_info;
  1013. for (; isym < isymend; isym++)
  1014. {
  1015. if (isym->st_shndx == sec_shndx)
  1016. {
  1017. if (isym->st_value > addr
  1018. && isym->st_value <= toaddr)
  1019. isym->st_value -= count;
  1020. if (isym->st_value <= addr
  1021. && isym->st_value + isym->st_size > addr)
  1022. {
  1023. /* If this assert fires then we have a symbol that ends
  1024. part way through an instruction. Does that make
  1025. sense? */
  1026. BFD_ASSERT (isym->st_value + isym->st_size >= addr + count);
  1027. isym->st_size -= count;
  1028. }
  1029. }
  1030. }
  1031. }
  1032. /* Now adjust the global symbols defined in this section. */
  1033. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1034. - symtab_hdr->sh_info);
  1035. sym_hashes = elf_sym_hashes (abfd);
  1036. end_hashes = sym_hashes + symcount;
  1037. for (; sym_hashes < end_hashes; sym_hashes++)
  1038. {
  1039. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1040. if ((sym_hash->root.type == bfd_link_hash_defined
  1041. || sym_hash->root.type == bfd_link_hash_defweak)
  1042. && sym_hash->root.u.def.section == sec)
  1043. {
  1044. if (sym_hash->root.u.def.value > addr
  1045. && sym_hash->root.u.def.value <= toaddr)
  1046. sym_hash->root.u.def.value -= count;
  1047. if (sym_hash->root.u.def.value <= addr
  1048. && (sym_hash->root.u.def.value + sym_hash->size > addr))
  1049. {
  1050. /* If this assert fires then we have a symbol that ends
  1051. part way through an instruction. Does that make
  1052. sense? */
  1053. BFD_ASSERT (sym_hash->root.u.def.value + sym_hash->size
  1054. >= addr + count);
  1055. sym_hash->size -= count;
  1056. }
  1057. }
  1058. }
  1059. return TRUE;
  1060. }
  1061. static bfd_boolean
  1062. pru_elf32_relax_section (bfd * abfd, asection * sec,
  1063. struct bfd_link_info * link_info,
  1064. bfd_boolean * again)
  1065. {
  1066. Elf_Internal_Shdr * symtab_hdr;
  1067. Elf_Internal_Rela * internal_relocs;
  1068. Elf_Internal_Rela * irel;
  1069. Elf_Internal_Rela * irelend;
  1070. bfd_byte * contents = NULL;
  1071. Elf_Internal_Sym * isymbuf = NULL;
  1072. /* Assume nothing changes. */
  1073. *again = FALSE;
  1074. /* We don't have to do anything for a relocatable link, if
  1075. this section does not have relocs, or if this is not a
  1076. code section. */
  1077. if (bfd_link_relocatable (link_info)
  1078. || (sec->flags & SEC_RELOC) == 0
  1079. || sec->reloc_count == 0 || (sec->flags & SEC_CODE) == 0)
  1080. return TRUE;
  1081. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1082. /* Get a copy of the native relocations. */
  1083. internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
  1084. link_info->keep_memory);
  1085. if (internal_relocs == NULL)
  1086. goto error_return;
  1087. /* Walk through them looking for relaxing opportunities. */
  1088. irelend = internal_relocs + sec->reloc_count;
  1089. for (irel = internal_relocs; irel < irelend; irel++)
  1090. {
  1091. bfd_vma symval;
  1092. /* Get the section contents if we haven't done so already. */
  1093. if (contents == NULL)
  1094. {
  1095. /* Get cached copy if it exists. */
  1096. if (elf_section_data (sec)->this_hdr.contents != NULL)
  1097. contents = elf_section_data (sec)->this_hdr.contents;
  1098. else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
  1099. goto error_return;
  1100. }
  1101. /* Read this BFD's local symbols if we haven't done so already. */
  1102. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  1103. {
  1104. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1105. if (isymbuf == NULL)
  1106. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  1107. symtab_hdr->sh_info, 0,
  1108. NULL, NULL, NULL);
  1109. if (isymbuf == NULL)
  1110. goto error_return;
  1111. }
  1112. /* Get the value of the symbol referred to by the reloc. */
  1113. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  1114. {
  1115. /* A local symbol. */
  1116. Elf_Internal_Sym *isym;
  1117. asection *sym_sec;
  1118. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  1119. if (isym->st_shndx == SHN_UNDEF)
  1120. sym_sec = bfd_und_section_ptr;
  1121. else if (isym->st_shndx == SHN_ABS)
  1122. sym_sec = bfd_abs_section_ptr;
  1123. else if (isym->st_shndx == SHN_COMMON)
  1124. sym_sec = bfd_com_section_ptr;
  1125. else
  1126. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1127. symval = (isym->st_value
  1128. + sym_sec->output_section->vma + sym_sec->output_offset);
  1129. }
  1130. else
  1131. {
  1132. unsigned long indx;
  1133. struct elf_link_hash_entry *h;
  1134. /* An external symbol. */
  1135. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  1136. h = elf_sym_hashes (abfd)[indx];
  1137. BFD_ASSERT (h != NULL);
  1138. if (h->root.type != bfd_link_hash_defined
  1139. && h->root.type != bfd_link_hash_defweak)
  1140. /* This appears to be a reference to an undefined
  1141. symbol. Just ignore it--it will be caught by the
  1142. regular reloc processing. */
  1143. continue;
  1144. symval = (h->root.u.def.value
  1145. + h->root.u.def.section->output_section->vma
  1146. + h->root.u.def.section->output_offset);
  1147. }
  1148. /* For simplicity of coding, we are going to modify the section
  1149. contents, the section relocs, and the BFD symbol table. We
  1150. must tell the rest of the code not to free up this
  1151. information. It would be possible to instead create a table
  1152. of changes which have to be made, as is done in coff-mips.c;
  1153. that would be more work, but would require less memory when
  1154. the linker is run. */
  1155. /* Check if we can remove an LDI instruction from the LDI32
  1156. pseudo instruction if the upper 16 operand bits are zero. */
  1157. if (ELF32_R_TYPE (irel->r_info) == (int) R_PRU_LDI32)
  1158. {
  1159. bfd_vma value = symval + irel->r_addend;
  1160. if (debug_relax)
  1161. printf ("R_PRU_LDI32 with value=0x%lx\n", (long) value);
  1162. if ((long) value >> 16 == 0)
  1163. {
  1164. /* Note that we've changed the relocs, section contents. */
  1165. elf_section_data (sec)->relocs = internal_relocs;
  1166. elf_section_data (sec)->this_hdr.contents = contents;
  1167. symtab_hdr->contents = (unsigned char *) isymbuf;
  1168. /* Delete bytes. */
  1169. if (!pru_elf_relax_delete_bytes (abfd, sec, irel->r_offset + 4, 4))
  1170. goto error_return;
  1171. /* We're done with deletion of the second instruction.
  1172. Set a regular LDI relocation for the first instruction
  1173. we left to load the 16-bit value into the 32-bit
  1174. register. */
  1175. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1176. R_PRU_U16);
  1177. /* That will change things, so, we should relax again.
  1178. Note that this is not required, and it may be slow. */
  1179. *again = TRUE;
  1180. }
  1181. }
  1182. }
  1183. if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
  1184. {
  1185. if (!link_info->keep_memory)
  1186. free (isymbuf);
  1187. else
  1188. {
  1189. /* Cache the symbols for elf_link_input_bfd. */
  1190. symtab_hdr->contents = (unsigned char *) isymbuf;
  1191. }
  1192. }
  1193. if (contents != NULL
  1194. && elf_section_data (sec)->this_hdr.contents != contents)
  1195. {
  1196. if (!link_info->keep_memory)
  1197. free (contents);
  1198. else
  1199. {
  1200. /* Cache the section contents for elf_link_input_bfd. */
  1201. elf_section_data (sec)->this_hdr.contents = contents;
  1202. }
  1203. }
  1204. if (internal_relocs != NULL
  1205. && elf_section_data (sec)->relocs != internal_relocs)
  1206. free (internal_relocs);
  1207. return TRUE;
  1208. error_return:
  1209. if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
  1210. free (isymbuf);
  1211. if (contents != NULL
  1212. && elf_section_data (sec)->this_hdr.contents != contents)
  1213. free (contents);
  1214. if (internal_relocs != NULL
  1215. && elf_section_data (sec)->relocs != internal_relocs)
  1216. free (internal_relocs);
  1217. return FALSE;
  1218. }
  1219. /* Free the derived linker hash table. */
  1220. static void
  1221. pru_elf32_link_hash_table_free (bfd *obfd)
  1222. {
  1223. _bfd_elf_link_hash_table_free (obfd);
  1224. }
  1225. /* Implement bfd_elf32_bfd_link_hash_table_create. */
  1226. static struct bfd_link_hash_table *
  1227. pru_elf32_link_hash_table_create (bfd *abfd)
  1228. {
  1229. struct elf_link_hash_table *ret;
  1230. bfd_size_type amt = sizeof (struct elf_link_hash_table);
  1231. ret = bfd_zmalloc (amt);
  1232. if (ret == NULL)
  1233. return NULL;
  1234. if (!_bfd_elf_link_hash_table_init (ret, abfd,
  1235. link_hash_newfunc,
  1236. sizeof (struct
  1237. elf_link_hash_entry),
  1238. PRU_ELF_DATA))
  1239. {
  1240. free (ret);
  1241. return NULL;
  1242. }
  1243. ret->root.hash_table_free = pru_elf32_link_hash_table_free;
  1244. return &ret->root;
  1245. }
  1246. #define ELF_ARCH bfd_arch_pru
  1247. #define ELF_TARGET_ID PRU_ELF_DATA
  1248. #define ELF_MACHINE_CODE EM_TI_PRU
  1249. #define ELF_MAXPAGESIZE 1
  1250. #define bfd_elf32_bfd_link_hash_table_create \
  1251. pru_elf32_link_hash_table_create
  1252. /* Relocation table lookup macros. */
  1253. #define bfd_elf32_bfd_reloc_type_lookup pru_elf32_bfd_reloc_type_lookup
  1254. #define bfd_elf32_bfd_reloc_name_lookup pru_elf32_bfd_reloc_name_lookup
  1255. /* elf_info_to_howto (using RELA relocations). */
  1256. #define elf_info_to_howto pru_elf32_info_to_howto
  1257. /* elf backend functions. */
  1258. #define elf_backend_rela_normal 1
  1259. #define elf_backend_relocate_section pru_elf32_relocate_section
  1260. #define bfd_elf32_bfd_relax_section pru_elf32_relax_section
  1261. #define TARGET_LITTLE_SYM pru_elf32_vec
  1262. #define TARGET_LITTLE_NAME "elf32-pru"
  1263. #include "elf32-target.h"