elf32-ip2k.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. /* Ubicom IP2xxx specific support for 32-bit ELF
  2. Copyright (C) 2000-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/ip2k.h"
  21. /* Struct used to pass miscellaneous paramaters which
  22. helps to avoid overly long parameter lists. */
  23. struct misc
  24. {
  25. Elf_Internal_Shdr * symtab_hdr;
  26. Elf_Internal_Rela * irelbase;
  27. bfd_byte * contents;
  28. Elf_Internal_Sym * isymbuf;
  29. };
  30. struct ip2k_opcode
  31. {
  32. unsigned short opcode;
  33. unsigned short mask;
  34. };
  35. static bool ip2k_relaxed = false;
  36. static const struct ip2k_opcode ip2k_page_opcode[] =
  37. {
  38. {0x0010, 0xFFF8}, /* Page. */
  39. {0x0000, 0x0000},
  40. };
  41. #define IS_PAGE_OPCODE(code) \
  42. ip2k_is_opcode (code, ip2k_page_opcode)
  43. static const struct ip2k_opcode ip2k_jmp_opcode[] =
  44. {
  45. {0xE000, 0xE000}, /* Jmp. */
  46. {0x0000, 0x0000},
  47. };
  48. #define IS_JMP_OPCODE(code) \
  49. ip2k_is_opcode (code, ip2k_jmp_opcode)
  50. static const struct ip2k_opcode ip2k_snc_opcode[] =
  51. {
  52. {0xA00B, 0xFFFF}, /* Snc. */
  53. {0x0000, 0x0000},
  54. };
  55. #define IS_SNC_OPCODE(code) \
  56. ip2k_is_opcode (code, ip2k_snc_opcode)
  57. static const struct ip2k_opcode ip2k_inc_1sp_opcode[] =
  58. {
  59. {0x2B81, 0xFFFF}, /* Inc 1(SP). */
  60. {0x0000, 0x0000},
  61. };
  62. #define IS_INC_1SP_OPCODE(code) \
  63. ip2k_is_opcode (code, ip2k_inc_1sp_opcode)
  64. static const struct ip2k_opcode ip2k_add_2sp_w_opcode[] =
  65. {
  66. {0x1F82, 0xFFFF}, /* Add 2(SP),w. */
  67. {0x0000, 0x0000},
  68. };
  69. #define IS_ADD_2SP_W_OPCODE(code) \
  70. ip2k_is_opcode (code, ip2k_add_2sp_w_opcode)
  71. static const struct ip2k_opcode ip2k_add_w_wreg_opcode[] =
  72. {
  73. {0x1C0A, 0xFFFF}, /* Add w,wreg. */
  74. {0x1E0A, 0xFFFF}, /* Add wreg,w. */
  75. {0x0000, 0x0000},
  76. };
  77. #define IS_ADD_W_WREG_OPCODE(code) \
  78. ip2k_is_opcode (code, ip2k_add_w_wreg_opcode)
  79. static const struct ip2k_opcode ip2k_add_pcl_w_opcode[] =
  80. {
  81. {0x1E09, 0xFFFF}, /* Add pcl,w. */
  82. {0x0000, 0x0000},
  83. };
  84. #define IS_ADD_PCL_W_OPCODE(code) \
  85. ip2k_is_opcode (code, ip2k_add_pcl_w_opcode)
  86. static const struct ip2k_opcode ip2k_skip_opcodes[] =
  87. {
  88. {0xB000, 0xF000}, /* sb */
  89. {0xA000, 0xF000}, /* snb */
  90. {0x7600, 0xFE00}, /* cse/csne #lit */
  91. {0x5800, 0xFC00}, /* incsnz */
  92. {0x4C00, 0xFC00}, /* decsnz */
  93. {0x4000, 0xFC00}, /* cse/csne */
  94. {0x3C00, 0xFC00}, /* incsz */
  95. {0x2C00, 0xFC00}, /* decsz */
  96. {0x0000, 0x0000},
  97. };
  98. #define IS_SKIP_OPCODE(code) \
  99. ip2k_is_opcode (code, ip2k_skip_opcodes)
  100. /* Relocation tables. */
  101. static reloc_howto_type ip2k_elf_howto_table [] =
  102. {
  103. #define IP2K_HOWTO(t,rs,s,bs,pr,bp,name,sm,dm) \
  104. HOWTO(t, /* type */ \
  105. rs, /* rightshift */ \
  106. s, /* size (0 = byte, 1 = short, 2 = long) */ \
  107. bs, /* bitsize */ \
  108. pr, /* pc_relative */ \
  109. bp, /* bitpos */ \
  110. complain_overflow_dont,/* complain_on_overflow */ \
  111. bfd_elf_generic_reloc,/* special_function */ \
  112. name, /* name */ \
  113. false, /* partial_inplace */ \
  114. sm, /* src_mask */ \
  115. dm, /* dst_mask */ \
  116. pr) /* pcrel_offset */
  117. /* This reloc does nothing. */
  118. IP2K_HOWTO (R_IP2K_NONE, 0,3,0, false, 0, "R_IP2K_NONE", 0, 0),
  119. /* A 16 bit absolute relocation. */
  120. IP2K_HOWTO (R_IP2K_16, 0,1,16, false, 0, "R_IP2K_16", 0, 0xffff),
  121. /* A 32 bit absolute relocation. */
  122. IP2K_HOWTO (R_IP2K_32, 0,2,32, false, 0, "R_IP2K_32", 0, 0xffffffff),
  123. /* A 8-bit data relocation for the FR9 field. Ninth bit is computed specially. */
  124. IP2K_HOWTO (R_IP2K_FR9, 0,1,9, false, 0, "R_IP2K_FR9", 0, 0x00ff),
  125. /* A 4-bit data relocation. */
  126. IP2K_HOWTO (R_IP2K_BANK, 8,1,4, false, 0, "R_IP2K_BANK", 0, 0x000f),
  127. /* A 13-bit insn relocation - word address => right-shift 1 bit extra. */
  128. IP2K_HOWTO (R_IP2K_ADDR16CJP, 1,1,13, false, 0, "R_IP2K_ADDR16CJP", 0, 0x1fff),
  129. /* A 3-bit insn relocation - word address => right-shift 1 bit extra. */
  130. IP2K_HOWTO (R_IP2K_PAGE3, 14,1,3, false, 0, "R_IP2K_PAGE3", 0, 0x0007),
  131. /* Two 8-bit data relocations. */
  132. IP2K_HOWTO (R_IP2K_LO8DATA, 0,1,8, false, 0, "R_IP2K_LO8DATA", 0, 0x00ff),
  133. IP2K_HOWTO (R_IP2K_HI8DATA, 8,1,8, false, 0, "R_IP2K_HI8DATA", 0, 0x00ff),
  134. /* Two 8-bit insn relocations. word address => right-shift 1 bit extra. */
  135. IP2K_HOWTO (R_IP2K_LO8INSN, 1,1,8, false, 0, "R_IP2K_LO8INSN", 0, 0x00ff),
  136. IP2K_HOWTO (R_IP2K_HI8INSN, 9,1,8, false, 0, "R_IP2K_HI8INSN", 0, 0x00ff),
  137. /* Special 1 bit relocation for SKIP instructions. */
  138. IP2K_HOWTO (R_IP2K_PC_SKIP, 1,1,1, false, 12, "R_IP2K_PC_SKIP", 0xfffe, 0x1000),
  139. /* 16 bit word address. */
  140. IP2K_HOWTO (R_IP2K_TEXT, 1,1,16, false, 0, "R_IP2K_TEXT", 0, 0xffff),
  141. /* A 7-bit offset relocation for the FR9 field. Eigth and ninth bit comes from insn. */
  142. IP2K_HOWTO (R_IP2K_FR_OFFSET, 0,1,9, false, 0, "R_IP2K_FR_OFFSET", 0x180, 0x007f),
  143. /* Bits 23:16 of an address. */
  144. IP2K_HOWTO (R_IP2K_EX8DATA, 16,1,8, false, 0, "R_IP2K_EX8DATA", 0, 0x00ff),
  145. };
  146. /* Map BFD reloc types to IP2K ELF reloc types. */
  147. static reloc_howto_type *
  148. ip2k_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  149. bfd_reloc_code_real_type code)
  150. {
  151. /* Note that the ip2k_elf_howto_table is indxed by the R_
  152. constants. Thus, the order that the howto records appear in the
  153. table *must* match the order of the relocation types defined in
  154. include/elf/ip2k.h. */
  155. switch (code)
  156. {
  157. case BFD_RELOC_NONE:
  158. return &ip2k_elf_howto_table[ (int) R_IP2K_NONE];
  159. case BFD_RELOC_16:
  160. return &ip2k_elf_howto_table[ (int) R_IP2K_16];
  161. case BFD_RELOC_32:
  162. return &ip2k_elf_howto_table[ (int) R_IP2K_32];
  163. case BFD_RELOC_IP2K_FR9:
  164. return &ip2k_elf_howto_table[ (int) R_IP2K_FR9];
  165. case BFD_RELOC_IP2K_BANK:
  166. return &ip2k_elf_howto_table[ (int) R_IP2K_BANK];
  167. case BFD_RELOC_IP2K_ADDR16CJP:
  168. return &ip2k_elf_howto_table[ (int) R_IP2K_ADDR16CJP];
  169. case BFD_RELOC_IP2K_PAGE3:
  170. return &ip2k_elf_howto_table[ (int) R_IP2K_PAGE3];
  171. case BFD_RELOC_IP2K_LO8DATA:
  172. return &ip2k_elf_howto_table[ (int) R_IP2K_LO8DATA];
  173. case BFD_RELOC_IP2K_HI8DATA:
  174. return &ip2k_elf_howto_table[ (int) R_IP2K_HI8DATA];
  175. case BFD_RELOC_IP2K_LO8INSN:
  176. return &ip2k_elf_howto_table[ (int) R_IP2K_LO8INSN];
  177. case BFD_RELOC_IP2K_HI8INSN:
  178. return &ip2k_elf_howto_table[ (int) R_IP2K_HI8INSN];
  179. case BFD_RELOC_IP2K_PC_SKIP:
  180. return &ip2k_elf_howto_table[ (int) R_IP2K_PC_SKIP];
  181. case BFD_RELOC_IP2K_TEXT:
  182. return &ip2k_elf_howto_table[ (int) R_IP2K_TEXT];
  183. case BFD_RELOC_IP2K_FR_OFFSET:
  184. return &ip2k_elf_howto_table[ (int) R_IP2K_FR_OFFSET];
  185. case BFD_RELOC_IP2K_EX8DATA:
  186. return &ip2k_elf_howto_table[ (int) R_IP2K_EX8DATA];
  187. default:
  188. /* Pacify gcc -Wall. */
  189. return NULL;
  190. }
  191. return NULL;
  192. }
  193. static reloc_howto_type *
  194. ip2k_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
  195. {
  196. unsigned int i;
  197. for (i = 0;
  198. i < sizeof (ip2k_elf_howto_table) / sizeof (ip2k_elf_howto_table[0]);
  199. i++)
  200. if (ip2k_elf_howto_table[i].name != NULL
  201. && strcasecmp (ip2k_elf_howto_table[i].name, r_name) == 0)
  202. return &ip2k_elf_howto_table[i];
  203. return NULL;
  204. }
  205. static void
  206. ip2k_get_mem (bfd *abfd ATTRIBUTE_UNUSED,
  207. bfd_byte *addr,
  208. int length,
  209. bfd_byte *ptr)
  210. {
  211. while (length --)
  212. * ptr ++ = bfd_get_8 (abfd, addr ++);
  213. }
  214. static bool
  215. ip2k_is_opcode (bfd_byte *code, const struct ip2k_opcode *opcodes)
  216. {
  217. unsigned short insn = (code[0] << 8) | code[1];
  218. while (opcodes->mask != 0)
  219. {
  220. if ((insn & opcodes->mask) == opcodes->opcode)
  221. return true;
  222. opcodes ++;
  223. }
  224. return false;
  225. }
  226. #define PAGENO(ABSADDR) ((ABSADDR) & 0xFFFFC000)
  227. #define BASEADDR(SEC) ((SEC)->output_section->vma + (SEC)->output_offset)
  228. #define UNDEFINED_SYMBOL (~(bfd_vma)0)
  229. /* Return the value of the symbol associated with the relocation IREL. */
  230. static bfd_vma
  231. symbol_value (bfd *abfd,
  232. Elf_Internal_Shdr *symtab_hdr,
  233. Elf_Internal_Sym *isymbuf,
  234. Elf_Internal_Rela *irel)
  235. {
  236. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  237. {
  238. Elf_Internal_Sym *isym;
  239. asection *sym_sec;
  240. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  241. if (isym->st_shndx == SHN_UNDEF)
  242. sym_sec = bfd_und_section_ptr;
  243. else if (isym->st_shndx == SHN_ABS)
  244. sym_sec = bfd_abs_section_ptr;
  245. else if (isym->st_shndx == SHN_COMMON)
  246. sym_sec = bfd_com_section_ptr;
  247. else
  248. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  249. return isym->st_value + BASEADDR (sym_sec);
  250. }
  251. else
  252. {
  253. unsigned long indx;
  254. struct elf_link_hash_entry *h;
  255. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  256. h = elf_sym_hashes (abfd)[indx];
  257. BFD_ASSERT (h != NULL);
  258. if (h->root.type != bfd_link_hash_defined
  259. && h->root.type != bfd_link_hash_defweak)
  260. return UNDEFINED_SYMBOL;
  261. return (h->root.u.def.value + BASEADDR (h->root.u.def.section));
  262. }
  263. }
  264. /* Determine if the instruction sequence matches that for
  265. the prologue of a switch dispatch table with fewer than
  266. 128 entries.
  267. sc
  268. page $nnn0
  269. jmp $nnn0
  270. add w,wreg
  271. add pcl,w
  272. addr=>
  273. page $nnn1
  274. jmp $nnn1
  275. page $nnn2
  276. jmp $nnn2
  277. ...
  278. page $nnnN
  279. jmp $nnnN
  280. After relaxation.
  281. sc
  282. page $nnn0
  283. jmp $nnn0
  284. add pcl,w
  285. addr=>
  286. jmp $nnn1
  287. jmp $nnn2
  288. ...
  289. jmp $nnnN */
  290. static int
  291. ip2k_is_switch_table_128 (bfd *abfd ATTRIBUTE_UNUSED,
  292. asection *sec,
  293. bfd_vma addr,
  294. bfd_byte *contents)
  295. {
  296. bfd_byte code[4];
  297. int table_index = 0;
  298. /* Check current page-jmp. */
  299. if (addr + 4 > sec->size)
  300. return -1;
  301. ip2k_get_mem (abfd, contents + addr, 4, code);
  302. if ((! IS_PAGE_OPCODE (code + 0))
  303. || (! IS_JMP_OPCODE (code + 2)))
  304. return -1;
  305. /* Search back. */
  306. while (1)
  307. {
  308. if (addr < 4)
  309. return -1;
  310. /* Check previous 2 instructions. */
  311. ip2k_get_mem (abfd, contents + addr - 4, 4, code);
  312. if ((IS_ADD_W_WREG_OPCODE (code + 0))
  313. && (IS_ADD_PCL_W_OPCODE (code + 2)))
  314. return table_index;
  315. if ((! IS_PAGE_OPCODE (code + 0))
  316. || (! IS_JMP_OPCODE (code + 2)))
  317. return -1;
  318. table_index++;
  319. addr -= 4;
  320. }
  321. }
  322. /* Determine if the instruction sequence matches that for
  323. the prologue switch dispatch table with fewer than
  324. 256 entries but more than 127.
  325. Before relaxation.
  326. push %lo8insn(label) ; Push address of table
  327. push %hi8insn(label)
  328. add w,wreg ; index*2 => offset
  329. snc ; CARRY SET?
  330. inc 1(sp) ; Propagate MSB into table address
  331. add 2(sp),w ; Add low bits of offset to table address
  332. snc ; and handle any carry-out
  333. inc 1(sp)
  334. addr=>
  335. page __indjmp ; Do an indirect jump to that location
  336. jmp __indjmp
  337. label: ; case dispatch table starts here
  338. page $nnn1
  339. jmp $nnn1
  340. page $nnn2
  341. jmp $nnn2
  342. ...
  343. page $nnnN
  344. jmp $nnnN
  345. After relaxation.
  346. push %lo8insn(label) ; Push address of table
  347. push %hi8insn(label)
  348. add 2(sp),w ; Add low bits of offset to table address
  349. snc ; and handle any carry-out
  350. inc 1(sp)
  351. addr=>
  352. page __indjmp ; Do an indirect jump to that location
  353. jmp __indjmp
  354. label: ; case dispatch table starts here
  355. jmp $nnn1
  356. jmp $nnn2
  357. ...
  358. jmp $nnnN */
  359. static int
  360. ip2k_is_switch_table_256 (bfd *abfd ATTRIBUTE_UNUSED,
  361. asection *sec,
  362. bfd_vma addr,
  363. bfd_byte *contents)
  364. {
  365. bfd_byte code[16];
  366. int table_index = 0;
  367. /* Check current page-jmp. */
  368. if (addr + 4 > sec->size)
  369. return -1;
  370. ip2k_get_mem (abfd, contents + addr, 4, code);
  371. if ((! IS_PAGE_OPCODE (code + 0))
  372. || (! IS_JMP_OPCODE (code + 2)))
  373. return -1;
  374. /* Search back. */
  375. while (1)
  376. {
  377. if (addr < 16)
  378. return -1;
  379. /* Check previous 8 instructions. */
  380. ip2k_get_mem (abfd, contents + addr - 16, 16, code);
  381. if ((IS_ADD_W_WREG_OPCODE (code + 0))
  382. && (IS_SNC_OPCODE (code + 2))
  383. && (IS_INC_1SP_OPCODE (code + 4))
  384. && (IS_ADD_2SP_W_OPCODE (code + 6))
  385. && (IS_SNC_OPCODE (code + 8))
  386. && (IS_INC_1SP_OPCODE (code + 10))
  387. && (IS_PAGE_OPCODE (code + 12))
  388. && (IS_JMP_OPCODE (code + 14)))
  389. return table_index;
  390. if ((IS_ADD_W_WREG_OPCODE (code + 2))
  391. && (IS_SNC_OPCODE (code + 4))
  392. && (IS_INC_1SP_OPCODE (code + 6))
  393. && (IS_ADD_2SP_W_OPCODE (code + 8))
  394. && (IS_SNC_OPCODE (code + 10))
  395. && (IS_INC_1SP_OPCODE (code + 12))
  396. && (IS_JMP_OPCODE (code + 14)))
  397. return table_index;
  398. if ((! IS_PAGE_OPCODE (code + 0))
  399. || (! IS_JMP_OPCODE (code + 2)))
  400. return -1;
  401. table_index++;
  402. addr -= 4;
  403. }
  404. }
  405. /* Returns the expected page state for the given instruction not including
  406. the effect of page instructions. */
  407. static bfd_vma
  408. ip2k_nominal_page_bits (bfd *abfd ATTRIBUTE_UNUSED,
  409. asection *sec,
  410. bfd_vma addr,
  411. bfd_byte *contents)
  412. {
  413. bfd_vma page = PAGENO (BASEADDR (sec) + addr);
  414. /* Check if section flows into this page. If not then the page
  415. bits are assumed to match the PC. This will be true unless
  416. the user has a page instruction without a call/jump, in which
  417. case they are on their own. */
  418. if (PAGENO (BASEADDR (sec)) == page)
  419. return page;
  420. /* Section flows across page boundary. The page bits should match
  421. the PC unless there is a possible flow from the previous page,
  422. in which case it is not possible to determine the value of the
  423. page bits. */
  424. while (PAGENO (BASEADDR (sec) + addr - 2) == page)
  425. {
  426. bfd_byte code[2];
  427. addr -= 2;
  428. ip2k_get_mem (abfd, contents + addr, 2, code);
  429. if (!IS_PAGE_OPCODE (code))
  430. continue;
  431. /* Found a page instruction, check if jump table. */
  432. if (ip2k_is_switch_table_128 (abfd, sec, addr, contents) != -1)
  433. /* Jump table => page is conditional. */
  434. continue;
  435. if (ip2k_is_switch_table_256 (abfd, sec, addr, contents) != -1)
  436. /* Jump table => page is conditional. */
  437. continue;
  438. /* Found a page instruction, check if conditional. */
  439. if (addr >= 2)
  440. {
  441. ip2k_get_mem (abfd, contents + addr - 2, 2, code);
  442. if (IS_SKIP_OPCODE (code))
  443. /* Page is conditional. */
  444. continue;
  445. }
  446. /* Unconditional page instruction => page bits should be correct. */
  447. return page;
  448. }
  449. /* Flow from previous page => page bits are impossible to determine. */
  450. return 0;
  451. }
  452. static bool
  453. ip2k_test_page_insn (bfd *abfd ATTRIBUTE_UNUSED,
  454. asection *sec,
  455. Elf_Internal_Rela *irel,
  456. struct misc *misc)
  457. {
  458. bfd_vma symval;
  459. /* Get the value of the symbol referred to by the reloc. */
  460. symval = symbol_value (abfd, misc->symtab_hdr, misc->isymbuf, irel);
  461. if (symval == UNDEFINED_SYMBOL)
  462. /* This appears to be a reference to an undefined
  463. symbol. Just ignore it--it will be caught by the
  464. regular reloc processing. */
  465. return false;
  466. /* Test if we can delete this page instruction. */
  467. if (PAGENO (symval + irel->r_addend) !=
  468. ip2k_nominal_page_bits (abfd, sec, irel->r_offset, misc->contents))
  469. return false;
  470. return true;
  471. }
  472. /* Parts of a Stabs entry. */
  473. #define STRDXOFF 0
  474. #define TYPEOFF 4
  475. #define OTHEROFF 5
  476. #define DESCOFF 6
  477. #define VALOFF 8
  478. #define STABSIZE 12
  479. /* Adjust all the relocations entries after adding or inserting instructions. */
  480. static void
  481. adjust_all_relocations (bfd *abfd,
  482. asection *sec,
  483. bfd_vma addr,
  484. bfd_vma endaddr,
  485. int count,
  486. int noadj)
  487. {
  488. Elf_Internal_Shdr *symtab_hdr;
  489. Elf_Internal_Sym *isymbuf, *isym, *isymend;
  490. unsigned int shndx;
  491. Elf_Internal_Rela *irel, *irelend, *irelbase;
  492. struct elf_link_hash_entry **sym_hashes;
  493. struct elf_link_hash_entry **end_hashes;
  494. unsigned int symcount;
  495. asection *stab;
  496. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  497. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  498. shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  499. irelbase = elf_section_data (sec)->relocs;
  500. irelend = irelbase + sec->reloc_count;
  501. for (irel = irelbase; irel < irelend; irel++)
  502. {
  503. if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE)
  504. {
  505. /* Get the value of the symbol referred to by the reloc. */
  506. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  507. {
  508. asection *sym_sec;
  509. /* A local symbol. */
  510. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  511. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  512. if (isym->st_shndx == shndx)
  513. {
  514. bfd_vma baseaddr = BASEADDR (sec);
  515. bfd_vma symval = BASEADDR (sym_sec) + isym->st_value
  516. + irel->r_addend;
  517. if ((baseaddr + addr + noadj) <= symval
  518. && symval < (baseaddr + endaddr))
  519. irel->r_addend += count;
  520. }
  521. }
  522. }
  523. /* Do this only for PC space relocations. */
  524. if (addr <= irel->r_offset && irel->r_offset < endaddr)
  525. irel->r_offset += count;
  526. }
  527. /* Now fix the stab relocations. */
  528. stab = bfd_get_section_by_name (abfd, ".stab");
  529. if (stab)
  530. {
  531. bfd_byte *stabcontents, *stabend, *stabp;
  532. bfd_size_type stab_size = stab->rawsize ? stab->rawsize : stab->size;
  533. irelbase = elf_section_data (stab)->relocs;
  534. irelend = irelbase + stab->reloc_count;
  535. /* Pull out the contents of the stab section. */
  536. if (elf_section_data (stab)->this_hdr.contents != NULL)
  537. stabcontents = elf_section_data (stab)->this_hdr.contents;
  538. else
  539. {
  540. if (!bfd_malloc_and_get_section (abfd, stab, &stabcontents))
  541. {
  542. free (stabcontents);
  543. return;
  544. }
  545. /* We need to remember this. */
  546. elf_section_data (stab)->this_hdr.contents = stabcontents;
  547. }
  548. stabend = stabcontents + stab_size;
  549. for (irel = irelbase; irel < irelend; irel++)
  550. {
  551. if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE)
  552. {
  553. /* Get the value of the symbol referred to by the reloc. */
  554. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  555. {
  556. asection *sym_sec;
  557. /* A local symbol. */
  558. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  559. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  560. if (sym_sec == sec)
  561. {
  562. const char *name;
  563. unsigned char type;
  564. bfd_vma value;
  565. bfd_vma baseaddr = BASEADDR (sec);
  566. bfd_vma symval = BASEADDR (sym_sec) + isym->st_value
  567. + irel->r_addend;
  568. if ((baseaddr + addr) <= symval
  569. && symval <= (baseaddr + endaddr))
  570. irel->r_addend += count;
  571. /* Go hunt up a function and fix its line info if needed. */
  572. stabp = stabcontents + irel->r_offset - 8;
  573. /* Go pullout the stab entry. */
  574. type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
  575. value = bfd_h_get_32 (abfd, stabp + VALOFF);
  576. name = bfd_get_stab_name (type);
  577. if (strcmp (name, "FUN") == 0)
  578. {
  579. int function_adjusted = 0;
  580. if (symval > (baseaddr + addr))
  581. /* Not in this function. */
  582. continue;
  583. /* Hey we got a function hit. */
  584. stabp += STABSIZE;
  585. for (;stabp < stabend; stabp += STABSIZE)
  586. {
  587. /* Go pullout the stab entry. */
  588. type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
  589. value = bfd_h_get_32 (abfd, stabp + VALOFF);
  590. name = bfd_get_stab_name (type);
  591. if (strcmp (name, "FUN") == 0)
  592. {
  593. /* Hit another function entry. */
  594. if (function_adjusted)
  595. {
  596. /* Adjust the value. */
  597. value += count;
  598. /* We need to put it back. */
  599. bfd_h_put_32 (abfd, value,stabp + VALOFF);
  600. }
  601. /* And then bale out. */
  602. break;
  603. }
  604. if (strcmp (name, "SLINE") == 0)
  605. {
  606. /* Got a line entry. */
  607. if ((baseaddr + addr) <= (symval + value))
  608. {
  609. /* Adjust the line entry. */
  610. value += count;
  611. /* We need to put it back. */
  612. bfd_h_put_32 (abfd, value,stabp + VALOFF);
  613. function_adjusted = 1;
  614. }
  615. }
  616. }
  617. }
  618. }
  619. }
  620. }
  621. }
  622. }
  623. /* When adding an instruction back it is sometimes necessary to move any
  624. global or local symbol that was referencing the first instruction of
  625. the moved block to refer to the first instruction of the inserted block.
  626. For example adding a PAGE instruction before a CALL or JMP requires
  627. that any label on the CALL or JMP is moved to the PAGE insn. */
  628. addr += noadj;
  629. /* Adjust the local symbols defined in this section. */
  630. isymend = isymbuf + symtab_hdr->sh_info;
  631. for (isym = isymbuf; isym < isymend; isym++)
  632. {
  633. if (isym->st_shndx == shndx
  634. && addr <= isym->st_value
  635. && isym->st_value < endaddr)
  636. isym->st_value += count;
  637. }
  638. /* Now adjust the global symbols defined in this section. */
  639. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  640. - symtab_hdr->sh_info);
  641. sym_hashes = elf_sym_hashes (abfd);
  642. end_hashes = sym_hashes + symcount;
  643. for (; sym_hashes < end_hashes; sym_hashes++)
  644. {
  645. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  646. if ((sym_hash->root.type == bfd_link_hash_defined
  647. || sym_hash->root.type == bfd_link_hash_defweak)
  648. && sym_hash->root.u.def.section == sec)
  649. {
  650. if (addr <= sym_hash->root.u.def.value
  651. && sym_hash->root.u.def.value < endaddr)
  652. sym_hash->root.u.def.value += count;
  653. }
  654. }
  655. return;
  656. }
  657. /* Delete some bytes from a section while relaxing. */
  658. static bool
  659. ip2k_elf_relax_delete_bytes (bfd *abfd,
  660. asection *sec,
  661. bfd_vma addr,
  662. int count)
  663. {
  664. bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
  665. bfd_vma endaddr = sec->size;
  666. /* Actually delete the bytes. */
  667. memmove (contents + addr, contents + addr + count,
  668. endaddr - addr - count);
  669. sec->size -= count;
  670. adjust_all_relocations (abfd, sec, addr + count, endaddr, -count, 0);
  671. return true;
  672. }
  673. static bool
  674. ip2k_delete_page_insn (bfd *abfd ATTRIBUTE_UNUSED,
  675. asection *sec,
  676. Elf_Internal_Rela *irel,
  677. bool *again,
  678. struct misc *misc)
  679. {
  680. /* Note that we've changed the relocs, section contents, etc. */
  681. elf_section_data (sec)->relocs = misc->irelbase;
  682. elf_section_data (sec)->this_hdr.contents = misc->contents;
  683. misc->symtab_hdr->contents = (bfd_byte *) misc->isymbuf;
  684. /* Fix the relocation's type. */
  685. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_IP2K_NONE);
  686. /* Delete the PAGE insn. */
  687. if (!ip2k_elf_relax_delete_bytes (abfd, sec, irel->r_offset, 2))
  688. return false;
  689. /* Modified => will need to iterate relaxation again. */
  690. *again = true;
  691. return true;
  692. }
  693. static bool
  694. ip2k_relax_switch_table_128 (bfd *abfd ATTRIBUTE_UNUSED,
  695. asection *sec,
  696. Elf_Internal_Rela *irel,
  697. bool *again,
  698. struct misc *misc)
  699. {
  700. Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
  701. Elf_Internal_Rela *ireltest = irel;
  702. bfd_byte code[4];
  703. bfd_vma addr;
  704. /* Test all page instructions. */
  705. addr = irel->r_offset;
  706. while (1)
  707. {
  708. if (addr + 4 > sec->size)
  709. break;
  710. ip2k_get_mem (abfd, misc->contents + addr, 4, code);
  711. if ((! IS_PAGE_OPCODE (code + 0))
  712. || (! IS_JMP_OPCODE (code + 2)))
  713. break;
  714. /* Validate relocation entry (every entry should have a matching
  715. relocation entry). */
  716. if (ireltest >= irelend)
  717. {
  718. _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
  719. return false;
  720. }
  721. if (ireltest->r_offset != addr)
  722. {
  723. _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
  724. return false;
  725. }
  726. if (! ip2k_test_page_insn (abfd, sec, ireltest, misc))
  727. /* Un-removable page insn => nothing can be done. */
  728. return true;
  729. addr += 4;
  730. ireltest += 2;
  731. }
  732. /* Relaxable. Adjust table header. */
  733. ip2k_get_mem (abfd, misc->contents + irel->r_offset - 4, 4, code);
  734. if ((! IS_ADD_W_WREG_OPCODE (code + 0))
  735. || (! IS_ADD_PCL_W_OPCODE (code + 2)))
  736. {
  737. _bfd_error_handler (_("ip2k relaxer: switch table header corrupt."));
  738. return false;
  739. }
  740. if (!ip2k_elf_relax_delete_bytes (abfd, sec, irel->r_offset - 4, 2))
  741. return false;
  742. *again = true;
  743. /* Delete all page instructions in table. */
  744. while (irel < ireltest)
  745. {
  746. if (!ip2k_delete_page_insn (abfd, sec, irel, again, misc))
  747. return false;
  748. irel += 2;
  749. }
  750. return true;
  751. }
  752. static bool
  753. ip2k_relax_switch_table_256 (bfd *abfd ATTRIBUTE_UNUSED,
  754. asection *sec,
  755. Elf_Internal_Rela *irel,
  756. bool *again,
  757. struct misc *misc)
  758. {
  759. Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
  760. Elf_Internal_Rela *ireltest = irel;
  761. bfd_byte code[12];
  762. bfd_vma addr;
  763. /* Test all page instructions. */
  764. addr = irel->r_offset;
  765. while (1)
  766. {
  767. if (addr + 4 > sec->size)
  768. break;
  769. ip2k_get_mem (abfd, misc->contents + addr, 4, code);
  770. if ((! IS_PAGE_OPCODE (code + 0))
  771. || (! IS_JMP_OPCODE (code + 2)))
  772. break;
  773. /* Validate relocation entry (every entry should have a matching
  774. relocation entry). */
  775. if (ireltest >= irelend)
  776. {
  777. _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
  778. return false;
  779. }
  780. if (ireltest->r_offset != addr)
  781. {
  782. _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
  783. return false;
  784. }
  785. if (!ip2k_test_page_insn (abfd, sec, ireltest, misc))
  786. /* Un-removable page insn => nothing can be done. */
  787. return true;
  788. addr += 4;
  789. ireltest += 2;
  790. }
  791. /* Relaxable. Adjust table header. */
  792. ip2k_get_mem (abfd, misc->contents + irel->r_offset - 4, 2, code);
  793. if (IS_PAGE_OPCODE (code))
  794. addr = irel->r_offset - 16;
  795. else
  796. addr = irel->r_offset - 14;
  797. ip2k_get_mem (abfd, misc->contents + addr, 12, code);
  798. if ((!IS_ADD_W_WREG_OPCODE (code + 0))
  799. || (!IS_SNC_OPCODE (code + 2))
  800. || (!IS_INC_1SP_OPCODE (code + 4))
  801. || (!IS_ADD_2SP_W_OPCODE (code + 6))
  802. || (!IS_SNC_OPCODE (code + 8))
  803. || (!IS_INC_1SP_OPCODE (code + 10)))
  804. {
  805. _bfd_error_handler (_("ip2k relaxer: switch table header corrupt."));
  806. return false;
  807. }
  808. /* Delete first 3 opcodes. */
  809. if (!ip2k_elf_relax_delete_bytes (abfd, sec, addr + 0, 6))
  810. return false;
  811. *again = true;
  812. /* Delete all page instructions in table. */
  813. while (irel < ireltest)
  814. {
  815. if (!ip2k_delete_page_insn (abfd, sec, irel, again, misc))
  816. return false;
  817. irel += 2;
  818. }
  819. return true;
  820. }
  821. /* This function handles relaxation of a section in a specific page. */
  822. static bool
  823. ip2k_elf_relax_section_page (bfd *abfd,
  824. asection *sec,
  825. bool *again,
  826. struct misc *misc,
  827. unsigned long page_start,
  828. unsigned long page_end)
  829. {
  830. Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
  831. Elf_Internal_Rela *irel;
  832. int switch_table_128;
  833. int switch_table_256;
  834. /* Walk thru the section looking for relaxation opportunities. */
  835. for (irel = misc->irelbase; irel < irelend; irel++)
  836. {
  837. if (ELF32_R_TYPE (irel->r_info) != (int) R_IP2K_PAGE3)
  838. /* Ignore non page instructions. */
  839. continue;
  840. if (BASEADDR (sec) + irel->r_offset < page_start)
  841. /* Ignore page instructions on earlier page - they have
  842. already been processed. Remember that there is code flow
  843. that crosses a page boundary. */
  844. continue;
  845. if (BASEADDR (sec) + irel->r_offset > page_end)
  846. /* Flow beyond end of page => nothing more to do for this page. */
  847. return true;
  848. /* Detect switch tables. */
  849. switch_table_128 = ip2k_is_switch_table_128 (abfd, sec, irel->r_offset, misc->contents);
  850. switch_table_256 = ip2k_is_switch_table_256 (abfd, sec, irel->r_offset, misc->contents);
  851. if ((switch_table_128 > 0) || (switch_table_256 > 0))
  852. /* If the index is greater than 0 then it has already been processed. */
  853. continue;
  854. if (switch_table_128 == 0)
  855. {
  856. if (!ip2k_relax_switch_table_128 (abfd, sec, irel, again, misc))
  857. return false;
  858. continue;
  859. }
  860. if (switch_table_256 == 0)
  861. {
  862. if (!ip2k_relax_switch_table_256 (abfd, sec, irel, again, misc))
  863. return false;
  864. continue;
  865. }
  866. /* Simple relax. */
  867. if (ip2k_test_page_insn (abfd, sec, irel, misc))
  868. {
  869. if (!ip2k_delete_page_insn (abfd, sec, irel, again, misc))
  870. return false;
  871. continue;
  872. }
  873. }
  874. return true;
  875. }
  876. /* This function handles relaxing for the ip2k.
  877. Principle: Start with the first page and remove page instructions that
  878. are not require on this first page. By removing page instructions more
  879. code will fit into this page - repeat until nothing more can be achieved
  880. for this page. Move on to the next page.
  881. Processing the pages one at a time from the lowest page allows a removal
  882. only policy to be used - pages can be removed but are never reinserted. */
  883. static bool
  884. ip2k_elf_relax_section (bfd *abfd,
  885. asection *sec,
  886. struct bfd_link_info *link_info,
  887. bool *again)
  888. {
  889. Elf_Internal_Shdr *symtab_hdr;
  890. Elf_Internal_Rela *internal_relocs;
  891. bfd_byte *contents = NULL;
  892. Elf_Internal_Sym *isymbuf = NULL;
  893. static asection * first_section = NULL;
  894. static unsigned long search_addr;
  895. static unsigned long page_start = 0;
  896. static unsigned long page_end = 0;
  897. static unsigned int pass = 0;
  898. static bool new_pass = false;
  899. static bool changed = false;
  900. struct misc misc;
  901. /* Assume nothing changes. */
  902. *again = false;
  903. if (first_section == NULL)
  904. {
  905. ip2k_relaxed = true;
  906. first_section = sec;
  907. }
  908. if (first_section == sec)
  909. {
  910. pass++;
  911. new_pass = true;
  912. }
  913. /* We don't have to do anything for a relocatable link,
  914. if this section does not have relocs, or if this is
  915. not a code section. */
  916. if (bfd_link_relocatable (link_info)
  917. || (sec->flags & SEC_RELOC) == 0
  918. || sec->reloc_count == 0
  919. || (sec->flags & SEC_CODE) == 0)
  920. return true;
  921. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  922. internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
  923. link_info->keep_memory);
  924. if (internal_relocs == NULL)
  925. goto error_return;
  926. /* Get section contents cached copy if it exists. */
  927. if (contents == NULL)
  928. {
  929. /* Get cached copy if it exists. */
  930. if (elf_section_data (sec)->this_hdr.contents != NULL)
  931. contents = elf_section_data (sec)->this_hdr.contents;
  932. else
  933. {
  934. /* Go get them off disk. */
  935. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  936. goto error_return;
  937. }
  938. }
  939. /* Read this BFD's symbols cached copy if it exists. */
  940. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  941. {
  942. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  943. if (isymbuf == NULL)
  944. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  945. symtab_hdr->sh_info, 0,
  946. NULL, NULL, NULL);
  947. if (isymbuf == NULL)
  948. goto error_return;
  949. }
  950. misc.symtab_hdr = symtab_hdr;
  951. misc.isymbuf = isymbuf;
  952. misc.irelbase = internal_relocs;
  953. misc.contents = contents;
  954. /* This is where all the relaxation actually get done. */
  955. if ((pass == 1) || (new_pass && !changed))
  956. {
  957. /* On the first pass we simply search for the lowest page that
  958. we havn't relaxed yet. Note that the pass count is reset
  959. each time a page is complete in order to move on to the next page.
  960. If we can't find any more pages then we are finished. */
  961. if (new_pass)
  962. {
  963. pass = 1;
  964. new_pass = false;
  965. changed = true; /* Pre-initialize to break out of pass 1. */
  966. search_addr = 0xFFFFFFFF;
  967. }
  968. if ((BASEADDR (sec) + sec->size < search_addr)
  969. && (BASEADDR (sec) + sec->size > page_end))
  970. {
  971. if (BASEADDR (sec) <= page_end)
  972. search_addr = page_end + 1;
  973. else
  974. search_addr = BASEADDR (sec);
  975. /* Found a page => more work to do. */
  976. *again = true;
  977. }
  978. }
  979. else
  980. {
  981. if (new_pass)
  982. {
  983. new_pass = false;
  984. changed = false;
  985. page_start = PAGENO (search_addr);
  986. page_end = page_start | 0x00003FFF;
  987. }
  988. /* Only process sections in range. */
  989. if ((BASEADDR (sec) + sec->size >= page_start)
  990. && (BASEADDR (sec) <= page_end))
  991. {
  992. if (!ip2k_elf_relax_section_page (abfd, sec, &changed, &misc, page_start, page_end))
  993. return false;
  994. }
  995. *again = true;
  996. }
  997. /* Perform some house keeping after relaxing the section. */
  998. if (isymbuf != NULL
  999. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1000. {
  1001. if (! link_info->keep_memory)
  1002. free (isymbuf);
  1003. else
  1004. symtab_hdr->contents = (unsigned char *) isymbuf;
  1005. }
  1006. if (contents != NULL
  1007. && elf_section_data (sec)->this_hdr.contents != contents)
  1008. {
  1009. if (! link_info->keep_memory)
  1010. free (contents);
  1011. else
  1012. {
  1013. /* Cache the section contents for elf_link_input_bfd. */
  1014. elf_section_data (sec)->this_hdr.contents = contents;
  1015. }
  1016. }
  1017. if (elf_section_data (sec)->relocs != internal_relocs)
  1018. free (internal_relocs);
  1019. return true;
  1020. error_return:
  1021. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  1022. free (isymbuf);
  1023. if (elf_section_data (sec)->this_hdr.contents != contents)
  1024. free (contents);
  1025. if (elf_section_data (sec)->relocs != internal_relocs)
  1026. free (internal_relocs);
  1027. return false;
  1028. }
  1029. /* Set the howto pointer for a IP2K ELF reloc. */
  1030. static bool
  1031. ip2k_info_to_howto_rela (bfd * abfd,
  1032. arelent * cache_ptr,
  1033. Elf_Internal_Rela * dst)
  1034. {
  1035. unsigned int r_type;
  1036. r_type = ELF32_R_TYPE (dst->r_info);
  1037. if (r_type >= (unsigned int) R_IP2K_max)
  1038. {
  1039. /* xgettext:c-format */
  1040. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  1041. abfd, r_type);
  1042. bfd_set_error (bfd_error_bad_value);
  1043. return false;
  1044. }
  1045. cache_ptr->howto = & ip2k_elf_howto_table [r_type];
  1046. return true;
  1047. }
  1048. /* Perform a single relocation.
  1049. By default we use the standard BFD routines. */
  1050. static bfd_reloc_status_type
  1051. ip2k_final_link_relocate (reloc_howto_type * howto,
  1052. bfd * input_bfd,
  1053. asection * input_section,
  1054. bfd_byte * contents,
  1055. Elf_Internal_Rela * rel,
  1056. bfd_vma relocation)
  1057. {
  1058. static bfd_vma page_addr = 0;
  1059. bfd_reloc_status_type r = bfd_reloc_ok;
  1060. switch (howto->type)
  1061. {
  1062. /* Handle data space relocations. */
  1063. case R_IP2K_FR9:
  1064. case R_IP2K_BANK:
  1065. if ((relocation & IP2K_DATA_MASK) == IP2K_DATA_VALUE)
  1066. relocation &= ~IP2K_DATA_MASK;
  1067. else
  1068. r = bfd_reloc_notsupported;
  1069. break;
  1070. case R_IP2K_LO8DATA:
  1071. case R_IP2K_HI8DATA:
  1072. case R_IP2K_EX8DATA:
  1073. break;
  1074. /* Handle insn space relocations. */
  1075. case R_IP2K_PAGE3:
  1076. page_addr = BASEADDR (input_section) + rel->r_offset;
  1077. if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
  1078. relocation &= ~IP2K_INSN_MASK;
  1079. else
  1080. r = bfd_reloc_notsupported;
  1081. break;
  1082. case R_IP2K_ADDR16CJP:
  1083. if (BASEADDR (input_section) + rel->r_offset != page_addr + 2)
  1084. {
  1085. /* No preceding page instruction, verify that it isn't needed. */
  1086. if (PAGENO (relocation + rel->r_addend) !=
  1087. ip2k_nominal_page_bits (input_bfd, input_section,
  1088. rel->r_offset, contents))
  1089. /* xgettext:c-format */
  1090. _bfd_error_handler
  1091. (_("ip2k linker: missing page instruction "
  1092. "at %#" PRIx64 " (dest = %#" PRIx64 ")"),
  1093. (uint64_t) (BASEADDR (input_section) + rel->r_offset),
  1094. (uint64_t) (relocation + rel->r_addend));
  1095. }
  1096. else if (ip2k_relaxed)
  1097. {
  1098. /* Preceding page instruction. Verify that the page instruction is
  1099. really needed. One reason for the relaxation to miss a page is if
  1100. the section is not marked as executable. */
  1101. if (!ip2k_is_switch_table_128 (input_bfd, input_section,
  1102. rel->r_offset - 2, contents)
  1103. && !ip2k_is_switch_table_256 (input_bfd, input_section,
  1104. rel->r_offset - 2, contents)
  1105. && (PAGENO (relocation + rel->r_addend) ==
  1106. ip2k_nominal_page_bits (input_bfd, input_section,
  1107. rel->r_offset - 2, contents)))
  1108. /* xgettext:c-format */
  1109. _bfd_error_handler
  1110. (_("ip2k linker: redundant page instruction "
  1111. "at %#" PRIx64 " (dest = %#" PRIx64 ")"),
  1112. (uint64_t) page_addr,
  1113. (uint64_t) (relocation + rel->r_addend));
  1114. }
  1115. if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
  1116. relocation &= ~IP2K_INSN_MASK;
  1117. else
  1118. r = bfd_reloc_notsupported;
  1119. break;
  1120. case R_IP2K_LO8INSN:
  1121. case R_IP2K_HI8INSN:
  1122. case R_IP2K_PC_SKIP:
  1123. if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
  1124. relocation &= ~IP2K_INSN_MASK;
  1125. else
  1126. r = bfd_reloc_notsupported;
  1127. break;
  1128. case R_IP2K_16:
  1129. /* If this is a relocation involving a TEXT
  1130. symbol, reduce it to a word address. */
  1131. if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
  1132. howto = &ip2k_elf_howto_table[ (int) R_IP2K_TEXT];
  1133. break;
  1134. /* Pass others through. */
  1135. default:
  1136. break;
  1137. }
  1138. /* Only install relocation if above tests did not disqualify it. */
  1139. if (r == bfd_reloc_ok)
  1140. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1141. contents, rel->r_offset,
  1142. relocation, rel->r_addend);
  1143. return r;
  1144. }
  1145. /* Relocate a IP2K ELF section.
  1146. The RELOCATE_SECTION function is called by the new ELF backend linker
  1147. to handle the relocations for a section.
  1148. The relocs are always passed as Rela structures; if the section
  1149. actually uses Rel structures, the r_addend field will always be
  1150. zero.
  1151. This function is responsible for adjusting the section contents as
  1152. necessary, and (if using Rela relocs and generating a relocatable
  1153. output file) adjusting the reloc addend as necessary.
  1154. This function does not have to worry about setting the reloc
  1155. address or the reloc symbol index.
  1156. LOCAL_SYMS is a pointer to the swapped in local symbols.
  1157. LOCAL_SECTIONS is an array giving the section in the input file
  1158. corresponding to the st_shndx field of each local symbol.
  1159. The global hash table entry for the global symbols can be found
  1160. via elf_sym_hashes (input_bfd).
  1161. When generating relocatable output, this function must handle
  1162. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  1163. going to be the section symbol corresponding to the output
  1164. section, which means that the addend must be adjusted
  1165. accordingly. */
  1166. static int
  1167. ip2k_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
  1168. struct bfd_link_info *info,
  1169. bfd *input_bfd,
  1170. asection *input_section,
  1171. bfd_byte *contents,
  1172. Elf_Internal_Rela *relocs,
  1173. Elf_Internal_Sym *local_syms,
  1174. asection **local_sections)
  1175. {
  1176. Elf_Internal_Shdr *symtab_hdr;
  1177. struct elf_link_hash_entry **sym_hashes;
  1178. Elf_Internal_Rela *rel;
  1179. Elf_Internal_Rela *relend;
  1180. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  1181. sym_hashes = elf_sym_hashes (input_bfd);
  1182. relend = relocs + input_section->reloc_count;
  1183. for (rel = relocs; rel < relend; rel ++)
  1184. {
  1185. reloc_howto_type * howto;
  1186. unsigned long r_symndx;
  1187. Elf_Internal_Sym * sym;
  1188. asection * sec;
  1189. struct elf_link_hash_entry * h;
  1190. bfd_vma relocation;
  1191. bfd_reloc_status_type r;
  1192. const char * name = NULL;
  1193. int r_type;
  1194. r_type = ELF32_R_TYPE (rel->r_info);
  1195. r_symndx = ELF32_R_SYM (rel->r_info);
  1196. howto = ip2k_elf_howto_table + r_type;
  1197. h = NULL;
  1198. sym = NULL;
  1199. sec = NULL;
  1200. if (r_symndx < symtab_hdr->sh_info)
  1201. {
  1202. sym = local_syms + r_symndx;
  1203. sec = local_sections [r_symndx];
  1204. relocation = BASEADDR (sec) + sym->st_value;
  1205. name = bfd_elf_string_from_elf_section
  1206. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  1207. name = name == NULL ? bfd_section_name (sec) : name;
  1208. }
  1209. else
  1210. {
  1211. bool warned, ignored;
  1212. bool unresolved_reloc;
  1213. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1214. r_symndx, symtab_hdr, sym_hashes,
  1215. h, sec, relocation,
  1216. unresolved_reloc, warned, ignored);
  1217. name = h->root.root.string;
  1218. }
  1219. if (sec != NULL && discarded_section (sec))
  1220. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  1221. rel, 1, relend, howto, 0, contents);
  1222. if (bfd_link_relocatable (info))
  1223. continue;
  1224. /* Finally, the sole IP2K-specific part. */
  1225. r = ip2k_final_link_relocate (howto, input_bfd, input_section,
  1226. contents, rel, relocation);
  1227. if (r != bfd_reloc_ok)
  1228. {
  1229. const char * msg = NULL;
  1230. switch (r)
  1231. {
  1232. case bfd_reloc_overflow:
  1233. (*info->callbacks->reloc_overflow)
  1234. (info, (h ? &h->root : NULL), name, howto->name,
  1235. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  1236. break;
  1237. case bfd_reloc_undefined:
  1238. (*info->callbacks->undefined_symbol)
  1239. (info, name, input_bfd, input_section, rel->r_offset, true);
  1240. break;
  1241. case bfd_reloc_outofrange:
  1242. msg = _("internal error: out of range error");
  1243. break;
  1244. /* This is how ip2k_final_link_relocate tells us of a non-kosher
  1245. reference between insn & data address spaces. */
  1246. case bfd_reloc_notsupported:
  1247. if (sym != NULL) /* Only if it's not an unresolved symbol. */
  1248. msg = _("unsupported relocation between data/insn address spaces");
  1249. break;
  1250. case bfd_reloc_dangerous:
  1251. msg = _("internal error: dangerous relocation");
  1252. break;
  1253. default:
  1254. msg = _("internal error: unknown error");
  1255. break;
  1256. }
  1257. if (msg)
  1258. (*info->callbacks->warning) (info, msg, name, input_bfd,
  1259. input_section, rel->r_offset);
  1260. }
  1261. }
  1262. return true;
  1263. }
  1264. #define TARGET_BIG_SYM ip2k_elf32_vec
  1265. #define TARGET_BIG_NAME "elf32-ip2k"
  1266. #define ELF_ARCH bfd_arch_ip2k
  1267. #define ELF_MACHINE_CODE EM_IP2K
  1268. #define ELF_MACHINE_ALT1 EM_IP2K_OLD
  1269. #define ELF_MAXPAGESIZE 1 /* No pages on the IP2K. */
  1270. #define elf_info_to_howto_rel NULL
  1271. #define elf_info_to_howto ip2k_info_to_howto_rela
  1272. #define elf_backend_can_gc_sections 1
  1273. #define elf_backend_rela_normal 1
  1274. #define elf_backend_relocate_section ip2k_elf_relocate_section
  1275. #define elf_symbol_leading_char '_'
  1276. #define bfd_elf32_bfd_reloc_type_lookup ip2k_reloc_type_lookup
  1277. #define bfd_elf32_bfd_reloc_name_lookup ip2k_reloc_name_lookup
  1278. #define bfd_elf32_bfd_relax_section ip2k_elf_relax_section
  1279. #include "elf32-target.h"