elf32-msp430.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  1. /* MSP430-specific support for 32-bit ELF
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Dmitry Diky <diwil@mail.ru>
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libiberty.h"
  20. #include "libbfd.h"
  21. #include "elf-bfd.h"
  22. #include "elf/msp430.h"
  23. static bfd_reloc_status_type
  24. rl78_sym_diff_handler (bfd * abfd,
  25. arelent * reloc,
  26. asymbol * sym ATTRIBUTE_UNUSED,
  27. void * addr ATTRIBUTE_UNUSED,
  28. asection * input_sec,
  29. bfd * out_bfd ATTRIBUTE_UNUSED,
  30. char ** error_message ATTRIBUTE_UNUSED)
  31. {
  32. bfd_size_type octets;
  33. octets = reloc->address * bfd_octets_per_byte (abfd);
  34. /* Catch the case where bfd_install_relocation would return
  35. bfd_reloc_outofrange because the SYM_DIFF reloc is being used in a very
  36. small section. It does not actually matter if this happens because all
  37. that SYM_DIFF does is compute a (4-byte) value. A second reloc then uses
  38. this value, and it is that reloc that must fit into the section.
  39. This happens in eg, gcc/testsuite/gcc.c-torture/compile/labels-3.c. */
  40. if ((octets + bfd_get_reloc_size (reloc->howto))
  41. > bfd_get_section_limit_octets (abfd, input_sec))
  42. return bfd_reloc_ok;
  43. return bfd_reloc_continue;
  44. }
  45. static reloc_howto_type elf_msp430_howto_table[] =
  46. {
  47. HOWTO (R_MSP430_NONE, /* type */
  48. 0, /* rightshift */
  49. 3, /* size (0 = byte, 1 = short, 2 = long) */
  50. 0, /* bitsize */
  51. FALSE, /* pc_relative */
  52. 0, /* bitpos */
  53. complain_overflow_dont,/* complain_on_overflow */
  54. bfd_elf_generic_reloc, /* special_function */
  55. "R_MSP430_NONE", /* name */
  56. FALSE, /* partial_inplace */
  57. 0, /* src_mask */
  58. 0, /* dst_mask */
  59. FALSE), /* pcrel_offset */
  60. HOWTO (R_MSP430_32, /* type */
  61. 0, /* rightshift */
  62. 2, /* size (0 = byte, 1 = short, 2 = long) */
  63. 32, /* bitsize */
  64. FALSE, /* pc_relative */
  65. 0, /* bitpos */
  66. complain_overflow_bitfield,/* complain_on_overflow */
  67. bfd_elf_generic_reloc, /* special_function */
  68. "R_MSP430_32", /* name */
  69. FALSE, /* partial_inplace */
  70. 0xffffffff, /* src_mask */
  71. 0xffffffff, /* dst_mask */
  72. FALSE), /* pcrel_offset */
  73. /* A 10 bit PC relative relocation. */
  74. HOWTO (R_MSP430_10_PCREL, /* type */
  75. 1, /* rightshift */
  76. 1, /* size (0 = byte, 1 = short, 2 = long) */
  77. 10, /* bitsize */
  78. TRUE, /* pc_relative */
  79. 0, /* bitpos */
  80. complain_overflow_bitfield,/* complain_on_overflow */
  81. bfd_elf_generic_reloc, /* special_function */
  82. "R_MSP430_10_PCREL", /* name */
  83. FALSE, /* partial_inplace */
  84. 0x3ff, /* src_mask */
  85. 0x3ff, /* dst_mask */
  86. TRUE), /* pcrel_offset */
  87. /* A 16 bit absolute relocation. */
  88. HOWTO (R_MSP430_16, /* type */
  89. 0, /* rightshift */
  90. 1, /* size (0 = byte, 1 = short, 2 = long) */
  91. 16, /* bitsize */
  92. FALSE, /* pc_relative */
  93. 0, /* bitpos */
  94. complain_overflow_dont,/* complain_on_overflow */
  95. bfd_elf_generic_reloc, /* special_function */
  96. "R_MSP430_16", /* name */
  97. FALSE, /* partial_inplace */
  98. 0, /* src_mask */
  99. 0xffff, /* dst_mask */
  100. FALSE), /* pcrel_offset */
  101. /* A 16 bit PC relative relocation for command address. */
  102. HOWTO (R_MSP430_16_PCREL, /* type */
  103. 1, /* rightshift */
  104. 1, /* size (0 = byte, 1 = short, 2 = long) */
  105. 16, /* bitsize */
  106. TRUE, /* pc_relative */
  107. 0, /* bitpos */
  108. complain_overflow_dont,/* complain_on_overflow */
  109. bfd_elf_generic_reloc, /* special_function */
  110. "R_MSP430_16_PCREL", /* name */
  111. FALSE, /* partial_inplace */
  112. 0, /* src_mask */
  113. 0xffff, /* dst_mask */
  114. TRUE), /* pcrel_offset */
  115. /* A 16 bit absolute relocation, byte operations. */
  116. HOWTO (R_MSP430_16_BYTE, /* type */
  117. 0, /* rightshift */
  118. 1, /* size (0 = byte, 1 = short, 2 = long) */
  119. 16, /* bitsize */
  120. FALSE, /* pc_relative */
  121. 0, /* bitpos */
  122. complain_overflow_dont,/* complain_on_overflow */
  123. bfd_elf_generic_reloc, /* special_function */
  124. "R_MSP430_16_BYTE", /* name */
  125. FALSE, /* partial_inplace */
  126. 0xffff, /* src_mask */
  127. 0xffff, /* dst_mask */
  128. FALSE), /* pcrel_offset */
  129. /* A 16 bit absolute relocation for command address. */
  130. HOWTO (R_MSP430_16_PCREL_BYTE,/* type */
  131. 1, /* rightshift */
  132. 1, /* size (0 = byte, 1 = short, 2 = long) */
  133. 16, /* bitsize */
  134. TRUE, /* pc_relative */
  135. 0, /* bitpos */
  136. complain_overflow_dont,/* complain_on_overflow */
  137. bfd_elf_generic_reloc, /* special_function */
  138. "R_MSP430_16_PCREL_BYTE",/* name */
  139. FALSE, /* partial_inplace */
  140. 0xffff, /* src_mask */
  141. 0xffff, /* dst_mask */
  142. TRUE), /* pcrel_offset */
  143. /* A 10 bit PC relative relocation for complicated polymorphs. */
  144. HOWTO (R_MSP430_2X_PCREL, /* type */
  145. 1, /* rightshift */
  146. 2, /* size (0 = byte, 1 = short, 2 = long) */
  147. 10, /* bitsize */
  148. TRUE, /* pc_relative */
  149. 0, /* bitpos */
  150. complain_overflow_bitfield,/* complain_on_overflow */
  151. bfd_elf_generic_reloc, /* special_function */
  152. "R_MSP430_2X_PCREL", /* name */
  153. FALSE, /* partial_inplace */
  154. 0x3ff, /* src_mask */
  155. 0x3ff, /* dst_mask */
  156. TRUE), /* pcrel_offset */
  157. /* A 16 bit relaxable relocation for command address. */
  158. HOWTO (R_MSP430_RL_PCREL, /* type */
  159. 1, /* rightshift */
  160. 1, /* size (0 = byte, 1 = short, 2 = long) */
  161. 16, /* bitsize */
  162. TRUE, /* pc_relative */
  163. 0, /* bitpos */
  164. complain_overflow_dont,/* complain_on_overflow */
  165. bfd_elf_generic_reloc, /* special_function */
  166. "R_MSP430_RL_PCREL", /* name */
  167. FALSE, /* partial_inplace */
  168. 0, /* src_mask */
  169. 0xffff, /* dst_mask */
  170. TRUE) /* pcrel_offset */
  171. /* A 8-bit absolute relocation. */
  172. , HOWTO (R_MSP430_8, /* type */
  173. 0, /* rightshift */
  174. 0, /* size (0 = byte, 1 = short, 2 = long) */
  175. 8, /* bitsize */
  176. FALSE, /* pc_relative */
  177. 0, /* bitpos */
  178. complain_overflow_dont,/* complain_on_overflow */
  179. bfd_elf_generic_reloc, /* special_function */
  180. "R_MSP430_8", /* name */
  181. FALSE, /* partial_inplace */
  182. 0, /* src_mask */
  183. 0xffff, /* dst_mask */
  184. FALSE), /* pcrel_offset */
  185. /* Together with a following reloc, allows for the difference
  186. between two symbols to be the real addend of the second reloc. */
  187. HOWTO (R_MSP430_SYM_DIFF, /* type */
  188. 0, /* rightshift */
  189. 2, /* size (0 = byte, 1 = short, 2 = long) */
  190. 32, /* bitsize */
  191. FALSE, /* pc_relative */
  192. 0, /* bitpos */
  193. complain_overflow_dont,/* complain_on_overflow */
  194. rl78_sym_diff_handler, /* special handler. */
  195. "R_MSP430_SYM_DIFF", /* name */
  196. FALSE, /* partial_inplace */
  197. 0xffffffff, /* src_mask */
  198. 0xffffffff, /* dst_mask */
  199. FALSE) /* pcrel_offset */
  200. };
  201. static reloc_howto_type elf_msp430x_howto_table[] =
  202. {
  203. HOWTO (R_MSP430_NONE, /* type */
  204. 0, /* rightshift */
  205. 3, /* size (0 = byte, 1 = short, 2 = long) */
  206. 0, /* bitsize */
  207. FALSE, /* pc_relative */
  208. 0, /* bitpos */
  209. complain_overflow_dont,/* complain_on_overflow */
  210. bfd_elf_generic_reloc, /* special_function */
  211. "R_MSP430_NONE", /* name */
  212. FALSE, /* partial_inplace */
  213. 0, /* src_mask */
  214. 0, /* dst_mask */
  215. FALSE), /* pcrel_offset */
  216. HOWTO (R_MSP430_ABS32, /* type */
  217. 0, /* rightshift */
  218. 2, /* size (0 = byte, 1 = short, 2 = long) */
  219. 32, /* bitsize */
  220. FALSE, /* pc_relative */
  221. 0, /* bitpos */
  222. complain_overflow_bitfield,/* complain_on_overflow */
  223. bfd_elf_generic_reloc, /* special_function */
  224. "R_MSP430_ABS32", /* name */
  225. FALSE, /* partial_inplace */
  226. 0xffffffff, /* src_mask */
  227. 0xffffffff, /* dst_mask */
  228. FALSE), /* pcrel_offset */
  229. HOWTO (R_MSP430_ABS16, /* type */
  230. 0, /* rightshift */
  231. 1, /* size (0 = byte, 1 = short, 2 = long) */
  232. 16, /* bitsize */
  233. FALSE, /* pc_relative */
  234. 0, /* bitpos */
  235. complain_overflow_dont,/* complain_on_overflow */
  236. bfd_elf_generic_reloc, /* special_function */
  237. "R_MSP430_ABS16", /* name */
  238. FALSE, /* partial_inplace */
  239. 0, /* src_mask */
  240. 0xffff, /* dst_mask */
  241. FALSE), /* pcrel_offset */
  242. HOWTO (R_MSP430_ABS8, /* type */
  243. 0, /* rightshift */
  244. 0, /* size (0 = byte, 1 = short, 2 = long) */
  245. 8, /* bitsize */
  246. FALSE, /* pc_relative */
  247. 0, /* bitpos */
  248. complain_overflow_bitfield,/* complain_on_overflow */
  249. bfd_elf_generic_reloc, /* special_function */
  250. "R_MSP430_ABS8", /* name */
  251. FALSE, /* partial_inplace */
  252. 0xff, /* src_mask */
  253. 0xff, /* dst_mask */
  254. FALSE), /* pcrel_offset */
  255. HOWTO (R_MSP430_PCR16, /* type */
  256. 1, /* rightshift */
  257. 1, /* size (0 = byte, 1 = short, 2 = long) */
  258. 16, /* bitsize */
  259. TRUE, /* pc_relative */
  260. 0, /* bitpos */
  261. complain_overflow_dont,/* complain_on_overflow */
  262. bfd_elf_generic_reloc, /* special_function */
  263. "R_MSP430_PCR16", /* name */
  264. FALSE, /* partial_inplace */
  265. 0, /* src_mask */
  266. 0xffff, /* dst_mask */
  267. TRUE), /* pcrel_offset */
  268. HOWTO (R_MSP430X_PCR20_EXT_SRC,/* type */
  269. 0, /* rightshift */
  270. 2, /* size (0 = byte, 1 = short, 2 = long) */
  271. 32, /* bitsize */
  272. TRUE, /* pc_relative */
  273. 0, /* bitpos */
  274. complain_overflow_dont,/* complain_on_overflow */
  275. bfd_elf_generic_reloc, /* special_function */
  276. "R_MSP430X_PCR20_EXT_SRC",/* name */
  277. FALSE, /* partial_inplace */
  278. 0, /* src_mask */
  279. 0xffff, /* dst_mask */
  280. TRUE), /* pcrel_offset */
  281. HOWTO (R_MSP430X_PCR20_EXT_DST,/* type */
  282. 0, /* rightshift */
  283. 2, /* size (0 = byte, 1 = short, 2 = long) */
  284. 32, /* bitsize */
  285. TRUE, /* pc_relative */
  286. 0, /* bitpos */
  287. complain_overflow_dont,/* complain_on_overflow */
  288. bfd_elf_generic_reloc, /* special_function */
  289. "R_MSP430X_PCR20_EXT_DST",/* name */
  290. FALSE, /* partial_inplace */
  291. 0, /* src_mask */
  292. 0xffff, /* dst_mask */
  293. TRUE), /* pcrel_offset */
  294. HOWTO (R_MSP430X_PCR20_EXT_ODST,/* type */
  295. 0, /* rightshift */
  296. 2, /* size (0 = byte, 1 = short, 2 = long) */
  297. 32, /* bitsize */
  298. TRUE, /* pc_relative */
  299. 0, /* bitpos */
  300. complain_overflow_dont,/* complain_on_overflow */
  301. bfd_elf_generic_reloc, /* special_function */
  302. "R_MSP430X_PCR20_EXT_ODST",/* name */
  303. FALSE, /* partial_inplace */
  304. 0, /* src_mask */
  305. 0xffff, /* dst_mask */
  306. TRUE), /* pcrel_offset */
  307. HOWTO (R_MSP430X_ABS20_EXT_SRC,/* type */
  308. 0, /* rightshift */
  309. 2, /* size (0 = byte, 1 = short, 2 = long) */
  310. 32, /* bitsize */
  311. TRUE, /* pc_relative */
  312. 0, /* bitpos */
  313. complain_overflow_dont,/* complain_on_overflow */
  314. bfd_elf_generic_reloc, /* special_function */
  315. "R_MSP430X_ABS20_EXT_SRC",/* name */
  316. FALSE, /* partial_inplace */
  317. 0, /* src_mask */
  318. 0xffff, /* dst_mask */
  319. TRUE), /* pcrel_offset */
  320. HOWTO (R_MSP430X_ABS20_EXT_DST,/* type */
  321. 0, /* rightshift */
  322. 2, /* size (0 = byte, 1 = short, 2 = long) */
  323. 32, /* bitsize */
  324. TRUE, /* pc_relative */
  325. 0, /* bitpos */
  326. complain_overflow_dont,/* complain_on_overflow */
  327. bfd_elf_generic_reloc, /* special_function */
  328. "R_MSP430X_ABS20_EXT_DST",/* name */
  329. FALSE, /* partial_inplace */
  330. 0, /* src_mask */
  331. 0xffff, /* dst_mask */
  332. TRUE), /* pcrel_offset */
  333. HOWTO (R_MSP430X_ABS20_EXT_ODST,/* type */
  334. 0, /* rightshift */
  335. 2, /* size (0 = byte, 1 = short, 2 = long) */
  336. 32, /* bitsize */
  337. TRUE, /* pc_relative */
  338. 0, /* bitpos */
  339. complain_overflow_dont,/* complain_on_overflow */
  340. bfd_elf_generic_reloc, /* special_function */
  341. "R_MSP430X_ABS20_EXT_ODST",/* name */
  342. FALSE, /* partial_inplace */
  343. 0, /* src_mask */
  344. 0xffff, /* dst_mask */
  345. TRUE), /* pcrel_offset */
  346. HOWTO (R_MSP430X_ABS20_ADR_SRC,/* type */
  347. 0, /* rightshift */
  348. 2, /* size (0 = byte, 1 = short, 2 = long) */
  349. 32, /* bitsize */
  350. TRUE, /* pc_relative */
  351. 0, /* bitpos */
  352. complain_overflow_dont,/* complain_on_overflow */
  353. bfd_elf_generic_reloc, /* special_function */
  354. "R_MSP430X_ABS20_ADR_SRC",/* name */
  355. FALSE, /* partial_inplace */
  356. 0, /* src_mask */
  357. 0xffff, /* dst_mask */
  358. TRUE), /* pcrel_offset */
  359. HOWTO (R_MSP430X_ABS20_ADR_DST,/* type */
  360. 0, /* rightshift */
  361. 2, /* size (0 = byte, 1 = short, 2 = long) */
  362. 32, /* bitsize */
  363. TRUE, /* pc_relative */
  364. 0, /* bitpos */
  365. complain_overflow_dont,/* complain_on_overflow */
  366. bfd_elf_generic_reloc, /* special_function */
  367. "R_MSP430X_ABS20_ADR_DST",/* name */
  368. FALSE, /* partial_inplace */
  369. 0, /* src_mask */
  370. 0xffff, /* dst_mask */
  371. TRUE), /* pcrel_offset */
  372. HOWTO (R_MSP430X_PCR16, /* type */
  373. 0, /* rightshift */
  374. 2, /* size (0 = byte, 1 = short, 2 = long) */
  375. 32, /* bitsize */
  376. TRUE, /* pc_relative */
  377. 0, /* bitpos */
  378. complain_overflow_dont,/* complain_on_overflow */
  379. bfd_elf_generic_reloc, /* special_function */
  380. "R_MSP430X_PCR16", /* name */
  381. FALSE, /* partial_inplace */
  382. 0, /* src_mask */
  383. 0xffff, /* dst_mask */
  384. TRUE), /* pcrel_offset */
  385. HOWTO (R_MSP430X_PCR20_CALL, /* type */
  386. 0, /* rightshift */
  387. 2, /* size (0 = byte, 1 = short, 2 = long) */
  388. 32, /* bitsize */
  389. TRUE, /* pc_relative */
  390. 0, /* bitpos */
  391. complain_overflow_dont,/* complain_on_overflow */
  392. bfd_elf_generic_reloc, /* special_function */
  393. "R_MSP430X_PCR20_CALL",/* name */
  394. FALSE, /* partial_inplace */
  395. 0, /* src_mask */
  396. 0xffff, /* dst_mask */
  397. TRUE), /* pcrel_offset */
  398. HOWTO (R_MSP430X_ABS16, /* type */
  399. 0, /* rightshift */
  400. 2, /* size (0 = byte, 1 = short, 2 = long) */
  401. 32, /* bitsize */
  402. TRUE, /* pc_relative */
  403. 0, /* bitpos */
  404. complain_overflow_dont,/* complain_on_overflow */
  405. bfd_elf_generic_reloc, /* special_function */
  406. "R_MSP430X_ABS16", /* name */
  407. FALSE, /* partial_inplace */
  408. 0, /* src_mask */
  409. 0xffff, /* dst_mask */
  410. TRUE), /* pcrel_offset */
  411. HOWTO (R_MSP430_ABS_HI16, /* type */
  412. 0, /* rightshift */
  413. 2, /* size (0 = byte, 1 = short, 2 = long) */
  414. 32, /* bitsize */
  415. TRUE, /* pc_relative */
  416. 0, /* bitpos */
  417. complain_overflow_dont,/* complain_on_overflow */
  418. bfd_elf_generic_reloc, /* special_function */
  419. "R_MSP430_ABS_HI16", /* name */
  420. FALSE, /* partial_inplace */
  421. 0, /* src_mask */
  422. 0xffff, /* dst_mask */
  423. TRUE), /* pcrel_offset */
  424. HOWTO (R_MSP430_PREL31, /* type */
  425. 0, /* rightshift */
  426. 2, /* size (0 = byte, 1 = short, 2 = long) */
  427. 32, /* bitsize */
  428. TRUE, /* pc_relative */
  429. 0, /* bitpos */
  430. complain_overflow_dont,/* complain_on_overflow */
  431. bfd_elf_generic_reloc, /* special_function */
  432. "R_MSP430_PREL31", /* name */
  433. FALSE, /* partial_inplace */
  434. 0, /* src_mask */
  435. 0xffff, /* dst_mask */
  436. TRUE), /* pcrel_offset */
  437. EMPTY_HOWTO (R_MSP430_EHTYPE),
  438. /* A 10 bit PC relative relocation. */
  439. HOWTO (R_MSP430X_10_PCREL, /* type */
  440. 1, /* rightshift */
  441. 1, /* size (0 = byte, 1 = short, 2 = long) */
  442. 10, /* bitsize */
  443. TRUE, /* pc_relative */
  444. 0, /* bitpos */
  445. complain_overflow_bitfield,/* complain_on_overflow */
  446. bfd_elf_generic_reloc, /* special_function */
  447. "R_MSP430X_10_PCREL", /* name */
  448. FALSE, /* partial_inplace */
  449. 0x3ff, /* src_mask */
  450. 0x3ff, /* dst_mask */
  451. TRUE), /* pcrel_offset */
  452. /* A 10 bit PC relative relocation for complicated polymorphs. */
  453. HOWTO (R_MSP430X_2X_PCREL, /* type */
  454. 1, /* rightshift */
  455. 2, /* size (0 = byte, 1 = short, 2 = long) */
  456. 10, /* bitsize */
  457. TRUE, /* pc_relative */
  458. 0, /* bitpos */
  459. complain_overflow_bitfield,/* complain_on_overflow */
  460. bfd_elf_generic_reloc, /* special_function */
  461. "R_MSP430X_2X_PCREL", /* name */
  462. FALSE, /* partial_inplace */
  463. 0x3ff, /* src_mask */
  464. 0x3ff, /* dst_mask */
  465. TRUE), /* pcrel_offset */
  466. /* Together with a following reloc, allows for the difference
  467. between two symbols to be the real addend of the second reloc. */
  468. HOWTO (R_MSP430X_SYM_DIFF, /* type */
  469. 0, /* rightshift */
  470. 2, /* size (0 = byte, 1 = short, 2 = long) */
  471. 32, /* bitsize */
  472. FALSE, /* pc_relative */
  473. 0, /* bitpos */
  474. complain_overflow_dont,/* complain_on_overflow */
  475. rl78_sym_diff_handler, /* special handler. */
  476. "R_MSP430X_SYM_DIFF", /* name */
  477. FALSE, /* partial_inplace */
  478. 0xffffffff, /* src_mask */
  479. 0xffffffff, /* dst_mask */
  480. FALSE) /* pcrel_offset */
  481. };
  482. /* Map BFD reloc types to MSP430 ELF reloc types. */
  483. struct msp430_reloc_map
  484. {
  485. bfd_reloc_code_real_type bfd_reloc_val;
  486. unsigned int elf_reloc_val;
  487. };
  488. static const struct msp430_reloc_map msp430_reloc_map[] =
  489. {
  490. {BFD_RELOC_NONE, R_MSP430_NONE},
  491. {BFD_RELOC_32, R_MSP430_32},
  492. {BFD_RELOC_MSP430_10_PCREL, R_MSP430_10_PCREL},
  493. {BFD_RELOC_16, R_MSP430_16_BYTE},
  494. {BFD_RELOC_MSP430_16_PCREL, R_MSP430_16_PCREL},
  495. {BFD_RELOC_MSP430_16, R_MSP430_16},
  496. {BFD_RELOC_MSP430_16_PCREL_BYTE, R_MSP430_16_PCREL_BYTE},
  497. {BFD_RELOC_MSP430_16_BYTE, R_MSP430_16_BYTE},
  498. {BFD_RELOC_MSP430_2X_PCREL, R_MSP430_2X_PCREL},
  499. {BFD_RELOC_MSP430_RL_PCREL, R_MSP430_RL_PCREL},
  500. {BFD_RELOC_8, R_MSP430_8},
  501. {BFD_RELOC_MSP430_SYM_DIFF, R_MSP430_SYM_DIFF}
  502. };
  503. static const struct msp430_reloc_map msp430x_reloc_map[] =
  504. {
  505. {BFD_RELOC_NONE, R_MSP430_NONE},
  506. {BFD_RELOC_32, R_MSP430_ABS32},
  507. {BFD_RELOC_16, R_MSP430_ABS16},
  508. {BFD_RELOC_8, R_MSP430_ABS8},
  509. {BFD_RELOC_MSP430_ABS8, R_MSP430_ABS8},
  510. {BFD_RELOC_MSP430X_PCR20_EXT_SRC, R_MSP430X_PCR20_EXT_SRC},
  511. {BFD_RELOC_MSP430X_PCR20_EXT_DST, R_MSP430X_PCR20_EXT_DST},
  512. {BFD_RELOC_MSP430X_PCR20_EXT_ODST, R_MSP430X_PCR20_EXT_ODST},
  513. {BFD_RELOC_MSP430X_ABS20_EXT_SRC, R_MSP430X_ABS20_EXT_SRC},
  514. {BFD_RELOC_MSP430X_ABS20_EXT_DST, R_MSP430X_ABS20_EXT_DST},
  515. {BFD_RELOC_MSP430X_ABS20_EXT_ODST, R_MSP430X_ABS20_EXT_ODST},
  516. {BFD_RELOC_MSP430X_ABS20_ADR_SRC, R_MSP430X_ABS20_ADR_SRC},
  517. {BFD_RELOC_MSP430X_ABS20_ADR_DST, R_MSP430X_ABS20_ADR_DST},
  518. {BFD_RELOC_MSP430X_PCR16, R_MSP430X_PCR16},
  519. {BFD_RELOC_MSP430X_PCR20_CALL, R_MSP430X_PCR20_CALL},
  520. {BFD_RELOC_MSP430X_ABS16, R_MSP430X_ABS16},
  521. {BFD_RELOC_MSP430_ABS_HI16, R_MSP430_ABS_HI16},
  522. {BFD_RELOC_MSP430_PREL31, R_MSP430_PREL31},
  523. {BFD_RELOC_MSP430_10_PCREL, R_MSP430X_10_PCREL},
  524. {BFD_RELOC_MSP430_2X_PCREL, R_MSP430X_2X_PCREL},
  525. {BFD_RELOC_MSP430_RL_PCREL, R_MSP430X_PCR16},
  526. {BFD_RELOC_MSP430_SYM_DIFF, R_MSP430X_SYM_DIFF}
  527. };
  528. static inline bfd_boolean
  529. uses_msp430x_relocs (bfd * abfd)
  530. {
  531. extern const bfd_target msp430_elf32_ti_vec;
  532. return bfd_get_mach (abfd) == bfd_mach_msp430x
  533. || abfd->xvec == & msp430_elf32_ti_vec;
  534. }
  535. static reloc_howto_type *
  536. bfd_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  537. bfd_reloc_code_real_type code)
  538. {
  539. unsigned int i;
  540. if (uses_msp430x_relocs (abfd))
  541. {
  542. for (i = ARRAY_SIZE (msp430x_reloc_map); i--;)
  543. if (msp430x_reloc_map[i].bfd_reloc_val == code)
  544. return elf_msp430x_howto_table + msp430x_reloc_map[i].elf_reloc_val;
  545. }
  546. else
  547. {
  548. for (i = 0; i < ARRAY_SIZE (msp430_reloc_map); i++)
  549. if (msp430_reloc_map[i].bfd_reloc_val == code)
  550. return &elf_msp430_howto_table[msp430_reloc_map[i].elf_reloc_val];
  551. }
  552. return NULL;
  553. }
  554. static reloc_howto_type *
  555. bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  556. const char *r_name)
  557. {
  558. unsigned int i;
  559. if (uses_msp430x_relocs (abfd))
  560. {
  561. for (i = ARRAY_SIZE (elf_msp430x_howto_table); i--;)
  562. if (elf_msp430x_howto_table[i].name != NULL
  563. && strcasecmp (elf_msp430x_howto_table[i].name, r_name) == 0)
  564. return elf_msp430x_howto_table + i;
  565. }
  566. else
  567. {
  568. for (i = 0;
  569. i < (sizeof (elf_msp430_howto_table)
  570. / sizeof (elf_msp430_howto_table[0]));
  571. i++)
  572. if (elf_msp430_howto_table[i].name != NULL
  573. && strcasecmp (elf_msp430_howto_table[i].name, r_name) == 0)
  574. return &elf_msp430_howto_table[i];
  575. }
  576. return NULL;
  577. }
  578. /* Set the howto pointer for an MSP430 ELF reloc. */
  579. static void
  580. msp430_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
  581. arelent * cache_ptr,
  582. Elf_Internal_Rela * dst)
  583. {
  584. unsigned int r_type;
  585. r_type = ELF32_R_TYPE (dst->r_info);
  586. if (uses_msp430x_relocs (abfd))
  587. {
  588. if (r_type >= (unsigned int) R_MSP430x_max)
  589. {
  590. _bfd_error_handler (_("%B: invalid MSP430X reloc number: %d"), abfd, r_type);
  591. r_type = 0;
  592. }
  593. cache_ptr->howto = elf_msp430x_howto_table + r_type;
  594. return;
  595. }
  596. if (r_type >= (unsigned int) R_MSP430_max)
  597. {
  598. _bfd_error_handler (_("%B: invalid MSP430 reloc number: %d"), abfd, r_type);
  599. r_type = 0;
  600. }
  601. cache_ptr->howto = &elf_msp430_howto_table[r_type];
  602. }
  603. /* Look through the relocs for a section during the first phase.
  604. Since we don't do .gots or .plts, we just need to consider the
  605. virtual table relocs for gc. */
  606. static bfd_boolean
  607. elf32_msp430_check_relocs (bfd * abfd, struct bfd_link_info * info,
  608. asection * sec, const Elf_Internal_Rela * relocs)
  609. {
  610. Elf_Internal_Shdr *symtab_hdr;
  611. struct elf_link_hash_entry **sym_hashes;
  612. const Elf_Internal_Rela *rel;
  613. const Elf_Internal_Rela *rel_end;
  614. if (bfd_link_relocatable (info))
  615. return TRUE;
  616. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  617. sym_hashes = elf_sym_hashes (abfd);
  618. rel_end = relocs + sec->reloc_count;
  619. for (rel = relocs; rel < rel_end; rel++)
  620. {
  621. struct elf_link_hash_entry *h;
  622. unsigned long r_symndx;
  623. r_symndx = ELF32_R_SYM (rel->r_info);
  624. if (r_symndx < symtab_hdr->sh_info)
  625. h = NULL;
  626. else
  627. {
  628. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  629. while (h->root.type == bfd_link_hash_indirect
  630. || h->root.type == bfd_link_hash_warning)
  631. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  632. /* PR15323, ref flags aren't set for references in the same
  633. object. */
  634. h->root.non_ir_ref = 1;
  635. }
  636. }
  637. return TRUE;
  638. }
  639. /* Perform a single relocation. By default we use the standard BFD
  640. routines, but a few relocs, we have to do them ourselves. */
  641. static bfd_reloc_status_type
  642. msp430_final_link_relocate (reloc_howto_type * howto,
  643. bfd * input_bfd,
  644. asection * input_section,
  645. bfd_byte * contents,
  646. Elf_Internal_Rela * rel,
  647. bfd_vma relocation,
  648. struct bfd_link_info * info)
  649. {
  650. static asection * sym_diff_section;
  651. static bfd_vma sym_diff_value;
  652. struct bfd_elf_section_data * esd = elf_section_data (input_section);
  653. bfd_reloc_status_type r = bfd_reloc_ok;
  654. bfd_vma x;
  655. bfd_signed_vma srel;
  656. bfd_boolean is_rel_reloc = FALSE;
  657. if (uses_msp430x_relocs (input_bfd))
  658. {
  659. /* See if we have a REL type relocation. */
  660. is_rel_reloc = (esd->rel.hdr != NULL);
  661. /* Sanity check - only one type of relocation per section.
  662. FIXME: Theoretically it is possible to have both types,
  663. but if that happens how can we distinguish between the two ? */
  664. BFD_ASSERT (! is_rel_reloc || ! esd->rela.hdr);
  665. /* If we are using a REL relocation then the addend should be empty. */
  666. BFD_ASSERT (! is_rel_reloc || rel->r_addend == 0);
  667. }
  668. if (sym_diff_section != NULL)
  669. {
  670. BFD_ASSERT (sym_diff_section == input_section);
  671. if (uses_msp430x_relocs (input_bfd))
  672. switch (howto->type)
  673. {
  674. case R_MSP430_ABS32:
  675. /* If we are computing a 32-bit value for the location lists
  676. and the result is 0 then we add one to the value. A zero
  677. value can result because of linker relaxation deleteing
  678. prologue instructions and using a value of 1 (for the begin
  679. and end offsets in the location list entry) results in a
  680. nul entry which does not prevent the following entries from
  681. being parsed. */
  682. if (relocation == sym_diff_value
  683. && strcmp (input_section->name, ".debug_loc") == 0)
  684. ++ relocation;
  685. /* Fall through. */
  686. case R_MSP430_ABS16:
  687. case R_MSP430X_ABS16:
  688. case R_MSP430_ABS8:
  689. BFD_ASSERT (! is_rel_reloc);
  690. relocation -= sym_diff_value;
  691. break;
  692. default:
  693. return bfd_reloc_dangerous;
  694. }
  695. else
  696. switch (howto->type)
  697. {
  698. case R_MSP430_32:
  699. case R_MSP430_16:
  700. case R_MSP430_16_BYTE:
  701. case R_MSP430_8:
  702. relocation -= sym_diff_value;
  703. break;
  704. default:
  705. return bfd_reloc_dangerous;
  706. }
  707. sym_diff_section = NULL;
  708. }
  709. if (uses_msp430x_relocs (input_bfd))
  710. switch (howto->type)
  711. {
  712. case R_MSP430X_SYM_DIFF:
  713. /* Cache the input section and value.
  714. The offset is unreliable, since relaxation may
  715. have reduced the following reloc's offset. */
  716. BFD_ASSERT (! is_rel_reloc);
  717. sym_diff_section = input_section;
  718. sym_diff_value = relocation;
  719. return bfd_reloc_ok;
  720. case R_MSP430_ABS16:
  721. contents += rel->r_offset;
  722. srel = (bfd_signed_vma) relocation;
  723. if (is_rel_reloc)
  724. srel += bfd_get_16 (input_bfd, contents);
  725. else
  726. srel += rel->r_addend;
  727. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  728. break;
  729. case R_MSP430X_10_PCREL:
  730. contents += rel->r_offset;
  731. srel = (bfd_signed_vma) relocation;
  732. if (is_rel_reloc)
  733. srel += bfd_get_16 (input_bfd, contents) & 0x3ff;
  734. else
  735. srel += rel->r_addend;
  736. srel -= rel->r_offset;
  737. srel -= 2; /* Branch instructions add 2 to the PC... */
  738. srel -= (input_section->output_section->vma +
  739. input_section->output_offset);
  740. if (srel & 1)
  741. return bfd_reloc_outofrange;
  742. /* MSP430 addresses commands as words. */
  743. srel >>= 1;
  744. /* Check for an overflow. */
  745. if (srel < -512 || srel > 511)
  746. {
  747. if (info->disable_target_specific_optimizations < 0)
  748. {
  749. static bfd_boolean warned = FALSE;
  750. if (! warned)
  751. {
  752. info->callbacks->warning
  753. (info,
  754. _("Try enabling relaxation to avoid relocation truncations"),
  755. NULL, input_bfd, input_section, relocation);
  756. warned = TRUE;
  757. }
  758. }
  759. return bfd_reloc_overflow;
  760. }
  761. x = bfd_get_16 (input_bfd, contents);
  762. x = (x & 0xfc00) | (srel & 0x3ff);
  763. bfd_put_16 (input_bfd, x, contents);
  764. break;
  765. case R_MSP430X_PCR20_EXT_ODST:
  766. /* [0,4]+[48,16] = ---F ---- ---- FFFF */
  767. contents += rel->r_offset;
  768. srel = (bfd_signed_vma) relocation;
  769. if (is_rel_reloc)
  770. {
  771. bfd_vma addend;
  772. addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
  773. addend |= bfd_get_16 (input_bfd, contents + 6);
  774. srel += addend;
  775. }
  776. else
  777. srel += rel->r_addend;
  778. srel -= rel->r_offset;
  779. srel -= (input_section->output_section->vma +
  780. input_section->output_offset);
  781. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 6);
  782. x = bfd_get_16 (input_bfd, contents);
  783. x = (x & 0xfff0) | ((srel >> 16) & 0xf);
  784. bfd_put_16 (input_bfd, x, contents);
  785. break;
  786. case R_MSP430X_ABS20_EXT_SRC:
  787. /* [7,4]+[32,16] = -78- ---- FFFF */
  788. contents += rel->r_offset;
  789. srel = (bfd_signed_vma) relocation;
  790. if (is_rel_reloc)
  791. {
  792. bfd_vma addend;
  793. addend = (bfd_get_16 (input_bfd, contents) & 0x0780) << 9;
  794. addend |= bfd_get_16 (input_bfd, contents + 4);
  795. srel += addend;
  796. }
  797. else
  798. srel += rel->r_addend;
  799. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
  800. srel >>= 16;
  801. x = bfd_get_16 (input_bfd, contents);
  802. x = (x & 0xf87f) | ((srel << 7) & 0x0780);
  803. bfd_put_16 (input_bfd, x, contents);
  804. break;
  805. case R_MSP430_16_PCREL:
  806. contents += rel->r_offset;
  807. srel = (bfd_signed_vma) relocation;
  808. if (is_rel_reloc)
  809. srel += bfd_get_16 (input_bfd, contents);
  810. else
  811. srel += rel->r_addend;
  812. srel -= rel->r_offset;
  813. /* Only branch instructions add 2 to the PC... */
  814. srel -= (input_section->output_section->vma +
  815. input_section->output_offset);
  816. if (srel & 1)
  817. return bfd_reloc_outofrange;
  818. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  819. break;
  820. case R_MSP430X_PCR20_EXT_DST:
  821. /* [0,4]+[32,16] = ---F ---- FFFF */
  822. contents += rel->r_offset;
  823. srel = (bfd_signed_vma) relocation;
  824. if (is_rel_reloc)
  825. {
  826. bfd_vma addend;
  827. addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
  828. addend |= bfd_get_16 (input_bfd, contents + 4);
  829. srel += addend;
  830. }
  831. else
  832. srel += rel->r_addend;
  833. srel -= rel->r_offset;
  834. srel -= (input_section->output_section->vma +
  835. input_section->output_offset);
  836. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
  837. srel >>= 16;
  838. x = bfd_get_16 (input_bfd, contents);
  839. x = (x & 0xfff0) | (srel & 0xf);
  840. bfd_put_16 (input_bfd, x, contents);
  841. break;
  842. case R_MSP430X_PCR20_EXT_SRC:
  843. /* [7,4]+[32,16] = -78- ---- FFFF */
  844. contents += rel->r_offset;
  845. srel = (bfd_signed_vma) relocation;
  846. if (is_rel_reloc)
  847. {
  848. bfd_vma addend;
  849. addend = ((bfd_get_16 (input_bfd, contents) & 0x0780) << 9);
  850. addend |= bfd_get_16 (input_bfd, contents + 4);
  851. srel += addend;;
  852. }
  853. else
  854. srel += rel->r_addend;
  855. srel -= rel->r_offset;
  856. /* Only branch instructions add 2 to the PC... */
  857. srel -= (input_section->output_section->vma +
  858. input_section->output_offset);
  859. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
  860. srel >>= 16;
  861. x = bfd_get_16 (input_bfd, contents);
  862. x = (x & 0xf87f) | ((srel << 7) & 0x0780);
  863. bfd_put_16 (input_bfd, x, contents);
  864. break;
  865. case R_MSP430_ABS8:
  866. contents += rel->r_offset;
  867. srel = (bfd_signed_vma) relocation;
  868. if (is_rel_reloc)
  869. srel += bfd_get_8 (input_bfd, contents);
  870. else
  871. srel += rel->r_addend;
  872. bfd_put_8 (input_bfd, srel & 0xff, contents);
  873. break;
  874. case R_MSP430X_ABS20_EXT_DST:
  875. /* [0,4]+[32,16] = ---F ---- FFFF */
  876. contents += rel->r_offset;
  877. srel = (bfd_signed_vma) relocation;
  878. if (is_rel_reloc)
  879. {
  880. bfd_vma addend;
  881. addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
  882. addend |= bfd_get_16 (input_bfd, contents + 4);
  883. srel += addend;
  884. }
  885. else
  886. srel += rel->r_addend;
  887. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
  888. srel >>= 16;
  889. x = bfd_get_16 (input_bfd, contents);
  890. x = (x & 0xfff0) | (srel & 0xf);
  891. bfd_put_16 (input_bfd, x, contents);
  892. break;
  893. case R_MSP430X_ABS20_EXT_ODST:
  894. /* [0,4]+[48,16] = ---F ---- ---- FFFF */
  895. contents += rel->r_offset;
  896. srel = (bfd_signed_vma) relocation;
  897. if (is_rel_reloc)
  898. {
  899. bfd_vma addend;
  900. addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
  901. addend |= bfd_get_16 (input_bfd, contents + 6);
  902. srel += addend;
  903. }
  904. else
  905. srel += rel->r_addend;
  906. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 6);
  907. srel >>= 16;
  908. x = bfd_get_16 (input_bfd, contents);
  909. x = (x & 0xfff0) | (srel & 0xf);
  910. bfd_put_16 (input_bfd, x, contents);
  911. break;
  912. case R_MSP430X_ABS20_ADR_SRC:
  913. /* [8,4]+[16,16] = -F-- FFFF */
  914. contents += rel->r_offset;
  915. srel = (bfd_signed_vma) relocation;
  916. if (is_rel_reloc)
  917. {
  918. bfd_vma addend;
  919. addend = ((bfd_get_16 (input_bfd, contents) & 0xf00) << 8);
  920. addend |= bfd_get_16 (input_bfd, contents + 2);
  921. srel += addend;
  922. }
  923. else
  924. srel += rel->r_addend;
  925. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 2);
  926. srel >>= 16;
  927. x = bfd_get_16 (input_bfd, contents);
  928. x = (x & 0xf0ff) | ((srel << 8) & 0x0f00);
  929. bfd_put_16 (input_bfd, x, contents);
  930. break;
  931. case R_MSP430X_ABS20_ADR_DST:
  932. /* [0,4]+[16,16] = ---F FFFF */
  933. contents += rel->r_offset;
  934. srel = (bfd_signed_vma) relocation;
  935. if (is_rel_reloc)
  936. {
  937. bfd_vma addend;
  938. addend = ((bfd_get_16 (input_bfd, contents) & 0xf) << 16);
  939. addend |= bfd_get_16 (input_bfd, contents + 2);
  940. srel += addend;
  941. }
  942. else
  943. srel += rel->r_addend;
  944. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 2);
  945. srel >>= 16;
  946. x = bfd_get_16 (input_bfd, contents);
  947. x = (x & 0xfff0) | (srel & 0xf);
  948. bfd_put_16 (input_bfd, x, contents);
  949. break;
  950. case R_MSP430X_ABS16:
  951. contents += rel->r_offset;
  952. srel = (bfd_signed_vma) relocation;
  953. if (is_rel_reloc)
  954. srel += bfd_get_16 (input_bfd, contents);
  955. else
  956. srel += rel->r_addend;
  957. x = srel;
  958. if (x > 0xffff)
  959. return bfd_reloc_overflow;
  960. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  961. break;
  962. case R_MSP430_ABS_HI16:
  963. /* The EABI specifies that this must be a RELA reloc. */
  964. BFD_ASSERT (! is_rel_reloc);
  965. contents += rel->r_offset;
  966. srel = (bfd_signed_vma) relocation;
  967. srel += rel->r_addend;
  968. bfd_put_16 (input_bfd, (srel >> 16) & 0xffff, contents);
  969. break;
  970. case R_MSP430X_PCR20_CALL:
  971. /* [0,4]+[16,16] = ---F FFFF*/
  972. contents += rel->r_offset;
  973. srel = (bfd_signed_vma) relocation;
  974. if (is_rel_reloc)
  975. {
  976. bfd_vma addend;
  977. addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
  978. addend |= bfd_get_16 (input_bfd, contents + 2);
  979. srel += addend;
  980. }
  981. else
  982. srel += rel->r_addend;
  983. srel -= rel->r_offset;
  984. srel -= (input_section->output_section->vma +
  985. input_section->output_offset);
  986. bfd_put_16 (input_bfd, srel & 0xffff, contents + 2);
  987. srel >>= 16;
  988. x = bfd_get_16 (input_bfd, contents);
  989. x = (x & 0xfff0) | (srel & 0xf);
  990. bfd_put_16 (input_bfd, x, contents);
  991. break;
  992. case R_MSP430X_PCR16:
  993. contents += rel->r_offset;
  994. srel = (bfd_signed_vma) relocation;
  995. if (is_rel_reloc)
  996. srel += bfd_get_16 (input_bfd, contents);
  997. else
  998. srel += rel->r_addend;
  999. srel -= rel->r_offset;
  1000. srel -= (input_section->output_section->vma +
  1001. input_section->output_offset);
  1002. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1003. break;
  1004. case R_MSP430_PREL31:
  1005. contents += rel->r_offset;
  1006. srel = (bfd_signed_vma) relocation;
  1007. if (is_rel_reloc)
  1008. srel += (bfd_get_32 (input_bfd, contents) & 0x7fffffff);
  1009. else
  1010. srel += rel->r_addend;
  1011. srel += rel->r_addend;
  1012. x = bfd_get_32 (input_bfd, contents);
  1013. x = (x & 0x80000000) | ((srel >> 31) & 0x7fffffff);
  1014. bfd_put_32 (input_bfd, x, contents);
  1015. break;
  1016. default:
  1017. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1018. contents, rel->r_offset,
  1019. relocation, rel->r_addend);
  1020. }
  1021. else
  1022. switch (howto->type)
  1023. {
  1024. case R_MSP430_10_PCREL:
  1025. contents += rel->r_offset;
  1026. srel = (bfd_signed_vma) relocation;
  1027. srel += rel->r_addend;
  1028. srel -= rel->r_offset;
  1029. srel -= 2; /* Branch instructions add 2 to the PC... */
  1030. srel -= (input_section->output_section->vma +
  1031. input_section->output_offset);
  1032. if (srel & 1)
  1033. return bfd_reloc_outofrange;
  1034. /* MSP430 addresses commands as words. */
  1035. srel >>= 1;
  1036. /* Check for an overflow. */
  1037. if (srel < -512 || srel > 511)
  1038. {
  1039. if (info->disable_target_specific_optimizations < 0)
  1040. {
  1041. static bfd_boolean warned = FALSE;
  1042. if (! warned)
  1043. {
  1044. info->callbacks->warning
  1045. (info,
  1046. _("Try enabling relaxation to avoid relocation truncations"),
  1047. NULL, input_bfd, input_section, relocation);
  1048. warned = TRUE;
  1049. }
  1050. }
  1051. return bfd_reloc_overflow;
  1052. }
  1053. x = bfd_get_16 (input_bfd, contents);
  1054. x = (x & 0xfc00) | (srel & 0x3ff);
  1055. bfd_put_16 (input_bfd, x, contents);
  1056. break;
  1057. case R_MSP430_2X_PCREL:
  1058. contents += rel->r_offset;
  1059. srel = (bfd_signed_vma) relocation;
  1060. srel += rel->r_addend;
  1061. srel -= rel->r_offset;
  1062. srel -= 2; /* Branch instructions add 2 to the PC... */
  1063. srel -= (input_section->output_section->vma +
  1064. input_section->output_offset);
  1065. if (srel & 1)
  1066. return bfd_reloc_outofrange;
  1067. /* MSP430 addresses commands as words. */
  1068. srel >>= 1;
  1069. /* Check for an overflow. */
  1070. if (srel < -512 || srel > 511)
  1071. return bfd_reloc_overflow;
  1072. x = bfd_get_16 (input_bfd, contents);
  1073. x = (x & 0xfc00) | (srel & 0x3ff);
  1074. bfd_put_16 (input_bfd, x, contents);
  1075. /* Handle second jump instruction. */
  1076. x = bfd_get_16 (input_bfd, contents - 2);
  1077. srel += 1;
  1078. x = (x & 0xfc00) | (srel & 0x3ff);
  1079. bfd_put_16 (input_bfd, x, contents - 2);
  1080. break;
  1081. case R_MSP430_RL_PCREL:
  1082. case R_MSP430_16_PCREL:
  1083. contents += rel->r_offset;
  1084. srel = (bfd_signed_vma) relocation;
  1085. srel += rel->r_addend;
  1086. srel -= rel->r_offset;
  1087. /* Only branch instructions add 2 to the PC... */
  1088. srel -= (input_section->output_section->vma +
  1089. input_section->output_offset);
  1090. if (srel & 1)
  1091. return bfd_reloc_outofrange;
  1092. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1093. break;
  1094. case R_MSP430_16_PCREL_BYTE:
  1095. contents += rel->r_offset;
  1096. srel = (bfd_signed_vma) relocation;
  1097. srel += rel->r_addend;
  1098. srel -= rel->r_offset;
  1099. /* Only branch instructions add 2 to the PC... */
  1100. srel -= (input_section->output_section->vma +
  1101. input_section->output_offset);
  1102. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1103. break;
  1104. case R_MSP430_16_BYTE:
  1105. contents += rel->r_offset;
  1106. srel = (bfd_signed_vma) relocation;
  1107. srel += rel->r_addend;
  1108. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1109. break;
  1110. case R_MSP430_16:
  1111. contents += rel->r_offset;
  1112. srel = (bfd_signed_vma) relocation;
  1113. srel += rel->r_addend;
  1114. if (srel & 1)
  1115. return bfd_reloc_notsupported;
  1116. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1117. break;
  1118. case R_MSP430_8:
  1119. contents += rel->r_offset;
  1120. srel = (bfd_signed_vma) relocation;
  1121. srel += rel->r_addend;
  1122. bfd_put_8 (input_bfd, srel & 0xff, contents);
  1123. break;
  1124. case R_MSP430_SYM_DIFF:
  1125. /* Cache the input section and value.
  1126. The offset is unreliable, since relaxation may
  1127. have reduced the following reloc's offset. */
  1128. sym_diff_section = input_section;
  1129. sym_diff_value = relocation;
  1130. return bfd_reloc_ok;
  1131. default:
  1132. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1133. contents, rel->r_offset,
  1134. relocation, rel->r_addend);
  1135. }
  1136. return r;
  1137. }
  1138. /* Relocate an MSP430 ELF section. */
  1139. static bfd_boolean
  1140. elf32_msp430_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
  1141. struct bfd_link_info * info,
  1142. bfd * input_bfd,
  1143. asection * input_section,
  1144. bfd_byte * contents,
  1145. Elf_Internal_Rela * relocs,
  1146. Elf_Internal_Sym * local_syms,
  1147. asection ** local_sections)
  1148. {
  1149. Elf_Internal_Shdr *symtab_hdr;
  1150. struct elf_link_hash_entry **sym_hashes;
  1151. Elf_Internal_Rela *rel;
  1152. Elf_Internal_Rela *relend;
  1153. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1154. sym_hashes = elf_sym_hashes (input_bfd);
  1155. relend = relocs + input_section->reloc_count;
  1156. for (rel = relocs; rel < relend; rel++)
  1157. {
  1158. reloc_howto_type *howto;
  1159. unsigned long r_symndx;
  1160. Elf_Internal_Sym *sym;
  1161. asection *sec;
  1162. struct elf_link_hash_entry *h;
  1163. bfd_vma relocation;
  1164. bfd_reloc_status_type r;
  1165. const char *name = NULL;
  1166. int r_type;
  1167. r_type = ELF32_R_TYPE (rel->r_info);
  1168. r_symndx = ELF32_R_SYM (rel->r_info);
  1169. if (uses_msp430x_relocs (input_bfd))
  1170. howto = elf_msp430x_howto_table + r_type;
  1171. else
  1172. howto = elf_msp430_howto_table + r_type;
  1173. h = NULL;
  1174. sym = NULL;
  1175. sec = NULL;
  1176. if (r_symndx < symtab_hdr->sh_info)
  1177. {
  1178. sym = local_syms + r_symndx;
  1179. sec = local_sections[r_symndx];
  1180. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1181. name = bfd_elf_string_from_elf_section
  1182. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  1183. name = (name == NULL || * name == 0) ? bfd_section_name (input_bfd, sec) : name;
  1184. }
  1185. else
  1186. {
  1187. bfd_boolean unresolved_reloc, warned, ignored;
  1188. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1189. r_symndx, symtab_hdr, sym_hashes,
  1190. h, sec, relocation,
  1191. unresolved_reloc, warned, ignored);
  1192. name = h->root.root.string;
  1193. }
  1194. if (sec != NULL && discarded_section (sec))
  1195. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  1196. rel, 1, relend, howto, 0, contents);
  1197. if (bfd_link_relocatable (info))
  1198. continue;
  1199. r = msp430_final_link_relocate (howto, input_bfd, input_section,
  1200. contents, rel, relocation, info);
  1201. if (r != bfd_reloc_ok)
  1202. {
  1203. const char *msg = (const char *) NULL;
  1204. switch (r)
  1205. {
  1206. case bfd_reloc_overflow:
  1207. r = info->callbacks->reloc_overflow
  1208. (info, (h ? &h->root : NULL), name, howto->name,
  1209. (bfd_vma) 0, input_bfd, input_section,
  1210. rel->r_offset);
  1211. break;
  1212. case bfd_reloc_undefined:
  1213. r = info->callbacks->undefined_symbol
  1214. (info, name, input_bfd, input_section, rel->r_offset, TRUE);
  1215. break;
  1216. case bfd_reloc_outofrange:
  1217. msg = _("internal error: branch/jump to an odd address detected");
  1218. break;
  1219. case bfd_reloc_notsupported:
  1220. msg = _("internal error: unsupported relocation error");
  1221. break;
  1222. case bfd_reloc_dangerous:
  1223. msg = _("internal error: dangerous relocation");
  1224. break;
  1225. default:
  1226. msg = _("internal error: unknown error");
  1227. break;
  1228. }
  1229. if (msg)
  1230. r = info->callbacks->warning
  1231. (info, msg, name, input_bfd, input_section, rel->r_offset);
  1232. if (!r)
  1233. return FALSE;
  1234. }
  1235. }
  1236. return TRUE;
  1237. }
  1238. /* The final processing done just before writing out a MSP430 ELF object
  1239. file. This gets the MSP430 architecture right based on the machine
  1240. number. */
  1241. static void
  1242. bfd_elf_msp430_final_write_processing (bfd * abfd,
  1243. bfd_boolean linker ATTRIBUTE_UNUSED)
  1244. {
  1245. unsigned long val;
  1246. switch (bfd_get_mach (abfd))
  1247. {
  1248. default:
  1249. case bfd_mach_msp110: val = E_MSP430_MACH_MSP430x11x1; break;
  1250. case bfd_mach_msp11: val = E_MSP430_MACH_MSP430x11; break;
  1251. case bfd_mach_msp12: val = E_MSP430_MACH_MSP430x12; break;
  1252. case bfd_mach_msp13: val = E_MSP430_MACH_MSP430x13; break;
  1253. case bfd_mach_msp14: val = E_MSP430_MACH_MSP430x14; break;
  1254. case bfd_mach_msp15: val = E_MSP430_MACH_MSP430x15; break;
  1255. case bfd_mach_msp16: val = E_MSP430_MACH_MSP430x16; break;
  1256. case bfd_mach_msp31: val = E_MSP430_MACH_MSP430x31; break;
  1257. case bfd_mach_msp32: val = E_MSP430_MACH_MSP430x32; break;
  1258. case bfd_mach_msp33: val = E_MSP430_MACH_MSP430x33; break;
  1259. case bfd_mach_msp41: val = E_MSP430_MACH_MSP430x41; break;
  1260. case bfd_mach_msp42: val = E_MSP430_MACH_MSP430x42; break;
  1261. case bfd_mach_msp43: val = E_MSP430_MACH_MSP430x43; break;
  1262. case bfd_mach_msp44: val = E_MSP430_MACH_MSP430x44; break;
  1263. case bfd_mach_msp20: val = E_MSP430_MACH_MSP430x20; break;
  1264. case bfd_mach_msp22: val = E_MSP430_MACH_MSP430x22; break;
  1265. case bfd_mach_msp23: val = E_MSP430_MACH_MSP430x23; break;
  1266. case bfd_mach_msp24: val = E_MSP430_MACH_MSP430x24; break;
  1267. case bfd_mach_msp26: val = E_MSP430_MACH_MSP430x26; break;
  1268. case bfd_mach_msp46: val = E_MSP430_MACH_MSP430x46; break;
  1269. case bfd_mach_msp47: val = E_MSP430_MACH_MSP430x47; break;
  1270. case bfd_mach_msp54: val = E_MSP430_MACH_MSP430x54; break;
  1271. case bfd_mach_msp430x: val = E_MSP430_MACH_MSP430X; break;
  1272. }
  1273. elf_elfheader (abfd)->e_machine = EM_MSP430;
  1274. elf_elfheader (abfd)->e_flags &= ~EF_MSP430_MACH;
  1275. elf_elfheader (abfd)->e_flags |= val;
  1276. }
  1277. /* Set the right machine number. */
  1278. static bfd_boolean
  1279. elf32_msp430_object_p (bfd * abfd)
  1280. {
  1281. int e_set = bfd_mach_msp14;
  1282. if (elf_elfheader (abfd)->e_machine == EM_MSP430
  1283. || elf_elfheader (abfd)->e_machine == EM_MSP430_OLD)
  1284. {
  1285. int e_mach = elf_elfheader (abfd)->e_flags & EF_MSP430_MACH;
  1286. switch (e_mach)
  1287. {
  1288. default:
  1289. case E_MSP430_MACH_MSP430x11: e_set = bfd_mach_msp11; break;
  1290. case E_MSP430_MACH_MSP430x11x1: e_set = bfd_mach_msp110; break;
  1291. case E_MSP430_MACH_MSP430x12: e_set = bfd_mach_msp12; break;
  1292. case E_MSP430_MACH_MSP430x13: e_set = bfd_mach_msp13; break;
  1293. case E_MSP430_MACH_MSP430x14: e_set = bfd_mach_msp14; break;
  1294. case E_MSP430_MACH_MSP430x15: e_set = bfd_mach_msp15; break;
  1295. case E_MSP430_MACH_MSP430x16: e_set = bfd_mach_msp16; break;
  1296. case E_MSP430_MACH_MSP430x31: e_set = bfd_mach_msp31; break;
  1297. case E_MSP430_MACH_MSP430x32: e_set = bfd_mach_msp32; break;
  1298. case E_MSP430_MACH_MSP430x33: e_set = bfd_mach_msp33; break;
  1299. case E_MSP430_MACH_MSP430x41: e_set = bfd_mach_msp41; break;
  1300. case E_MSP430_MACH_MSP430x42: e_set = bfd_mach_msp42; break;
  1301. case E_MSP430_MACH_MSP430x43: e_set = bfd_mach_msp43; break;
  1302. case E_MSP430_MACH_MSP430x44: e_set = bfd_mach_msp44; break;
  1303. case E_MSP430_MACH_MSP430x20: e_set = bfd_mach_msp20; break;
  1304. case E_MSP430_MACH_MSP430x22: e_set = bfd_mach_msp22; break;
  1305. case E_MSP430_MACH_MSP430x23: e_set = bfd_mach_msp23; break;
  1306. case E_MSP430_MACH_MSP430x24: e_set = bfd_mach_msp24; break;
  1307. case E_MSP430_MACH_MSP430x26: e_set = bfd_mach_msp26; break;
  1308. case E_MSP430_MACH_MSP430x46: e_set = bfd_mach_msp46; break;
  1309. case E_MSP430_MACH_MSP430x47: e_set = bfd_mach_msp47; break;
  1310. case E_MSP430_MACH_MSP430x54: e_set = bfd_mach_msp54; break;
  1311. case E_MSP430_MACH_MSP430X: e_set = bfd_mach_msp430x; break;
  1312. }
  1313. }
  1314. return bfd_default_set_arch_mach (abfd, bfd_arch_msp430, e_set);
  1315. }
  1316. /* These functions handle relaxing for the msp430.
  1317. Relaxation required only in two cases:
  1318. - Bad hand coding like jumps from one section to another or
  1319. from file to file.
  1320. - Sibling calls. This will affect only 'jump label' polymorph. Without
  1321. relaxing this enlarges code by 2 bytes. Sibcalls implemented but
  1322. do not work in gcc's port by the reason I do not know.
  1323. - To convert out of range conditional jump instructions (found inside
  1324. a function) into inverted jumps over an unconditional branch instruction.
  1325. Anyway, if a relaxation required, user should pass -relax option to the
  1326. linker.
  1327. There are quite a few relaxing opportunities available on the msp430:
  1328. ================================================================
  1329. 1. 3 words -> 1 word
  1330. eq == jeq label jne +4; br lab
  1331. ne != jne label jeq +4; br lab
  1332. lt < jl label jge +4; br lab
  1333. ltu < jlo label lhs +4; br lab
  1334. ge >= jge label jl +4; br lab
  1335. geu >= jhs label jlo +4; br lab
  1336. 2. 4 words -> 1 word
  1337. ltn < jn jn +2; jmp +4; br lab
  1338. 3. 4 words -> 2 words
  1339. gt > jeq +2; jge label jeq +6; jl +4; br label
  1340. gtu > jeq +2; jhs label jeq +6; jlo +4; br label
  1341. 4. 4 words -> 2 words and 2 labels
  1342. leu <= jeq label; jlo label jeq +2; jhs +4; br label
  1343. le <= jeq label; jl label jeq +2; jge +4; br label
  1344. =================================================================
  1345. codemap for first cases is (labels masked ):
  1346. eq: 0x2002,0x4010,0x0000 -> 0x2400
  1347. ne: 0x2402,0x4010,0x0000 -> 0x2000
  1348. lt: 0x3402,0x4010,0x0000 -> 0x3800
  1349. ltu: 0x2c02,0x4010,0x0000 -> 0x2800
  1350. ge: 0x3802,0x4010,0x0000 -> 0x3400
  1351. geu: 0x2802,0x4010,0x0000 -> 0x2c00
  1352. second case:
  1353. ltn: 0x3001,0x3c02,0x4010,0x0000 -> 0x3000
  1354. third case:
  1355. gt: 0x2403,0x3802,0x4010,0x0000 -> 0x2401,0x3400
  1356. gtu: 0x2403,0x2802,0x4010,0x0000 -> 0x2401,0x2c00
  1357. fourth case:
  1358. leu: 0x2401,0x2c02,0x4010,0x0000 -> 0x2400,0x2800
  1359. le: 0x2401,0x3402,0x4010,0x0000 -> 0x2400,0x3800
  1360. Unspecified case :)
  1361. jump: 0x4010,0x0000 -> 0x3c00. */
  1362. #define NUMB_RELAX_CODES 12
  1363. static struct rcodes_s
  1364. {
  1365. int f0, f1; /* From code. */
  1366. int t0, t1; /* To code. */
  1367. int labels; /* Position of labels: 1 - one label at first
  1368. word, 2 - one at second word, 3 - two
  1369. labels at both. */
  1370. int cdx; /* Words to match. */
  1371. int bs; /* Shrink bytes. */
  1372. int off; /* Offset from old label for new code. */
  1373. int ncl; /* New code length. */
  1374. } rcode[] =
  1375. {/* lab,cdx,bs,off,ncl */
  1376. { 0x0000, 0x0000, 0x3c00, 0x0000, 1, 0, 2, 2, 2}, /* jump */
  1377. { 0x0000, 0x2002, 0x2400, 0x0000, 1, 1, 4, 4, 2}, /* eq */
  1378. { 0x0000, 0x2402, 0x2000, 0x0000, 1, 1, 4, 4, 2}, /* ne */
  1379. { 0x0000, 0x3402, 0x3800, 0x0000, 1, 1, 4, 4, 2}, /* lt */
  1380. { 0x0000, 0x2c02, 0x2800, 0x0000, 1, 1, 4, 4, 2}, /* ltu */
  1381. { 0x0000, 0x3802, 0x3400, 0x0000, 1, 1, 4, 4, 2}, /* ge */
  1382. { 0x0000, 0x2802, 0x2c00, 0x0000, 1, 1, 4, 4, 2}, /* geu */
  1383. { 0x3001, 0x3c02, 0x3000, 0x0000, 1, 2, 6, 6, 2}, /* ltn */
  1384. { 0x2403, 0x3802, 0x2401, 0x3400, 2, 2, 4, 6, 4}, /* gt */
  1385. { 0x2403, 0x2802, 0x2401, 0x2c00, 2, 2, 4, 6, 4}, /* gtu */
  1386. { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6, 4}, /* leu , 2 labels */
  1387. { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6, 4}, /* le , 2 labels */
  1388. { 0, 0, 0, 0, 0, 0, 0, 0, 0}
  1389. };
  1390. /* Return TRUE if a symbol exists at the given address. */
  1391. static bfd_boolean
  1392. msp430_elf_symbol_address_p (bfd * abfd,
  1393. asection * sec,
  1394. Elf_Internal_Sym * isym,
  1395. bfd_vma addr)
  1396. {
  1397. Elf_Internal_Shdr *symtab_hdr;
  1398. unsigned int sec_shndx;
  1399. Elf_Internal_Sym *isymend;
  1400. struct elf_link_hash_entry **sym_hashes;
  1401. struct elf_link_hash_entry **end_hashes;
  1402. unsigned int symcount;
  1403. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1404. /* Examine all the local symbols. */
  1405. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1406. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  1407. if (isym->st_shndx == sec_shndx && isym->st_value == addr)
  1408. return TRUE;
  1409. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1410. - symtab_hdr->sh_info);
  1411. sym_hashes = elf_sym_hashes (abfd);
  1412. end_hashes = sym_hashes + symcount;
  1413. for (; sym_hashes < end_hashes; sym_hashes++)
  1414. {
  1415. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1416. if ((sym_hash->root.type == bfd_link_hash_defined
  1417. || sym_hash->root.type == bfd_link_hash_defweak)
  1418. && sym_hash->root.u.def.section == sec
  1419. && sym_hash->root.u.def.value == addr)
  1420. return TRUE;
  1421. }
  1422. return FALSE;
  1423. }
  1424. /* Adjust all local symbols defined as '.section + 0xXXXX' (.section has
  1425. sec_shndx) referenced from current and other sections. */
  1426. static bfd_boolean
  1427. msp430_elf_relax_adjust_locals (bfd * abfd, asection * sec, bfd_vma addr,
  1428. int count, unsigned int sec_shndx,
  1429. bfd_vma toaddr)
  1430. {
  1431. Elf_Internal_Shdr *symtab_hdr;
  1432. Elf_Internal_Rela *irel;
  1433. Elf_Internal_Rela *irelend;
  1434. Elf_Internal_Sym *isym;
  1435. irel = elf_section_data (sec)->relocs;
  1436. if (irel == NULL)
  1437. return TRUE;
  1438. irelend = irel + sec->reloc_count;
  1439. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1440. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1441. for (;irel < irelend; irel++)
  1442. {
  1443. unsigned int sidx = ELF32_R_SYM(irel->r_info);
  1444. Elf_Internal_Sym *lsym = isym + sidx;
  1445. /* Adjust symbols referenced by .sec+0xXX. */
  1446. if (irel->r_addend > addr && irel->r_addend < toaddr
  1447. && sidx < symtab_hdr->sh_info
  1448. && lsym->st_shndx == sec_shndx)
  1449. irel->r_addend -= count;
  1450. }
  1451. return TRUE;
  1452. }
  1453. /* Delete some bytes from a section while relaxing. */
  1454. static bfd_boolean
  1455. msp430_elf_relax_delete_bytes (bfd * abfd, asection * sec, bfd_vma addr,
  1456. int count)
  1457. {
  1458. Elf_Internal_Shdr *symtab_hdr;
  1459. unsigned int sec_shndx;
  1460. bfd_byte *contents;
  1461. Elf_Internal_Rela *irel;
  1462. Elf_Internal_Rela *irelend;
  1463. bfd_vma toaddr;
  1464. Elf_Internal_Sym *isym;
  1465. Elf_Internal_Sym *isymend;
  1466. struct elf_link_hash_entry **sym_hashes;
  1467. struct elf_link_hash_entry **end_hashes;
  1468. unsigned int symcount;
  1469. asection *p;
  1470. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1471. contents = elf_section_data (sec)->this_hdr.contents;
  1472. toaddr = sec->size;
  1473. irel = elf_section_data (sec)->relocs;
  1474. irelend = irel + sec->reloc_count;
  1475. /* Actually delete the bytes. */
  1476. memmove (contents + addr, contents + addr + count,
  1477. (size_t) (toaddr - addr - count));
  1478. sec->size -= count;
  1479. /* Adjust all the relocs. */
  1480. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1481. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1482. for (; irel < irelend; irel++)
  1483. {
  1484. /* Get the new reloc address. */
  1485. if ((irel->r_offset > addr && irel->r_offset < toaddr))
  1486. irel->r_offset -= count;
  1487. }
  1488. for (p = abfd->sections; p != NULL; p = p->next)
  1489. msp430_elf_relax_adjust_locals (abfd,p,addr,count,sec_shndx,toaddr);
  1490. /* Adjust the local symbols defined in this section. */
  1491. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1492. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1493. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  1494. {
  1495. const char * name;
  1496. name = bfd_elf_string_from_elf_section
  1497. (abfd, symtab_hdr->sh_link, isym->st_name);
  1498. name = (name == NULL || * name == 0) ? bfd_section_name (abfd, sec) : name;
  1499. if (isym->st_shndx != sec_shndx)
  1500. continue;
  1501. if (isym->st_value > addr
  1502. && (isym->st_value < toaddr
  1503. /* We also adjust a symbol at the end of the section if its name is
  1504. on the list below. These symbols are used for debug info
  1505. generation and they refer to the end of the current section, not
  1506. the start of the next section. */
  1507. || (isym->st_value == toaddr
  1508. && name != NULL
  1509. && (CONST_STRNEQ (name, ".Letext")
  1510. || CONST_STRNEQ (name, ".LFE")))))
  1511. {
  1512. if (isym->st_value < addr + count)
  1513. isym->st_value = addr;
  1514. else
  1515. isym->st_value -= count;
  1516. }
  1517. /* Adjust the function symbol's size as well. */
  1518. else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC
  1519. && isym->st_value + isym->st_size > addr
  1520. && isym->st_value + isym->st_size < toaddr)
  1521. isym->st_size -= count;
  1522. }
  1523. /* Now adjust the global symbols defined in this section. */
  1524. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1525. - symtab_hdr->sh_info);
  1526. sym_hashes = elf_sym_hashes (abfd);
  1527. end_hashes = sym_hashes + symcount;
  1528. for (; sym_hashes < end_hashes; sym_hashes++)
  1529. {
  1530. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1531. if ((sym_hash->root.type == bfd_link_hash_defined
  1532. || sym_hash->root.type == bfd_link_hash_defweak)
  1533. && sym_hash->root.u.def.section == sec
  1534. && sym_hash->root.u.def.value > addr
  1535. && sym_hash->root.u.def.value < toaddr)
  1536. {
  1537. if (sym_hash->root.u.def.value < addr + count)
  1538. sym_hash->root.u.def.value = addr;
  1539. else
  1540. sym_hash->root.u.def.value -= count;
  1541. }
  1542. /* Adjust the function symbol's size as well. */
  1543. else if (sym_hash->root.type == bfd_link_hash_defined
  1544. && sym_hash->root.u.def.section == sec
  1545. && sym_hash->type == STT_FUNC
  1546. && sym_hash->root.u.def.value + sym_hash->size > addr
  1547. && sym_hash->root.u.def.value + sym_hash->size < toaddr)
  1548. sym_hash->size -= count;
  1549. }
  1550. return TRUE;
  1551. }
  1552. /* Insert two words into a section whilst relaxing. */
  1553. static bfd_byte *
  1554. msp430_elf_relax_add_two_words (bfd * abfd, asection * sec, bfd_vma addr,
  1555. int word1, int word2)
  1556. {
  1557. Elf_Internal_Shdr *symtab_hdr;
  1558. unsigned int sec_shndx;
  1559. bfd_byte *contents;
  1560. Elf_Internal_Rela *irel;
  1561. Elf_Internal_Rela *irelend;
  1562. Elf_Internal_Sym *isym;
  1563. Elf_Internal_Sym *isymend;
  1564. struct elf_link_hash_entry **sym_hashes;
  1565. struct elf_link_hash_entry **end_hashes;
  1566. unsigned int symcount;
  1567. bfd_vma sec_end;
  1568. asection *p;
  1569. contents = elf_section_data (sec)->this_hdr.contents;
  1570. sec_end = sec->size;
  1571. /* Make space for the new words. */
  1572. contents = bfd_realloc (contents, sec_end + 4);
  1573. memmove (contents + addr + 4, contents + addr, sec_end - addr);
  1574. /* Insert the new words. */
  1575. bfd_put_16 (abfd, word1, contents + addr);
  1576. bfd_put_16 (abfd, word2, contents + addr + 2);
  1577. /* Update the section information. */
  1578. sec->size += 4;
  1579. elf_section_data (sec)->this_hdr.contents = contents;
  1580. /* Adjust all the relocs. */
  1581. irel = elf_section_data (sec)->relocs;
  1582. irelend = irel + sec->reloc_count;
  1583. for (; irel < irelend; irel++)
  1584. if ((irel->r_offset >= addr && irel->r_offset < sec_end))
  1585. irel->r_offset += 4;
  1586. /* Adjust the local symbols defined in this section. */
  1587. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1588. for (p = abfd->sections; p != NULL; p = p->next)
  1589. msp430_elf_relax_adjust_locals (abfd, p, addr, -4,
  1590. sec_shndx, sec_end);
  1591. /* Adjust the global symbols affected by the move. */
  1592. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1593. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1594. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  1595. if (isym->st_shndx == sec_shndx
  1596. && isym->st_value >= addr && isym->st_value < sec_end)
  1597. isym->st_value += 4;
  1598. /* Now adjust the global symbols defined in this section. */
  1599. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1600. - symtab_hdr->sh_info);
  1601. sym_hashes = elf_sym_hashes (abfd);
  1602. end_hashes = sym_hashes + symcount;
  1603. for (; sym_hashes < end_hashes; sym_hashes++)
  1604. {
  1605. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1606. if ((sym_hash->root.type == bfd_link_hash_defined
  1607. || sym_hash->root.type == bfd_link_hash_defweak)
  1608. && sym_hash->root.u.def.section == sec
  1609. && sym_hash->root.u.def.value >= addr
  1610. && sym_hash->root.u.def.value < sec_end)
  1611. sym_hash->root.u.def.value += 4;
  1612. }
  1613. return contents;
  1614. }
  1615. static bfd_boolean
  1616. msp430_elf_relax_section (bfd * abfd, asection * sec,
  1617. struct bfd_link_info * link_info,
  1618. bfd_boolean * again)
  1619. {
  1620. Elf_Internal_Shdr * symtab_hdr;
  1621. Elf_Internal_Rela * internal_relocs;
  1622. Elf_Internal_Rela * irel;
  1623. Elf_Internal_Rela * irelend;
  1624. bfd_byte * contents = NULL;
  1625. Elf_Internal_Sym * isymbuf = NULL;
  1626. /* Assume nothing changes. */
  1627. *again = FALSE;
  1628. /* We don't have to do anything for a relocatable link, if
  1629. this section does not have relocs, or if this is not a
  1630. code section. */
  1631. if (bfd_link_relocatable (link_info)
  1632. || (sec->flags & SEC_RELOC) == 0
  1633. || sec->reloc_count == 0 || (sec->flags & SEC_CODE) == 0)
  1634. return TRUE;
  1635. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1636. /* Get a copy of the native relocations. */
  1637. internal_relocs =
  1638. _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, link_info->keep_memory);
  1639. if (internal_relocs == NULL)
  1640. goto error_return;
  1641. /* Walk through them looking for relaxing opportunities. */
  1642. irelend = internal_relocs + sec->reloc_count;
  1643. /* Do code size growing relocs first. */
  1644. for (irel = internal_relocs; irel < irelend; irel++)
  1645. {
  1646. bfd_vma symval;
  1647. /* If this isn't something that can be relaxed, then ignore
  1648. this reloc. */
  1649. if (uses_msp430x_relocs (abfd)
  1650. && ELF32_R_TYPE (irel->r_info) == (int) R_MSP430X_10_PCREL)
  1651. ;
  1652. else if (! uses_msp430x_relocs (abfd)
  1653. && ELF32_R_TYPE (irel->r_info) == (int) R_MSP430_10_PCREL)
  1654. ;
  1655. else
  1656. continue;
  1657. /* Get the section contents if we haven't done so already. */
  1658. if (contents == NULL)
  1659. {
  1660. /* Get cached copy if it exists. */
  1661. if (elf_section_data (sec)->this_hdr.contents != NULL)
  1662. contents = elf_section_data (sec)->this_hdr.contents;
  1663. else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
  1664. goto error_return;
  1665. }
  1666. /* Read this BFD's local symbols if we haven't done so already. */
  1667. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  1668. {
  1669. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1670. if (isymbuf == NULL)
  1671. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  1672. symtab_hdr->sh_info, 0,
  1673. NULL, NULL, NULL);
  1674. if (isymbuf == NULL)
  1675. goto error_return;
  1676. }
  1677. /* Get the value of the symbol referred to by the reloc. */
  1678. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  1679. {
  1680. /* A local symbol. */
  1681. Elf_Internal_Sym *isym;
  1682. asection *sym_sec;
  1683. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  1684. if (isym->st_shndx == SHN_UNDEF)
  1685. sym_sec = bfd_und_section_ptr;
  1686. else if (isym->st_shndx == SHN_ABS)
  1687. sym_sec = bfd_abs_section_ptr;
  1688. else if (isym->st_shndx == SHN_COMMON)
  1689. sym_sec = bfd_com_section_ptr;
  1690. else
  1691. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1692. symval = (isym->st_value
  1693. + sym_sec->output_section->vma + sym_sec->output_offset);
  1694. }
  1695. else
  1696. {
  1697. unsigned long indx;
  1698. struct elf_link_hash_entry *h;
  1699. /* An external symbol. */
  1700. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  1701. h = elf_sym_hashes (abfd)[indx];
  1702. BFD_ASSERT (h != NULL);
  1703. if (h->root.type != bfd_link_hash_defined
  1704. && h->root.type != bfd_link_hash_defweak)
  1705. /* This appears to be a reference to an undefined
  1706. symbol. Just ignore it--it will be caught by the
  1707. regular reloc processing. */
  1708. continue;
  1709. symval = (h->root.u.def.value
  1710. + h->root.u.def.section->output_section->vma
  1711. + h->root.u.def.section->output_offset);
  1712. }
  1713. /* For simplicity of coding, we are going to modify the section
  1714. contents, the section relocs, and the BFD symbol table. We
  1715. must tell the rest of the code not to free up this
  1716. information. It would be possible to instead create a table
  1717. of changes which have to be made, as is done in coff-mips.c;
  1718. that would be more work, but would require less memory when
  1719. the linker is run. */
  1720. bfd_signed_vma value = symval;
  1721. int opcode;
  1722. /* Compute the value that will be relocated. */
  1723. value += irel->r_addend;
  1724. /* Convert to PC relative. */
  1725. value -= (sec->output_section->vma + sec->output_offset);
  1726. value -= irel->r_offset;
  1727. value -= 2;
  1728. /* Scale. */
  1729. value >>= 1;
  1730. /* If it is in range then no modifications are needed. */
  1731. if (value >= -512 && value <= 511)
  1732. continue;
  1733. /* Get the opcode. */
  1734. opcode = bfd_get_16 (abfd, contents + irel->r_offset);
  1735. /* Compute the new opcode. We are going to convert:
  1736. J<cond> label
  1737. into:
  1738. J<inv-cond> 1f
  1739. BR[A] #label
  1740. 1: */
  1741. switch (opcode & 0xfc00)
  1742. {
  1743. case 0x3800: opcode = 0x3402; break; /* Jl -> Jge +2 */
  1744. case 0x3400: opcode = 0x3802; break; /* Jge -> Jl +2 */
  1745. case 0x2c00: opcode = 0x2802; break; /* Jhs -> Jlo +2 */
  1746. case 0x2800: opcode = 0x2c02; break; /* Jlo -> Jhs +2 */
  1747. case 0x2400: opcode = 0x2002; break; /* Jeq -> Jne +2 */
  1748. case 0x2000: opcode = 0x2402; break; /* jne -> Jeq +2 */
  1749. case 0x3000: /* jn */
  1750. /* There is no direct inverse of the Jn insn.
  1751. FIXME: we could do this as:
  1752. Jn 1f
  1753. br 2f
  1754. 1: br label
  1755. 2: */
  1756. continue;
  1757. default:
  1758. /* Not a conditional branch instruction. */
  1759. /* fprintf (stderr, "unrecog: %x\n", opcode); */
  1760. continue;
  1761. }
  1762. /* Note that we've changed the relocs, section contents, etc. */
  1763. elf_section_data (sec)->relocs = internal_relocs;
  1764. elf_section_data (sec)->this_hdr.contents = contents;
  1765. symtab_hdr->contents = (unsigned char *) isymbuf;
  1766. /* Install the new opcode. */
  1767. bfd_put_16 (abfd, opcode, contents + irel->r_offset);
  1768. /* Insert the new branch instruction. */
  1769. if (uses_msp430x_relocs (abfd))
  1770. {
  1771. /* Insert an absolute branch (aka MOVA) instruction. */
  1772. contents = msp430_elf_relax_add_two_words
  1773. (abfd, sec, irel->r_offset + 2, 0x0080, 0x0000);
  1774. /* Update the relocation to point to the inserted branch
  1775. instruction. Note - we are changing a PC-relative reloc
  1776. into an absolute reloc, but this is OK because we have
  1777. arranged with the assembler to have the reloc's value be
  1778. a (local) symbol, not a section+offset value. */
  1779. irel->r_offset += 2;
  1780. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1781. R_MSP430X_ABS20_ADR_SRC);
  1782. }
  1783. else
  1784. {
  1785. contents = msp430_elf_relax_add_two_words
  1786. (abfd, sec, irel->r_offset + 2, 0x4030, 0x0000);
  1787. /* See comment above about converting a 10-bit PC-rel
  1788. relocation into a 16-bit absolute relocation. */
  1789. irel->r_offset += 4;
  1790. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1791. R_MSP430_16);
  1792. }
  1793. /* Growing the section may mean that other
  1794. conditional branches need to be fixed. */
  1795. *again = TRUE;
  1796. }
  1797. for (irel = internal_relocs; irel < irelend; irel++)
  1798. {
  1799. bfd_vma symval;
  1800. /* Get the section contents if we haven't done so already. */
  1801. if (contents == NULL)
  1802. {
  1803. /* Get cached copy if it exists. */
  1804. if (elf_section_data (sec)->this_hdr.contents != NULL)
  1805. contents = elf_section_data (sec)->this_hdr.contents;
  1806. else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
  1807. goto error_return;
  1808. }
  1809. /* Read this BFD's local symbols if we haven't done so already. */
  1810. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  1811. {
  1812. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1813. if (isymbuf == NULL)
  1814. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  1815. symtab_hdr->sh_info, 0,
  1816. NULL, NULL, NULL);
  1817. if (isymbuf == NULL)
  1818. goto error_return;
  1819. }
  1820. /* Get the value of the symbol referred to by the reloc. */
  1821. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  1822. {
  1823. /* A local symbol. */
  1824. Elf_Internal_Sym *isym;
  1825. asection *sym_sec;
  1826. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  1827. if (isym->st_shndx == SHN_UNDEF)
  1828. sym_sec = bfd_und_section_ptr;
  1829. else if (isym->st_shndx == SHN_ABS)
  1830. sym_sec = bfd_abs_section_ptr;
  1831. else if (isym->st_shndx == SHN_COMMON)
  1832. sym_sec = bfd_com_section_ptr;
  1833. else
  1834. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1835. symval = (isym->st_value
  1836. + sym_sec->output_section->vma + sym_sec->output_offset);
  1837. }
  1838. else
  1839. {
  1840. unsigned long indx;
  1841. struct elf_link_hash_entry *h;
  1842. /* An external symbol. */
  1843. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  1844. h = elf_sym_hashes (abfd)[indx];
  1845. BFD_ASSERT (h != NULL);
  1846. if (h->root.type != bfd_link_hash_defined
  1847. && h->root.type != bfd_link_hash_defweak)
  1848. /* This appears to be a reference to an undefined
  1849. symbol. Just ignore it--it will be caught by the
  1850. regular reloc processing. */
  1851. continue;
  1852. symval = (h->root.u.def.value
  1853. + h->root.u.def.section->output_section->vma
  1854. + h->root.u.def.section->output_offset);
  1855. }
  1856. /* For simplicity of coding, we are going to modify the section
  1857. contents, the section relocs, and the BFD symbol table. We
  1858. must tell the rest of the code not to free up this
  1859. information. It would be possible to instead create a table
  1860. of changes which have to be made, as is done in coff-mips.c;
  1861. that would be more work, but would require less memory when
  1862. the linker is run. */
  1863. /* Try to turn a 16bit pc-relative branch into a 10bit pc-relative
  1864. branch. */
  1865. /* Paranoia? paranoia... */
  1866. if (! uses_msp430x_relocs (abfd)
  1867. && ELF32_R_TYPE (irel->r_info) == (int) R_MSP430_RL_PCREL)
  1868. {
  1869. bfd_vma value = symval;
  1870. /* Deal with pc-relative gunk. */
  1871. value -= (sec->output_section->vma + sec->output_offset);
  1872. value -= irel->r_offset;
  1873. value += irel->r_addend;
  1874. /* See if the value will fit in 10 bits, note the high value is
  1875. 1016 as the target will be two bytes closer if we are
  1876. able to relax. */
  1877. if ((long) value < 1016 && (long) value > -1016)
  1878. {
  1879. int code0 = 0, code1 = 0, code2 = 0;
  1880. int i;
  1881. struct rcodes_s *rx;
  1882. /* Get the opcode. */
  1883. if (irel->r_offset >= 6)
  1884. code0 = bfd_get_16 (abfd, contents + irel->r_offset - 6);
  1885. if (irel->r_offset >= 4)
  1886. code1 = bfd_get_16 (abfd, contents + irel->r_offset - 4);
  1887. code2 = bfd_get_16 (abfd, contents + irel->r_offset - 2);
  1888. if (code2 != 0x4010)
  1889. continue;
  1890. /* Check r4 and r3. */
  1891. for (i = NUMB_RELAX_CODES - 1; i >= 0; i--)
  1892. {
  1893. rx = &rcode[i];
  1894. if (rx->cdx == 2 && rx->f0 == code0 && rx->f1 == code1)
  1895. break;
  1896. else if (rx->cdx == 1 && rx->f1 == code1)
  1897. break;
  1898. else if (rx->cdx == 0) /* This is an unconditional jump. */
  1899. break;
  1900. }
  1901. /* Check labels:
  1902. .Label0: ; we do not care about this label
  1903. jeq +6
  1904. .Label1: ; make sure there is no label here
  1905. jl +4
  1906. .Label2: ; make sure there is no label here
  1907. br .Label_dst
  1908. So, if there is .Label1 or .Label2 we cannot relax this code.
  1909. This actually should not happen, cause for relaxable
  1910. instructions we use RL_PCREL reloc instead of 16_PCREL.
  1911. Will change this in the future. */
  1912. if (rx->cdx > 0
  1913. && msp430_elf_symbol_address_p (abfd, sec, isymbuf,
  1914. irel->r_offset - 2))
  1915. continue;
  1916. if (rx->cdx > 1
  1917. && msp430_elf_symbol_address_p (abfd, sec, isymbuf,
  1918. irel->r_offset - 4))
  1919. continue;
  1920. /* Note that we've changed the relocs, section contents, etc. */
  1921. elf_section_data (sec)->relocs = internal_relocs;
  1922. elf_section_data (sec)->this_hdr.contents = contents;
  1923. symtab_hdr->contents = (unsigned char *) isymbuf;
  1924. /* Fix the relocation's type. */
  1925. if (uses_msp430x_relocs (abfd))
  1926. {
  1927. if (rx->labels == 3) /* Handle special cases. */
  1928. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1929. R_MSP430X_2X_PCREL);
  1930. else
  1931. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1932. R_MSP430X_10_PCREL);
  1933. }
  1934. else
  1935. {
  1936. if (rx->labels == 3) /* Handle special cases. */
  1937. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1938. R_MSP430_2X_PCREL);
  1939. else
  1940. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1941. R_MSP430_10_PCREL);
  1942. }
  1943. /* Fix the opcode right way. */
  1944. bfd_put_16 (abfd, rx->t0, contents + irel->r_offset - rx->off);
  1945. if (rx->t1)
  1946. bfd_put_16 (abfd, rx->t1,
  1947. contents + irel->r_offset - rx->off + 2);
  1948. /* Delete bytes. */
  1949. if (!msp430_elf_relax_delete_bytes (abfd, sec,
  1950. irel->r_offset - rx->off +
  1951. rx->ncl, rx->bs))
  1952. goto error_return;
  1953. /* Handle unconditional jumps. */
  1954. if (rx->cdx == 0)
  1955. irel->r_offset -= 2;
  1956. /* That will change things, so, we should relax again.
  1957. Note that this is not required, and it may be slow. */
  1958. *again = TRUE;
  1959. }
  1960. }
  1961. /* Try to turn a 16-bit absolute branch into a 10-bit pc-relative
  1962. branch. */
  1963. if (uses_msp430x_relocs (abfd)
  1964. && ELF32_R_TYPE (irel->r_info) == R_MSP430X_ABS16)
  1965. {
  1966. bfd_vma value = symval;
  1967. value -= (sec->output_section->vma + sec->output_offset);
  1968. value -= irel->r_offset;
  1969. value += irel->r_addend;
  1970. /* See if the value will fit in 10 bits, note the high value is
  1971. 1016 as the target will be two bytes closer if we are
  1972. able to relax. */
  1973. if ((long) value < 1016 && (long) value > -1016)
  1974. {
  1975. int code2;
  1976. /* Get the opcode. */
  1977. code2 = bfd_get_16 (abfd, contents + irel->r_offset - 2);
  1978. if (code2 != 0x4030)
  1979. continue;
  1980. /* FIXME: check r4 and r3 ? */
  1981. /* FIXME: Handle 0x4010 as well ? */
  1982. /* Note that we've changed the relocs, section contents, etc. */
  1983. elf_section_data (sec)->relocs = internal_relocs;
  1984. elf_section_data (sec)->this_hdr.contents = contents;
  1985. symtab_hdr->contents = (unsigned char *) isymbuf;
  1986. /* Fix the relocation's type. */
  1987. if (uses_msp430x_relocs (abfd))
  1988. {
  1989. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1990. R_MSP430X_10_PCREL);
  1991. }
  1992. else
  1993. {
  1994. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1995. R_MSP430_10_PCREL);
  1996. }
  1997. /* Fix the opcode right way. */
  1998. bfd_put_16 (abfd, 0x3c00, contents + irel->r_offset - 2);
  1999. irel->r_offset -= 2;
  2000. /* Delete bytes. */
  2001. if (!msp430_elf_relax_delete_bytes (abfd, sec,
  2002. irel->r_offset + 2, 2))
  2003. goto error_return;
  2004. /* That will change things, so, we should relax again.
  2005. Note that this is not required, and it may be slow. */
  2006. *again = TRUE;
  2007. }
  2008. }
  2009. }
  2010. if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
  2011. {
  2012. if (!link_info->keep_memory)
  2013. free (isymbuf);
  2014. else
  2015. {
  2016. /* Cache the symbols for elf_link_input_bfd. */
  2017. symtab_hdr->contents = (unsigned char *) isymbuf;
  2018. }
  2019. }
  2020. if (contents != NULL
  2021. && elf_section_data (sec)->this_hdr.contents != contents)
  2022. {
  2023. if (!link_info->keep_memory)
  2024. free (contents);
  2025. else
  2026. {
  2027. /* Cache the section contents for elf_link_input_bfd. */
  2028. elf_section_data (sec)->this_hdr.contents = contents;
  2029. }
  2030. }
  2031. if (internal_relocs != NULL
  2032. && elf_section_data (sec)->relocs != internal_relocs)
  2033. free (internal_relocs);
  2034. return TRUE;
  2035. error_return:
  2036. if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
  2037. free (isymbuf);
  2038. if (contents != NULL
  2039. && elf_section_data (sec)->this_hdr.contents != contents)
  2040. free (contents);
  2041. if (internal_relocs != NULL
  2042. && elf_section_data (sec)->relocs != internal_relocs)
  2043. free (internal_relocs);
  2044. return FALSE;
  2045. }
  2046. /* Handle an MSP430 specific section when reading an object file.
  2047. This is called when bfd_section_from_shdr finds a section with
  2048. an unknown type. */
  2049. static bfd_boolean
  2050. elf32_msp430_section_from_shdr (bfd *abfd,
  2051. Elf_Internal_Shdr * hdr,
  2052. const char *name,
  2053. int shindex)
  2054. {
  2055. switch (hdr->sh_type)
  2056. {
  2057. case SHT_MSP430_SEC_FLAGS:
  2058. case SHT_MSP430_SYM_ALIASES:
  2059. case SHT_MSP430_ATTRIBUTES:
  2060. return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
  2061. default:
  2062. return FALSE;
  2063. }
  2064. }
  2065. static bfd_boolean
  2066. elf32_msp430_obj_attrs_handle_unknown (bfd *abfd, int tag)
  2067. {
  2068. _bfd_error_handler
  2069. (_("Warning: %B: Unknown MSPABI object attribute %d"),
  2070. abfd, tag);
  2071. return TRUE;
  2072. }
  2073. /* Determine whether an object attribute tag takes an integer, a
  2074. string or both. */
  2075. static int
  2076. elf32_msp430_obj_attrs_arg_type (int tag)
  2077. {
  2078. if (tag == Tag_compatibility)
  2079. return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
  2080. if (tag < 32)
  2081. return ATTR_TYPE_FLAG_INT_VAL;
  2082. return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
  2083. }
  2084. static inline const char *
  2085. isa_type (int isa)
  2086. {
  2087. switch (isa)
  2088. {
  2089. case 1: return "MSP430";
  2090. case 2: return "MSP430X";
  2091. default: return "unknown";
  2092. }
  2093. }
  2094. static inline const char *
  2095. code_model (int model)
  2096. {
  2097. switch (model)
  2098. {
  2099. case 1: return "small";
  2100. case 2: return "large";
  2101. default: return "unknown";
  2102. }
  2103. }
  2104. static inline const char *
  2105. data_model (int model)
  2106. {
  2107. switch (model)
  2108. {
  2109. case 1: return "small";
  2110. case 2: return "large";
  2111. case 3: return "restricted large";
  2112. default: return "unknown";
  2113. }
  2114. }
  2115. /* Merge MSPABI object attributes from IBFD into OBFD.
  2116. Raise an error if there are conflicting attributes. */
  2117. static bfd_boolean
  2118. elf32_msp430_merge_mspabi_attributes (bfd *ibfd, bfd *obfd)
  2119. {
  2120. obj_attribute *in_attr;
  2121. obj_attribute *out_attr;
  2122. bfd_boolean result = TRUE;
  2123. static bfd * first_input_bfd = NULL;
  2124. /* Skip linker created files. */
  2125. if (ibfd->flags & BFD_LINKER_CREATED)
  2126. return TRUE;
  2127. /* If this is the first real object just copy the attributes. */
  2128. if (!elf_known_obj_attributes_proc (obfd)[0].i)
  2129. {
  2130. _bfd_elf_copy_obj_attributes (ibfd, obfd);
  2131. out_attr = elf_known_obj_attributes_proc (obfd);
  2132. /* Use the Tag_null value to indicate that
  2133. the attributes have been initialized. */
  2134. out_attr[0].i = 1;
  2135. first_input_bfd = ibfd;
  2136. return TRUE;
  2137. }
  2138. in_attr = elf_known_obj_attributes_proc (ibfd);
  2139. out_attr = elf_known_obj_attributes_proc (obfd);
  2140. /* The ISAs must be the same. */
  2141. if (in_attr[OFBA_MSPABI_Tag_ISA].i != out_attr[OFBA_MSPABI_Tag_ISA].i)
  2142. {
  2143. _bfd_error_handler
  2144. (_("error: %B uses %s instructions but %B uses %s"),
  2145. ibfd, first_input_bfd,
  2146. isa_type (in_attr[OFBA_MSPABI_Tag_ISA].i),
  2147. isa_type (out_attr[OFBA_MSPABI_Tag_ISA].i));
  2148. result = FALSE;
  2149. }
  2150. /* The code models must be the same. */
  2151. if (in_attr[OFBA_MSPABI_Tag_Code_Model].i !=
  2152. out_attr[OFBA_MSPABI_Tag_Code_Model].i)
  2153. {
  2154. _bfd_error_handler
  2155. (_("error: %B uses the %s code model whereas %B uses the %s code model"),
  2156. ibfd, first_input_bfd,
  2157. code_model (in_attr[OFBA_MSPABI_Tag_Code_Model].i),
  2158. code_model (out_attr[OFBA_MSPABI_Tag_Code_Model].i));
  2159. result = FALSE;
  2160. }
  2161. /* The large code model is only supported by the MSP430X. */
  2162. if (in_attr[OFBA_MSPABI_Tag_Code_Model].i == 2
  2163. && out_attr[OFBA_MSPABI_Tag_ISA].i != 2)
  2164. {
  2165. _bfd_error_handler
  2166. (_("error: %B uses the large code model but %B uses MSP430 instructions"),
  2167. ibfd, first_input_bfd);
  2168. result = FALSE;
  2169. }
  2170. /* The data models must be the same. */
  2171. if (in_attr[OFBA_MSPABI_Tag_Data_Model].i !=
  2172. out_attr[OFBA_MSPABI_Tag_Data_Model].i)
  2173. {
  2174. _bfd_error_handler
  2175. (_("error: %B uses the %s data model whereas %B uses the %s data model"),
  2176. ibfd, first_input_bfd,
  2177. data_model (in_attr[OFBA_MSPABI_Tag_Data_Model].i),
  2178. data_model (out_attr[OFBA_MSPABI_Tag_Data_Model].i));
  2179. result = FALSE;
  2180. }
  2181. /* The small code model requires the use of the small data model. */
  2182. if (in_attr[OFBA_MSPABI_Tag_Code_Model].i == 1
  2183. && out_attr[OFBA_MSPABI_Tag_Data_Model].i != 1)
  2184. {
  2185. _bfd_error_handler
  2186. (_("error: %B uses the small code model but %B uses the %s data model"),
  2187. ibfd, first_input_bfd,
  2188. data_model (out_attr[OFBA_MSPABI_Tag_Data_Model].i));
  2189. result = FALSE;
  2190. }
  2191. /* The large data models are only supported by the MSP430X. */
  2192. if (in_attr[OFBA_MSPABI_Tag_Data_Model].i > 1
  2193. && out_attr[OFBA_MSPABI_Tag_ISA].i != 2)
  2194. {
  2195. _bfd_error_handler
  2196. (_("error: %B uses the %s data model but %B only uses MSP430 instructions"),
  2197. ibfd, first_input_bfd,
  2198. data_model (in_attr[OFBA_MSPABI_Tag_Data_Model].i));
  2199. result = FALSE;
  2200. }
  2201. return result;
  2202. }
  2203. /* Merge backend specific data from an object file to the output
  2204. object file when linking. */
  2205. static bfd_boolean
  2206. elf32_msp430_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
  2207. {
  2208. /* Make sure that the machine number reflects the most
  2209. advanced version of the MSP architecture required. */
  2210. #define max(a,b) ((a) > (b) ? (a) : (b))
  2211. if (bfd_get_mach (ibfd) != bfd_get_mach (obfd))
  2212. bfd_default_set_arch_mach (obfd, bfd_get_arch (obfd),
  2213. max (bfd_get_mach (ibfd), bfd_get_mach (obfd)));
  2214. #undef max
  2215. return elf32_msp430_merge_mspabi_attributes (ibfd, obfd);
  2216. }
  2217. static bfd_boolean
  2218. msp430_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
  2219. {
  2220. return _bfd_elf_is_local_label_name (abfd, sym->name);
  2221. }
  2222. static bfd_boolean
  2223. uses_large_model (bfd *abfd)
  2224. {
  2225. obj_attribute * attr;
  2226. if (abfd->flags & BFD_LINKER_CREATED)
  2227. return FALSE;
  2228. attr = elf_known_obj_attributes_proc (abfd);
  2229. if (attr == NULL)
  2230. return FALSE;
  2231. return attr[OFBA_MSPABI_Tag_Code_Model].i == 2;
  2232. }
  2233. static unsigned int
  2234. elf32_msp430_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
  2235. {
  2236. return uses_large_model (abfd) ? 4 : 2;
  2237. }
  2238. /* This is gross. The MSP430 EABI says that (sec 11.5):
  2239. "An implementation may choose to use Rel or Rela
  2240. type relocations for other relocations."
  2241. But it also says that:
  2242. "Certain relocations are identified as Rela only. [snip]
  2243. Where Rela is specified, an implementation must honor
  2244. this requirement."
  2245. There is one relocation marked as requiring RELA - R_MSP430_ABS_HI16 - but
  2246. to keep things simple we choose to use RELA relocations throughout. The
  2247. problem is that the TI compiler generates REL relocations, so we have to
  2248. be able to accept those as well. */
  2249. #define elf_backend_may_use_rel_p 1
  2250. #define elf_backend_may_use_rela_p 1
  2251. #define elf_backend_default_use_rela_p 1
  2252. #undef elf_backend_obj_attrs_vendor
  2253. #define elf_backend_obj_attrs_vendor "mspabi"
  2254. #undef elf_backend_obj_attrs_section
  2255. #define elf_backend_obj_attrs_section ".MSP430.attributes"
  2256. #undef elf_backend_obj_attrs_section_type
  2257. #define elf_backend_obj_attrs_section_type SHT_MSP430_ATTRIBUTES
  2258. #define elf_backend_section_from_shdr elf32_msp430_section_from_shdr
  2259. #define elf_backend_obj_attrs_handle_unknown elf32_msp430_obj_attrs_handle_unknown
  2260. #undef elf_backend_obj_attrs_arg_type
  2261. #define elf_backend_obj_attrs_arg_type elf32_msp430_obj_attrs_arg_type
  2262. #define bfd_elf32_bfd_merge_private_bfd_data elf32_msp430_merge_private_bfd_data
  2263. #define elf_backend_eh_frame_address_size elf32_msp430_eh_frame_address_size
  2264. #define ELF_ARCH bfd_arch_msp430
  2265. #define ELF_MACHINE_CODE EM_MSP430
  2266. #define ELF_MACHINE_ALT1 EM_MSP430_OLD
  2267. #define ELF_MAXPAGESIZE 4
  2268. #define ELF_OSABI ELFOSABI_STANDALONE
  2269. #define TARGET_LITTLE_SYM msp430_elf32_vec
  2270. #define TARGET_LITTLE_NAME "elf32-msp430"
  2271. #define elf_info_to_howto msp430_info_to_howto_rela
  2272. #define elf_info_to_howto_rel NULL
  2273. #define elf_backend_relocate_section elf32_msp430_relocate_section
  2274. #define elf_backend_check_relocs elf32_msp430_check_relocs
  2275. #define elf_backend_can_gc_sections 1
  2276. #define elf_backend_final_write_processing bfd_elf_msp430_final_write_processing
  2277. #define elf_backend_object_p elf32_msp430_object_p
  2278. #define bfd_elf32_bfd_relax_section msp430_elf_relax_section
  2279. #define bfd_elf32_bfd_is_target_special_symbol msp430_elf_is_target_special_symbol
  2280. #undef elf32_bed
  2281. #define elf32_bed elf32_msp430_bed
  2282. #include "elf32-target.h"
  2283. /* The TI compiler sets the OSABI field to ELFOSABI_NONE. */
  2284. #undef TARGET_LITTLE_SYM
  2285. #define TARGET_LITTLE_SYM msp430_elf32_ti_vec
  2286. #undef elf32_bed
  2287. #define elf32_bed elf32_msp430_ti_bed
  2288. #undef ELF_OSABI
  2289. #define ELF_OSABI ELFOSABI_NONE
  2290. static const struct bfd_elf_special_section msp430_ti_elf_special_sections[] =
  2291. {
  2292. /* prefix, prefix_length, suffix_len, type, attributes. */
  2293. { STRING_COMMA_LEN (".TI.symbol.alias"), 0, SHT_MSP430_SYM_ALIASES, 0 },
  2294. { STRING_COMMA_LEN (".TI.section.flags"), 0, SHT_MSP430_SEC_FLAGS, 0 },
  2295. { STRING_COMMA_LEN ("_TI_build_attrib"), 0, SHT_MSP430_ATTRIBUTES, 0 },
  2296. { NULL, 0, 0, 0, 0 }
  2297. };
  2298. #undef elf_backend_special_sections
  2299. #define elf_backend_special_sections msp430_ti_elf_special_sections
  2300. #include "elf32-target.h"