elf32-pru.c 45 KB

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