elf32-m32c.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. /* M16C/M32C specific support for 32-bit ELF.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "elf-bfd.h"
  20. #include "elf/m32c.h"
  21. #include "libiberty.h"
  22. /* Forward declarations. */
  23. static reloc_howto_type * m32c_reloc_type_lookup
  24. (bfd *, bfd_reloc_code_real_type);
  25. static bool m32c_info_to_howto_rela
  26. (bfd *, arelent *, Elf_Internal_Rela *);
  27. static int m32c_elf_relocate_section
  28. (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
  29. static bool m32c_elf_check_relocs
  30. (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
  31. static bool m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
  32. #ifdef DEBUG
  33. char * m32c_get_reloc (long reloc);
  34. void dump_symtab (bfd *, void *, void *);
  35. #endif
  36. static bool m32c_elf_relax_section
  37. (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bool *again);
  38. static bfd_reloc_status_type m32c_apply_reloc_24
  39. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  40. static reloc_howto_type m32c_elf_howto_table [] =
  41. {
  42. /* This reloc does nothing. */
  43. HOWTO (R_M32C_NONE, /* type */
  44. 0, /* rightshift */
  45. 3, /* size (0 = byte, 1 = short, 2 = long) */
  46. 0, /* bitsize */
  47. false, /* pc_relative */
  48. 0, /* bitpos */
  49. complain_overflow_dont, /* complain_on_overflow */
  50. bfd_elf_generic_reloc, /* special_function */
  51. "R_M32C_NONE", /* name */
  52. false, /* partial_inplace */
  53. 0, /* src_mask */
  54. 0, /* dst_mask */
  55. false), /* pcrel_offset */
  56. /* GCC intentionally overflows these next two in order to work
  57. around limitations in the addressing modes, so don't complain
  58. about overflow. */
  59. HOWTO (R_M32C_16, /* type */
  60. 0, /* rightshift */
  61. 1, /* size (0 = byte, 1 = short, 2 = long) */
  62. 16, /* bitsize */
  63. false, /* pc_relative */
  64. 0, /* bitpos */
  65. complain_overflow_dont, /* complain_on_overflow */
  66. bfd_elf_generic_reloc, /* special_function */
  67. "R_M32C_16", /* name */
  68. false, /* partial_inplace */
  69. 0, /* src_mask */
  70. 0xffff, /* dst_mask */
  71. false), /* pcrel_offset */
  72. HOWTO (R_M32C_24, /* type */
  73. 0, /* rightshift */
  74. 2, /* size (0 = byte, 1 = short, 2 = long) */
  75. 24, /* bitsize */
  76. false, /* pc_relative */
  77. 0, /* bitpos */
  78. complain_overflow_dont, /* complain_on_overflow */
  79. m32c_apply_reloc_24, /* special_function */
  80. "R_M32C_24", /* name */
  81. false, /* partial_inplace */
  82. 0, /* src_mask */
  83. 0xffffff, /* dst_mask */
  84. false), /* pcrel_offset */
  85. HOWTO (R_M32C_32, /* type */
  86. 0, /* rightshift */
  87. 2, /* size (0 = byte, 1 = short, 2 = long) */
  88. 32, /* bitsize */
  89. false, /* pc_relative */
  90. 0, /* bitpos */
  91. complain_overflow_bitfield, /* complain_on_overflow */
  92. bfd_elf_generic_reloc, /* special_function */
  93. "R_M32C_32", /* name */
  94. false, /* partial_inplace */
  95. 0, /* src_mask */
  96. 0xffffffff, /* dst_mask */
  97. false), /* pcrel_offset */
  98. HOWTO (R_M32C_8_PCREL, /* type */
  99. 0, /* rightshift */
  100. 0, /* size (0 = byte, 1 = short, 2 = long) */
  101. 8, /* bitsize */
  102. true, /* pc_relative */
  103. 0, /* bitpos */
  104. complain_overflow_signed, /* complain_on_overflow */
  105. bfd_elf_generic_reloc, /* special_function */
  106. "R_M32C_8_PCREL", /* name */
  107. false, /* partial_inplace */
  108. 0, /* src_mask */
  109. 0xff, /* dst_mask */
  110. true), /* pcrel_offset */
  111. HOWTO (R_M32C_16_PCREL, /* type */
  112. 0, /* rightshift */
  113. 1, /* size (0 = byte, 1 = short, 2 = long) */
  114. 16, /* bitsize */
  115. true, /* pc_relative */
  116. 0, /* bitpos */
  117. complain_overflow_signed, /* complain_on_overflow */
  118. bfd_elf_generic_reloc, /* special_function */
  119. "R_M32C_16_PCREL", /* name */
  120. false, /* partial_inplace */
  121. 0, /* src_mask */
  122. 0xffff, /* dst_mask */
  123. true), /* pcrel_offset */
  124. HOWTO (R_M32C_8, /* type */
  125. 0, /* rightshift */
  126. 0, /* size (0 = byte, 1 = short, 2 = long) */
  127. 8, /* bitsize */
  128. false, /* pc_relative */
  129. 0, /* bitpos */
  130. complain_overflow_unsigned, /* complain_on_overflow */
  131. bfd_elf_generic_reloc, /* special_function */
  132. "R_M32C_8", /* name */
  133. false, /* partial_inplace */
  134. 0, /* src_mask */
  135. 0xff, /* dst_mask */
  136. false), /* pcrel_offset */
  137. HOWTO (R_M32C_LO16, /* type */
  138. 0, /* rightshift */
  139. 1, /* size (0 = byte, 1 = short, 2 = long) */
  140. 16, /* bitsize */
  141. false, /* pc_relative */
  142. 0, /* bitpos */
  143. complain_overflow_dont, /* complain_on_overflow */
  144. bfd_elf_generic_reloc, /* special_function */
  145. "R_M32C_LO16", /* name */
  146. false, /* partial_inplace */
  147. 0, /* src_mask */
  148. 0xffff, /* dst_mask */
  149. false), /* pcrel_offset */
  150. HOWTO (R_M32C_HI8, /* type */
  151. 0, /* rightshift */
  152. 0, /* size (0 = byte, 1 = short, 2 = long) */
  153. 8, /* bitsize */
  154. false, /* pc_relative */
  155. 0, /* bitpos */
  156. complain_overflow_dont, /* complain_on_overflow */
  157. bfd_elf_generic_reloc, /* special_function */
  158. "R_M32C_HI8", /* name */
  159. false, /* partial_inplace */
  160. 0, /* src_mask */
  161. 0xff, /* dst_mask */
  162. false), /* pcrel_offset */
  163. HOWTO (R_M32C_HI16, /* type */
  164. 0, /* rightshift */
  165. 1, /* size (0 = byte, 1 = short, 2 = long) */
  166. 16, /* bitsize */
  167. false, /* pc_relative */
  168. 0, /* bitpos */
  169. complain_overflow_dont, /* complain_on_overflow */
  170. bfd_elf_generic_reloc, /* special_function */
  171. "R_M32C_HI16", /* name */
  172. false, /* partial_inplace */
  173. 0, /* src_mask */
  174. 0xffff, /* dst_mask */
  175. false), /* pcrel_offset */
  176. HOWTO (R_M32C_RL_JUMP, /* type */
  177. 0, /* rightshift */
  178. 0, /* size (0 = byte, 1 = short, 2 = long) */
  179. 0, /* bitsize */
  180. false, /* pc_relative */
  181. 0, /* bitpos */
  182. complain_overflow_signed, /* complain_on_overflow */
  183. bfd_elf_generic_reloc, /* special_function */
  184. "R_M32C_RL_JUMP", /* name */
  185. false, /* partial_inplace */
  186. 0, /* src_mask */
  187. 0, /* dst_mask */
  188. false), /* pcrel_offset */
  189. HOWTO (R_M32C_RL_1ADDR, /* type */
  190. 0, /* rightshift */
  191. 0, /* size (0 = byte, 1 = short, 2 = long) */
  192. 0, /* bitsize */
  193. false, /* pc_relative */
  194. 0, /* bitpos */
  195. complain_overflow_signed, /* complain_on_overflow */
  196. bfd_elf_generic_reloc, /* special_function */
  197. "R_M32C_RL_1ADDR", /* name */
  198. false, /* partial_inplace */
  199. 0, /* src_mask */
  200. 0, /* dst_mask */
  201. false), /* pcrel_offset */
  202. HOWTO (R_M32C_RL_2ADDR, /* type */
  203. 0, /* rightshift */
  204. 0, /* size (0 = byte, 1 = short, 2 = long) */
  205. 0, /* bitsize */
  206. false, /* pc_relative */
  207. 0, /* bitpos */
  208. complain_overflow_signed, /* complain_on_overflow */
  209. bfd_elf_generic_reloc, /* special_function */
  210. "R_M32C_RL_2ADDR", /* name */
  211. false, /* partial_inplace */
  212. 0, /* src_mask */
  213. 0, /* dst_mask */
  214. false), /* pcrel_offset */
  215. };
  216. /* Map BFD reloc types to M32C ELF reloc types. */
  217. struct m32c_reloc_map
  218. {
  219. bfd_reloc_code_real_type bfd_reloc_val;
  220. unsigned int m32c_reloc_val;
  221. };
  222. static const struct m32c_reloc_map m32c_reloc_map [] =
  223. {
  224. { BFD_RELOC_NONE, R_M32C_NONE },
  225. { BFD_RELOC_16, R_M32C_16 },
  226. { BFD_RELOC_24, R_M32C_24 },
  227. { BFD_RELOC_32, R_M32C_32 },
  228. { BFD_RELOC_8_PCREL, R_M32C_8_PCREL },
  229. { BFD_RELOC_16_PCREL, R_M32C_16_PCREL },
  230. { BFD_RELOC_8, R_M32C_8 },
  231. { BFD_RELOC_LO16, R_M32C_LO16 },
  232. { BFD_RELOC_HI16, R_M32C_HI16 },
  233. { BFD_RELOC_M32C_HI8, R_M32C_HI8 },
  234. { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP },
  235. { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR },
  236. { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR }
  237. };
  238. static reloc_howto_type *
  239. m32c_reloc_type_lookup
  240. (bfd * abfd ATTRIBUTE_UNUSED,
  241. bfd_reloc_code_real_type code)
  242. {
  243. unsigned int i;
  244. for (i = ARRAY_SIZE (m32c_reloc_map); i--;)
  245. if (m32c_reloc_map [i].bfd_reloc_val == code)
  246. return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val];
  247. return NULL;
  248. }
  249. static reloc_howto_type *
  250. m32c_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
  251. {
  252. unsigned int i;
  253. for (i = 0;
  254. i < sizeof (m32c_elf_howto_table) / sizeof (m32c_elf_howto_table[0]);
  255. i++)
  256. if (m32c_elf_howto_table[i].name != NULL
  257. && strcasecmp (m32c_elf_howto_table[i].name, r_name) == 0)
  258. return &m32c_elf_howto_table[i];
  259. return NULL;
  260. }
  261. /* Set the howto pointer for an M32C ELF reloc. */
  262. static bool
  263. m32c_info_to_howto_rela (bfd * abfd,
  264. arelent * cache_ptr,
  265. Elf_Internal_Rela * dst)
  266. {
  267. unsigned int r_type;
  268. r_type = ELF32_R_TYPE (dst->r_info);
  269. if (r_type >= (unsigned int) R_M32C_max)
  270. {
  271. /* xgettext:c-format */
  272. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  273. abfd, r_type);
  274. bfd_set_error (bfd_error_bad_value);
  275. return false;
  276. }
  277. cache_ptr->howto = & m32c_elf_howto_table [r_type];
  278. return true;
  279. }
  280. /* Apply R_M32C_24 relocations. We have to do this because it's not a
  281. power-of-two size, and the generic code may think it overruns the
  282. section if it's right at the end.
  283. Must return something other than bfd_reloc_continue to avoid the
  284. above problem. Typical return values include bfd_reloc_ok or
  285. bfd_reloc_overflow.
  286. */
  287. static bfd_reloc_status_type m32c_apply_reloc_24 (bfd *abfd ATTRIBUTE_UNUSED,
  288. arelent *reloc_entry,
  289. asymbol *symbol,
  290. void *vdata_start ATTRIBUTE_UNUSED,
  291. asection *input_section,
  292. bfd *ibfd ATTRIBUTE_UNUSED,
  293. char **error_msg ATTRIBUTE_UNUSED)
  294. {
  295. bfd_vma relocation;
  296. bfd_reloc_status_type s;
  297. s = bfd_elf_generic_reloc (abfd, reloc_entry, symbol,
  298. vdata_start,
  299. input_section, ibfd, error_msg);
  300. if (s != bfd_reloc_continue)
  301. return s;
  302. /* Get symbol value. (Common symbols are special.) */
  303. if (bfd_is_com_section (symbol->section))
  304. relocation = 0;
  305. else
  306. relocation = symbol->value;
  307. relocation += symbol->section->output_offset;
  308. /* Add in supplied addend. */
  309. relocation += reloc_entry->addend;
  310. reloc_entry->addend = relocation;
  311. reloc_entry->address += input_section->output_offset;
  312. return bfd_reloc_ok;
  313. }
  314. /* Relocate an M32C ELF section.
  315. There is some attempt to make this function usable for many architectures,
  316. both USE_REL and USE_RELA ['twould be nice if such a critter existed],
  317. if only to serve as a learning tool.
  318. The RELOCATE_SECTION function is called by the new ELF backend linker
  319. to handle the relocations for a section.
  320. The relocs are always passed as Rela structures; if the section
  321. actually uses Rel structures, the r_addend field will always be
  322. zero.
  323. This function is responsible for adjusting the section contents as
  324. necessary, and (if using Rela relocs and generating a relocatable
  325. output file) adjusting the reloc addend as necessary.
  326. This function does not have to worry about setting the reloc
  327. address or the reloc symbol index.
  328. LOCAL_SYMS is a pointer to the swapped in local symbols.
  329. LOCAL_SECTIONS is an array giving the section in the input file
  330. corresponding to the st_shndx field of each local symbol.
  331. The global hash table entry for the global symbols can be found
  332. via elf_sym_hashes (input_bfd).
  333. When generating relocatable output, this function must handle
  334. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  335. going to be the section symbol corresponding to the output
  336. section, which means that the addend must be adjusted
  337. accordingly. */
  338. static int
  339. m32c_elf_relocate_section
  340. (bfd * output_bfd ATTRIBUTE_UNUSED,
  341. struct bfd_link_info * info,
  342. bfd * input_bfd,
  343. asection * input_section,
  344. bfd_byte * contents,
  345. Elf_Internal_Rela * relocs,
  346. Elf_Internal_Sym * local_syms,
  347. asection ** local_sections)
  348. {
  349. Elf_Internal_Shdr * symtab_hdr;
  350. struct elf_link_hash_entry ** sym_hashes;
  351. Elf_Internal_Rela * rel;
  352. Elf_Internal_Rela * relend;
  353. asection *splt;
  354. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  355. sym_hashes = elf_sym_hashes (input_bfd);
  356. relend = relocs + input_section->reloc_count;
  357. splt = elf_hash_table (info)->splt;
  358. for (rel = relocs; rel < relend; rel ++)
  359. {
  360. reloc_howto_type * howto;
  361. unsigned long r_symndx;
  362. Elf_Internal_Sym * sym;
  363. asection * sec;
  364. struct elf_link_hash_entry * h;
  365. bfd_vma relocation;
  366. bfd_reloc_status_type r;
  367. const char * name = NULL;
  368. int r_type;
  369. r_type = ELF32_R_TYPE (rel->r_info);
  370. /* These are only used for relaxing; we don't actually relocate
  371. anything with them, so skip them. */
  372. if (r_type == R_M32C_RL_JUMP
  373. || r_type == R_M32C_RL_1ADDR
  374. || r_type == R_M32C_RL_2ADDR)
  375. continue;
  376. r_symndx = ELF32_R_SYM (rel->r_info);
  377. howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
  378. h = NULL;
  379. sym = NULL;
  380. sec = NULL;
  381. relocation = 0;
  382. if (r_symndx < symtab_hdr->sh_info)
  383. {
  384. sym = local_syms + r_symndx;
  385. sec = local_sections [r_symndx];
  386. relocation = (sec->output_section->vma
  387. + sec->output_offset
  388. + sym->st_value);
  389. name = bfd_elf_string_from_elf_section
  390. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  391. name = sym->st_name == 0 ? bfd_section_name (sec) : name;
  392. }
  393. else
  394. {
  395. h = sym_hashes [r_symndx - symtab_hdr->sh_info];
  396. if (info->wrap_hash != NULL
  397. && (input_section->flags & SEC_DEBUGGING) != 0)
  398. h = ((struct elf_link_hash_entry *)
  399. unwrap_hash_lookup (info, input_bfd, &h->root));
  400. while (h->root.type == bfd_link_hash_indirect
  401. || h->root.type == bfd_link_hash_warning)
  402. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  403. name = h->root.root.string;
  404. if (h->root.type == bfd_link_hash_defined
  405. || h->root.type == bfd_link_hash_defweak)
  406. {
  407. sec = h->root.u.def.section;
  408. relocation = (h->root.u.def.value
  409. + sec->output_section->vma
  410. + sec->output_offset);
  411. }
  412. else if (h->root.type == bfd_link_hash_undefweak)
  413. ;
  414. else if (!bfd_link_relocatable (info))
  415. (*info->callbacks->undefined_symbol) (info, h->root.root.string,
  416. input_bfd, input_section,
  417. rel->r_offset, true);
  418. }
  419. if (sec != NULL && discarded_section (sec))
  420. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  421. rel, 1, relend, howto, 0, contents);
  422. if (bfd_link_relocatable (info))
  423. {
  424. /* This is a relocatable link. We don't have to change
  425. anything, unless the reloc is against a section symbol,
  426. in which case we have to adjust according to where the
  427. section symbol winds up in the output section. */
  428. if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  429. rel->r_addend += sec->output_offset;
  430. continue;
  431. }
  432. switch (ELF32_R_TYPE (rel->r_info))
  433. {
  434. case R_M32C_16:
  435. {
  436. bfd_vma *plt_offset;
  437. if (h != NULL)
  438. plt_offset = &h->plt.offset;
  439. else
  440. plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
  441. /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
  442. relocation, *plt_offset);*/
  443. if (relocation <= 0xffff)
  444. {
  445. /* If the symbol is in range for a 16-bit address, we should
  446. have deallocated the plt entry in relax_section. */
  447. BFD_ASSERT (*plt_offset == (bfd_vma) -1);
  448. }
  449. else
  450. {
  451. /* If the symbol is out of range for a 16-bit address,
  452. we must have allocated a plt entry. */
  453. BFD_ASSERT (*plt_offset != (bfd_vma) -1);
  454. /* If this is the first time we've processed this symbol,
  455. fill in the plt entry with the correct symbol address. */
  456. if ((*plt_offset & 1) == 0)
  457. {
  458. unsigned int x;
  459. x = 0x000000fc; /* jmpf */
  460. x |= (relocation << 8) & 0xffffff00;
  461. bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
  462. *plt_offset |= 1;
  463. }
  464. relocation = (splt->output_section->vma
  465. + splt->output_offset
  466. + (*plt_offset & -2));
  467. if (name)
  468. {
  469. char *newname = bfd_malloc (strlen(name)+5);
  470. strcpy (newname, name);
  471. strcat(newname, ".plt");
  472. _bfd_generic_link_add_one_symbol (info,
  473. input_bfd,
  474. newname,
  475. BSF_FUNCTION | BSF_WEAK,
  476. splt,
  477. (*plt_offset & -2),
  478. 0,
  479. 1,
  480. 0,
  481. 0);
  482. }
  483. }
  484. }
  485. break;
  486. case R_M32C_HI8:
  487. case R_M32C_HI16:
  488. relocation >>= 16;
  489. break;
  490. }
  491. #if 0
  492. printf ("relocate %s at %06lx relocation %06lx addend %ld ",
  493. m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
  494. rel->r_offset + input_section->output_section->vma + input_section->output_offset,
  495. relocation, rel->r_addend);
  496. {
  497. int i;
  498. for (i=0; i<4; i++)
  499. printf (" %02x", contents[rel->r_offset+i]);
  500. printf ("\n");
  501. }
  502. #endif
  503. switch (ELF32_R_TYPE(rel->r_info))
  504. {
  505. case R_M32C_24:
  506. /* Like m32c_apply_reloc_24, we must handle this one separately. */
  507. relocation += rel->r_addend;
  508. /* Sanity check the address. */
  509. if (rel->r_offset + 3
  510. > bfd_get_section_limit_octets (input_bfd, input_section))
  511. r = bfd_reloc_outofrange;
  512. else
  513. {
  514. bfd_put_8 (input_bfd, relocation & 0xff, contents + rel->r_offset);
  515. bfd_put_8 (input_bfd, (relocation >> 8) & 0xff, contents + rel->r_offset + 1);
  516. bfd_put_8 (input_bfd, (relocation >> 16) & 0xff, contents + rel->r_offset + 2);
  517. r = bfd_reloc_ok;
  518. }
  519. break;
  520. default:
  521. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  522. contents, rel->r_offset, relocation,
  523. rel->r_addend);
  524. break;
  525. }
  526. if (r != bfd_reloc_ok)
  527. {
  528. const char * msg = (const char *) NULL;
  529. switch (r)
  530. {
  531. case bfd_reloc_overflow:
  532. (*info->callbacks->reloc_overflow)
  533. (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
  534. input_bfd, input_section, rel->r_offset);
  535. break;
  536. case bfd_reloc_undefined:
  537. (*info->callbacks->undefined_symbol)
  538. (info, name, input_bfd, input_section, rel->r_offset, true);
  539. break;
  540. case bfd_reloc_outofrange:
  541. msg = _("internal error: out of range error");
  542. break;
  543. case bfd_reloc_notsupported:
  544. msg = _("internal error: unsupported relocation error");
  545. break;
  546. case bfd_reloc_dangerous:
  547. msg = _("internal error: dangerous relocation");
  548. break;
  549. default:
  550. msg = _("internal error: unknown error");
  551. break;
  552. }
  553. if (msg)
  554. (*info->callbacks->warning) (info, msg, name, input_bfd,
  555. input_section, rel->r_offset);
  556. }
  557. }
  558. return true;
  559. }
  560. /* We support 16-bit pointers to code above 64k by generating a thunk
  561. below 64k containing a JMP instruction to the final address. */
  562. static bool
  563. m32c_elf_check_relocs
  564. (bfd * abfd,
  565. struct bfd_link_info * info,
  566. asection * sec,
  567. const Elf_Internal_Rela * relocs)
  568. {
  569. Elf_Internal_Shdr * symtab_hdr;
  570. struct elf_link_hash_entry ** sym_hashes;
  571. const Elf_Internal_Rela * rel;
  572. const Elf_Internal_Rela * rel_end;
  573. bfd_vma *local_plt_offsets;
  574. asection *splt;
  575. bfd *dynobj;
  576. if (bfd_link_relocatable (info))
  577. return true;
  578. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  579. sym_hashes = elf_sym_hashes (abfd);
  580. local_plt_offsets = elf_local_got_offsets (abfd);
  581. splt = NULL;
  582. dynobj = elf_hash_table(info)->dynobj;
  583. rel_end = relocs + sec->reloc_count;
  584. for (rel = relocs; rel < rel_end; rel++)
  585. {
  586. struct elf_link_hash_entry *h;
  587. unsigned long r_symndx;
  588. bfd_vma *offset;
  589. r_symndx = ELF32_R_SYM (rel->r_info);
  590. if (r_symndx < symtab_hdr->sh_info)
  591. h = NULL;
  592. else
  593. {
  594. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  595. while (h->root.type == bfd_link_hash_indirect
  596. || h->root.type == bfd_link_hash_warning)
  597. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  598. }
  599. switch (ELF32_R_TYPE (rel->r_info))
  600. {
  601. /* This relocation describes a 16-bit pointer to a function.
  602. We may need to allocate a thunk in low memory; reserve memory
  603. for it now. */
  604. case R_M32C_16:
  605. if (dynobj == NULL)
  606. elf_hash_table (info)->dynobj = dynobj = abfd;
  607. splt = elf_hash_table (info)->splt;
  608. if (splt == NULL)
  609. {
  610. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  611. | SEC_IN_MEMORY | SEC_LINKER_CREATED
  612. | SEC_READONLY | SEC_CODE);
  613. splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
  614. flags);
  615. elf_hash_table (info)->splt = splt;
  616. if (splt == NULL
  617. || !bfd_set_section_alignment (splt, 1))
  618. return false;
  619. }
  620. if (h != NULL)
  621. offset = &h->plt.offset;
  622. else
  623. {
  624. if (local_plt_offsets == NULL)
  625. {
  626. size_t size;
  627. unsigned int i;
  628. size = symtab_hdr->sh_info * sizeof (bfd_vma);
  629. local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
  630. if (local_plt_offsets == NULL)
  631. return false;
  632. elf_local_got_offsets (abfd) = local_plt_offsets;
  633. for (i = 0; i < symtab_hdr->sh_info; i++)
  634. local_plt_offsets[i] = (bfd_vma) -1;
  635. }
  636. offset = &local_plt_offsets[r_symndx];
  637. }
  638. if (*offset == (bfd_vma) -1)
  639. {
  640. *offset = splt->size;
  641. splt->size += 4;
  642. }
  643. break;
  644. }
  645. }
  646. return true;
  647. }
  648. /* This must exist if dynobj is ever set. */
  649. static bool
  650. m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
  651. struct bfd_link_info *info)
  652. {
  653. bfd *dynobj = elf_hash_table (info)->dynobj;
  654. asection *splt = elf_hash_table (info)->splt;
  655. /* As an extra sanity check, verify that all plt entries have
  656. been filled in. */
  657. if (dynobj != NULL && splt != NULL)
  658. {
  659. bfd_byte *contents = splt->contents;
  660. unsigned int i, size = splt->size;
  661. for (i = 0; i < size; i += 4)
  662. {
  663. unsigned int x = bfd_get_32 (dynobj, contents + i);
  664. BFD_ASSERT (x != 0);
  665. }
  666. }
  667. return true;
  668. }
  669. static bool
  670. m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  671. struct bfd_link_info *info)
  672. {
  673. bfd *dynobj;
  674. asection *splt;
  675. if (bfd_link_relocatable (info))
  676. return true;
  677. dynobj = elf_hash_table (info)->dynobj;
  678. if (dynobj == NULL)
  679. return true;
  680. splt = elf_hash_table (info)->splt;
  681. BFD_ASSERT (splt != NULL);
  682. splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
  683. if (splt->contents == NULL)
  684. return false;
  685. return true;
  686. }
  687. /* Function to set the ELF flag bits. */
  688. static bool
  689. m32c_elf_set_private_flags (bfd *abfd, flagword flags)
  690. {
  691. elf_elfheader (abfd)->e_flags = flags;
  692. elf_flags_init (abfd) = true;
  693. return true;
  694. }
  695. /* Merge backend specific data from an object file to the output
  696. object file when linking. */
  697. static bool
  698. m32c_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  699. {
  700. bfd *obfd = info->output_bfd;
  701. flagword old_flags, old_partial;
  702. flagword new_flags, new_partial;
  703. bool error = false;
  704. char new_opt[80];
  705. char old_opt[80];
  706. new_opt[0] = old_opt[0] = '\0';
  707. new_flags = elf_elfheader (ibfd)->e_flags;
  708. old_flags = elf_elfheader (obfd)->e_flags;
  709. #ifdef DEBUG
  710. _bfd_error_handler
  711. ("old_flags = 0x%.8x, new_flags = 0x%.8x, init = %s, filename = %s",
  712. old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
  713. bfd_get_filename (ibfd));
  714. #endif
  715. if (!elf_flags_init (obfd))
  716. {
  717. /* First call, no flags set. */
  718. elf_flags_init (obfd) = true;
  719. elf_elfheader (obfd)->e_flags = new_flags;
  720. }
  721. else if (new_flags == old_flags)
  722. /* Compatible flags are ok. */
  723. ;
  724. else /* Possibly incompatible flags. */
  725. {
  726. /* Warn if different cpu is used (allow a specific cpu to override
  727. the generic cpu). */
  728. new_partial = (new_flags & EF_M32C_CPU_MASK);
  729. old_partial = (old_flags & EF_M32C_CPU_MASK);
  730. if (new_partial == old_partial)
  731. ;
  732. else
  733. {
  734. switch (new_partial)
  735. {
  736. default: strcat (new_opt, " -m16c"); break;
  737. case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
  738. case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
  739. }
  740. switch (old_partial)
  741. {
  742. default: strcat (old_opt, " -m16c"); break;
  743. case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
  744. case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
  745. }
  746. }
  747. /* Print out any mismatches from above. */
  748. if (new_opt[0])
  749. {
  750. error = true;
  751. _bfd_error_handler
  752. /* xgettext:c-format */
  753. (_("%pB: compiled with %s and linked with modules compiled with %s"),
  754. ibfd, new_opt, old_opt);
  755. }
  756. new_flags &= ~ EF_M32C_ALL_FLAGS;
  757. old_flags &= ~ EF_M32C_ALL_FLAGS;
  758. /* Warn about any other mismatches. */
  759. if (new_flags != old_flags)
  760. {
  761. error = true;
  762. _bfd_error_handler
  763. /* xgettext:c-format */
  764. (_("%pB: uses different e_flags (%#x) fields"
  765. " than previous modules (%#x)"),
  766. ibfd, new_flags, old_flags);
  767. }
  768. }
  769. if (error)
  770. bfd_set_error (bfd_error_bad_value);
  771. return !error;
  772. }
  773. static bool
  774. m32c_elf_print_private_bfd_data (bfd *abfd, void *ptr)
  775. {
  776. FILE *file = (FILE *) ptr;
  777. flagword flags;
  778. BFD_ASSERT (abfd != NULL && ptr != NULL);
  779. /* Print normal ELF private data. */
  780. _bfd_elf_print_private_bfd_data (abfd, ptr);
  781. flags = elf_elfheader (abfd)->e_flags;
  782. fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
  783. switch (flags & EF_M32C_CPU_MASK)
  784. {
  785. default: break;
  786. case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
  787. case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
  788. }
  789. fputc ('\n', file);
  790. return true;
  791. }
  792. /* Return the MACH for an e_flags value. */
  793. static int
  794. elf32_m32c_machine (bfd *abfd)
  795. {
  796. switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
  797. {
  798. case EF_M32C_CPU_M16C: return bfd_mach_m16c;
  799. case EF_M32C_CPU_M32C: return bfd_mach_m32c;
  800. }
  801. return bfd_mach_m16c;
  802. }
  803. static bool
  804. m32c_elf_object_p (bfd *abfd)
  805. {
  806. bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
  807. elf32_m32c_machine (abfd));
  808. return true;
  809. }
  810. #ifdef DEBUG
  811. void
  812. dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
  813. {
  814. size_t locsymcount;
  815. Elf_Internal_Sym *isymbuf;
  816. Elf_Internal_Sym *isymend;
  817. Elf_Internal_Sym *isym;
  818. Elf_Internal_Shdr *symtab_hdr;
  819. bool free_internal = 0, free_external = 0;
  820. char * st_info_str;
  821. char * st_info_stb_str;
  822. char * st_other_str;
  823. char * st_shndx_str;
  824. if (! internal_syms)
  825. {
  826. internal_syms = bfd_malloc (1000);
  827. free_internal = 1;
  828. }
  829. if (! external_syms)
  830. {
  831. external_syms = bfd_malloc (1000);
  832. free_external = 1;
  833. }
  834. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  835. locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
  836. if (free_internal)
  837. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  838. symtab_hdr->sh_info, 0,
  839. internal_syms, external_syms, NULL);
  840. else
  841. isymbuf = internal_syms;
  842. isymend = isymbuf + locsymcount;
  843. for (isym = isymbuf ; isym < isymend ; isym++)
  844. {
  845. switch (ELF_ST_TYPE (isym->st_info))
  846. {
  847. case STT_FUNC:
  848. st_info_str = "STT_FUNC";
  849. break;
  850. case STT_SECTION:
  851. st_info_str = "STT_SECTION";
  852. break;
  853. case STT_FILE:
  854. st_info_str = "STT_FILE";
  855. break;
  856. case STT_OBJECT:
  857. st_info_str = "STT_OBJECT";
  858. break;
  859. case STT_TLS:
  860. st_info_str = "STT_TLS";
  861. break;
  862. default:
  863. st_info_str = "";
  864. }
  865. switch (ELF_ST_BIND (isym->st_info))
  866. {
  867. case STB_LOCAL:
  868. st_info_stb_str = "STB_LOCAL";
  869. break;
  870. case STB_GLOBAL:
  871. st_info_stb_str = "STB_GLOBAL";
  872. break;
  873. default:
  874. st_info_stb_str = "";
  875. }
  876. switch (ELF_ST_VISIBILITY (isym->st_other))
  877. {
  878. case STV_DEFAULT:
  879. st_other_str = "STV_DEFAULT";
  880. break;
  881. case STV_INTERNAL:
  882. st_other_str = "STV_INTERNAL";
  883. break;
  884. case STV_PROTECTED:
  885. st_other_str = "STV_PROTECTED";
  886. break;
  887. default:
  888. st_other_str = "";
  889. }
  890. switch (isym->st_shndx)
  891. {
  892. case SHN_ABS:
  893. st_shndx_str = "SHN_ABS";
  894. break;
  895. case SHN_COMMON:
  896. st_shndx_str = "SHN_COMMON";
  897. break;
  898. case SHN_UNDEF:
  899. st_shndx_str = "SHN_UNDEF";
  900. break;
  901. default:
  902. st_shndx_str = "";
  903. }
  904. printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
  905. "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
  906. isym,
  907. (unsigned long) isym->st_value,
  908. (unsigned long) isym->st_size,
  909. isym->st_name,
  910. bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
  911. isym->st_name),
  912. isym->st_info, st_info_str, st_info_stb_str,
  913. isym->st_other, st_other_str,
  914. isym->st_shndx, st_shndx_str);
  915. }
  916. if (free_internal)
  917. free (internal_syms);
  918. if (free_external)
  919. free (external_syms);
  920. }
  921. char *
  922. m32c_get_reloc (long reloc)
  923. {
  924. if (0 <= reloc && reloc < R_M32C_max)
  925. return m32c_elf_howto_table[reloc].name;
  926. else
  927. return "";
  928. }
  929. #endif /* DEBUG */
  930. /* Handle relaxing. */
  931. /* A subroutine of m32c_elf_relax_section. If the global symbol H
  932. is within the low 64k, remove any entry for it in the plt. */
  933. struct relax_plt_data
  934. {
  935. asection *splt;
  936. bool *again;
  937. };
  938. static bool
  939. m32c_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
  940. {
  941. struct relax_plt_data *data = (struct relax_plt_data *) xdata;
  942. if (h->plt.offset != (bfd_vma) -1)
  943. {
  944. bfd_vma address;
  945. if (h->root.type == bfd_link_hash_undefined
  946. || h->root.type == bfd_link_hash_undefweak)
  947. address = 0;
  948. else
  949. address = (h->root.u.def.section->output_section->vma
  950. + h->root.u.def.section->output_offset
  951. + h->root.u.def.value);
  952. if (address <= 0xffff)
  953. {
  954. h->plt.offset = -1;
  955. data->splt->size -= 4;
  956. *data->again = true;
  957. }
  958. }
  959. return true;
  960. }
  961. /* A subroutine of m32c_elf_relax_section. If the global symbol H
  962. previously had a plt entry, give it a new entry offset. */
  963. static bool
  964. m32c_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
  965. {
  966. bfd_vma *entry = (bfd_vma *) xdata;
  967. if (h->plt.offset != (bfd_vma) -1)
  968. {
  969. h->plt.offset = *entry;
  970. *entry += 4;
  971. }
  972. return true;
  973. }
  974. static bool
  975. m32c_elf_relax_plt_section (asection *splt,
  976. struct bfd_link_info *info,
  977. bool *again)
  978. {
  979. struct relax_plt_data relax_plt_data;
  980. bfd *ibfd;
  981. /* Assume nothing changes. */
  982. *again = false;
  983. if (bfd_link_relocatable (info))
  984. return true;
  985. /* Quick check for an empty plt. */
  986. if (splt->size == 0)
  987. return true;
  988. /* Map across all global symbols; see which ones happen to
  989. fall in the low 64k. */
  990. relax_plt_data.splt = splt;
  991. relax_plt_data.again = again;
  992. elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
  993. &relax_plt_data);
  994. /* Likewise for local symbols, though that's somewhat less convenient
  995. as we have to walk the list of input bfds and swap in symbol data. */
  996. for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
  997. {
  998. bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
  999. Elf_Internal_Shdr *symtab_hdr;
  1000. Elf_Internal_Sym *isymbuf = NULL;
  1001. unsigned int idx;
  1002. if (! local_plt_offsets)
  1003. continue;
  1004. symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  1005. if (symtab_hdr->sh_info != 0)
  1006. {
  1007. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1008. if (isymbuf == NULL)
  1009. isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
  1010. symtab_hdr->sh_info, 0,
  1011. NULL, NULL, NULL);
  1012. if (isymbuf == NULL)
  1013. return false;
  1014. }
  1015. for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
  1016. {
  1017. Elf_Internal_Sym *isym;
  1018. asection *tsec;
  1019. bfd_vma address;
  1020. if (local_plt_offsets[idx] == (bfd_vma) -1)
  1021. continue;
  1022. isym = &isymbuf[idx];
  1023. if (isym->st_shndx == SHN_UNDEF)
  1024. continue;
  1025. else if (isym->st_shndx == SHN_ABS)
  1026. tsec = bfd_abs_section_ptr;
  1027. else if (isym->st_shndx == SHN_COMMON)
  1028. tsec = bfd_com_section_ptr;
  1029. else
  1030. tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
  1031. address = (tsec->output_section->vma
  1032. + tsec->output_offset
  1033. + isym->st_value);
  1034. if (address <= 0xffff)
  1035. {
  1036. local_plt_offsets[idx] = -1;
  1037. splt->size -= 4;
  1038. *again = true;
  1039. }
  1040. }
  1041. if (isymbuf != NULL
  1042. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1043. {
  1044. if (! info->keep_memory)
  1045. free (isymbuf);
  1046. else
  1047. {
  1048. /* Cache the symbols for elf_link_input_bfd. */
  1049. symtab_hdr->contents = (unsigned char *) isymbuf;
  1050. }
  1051. }
  1052. }
  1053. /* If we changed anything, walk the symbols again to reallocate
  1054. .plt entry addresses. */
  1055. if (*again && splt->size > 0)
  1056. {
  1057. bfd_vma entry = 0;
  1058. elf_link_hash_traverse (elf_hash_table (info),
  1059. m32c_relax_plt_realloc, &entry);
  1060. for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
  1061. {
  1062. bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
  1063. unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
  1064. unsigned int idx;
  1065. if (! local_plt_offsets)
  1066. continue;
  1067. for (idx = 0; idx < nlocals; ++idx)
  1068. if (local_plt_offsets[idx] != (bfd_vma) -1)
  1069. {
  1070. local_plt_offsets[idx] = entry;
  1071. entry += 4;
  1072. }
  1073. }
  1074. }
  1075. return true;
  1076. }
  1077. static int
  1078. compare_reloc (const void *e1, const void *e2)
  1079. {
  1080. const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
  1081. const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
  1082. if (i1->r_offset == i2->r_offset)
  1083. return 0;
  1084. else
  1085. return i1->r_offset < i2->r_offset ? -1 : 1;
  1086. }
  1087. #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
  1088. static bfd_vma
  1089. m32c_offset_for_reloc (bfd *abfd,
  1090. Elf_Internal_Rela *rel,
  1091. Elf_Internal_Shdr *symtab_hdr,
  1092. bfd_byte *shndx_buf ATTRIBUTE_UNUSED,
  1093. Elf_Internal_Sym *intsyms)
  1094. {
  1095. bfd_vma symval;
  1096. /* Get the value of the symbol referred to by the reloc. */
  1097. if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
  1098. {
  1099. /* A local symbol. */
  1100. Elf_Internal_Sym *isym;
  1101. asection *ssec;
  1102. isym = intsyms + ELF32_R_SYM (rel->r_info);
  1103. ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1104. symval = isym->st_value;
  1105. if (ssec)
  1106. symval += ssec->output_section->vma
  1107. + ssec->output_offset;
  1108. }
  1109. else
  1110. {
  1111. unsigned long indx;
  1112. struct elf_link_hash_entry *h;
  1113. /* An external symbol. */
  1114. indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
  1115. h = elf_sym_hashes (abfd)[indx];
  1116. BFD_ASSERT (h != NULL);
  1117. if (h->root.type != bfd_link_hash_defined
  1118. && h->root.type != bfd_link_hash_defweak)
  1119. /* This appears to be a reference to an undefined
  1120. symbol. Just ignore it--it will be caught by the
  1121. regular reloc processing. */
  1122. return 0;
  1123. symval = (h->root.u.def.value
  1124. + h->root.u.def.section->output_section->vma
  1125. + h->root.u.def.section->output_offset);
  1126. }
  1127. return symval;
  1128. }
  1129. static int bytes_saved = 0;
  1130. static int bytes_to_reloc[] = {
  1131. R_M32C_NONE,
  1132. R_M32C_8,
  1133. R_M32C_16,
  1134. R_M32C_24,
  1135. R_M32C_32
  1136. };
  1137. /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
  1138. /* Mask for the number of relocs associated with this insn. */
  1139. #define RLA_RELOCS 0x0000000f
  1140. /* Number of bytes gas emitted (before gas's relaxing) */
  1141. #define RLA_NBYTES 0x00000ff0
  1142. /* If the displacement is within the given range and the new encoding
  1143. differs from the old encoding (the index), then the insn can be
  1144. relaxed to the new encoding. */
  1145. typedef const struct {
  1146. int bytes;
  1147. unsigned int max_disp;
  1148. unsigned char new_encoding;
  1149. } EncodingTable;
  1150. static EncodingTable m16c_addr_encodings[] = {
  1151. { 0, 0, 0 }, /* R0 */
  1152. { 0, 0, 1 }, /* R1 */
  1153. { 0, 0, 2 }, /* R2 */
  1154. { 0, 0, 3 }, /* R3 */
  1155. { 0, 0, 4 }, /* A0 */
  1156. { 0, 0, 5 }, /* A1 */
  1157. { 0, 0, 6 }, /* [A0] */
  1158. { 0, 0, 7 }, /* [A1] */
  1159. { 1, 0, 6 }, /* udsp:8[A0] */
  1160. { 1, 0, 7 }, /* udsp:8[A1] */
  1161. { 1, 0, 10 }, /* udsp:8[SB] */
  1162. { 1, 0, 11 }, /* sdsp:8[FB] */
  1163. { 2, 255, 8 }, /* udsp:16[A0] */
  1164. { 2, 255, 9 }, /* udsp:16[A1] */
  1165. { 2, 255, 10 }, /* udsp:16[SB] */
  1166. { 2, 0, 15 }, /* abs:16 */
  1167. };
  1168. static EncodingTable m16c_jmpaddr_encodings[] = {
  1169. { 0, 0, 0 }, /* R0 */
  1170. { 0, 0, 1 }, /* R1 */
  1171. { 0, 0, 2 }, /* R2 */
  1172. { 0, 0, 3 }, /* R3 */
  1173. { 0, 0, 4 }, /* A0 */
  1174. { 0, 0, 5 }, /* A1 */
  1175. { 0, 0, 6 }, /* [A0] */
  1176. { 0, 0, 7 }, /* [A1] */
  1177. { 1, 0, 6 }, /* udsp:8[A0] */
  1178. { 1, 0, 7 }, /* udsp:8[A1] */
  1179. { 1, 0, 10 }, /* udsp:8[SB] */
  1180. { 1, 0, 11 }, /* sdsp:8[FB] */
  1181. { 3, 255, 8 }, /* udsp:20[A0] */
  1182. { 3, 255, 9 }, /* udsp:20[A1] */
  1183. { 2, 255, 10 }, /* udsp:16[SB] */
  1184. { 2, 0, 15 }, /* abs:16 */
  1185. };
  1186. static EncodingTable m32c_addr_encodings[] = {
  1187. { 0, 0, 0 }, /* [A0] */
  1188. { 0, 0, 1 }, /* [A1] */
  1189. { 0, 0, 2 }, /* A0 */
  1190. { 0, 0, 3 }, /* A1 */
  1191. { 1, 0, 0 }, /* udsp:8[A0] */
  1192. { 1, 0, 1 }, /* udsp:8[A1] */
  1193. { 1, 0, 6 }, /* udsp:8[SB] */
  1194. { 1, 0, 7 }, /* sdsp:8[FB] */
  1195. { 2, 255, 4 }, /* udsp:16[A0] */
  1196. { 2, 255, 5 }, /* udsp:16[A1] */
  1197. { 2, 255, 6 }, /* udsp:16[SB] */
  1198. { 2, 127, 7 }, /* sdsp:16[FB] */
  1199. { 3, 65535, 8 }, /* udsp:24[A0] */
  1200. { 3, 65535, 9 }, /* udsp:24[A1] */
  1201. { 3, 65535, 15 }, /* abs24 */
  1202. { 2, 0, 15 }, /* abs16 */
  1203. { 0, 0, 16 }, /* R2 */
  1204. { 0, 0, 17 }, /* R3 */
  1205. { 0, 0, 18 }, /* R0 */
  1206. { 0, 0, 19 }, /* R1 */
  1207. { 0, 0, 20 }, /* */
  1208. { 0, 0, 21 }, /* */
  1209. { 0, 0, 22 }, /* */
  1210. { 0, 0, 23 }, /* */
  1211. { 0, 0, 24 }, /* */
  1212. { 0, 0, 25 }, /* */
  1213. { 0, 0, 26 }, /* */
  1214. { 0, 0, 27 }, /* */
  1215. { 0, 0, 28 }, /* */
  1216. { 0, 0, 29 }, /* */
  1217. { 0, 0, 30 }, /* */
  1218. { 0, 0, 31 }, /* */
  1219. };
  1220. static bool
  1221. m32c_elf_relax_section (bfd *abfd,
  1222. asection *sec,
  1223. struct bfd_link_info *link_info,
  1224. bool *again)
  1225. {
  1226. Elf_Internal_Shdr *symtab_hdr;
  1227. Elf_Internal_Shdr *shndx_hdr;
  1228. Elf_Internal_Rela *internal_relocs;
  1229. Elf_Internal_Rela *free_relocs = NULL;
  1230. Elf_Internal_Rela *irel, *irelend, *srel;
  1231. bfd_byte * contents = NULL;
  1232. bfd_byte * free_contents = NULL;
  1233. Elf_Internal_Sym *intsyms = NULL;
  1234. Elf_Internal_Sym *free_intsyms = NULL;
  1235. bfd_byte *shndx_buf = NULL;
  1236. int machine;
  1237. if (is_elf_hash_table (link_info->hash)
  1238. && abfd == elf_hash_table (link_info)->dynobj
  1239. && (sec->flags & SEC_LINKER_CREATED) != 0
  1240. && strcmp (sec->name, ".plt") == 0)
  1241. return m32c_elf_relax_plt_section (sec, link_info, again);
  1242. /* Assume nothing changes. */
  1243. *again = false;
  1244. machine = elf32_m32c_machine (abfd);
  1245. /* We don't have to do anything for a relocatable link, if
  1246. this section does not have relocs, or if this is not a
  1247. code section. */
  1248. if (bfd_link_relocatable (link_info)
  1249. || (sec->flags & SEC_RELOC) == 0
  1250. || sec->reloc_count == 0
  1251. || (sec->flags & SEC_CODE) == 0)
  1252. return true;
  1253. symtab_hdr = & elf_symtab_hdr (abfd);
  1254. if (elf_symtab_shndx_list (abfd))
  1255. shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
  1256. else
  1257. shndx_hdr = NULL;
  1258. /* Get the section contents. */
  1259. if (elf_section_data (sec)->this_hdr.contents != NULL)
  1260. contents = elf_section_data (sec)->this_hdr.contents;
  1261. /* Go get them off disk. */
  1262. else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  1263. goto error_return;
  1264. /* Read this BFD's symbols. */
  1265. /* Get cached copy if it exists. */
  1266. if (symtab_hdr->contents != NULL)
  1267. {
  1268. intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
  1269. }
  1270. else
  1271. {
  1272. intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
  1273. symtab_hdr->contents = (bfd_byte *) intsyms;
  1274. }
  1275. if (shndx_hdr && shndx_hdr->sh_size != 0)
  1276. {
  1277. size_t amt;
  1278. if (_bfd_mul_overflow (symtab_hdr->sh_info,
  1279. sizeof (Elf_External_Sym_Shndx), &amt))
  1280. {
  1281. bfd_set_error (bfd_error_file_too_big);
  1282. goto error_return;
  1283. }
  1284. if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0)
  1285. goto error_return;
  1286. shndx_buf = _bfd_malloc_and_read (abfd, amt, amt);
  1287. if (shndx_buf == NULL)
  1288. goto error_return;
  1289. shndx_hdr->contents = shndx_buf;
  1290. }
  1291. /* Get a copy of the native relocations. */
  1292. internal_relocs = (_bfd_elf_link_read_relocs
  1293. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  1294. link_info->keep_memory));
  1295. if (internal_relocs == NULL)
  1296. goto error_return;
  1297. if (! link_info->keep_memory)
  1298. free_relocs = internal_relocs;
  1299. /* The RL_ relocs must be just before the operand relocs they go
  1300. with, so we must sort them to guarantee this. */
  1301. qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
  1302. compare_reloc);
  1303. /* Walk through them looking for relaxing opportunities. */
  1304. irelend = internal_relocs + sec->reloc_count;
  1305. for (irel = internal_relocs; irel < irelend; irel++)
  1306. {
  1307. bfd_vma symval;
  1308. unsigned char *insn, *gap, *einsn;
  1309. bfd_vma pc;
  1310. bfd_signed_vma pcrel;
  1311. int relax_relocs;
  1312. int gap_size;
  1313. int new_type;
  1314. int posn;
  1315. int enc;
  1316. EncodingTable *enctbl;
  1317. EncodingTable *e;
  1318. if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
  1319. && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
  1320. && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
  1321. continue;
  1322. srel = irel;
  1323. /* There will always be room for the relaxed insn, since it is smaller
  1324. than the one it would replace. */
  1325. BFD_ASSERT (irel->r_offset < sec->size);
  1326. insn = contents + irel->r_offset;
  1327. relax_relocs = irel->r_addend % 16;
  1328. /* Ok, we only have three relocs we care about, and they're all
  1329. fake. The lower four bits of the addend is always the number
  1330. of following relocs (hence the qsort above) that are assigned
  1331. to this opcode. The next 8 bits of the addend indicates the
  1332. number of bytes in the insn. We use the rest of them
  1333. ourselves as flags for the more expensive operations (defines
  1334. above). The three relocs are:
  1335. RL_JUMP: This marks all direct jump insns. We check the
  1336. displacement and replace them with shorter jumps if
  1337. they're in range. We also use this to find JMP.S
  1338. insns and manually shorten them when we delete bytes.
  1339. We have to decode these insns to figure out what to
  1340. do.
  1341. RL_1ADDR: This is a :G or :Q insn, which has a single
  1342. "standard" operand. We have to extract the type
  1343. field, see if it's a wide displacement, then figure
  1344. out if we can replace it with a narrow displacement.
  1345. We don't have to decode these insns.
  1346. RL_2ADDR: Similarly, but two "standard" operands. Note that
  1347. r_addend may still be 1, as standard operands don't
  1348. always have displacements. Gas shouldn't give us one
  1349. with zero operands, but since we don't know which one
  1350. has the displacement, we check them both anyway.
  1351. These all point to the beginning of the insn itself, not the
  1352. operands.
  1353. Note that we only relax one step at a time, relying on the
  1354. linker to call us repeatedly. Thus, there is no code for
  1355. JMP.A->JMP.B although that will happen in two steps.
  1356. Likewise, for 2ADDR relaxes, we do one operand per cycle.
  1357. */
  1358. /* Get the value of the symbol referred to by the reloc. Just
  1359. in case this is the last reloc in the list, use the RL's
  1360. addend to choose between this reloc (no addend) or the next
  1361. (yes addend, which means at least one following reloc). */
  1362. srel = irel + (relax_relocs ? 1 : 0);
  1363. symval = OFFSET_FOR_RELOC (srel);
  1364. /* Setting gap_size nonzero is the flag which means "something
  1365. shrunk". */
  1366. gap_size = 0;
  1367. gap = NULL;
  1368. new_type = ELF32_R_TYPE(srel->r_info);
  1369. pc = sec->output_section->vma + sec->output_offset
  1370. + srel->r_offset;
  1371. pcrel = symval - pc + srel->r_addend;
  1372. if (machine == bfd_mach_m16c)
  1373. {
  1374. /* R8C / M16C */
  1375. switch (ELF32_R_TYPE(irel->r_info))
  1376. {
  1377. case R_M32C_RL_JUMP:
  1378. switch (insn[0])
  1379. {
  1380. case 0xfe: /* jmp.b */
  1381. if (pcrel >= 2 && pcrel <= 9)
  1382. {
  1383. /* Relax JMP.B -> JMP.S. We need to get rid of
  1384. the following reloc though. */
  1385. insn[0] = 0x60 | (pcrel - 2);
  1386. new_type = R_M32C_NONE;
  1387. irel->r_addend = 0x10;
  1388. gap_size = 1;
  1389. gap = insn + 1;
  1390. }
  1391. break;
  1392. case 0xf4: /* jmp.w */
  1393. /* 128 is allowed because it will be one byte closer
  1394. after relaxing. Likewise for all other pc-rel
  1395. jumps. */
  1396. if (pcrel <= 128 && pcrel >= -128)
  1397. {
  1398. /* Relax JMP.W -> JMP.B */
  1399. insn[0] = 0xfe;
  1400. insn[1] = 0;
  1401. new_type = R_M32C_8_PCREL;
  1402. gap_size = 1;
  1403. gap = insn + 2;
  1404. }
  1405. break;
  1406. case 0xfc: /* jmp.a */
  1407. if (pcrel <= 32768 && pcrel >= -32768)
  1408. {
  1409. /* Relax JMP.A -> JMP.W */
  1410. insn[0] = 0xf4;
  1411. insn[1] = 0;
  1412. insn[2] = 0;
  1413. new_type = R_M32C_16_PCREL;
  1414. gap_size = 1;
  1415. gap = insn + 3;
  1416. }
  1417. break;
  1418. case 0xfd: /* jsr.a */
  1419. if (pcrel <= 32768 && pcrel >= -32768)
  1420. {
  1421. /* Relax JSR.A -> JSR.W */
  1422. insn[0] = 0xf5;
  1423. insn[1] = 0;
  1424. insn[2] = 0;
  1425. new_type = R_M32C_16_PCREL;
  1426. gap_size = 1;
  1427. gap = insn + 3;
  1428. }
  1429. break;
  1430. }
  1431. break;
  1432. case R_M32C_RL_2ADDR:
  1433. /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
  1434. enctbl = m16c_addr_encodings;
  1435. posn = 2;
  1436. enc = (insn[1] >> 4) & 0x0f;
  1437. e = & enctbl[enc];
  1438. if (srel->r_offset == irel->r_offset + posn
  1439. && e->new_encoding != enc
  1440. && symval <= e->max_disp)
  1441. {
  1442. insn[1] &= 0x0f;
  1443. insn[1] |= e->new_encoding << 4;
  1444. gap_size = e->bytes - enctbl[e->new_encoding].bytes;
  1445. gap = insn + posn + enctbl[e->new_encoding].bytes;
  1446. new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
  1447. break;
  1448. }
  1449. if (relax_relocs == 2)
  1450. srel ++;
  1451. posn += e->bytes;
  1452. goto try_1addr_16;
  1453. case R_M32C_RL_1ADDR:
  1454. /* xxxx xxxx xxxx dest [disp] */
  1455. enctbl = m16c_addr_encodings;
  1456. posn = 2;
  1457. /* Check the opcode for jumps. We know it's safe to
  1458. do this because all 2ADDR insns are at least two
  1459. bytes long. */
  1460. enc = insn[0] * 256 + insn[1];
  1461. enc &= 0xfff0;
  1462. if (enc == 0x7d20
  1463. || enc == 0x7d00
  1464. || enc == 0x7d30
  1465. || enc == 0x7d10)
  1466. {
  1467. enctbl = m16c_jmpaddr_encodings;
  1468. }
  1469. try_1addr_16:
  1470. /* srel, posn, and enc must be set here. */
  1471. symval = OFFSET_FOR_RELOC (srel);
  1472. enc = insn[1] & 0x0f;
  1473. e = & enctbl[enc];
  1474. if (srel->r_offset == irel->r_offset + posn
  1475. && e->new_encoding != enc
  1476. && symval <= e->max_disp)
  1477. {
  1478. insn[1] &= 0xf0;
  1479. insn[1] |= e->new_encoding;
  1480. gap_size = e->bytes - enctbl[e->new_encoding].bytes;
  1481. gap = insn + posn + enctbl[e->new_encoding].bytes;
  1482. new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
  1483. break;
  1484. }
  1485. break;
  1486. } /* Ends switch (reloc type) for m16c. */
  1487. }
  1488. else /* machine == bfd_mach_m32c */
  1489. {
  1490. /* M32CM / M32C */
  1491. switch (ELF32_R_TYPE(irel->r_info))
  1492. {
  1493. case R_M32C_RL_JUMP:
  1494. switch (insn[0])
  1495. {
  1496. case 0xbb: /* jmp.b */
  1497. if (pcrel >= 2 && pcrel <= 9)
  1498. {
  1499. int p = pcrel - 2;
  1500. /* Relax JMP.B -> JMP.S. We need to get rid of
  1501. the following reloc though. */
  1502. insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
  1503. new_type = R_M32C_NONE;
  1504. irel->r_addend = 0x10;
  1505. gap_size = 1;
  1506. gap = insn + 1;
  1507. }
  1508. break;
  1509. case 0xce: /* jmp.w */
  1510. if (pcrel <= 128 && pcrel >= -128)
  1511. {
  1512. /* Relax JMP.W -> JMP.B */
  1513. insn[0] = 0xbb;
  1514. insn[1] = 0;
  1515. new_type = R_M32C_8_PCREL;
  1516. gap_size = 1;
  1517. gap = insn + 2;
  1518. }
  1519. break;
  1520. case 0xcc: /* jmp.a */
  1521. if (pcrel <= 32768 && pcrel >= -32768)
  1522. {
  1523. /* Relax JMP.A -> JMP.W */
  1524. insn[0] = 0xce;
  1525. insn[1] = 0;
  1526. insn[2] = 0;
  1527. new_type = R_M32C_16_PCREL;
  1528. gap_size = 1;
  1529. gap = insn + 3;
  1530. }
  1531. break;
  1532. case 0xcd: /* jsr.a */
  1533. if (pcrel <= 32768 && pcrel >= -32768)
  1534. {
  1535. /* Relax JSR.A -> JSR.W */
  1536. insn[0] = 0xcf;
  1537. insn[1] = 0;
  1538. insn[2] = 0;
  1539. new_type = R_M32C_16_PCREL;
  1540. gap_size = 1;
  1541. gap = insn + 3;
  1542. }
  1543. break;
  1544. }
  1545. break;
  1546. case R_M32C_RL_2ADDR:
  1547. /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
  1548. einsn = insn;
  1549. posn = 2;
  1550. if (einsn[0] == 1)
  1551. {
  1552. /* prefix; remove it as far as the RL reloc is concerned. */
  1553. einsn ++;
  1554. posn ++;
  1555. }
  1556. enctbl = m32c_addr_encodings;
  1557. enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
  1558. e = & enctbl[enc];
  1559. if (srel->r_offset == irel->r_offset + posn
  1560. && e->new_encoding != enc
  1561. && symval <= e->max_disp)
  1562. {
  1563. einsn[0] &= 0x8f;
  1564. einsn[0] |= (e->new_encoding & 0x1c) << 2;
  1565. einsn[1] &= 0xcf;
  1566. einsn[1] |= (e->new_encoding & 0x03) << 4;
  1567. gap_size = e->bytes - enctbl[e->new_encoding].bytes;
  1568. gap = insn + posn + enctbl[e->new_encoding].bytes;
  1569. new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
  1570. break;
  1571. }
  1572. if (relax_relocs == 2)
  1573. srel ++;
  1574. posn += e->bytes;
  1575. goto try_1addr_32;
  1576. case R_M32C_RL_1ADDR:
  1577. /* xxxx DDDx DDxx xxxx [disp] */
  1578. einsn = insn;
  1579. posn = 2;
  1580. if (einsn[0] == 1)
  1581. {
  1582. /* prefix; remove it as far as the RL reloc is concerned. */
  1583. einsn ++;
  1584. posn ++;
  1585. }
  1586. enctbl = m32c_addr_encodings;
  1587. try_1addr_32:
  1588. /* srel, posn, and enc must be set here. */
  1589. symval = OFFSET_FOR_RELOC (srel);
  1590. enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
  1591. e = & enctbl[enc];
  1592. if (srel->r_offset == irel->r_offset + posn
  1593. && e->new_encoding != enc
  1594. && symval <= e->max_disp)
  1595. {
  1596. einsn[0] &= 0xf1;
  1597. einsn[0] |= (e->new_encoding & 0x1c) >> 1;
  1598. einsn[1] &= 0x3f;
  1599. einsn[1] |= (e->new_encoding & 0x03) << 6;
  1600. gap_size = e->bytes - enctbl[e->new_encoding].bytes;
  1601. gap = insn + posn + enctbl[e->new_encoding].bytes;
  1602. new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
  1603. break;
  1604. }
  1605. break;
  1606. } /* Ends switch (reloc type) for m32c. */
  1607. }
  1608. if (gap_size == 0)
  1609. continue;
  1610. *again = true;
  1611. srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
  1612. /* Note that we've changed the relocs, section contents, etc. */
  1613. elf_section_data (sec)->relocs = internal_relocs;
  1614. free_relocs = NULL;
  1615. elf_section_data (sec)->this_hdr.contents = contents;
  1616. free_contents = NULL;
  1617. symtab_hdr->contents = (bfd_byte *) intsyms;
  1618. free_intsyms = NULL;
  1619. bytes_saved += gap_size;
  1620. if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
  1621. goto error_return;
  1622. } /* next relocation */
  1623. free (free_relocs);
  1624. free_relocs = NULL;
  1625. if (free_contents != NULL)
  1626. {
  1627. if (! link_info->keep_memory)
  1628. free (free_contents);
  1629. /* Cache the section contents for elf_link_input_bfd. */
  1630. else
  1631. elf_section_data (sec)->this_hdr.contents = contents;
  1632. free_contents = NULL;
  1633. }
  1634. if (shndx_buf != NULL)
  1635. {
  1636. shndx_hdr->contents = NULL;
  1637. free (shndx_buf);
  1638. }
  1639. if (free_intsyms != NULL)
  1640. {
  1641. if (! link_info->keep_memory)
  1642. free (free_intsyms);
  1643. /* Cache the symbols for elf_link_input_bfd. */
  1644. else
  1645. {
  1646. symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
  1647. }
  1648. free_intsyms = NULL;
  1649. }
  1650. return true;
  1651. error_return:
  1652. free (free_relocs);
  1653. free (free_contents);
  1654. if (shndx_buf != NULL)
  1655. {
  1656. shndx_hdr->contents = NULL;
  1657. free (shndx_buf);
  1658. }
  1659. free (free_intsyms);
  1660. return false;
  1661. }
  1662. /* Delete some bytes from a section while relaxing. */
  1663. static bool
  1664. m32c_elf_relax_delete_bytes (bfd *abfd,
  1665. asection *sec,
  1666. bfd_vma addr,
  1667. int count)
  1668. {
  1669. Elf_Internal_Shdr *symtab_hdr;
  1670. Elf_Internal_Shdr *shndx_hdr;
  1671. int sec_shndx;
  1672. bfd_byte *contents;
  1673. Elf_Internal_Rela *irel;
  1674. Elf_Internal_Rela *irelend;
  1675. bfd_vma toaddr;
  1676. Elf_Internal_Sym *isym;
  1677. Elf_Internal_Sym *isymend;
  1678. Elf_Internal_Sym *intsyms;
  1679. Elf_External_Sym_Shndx *shndx_buf;
  1680. Elf_External_Sym_Shndx *shndx;
  1681. struct elf_link_hash_entry ** sym_hashes;
  1682. struct elf_link_hash_entry ** end_hashes;
  1683. unsigned int symcount;
  1684. contents = elf_section_data (sec)->this_hdr.contents;
  1685. toaddr = sec->size;
  1686. irel = elf_section_data (sec)->relocs;
  1687. irelend = irel + sec->reloc_count;
  1688. /* Actually delete the bytes. */
  1689. memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
  1690. sec->size -= count;
  1691. /* Adjust all the relocs. */
  1692. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
  1693. {
  1694. /* Get the new reloc address. */
  1695. if (irel->r_offset > addr && irel->r_offset < toaddr)
  1696. irel->r_offset -= count;
  1697. if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
  1698. && irel->r_addend == 0x10 /* one byte insn, no relocs */
  1699. && irel->r_offset + 1 < addr
  1700. && irel->r_offset + 7 > addr)
  1701. {
  1702. bfd_vma disp;
  1703. unsigned char *insn = &contents[irel->r_offset];
  1704. disp = *insn;
  1705. /* This is a JMP.S, which we have to manually update. */
  1706. if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
  1707. {
  1708. if ((*insn & 0xf8) != 0x60)
  1709. continue;
  1710. disp = (disp & 7);
  1711. }
  1712. else
  1713. {
  1714. if ((*insn & 0xce) != 0x4a)
  1715. continue;
  1716. disp = ((disp & 0x30) >> 3) | (disp & 1);
  1717. }
  1718. if (irel->r_offset + disp + 2 >= addr+count)
  1719. {
  1720. disp -= count;
  1721. if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
  1722. {
  1723. *insn = (*insn & 0xf8) | disp;
  1724. }
  1725. else
  1726. {
  1727. *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
  1728. }
  1729. }
  1730. }
  1731. }
  1732. /* Adjust the local symbols defined in this section. */
  1733. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1734. intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
  1735. isym = intsyms;
  1736. isymend = isym + symtab_hdr->sh_info;
  1737. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1738. if (elf_symtab_shndx_list (abfd))
  1739. {
  1740. shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
  1741. shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
  1742. }
  1743. else
  1744. {
  1745. shndx_hdr = NULL;
  1746. shndx_buf = NULL;
  1747. }
  1748. shndx = shndx_buf;
  1749. for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
  1750. {
  1751. /* If the symbol is in the range of memory we just moved, we
  1752. have to adjust its value. */
  1753. if ((int) isym->st_shndx == sec_shndx
  1754. && isym->st_value > addr
  1755. && isym->st_value < toaddr)
  1756. {
  1757. isym->st_value -= count;
  1758. }
  1759. /* If the symbol *spans* the bytes we just deleted (i.e. it's
  1760. *end* is in the moved bytes but it's *start* isn't), then we
  1761. must adjust its size. */
  1762. if ((int) isym->st_shndx == sec_shndx
  1763. && isym->st_value < addr
  1764. && isym->st_value + isym->st_size > addr
  1765. && isym->st_value + isym->st_size < toaddr)
  1766. {
  1767. isym->st_size -= count;
  1768. }
  1769. }
  1770. /* Now adjust the global symbols defined in this section. */
  1771. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1772. - symtab_hdr->sh_info);
  1773. sym_hashes = elf_sym_hashes (abfd);
  1774. end_hashes = sym_hashes + symcount;
  1775. for (; sym_hashes < end_hashes; sym_hashes ++)
  1776. {
  1777. struct elf_link_hash_entry * sym_hash = * sym_hashes;
  1778. if (sym_hash &&
  1779. (sym_hash->root.type == bfd_link_hash_defined
  1780. || sym_hash->root.type == bfd_link_hash_defweak)
  1781. && sym_hash->root.u.def.section == sec)
  1782. {
  1783. if (sym_hash->root.u.def.value > addr
  1784. && sym_hash->root.u.def.value < toaddr)
  1785. {
  1786. sym_hash->root.u.def.value -= count;
  1787. }
  1788. if (sym_hash->root.u.def.value < addr
  1789. && sym_hash->root.u.def.value + sym_hash->size > addr
  1790. && sym_hash->root.u.def.value + sym_hash->size < toaddr)
  1791. {
  1792. sym_hash->size -= count;
  1793. }
  1794. }
  1795. }
  1796. return true;
  1797. }
  1798. /* This is for versions of gcc prior to 4.3. */
  1799. static unsigned int
  1800. _bfd_m32c_elf_eh_frame_address_size (bfd *abfd,
  1801. const asection *sec ATTRIBUTE_UNUSED)
  1802. {
  1803. if ((elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) == EF_M32C_CPU_M16C)
  1804. return 2;
  1805. return 4;
  1806. }
  1807. #define ELF_ARCH bfd_arch_m32c
  1808. #define ELF_MACHINE_CODE EM_M32C
  1809. #define ELF_MACHINE_ALT1 EM_M32C_OLD
  1810. #define ELF_MAXPAGESIZE 0x100
  1811. #if 0
  1812. #define TARGET_BIG_SYM m32c_elf32_vec
  1813. #define TARGET_BIG_NAME "elf32-m32c"
  1814. #else
  1815. #define TARGET_LITTLE_SYM m32c_elf32_vec
  1816. #define TARGET_LITTLE_NAME "elf32-m32c"
  1817. #endif
  1818. #define elf_info_to_howto_rel NULL
  1819. #define elf_info_to_howto m32c_info_to_howto_rela
  1820. #define elf_backend_object_p m32c_elf_object_p
  1821. #define elf_backend_relocate_section m32c_elf_relocate_section
  1822. #define elf_backend_check_relocs m32c_elf_check_relocs
  1823. #define elf_backend_object_p m32c_elf_object_p
  1824. #define elf_symbol_leading_char ('_')
  1825. #define elf_backend_always_size_sections \
  1826. m32c_elf_always_size_sections
  1827. #define elf_backend_finish_dynamic_sections \
  1828. m32c_elf_finish_dynamic_sections
  1829. #define elf_backend_can_gc_sections 1
  1830. #define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size
  1831. #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
  1832. #define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup
  1833. #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
  1834. #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
  1835. #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
  1836. #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
  1837. #include "elf32-target.h"