elf32-score7.c 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890
  1. /* 32-bit ELF support for S+core.
  2. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. Contributed by
  4. Brain.lin (brain.lin@sunplusct.com)
  5. Mei Ligang (ligang@sunnorth.com.cn)
  6. Pei-Lin Tsai (pltsai@sunplus.com)
  7. This file is part of BFD, the Binary File Descriptor library.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 3 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19. MA 02110-1301, USA. */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "libbfd.h"
  23. #include "libiberty.h"
  24. #include "elf-bfd.h"
  25. #include "elf/score.h"
  26. #include "elf/common.h"
  27. #include "elf/internal.h"
  28. #include "hashtab.h"
  29. #include "elf32-score.h"
  30. /* The SCORE ELF linker needs additional information for each symbol in
  31. the global hash table. */
  32. struct score_elf_link_hash_entry
  33. {
  34. struct elf_link_hash_entry root;
  35. /* Number of R_SCORE_ABS32, R_SCORE_REL32 relocs against this symbol. */
  36. unsigned int possibly_dynamic_relocs;
  37. /* If the R_SCORE_ABS32, R_SCORE_REL32 reloc is against a readonly section. */
  38. bfd_boolean readonly_reloc;
  39. /* We must not create a stub for a symbol that has relocations related to
  40. taking the function's address, i.e. any but R_SCORE_CALL15 ones. */
  41. bfd_boolean no_fn_stub;
  42. /* Are we forced local? This will only be set if we have converted
  43. the initial global GOT entry to a local GOT entry. */
  44. bfd_boolean forced_local;
  45. };
  46. /* Traverse a score ELF linker hash table. */
  47. #define score_elf_link_hash_traverse(table, func, info) \
  48. (elf_link_hash_traverse \
  49. ((table), \
  50. (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
  51. (info)))
  52. /* This structure is used to hold .got entries while estimating got sizes. */
  53. struct score_got_entry
  54. {
  55. /* The input bfd in which the symbol is defined. */
  56. bfd *abfd;
  57. /* The index of the symbol, as stored in the relocation r_info, if
  58. we have a local symbol; -1 otherwise. */
  59. long symndx;
  60. union
  61. {
  62. /* If abfd == NULL, an address that must be stored in the got. */
  63. bfd_vma address;
  64. /* If abfd != NULL && symndx != -1, the addend of the relocation
  65. that should be added to the symbol value. */
  66. bfd_vma addend;
  67. /* If abfd != NULL && symndx == -1, the hash table entry
  68. corresponding to a global symbol in the got (or, local, if
  69. h->forced_local). */
  70. struct score_elf_link_hash_entry *h;
  71. } d;
  72. /* The offset from the beginning of the .got section to the entry
  73. corresponding to this symbol+addend. If it's a global symbol
  74. whose offset is yet to be decided, it's going to be -1. */
  75. long gotidx;
  76. };
  77. /* This structure is passed to score_elf_sort_hash_table_f when sorting
  78. the dynamic symbols. */
  79. struct score_elf_hash_sort_data
  80. {
  81. /* The symbol in the global GOT with the lowest dynamic symbol table index. */
  82. struct elf_link_hash_entry *low;
  83. /* The least dynamic symbol table index corresponding to a symbol with a GOT entry. */
  84. long min_got_dynindx;
  85. /* The greatest dynamic symbol table index corresponding to a symbol
  86. with a GOT entry that is not referenced (e.g., a dynamic symbol
  87. with dynamic relocations pointing to it from non-primary GOTs). */
  88. long max_unref_got_dynindx;
  89. /* The greatest dynamic symbol table index not corresponding to a
  90. symbol without a GOT entry. */
  91. long max_non_got_dynindx;
  92. };
  93. struct score_got_info
  94. {
  95. /* The global symbol in the GOT with the lowest index in the dynamic
  96. symbol table. */
  97. struct elf_link_hash_entry *global_gotsym;
  98. /* The number of global .got entries. */
  99. unsigned int global_gotno;
  100. /* The number of local .got entries. */
  101. unsigned int local_gotno;
  102. /* The number of local .got entries we have used. */
  103. unsigned int assigned_gotno;
  104. /* A hash table holding members of the got. */
  105. struct htab *got_entries;
  106. /* In multi-got links, a pointer to the next got (err, rather, most
  107. of the time, it points to the previous got). */
  108. struct score_got_info *next;
  109. };
  110. /* A structure used to count GOT entries, for GOT entry or ELF symbol table traversal. */
  111. struct _score_elf_section_data
  112. {
  113. struct bfd_elf_section_data elf;
  114. union
  115. {
  116. struct score_got_info *got_info;
  117. bfd_byte *tdata;
  118. }
  119. u;
  120. };
  121. #define score_elf_section_data(sec) \
  122. ((struct _score_elf_section_data *) elf_section_data (sec))
  123. /* The size of a symbol-table entry. */
  124. #define SCORE_ELF_SYM_SIZE(abfd) \
  125. (get_elf_backend_data (abfd)->s->sizeof_sym)
  126. /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
  127. from smaller values. Start with zero, widen, *then* decrement. */
  128. #define MINUS_ONE (((bfd_vma)0) - 1)
  129. #define MINUS_TWO (((bfd_vma)0) - 2)
  130. #define PDR_SIZE 32
  131. /* The number of local .got entries we reserve. */
  132. #define SCORE_RESERVED_GOTNO (2)
  133. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
  134. /* The offset of $gp from the beginning of the .got section. */
  135. #define ELF_SCORE_GP_OFFSET(abfd) (0x3ff0)
  136. /* The maximum size of the GOT for it to be addressable using 15-bit offsets from $gp. */
  137. #define SCORE_ELF_GOT_MAX_SIZE(abfd) (ELF_SCORE_GP_OFFSET(abfd) + 0x3fff)
  138. #define SCORE_ELF_STUB_SECTION_NAME (".SCORE.stub")
  139. #define SCORE_FUNCTION_STUB_SIZE (16)
  140. #define STUB_LW 0xc3bcc010 /* lw r29, [r28, -0x3ff0] */
  141. #define STUB_MOVE 0x8323bc56 /* mv r25, r3 */
  142. #define STUB_LI16 0x87548000 /* ori r26, .dynsym_index */
  143. #define STUB_BRL 0x801dbc09 /* brl r29 */
  144. #define SCORE_ELF_GOT_SIZE(abfd) \
  145. (get_elf_backend_data (abfd)->s->arch_size / 8)
  146. #define SCORE_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
  147. (_bfd_elf_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val))
  148. /* The size of an external dynamic table entry. */
  149. #define SCORE_ELF_DYN_SIZE(abfd) \
  150. (get_elf_backend_data (abfd)->s->sizeof_dyn)
  151. /* The size of an external REL relocation. */
  152. #define SCORE_ELF_REL_SIZE(abfd) \
  153. (get_elf_backend_data (abfd)->s->sizeof_rel)
  154. /* The default alignment for sections, as a power of two. */
  155. #define SCORE_ELF_LOG_FILE_ALIGN(abfd)\
  156. (get_elf_backend_data (abfd)->s->log_file_align)
  157. static bfd_byte *hi16_rel_addr;
  158. /* This will be used when we sort the dynamic relocation records. */
  159. static bfd *reldyn_sorting_bfd;
  160. /* SCORE ELF uses two common sections. One is the usual one, and the
  161. other is for small objects. All the small objects are kept
  162. together, and then referenced via the gp pointer, which yields
  163. faster assembler code. This is what we use for the small common
  164. section. This approach is copied from ecoff.c. */
  165. static asection score_elf_scom_section;
  166. static asymbol score_elf_scom_symbol;
  167. static asymbol * score_elf_scom_symbol_ptr;
  168. static bfd_reloc_status_type
  169. score_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  170. arelent *reloc_entry,
  171. asymbol *symbol ATTRIBUTE_UNUSED,
  172. void * data,
  173. asection *input_section ATTRIBUTE_UNUSED,
  174. bfd *output_bfd ATTRIBUTE_UNUSED,
  175. char **error_message ATTRIBUTE_UNUSED)
  176. {
  177. hi16_rel_addr = (bfd_byte *) data + reloc_entry->address;
  178. return bfd_reloc_ok;
  179. }
  180. static bfd_reloc_status_type
  181. score_elf_lo16_reloc (bfd *abfd,
  182. arelent *reloc_entry,
  183. asymbol *symbol ATTRIBUTE_UNUSED,
  184. void * data,
  185. asection *input_section,
  186. bfd *output_bfd ATTRIBUTE_UNUSED,
  187. char **error_message ATTRIBUTE_UNUSED)
  188. {
  189. bfd_vma addend = 0, offset = 0;
  190. unsigned long val;
  191. unsigned long hi16_offset, hi16_value, uvalue;
  192. hi16_value = bfd_get_32 (abfd, hi16_rel_addr);
  193. hi16_offset = ((((hi16_value >> 16) & 0x3) << 15) | (hi16_value & 0x7fff)) >> 1;
  194. addend = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  195. offset = ((((addend >> 16) & 0x3) << 15) | (addend & 0x7fff)) >> 1;
  196. val = reloc_entry->addend;
  197. if (reloc_entry->address > input_section->size)
  198. return bfd_reloc_outofrange;
  199. uvalue = ((hi16_offset << 16) | (offset & 0xffff)) + val;
  200. hi16_offset = (uvalue >> 16) << 1;
  201. hi16_value = (hi16_value & ~0x37fff) | (hi16_offset & 0x7fff) | ((hi16_offset << 1) & 0x30000);
  202. bfd_put_32 (abfd, hi16_value, hi16_rel_addr);
  203. offset = (uvalue & 0xffff) << 1;
  204. addend = (addend & ~0x37fff) | (offset & 0x7fff) | ((offset << 1) & 0x30000);
  205. bfd_put_32 (abfd, addend, (bfd_byte *) data + reloc_entry->address);
  206. return bfd_reloc_ok;
  207. }
  208. /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
  209. dangerous relocation. */
  210. static bfd_boolean
  211. score_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp)
  212. {
  213. unsigned int count;
  214. asymbol **sym;
  215. unsigned int i;
  216. /* If we've already figured out what GP will be, just return it. */
  217. *pgp = _bfd_get_gp_value (output_bfd);
  218. if (*pgp)
  219. return TRUE;
  220. count = bfd_get_symcount (output_bfd);
  221. sym = bfd_get_outsymbols (output_bfd);
  222. /* The linker script will have created a symbol named `_gp' with the
  223. appropriate value. */
  224. if (sym == NULL)
  225. i = count;
  226. else
  227. {
  228. for (i = 0; i < count; i++, sym++)
  229. {
  230. const char *name;
  231. name = bfd_asymbol_name (*sym);
  232. if (*name == '_' && strcmp (name, "_gp") == 0)
  233. {
  234. *pgp = bfd_asymbol_value (*sym);
  235. _bfd_set_gp_value (output_bfd, *pgp);
  236. break;
  237. }
  238. }
  239. }
  240. if (i >= count)
  241. {
  242. /* Only get the error once. */
  243. *pgp = 4;
  244. _bfd_set_gp_value (output_bfd, *pgp);
  245. return FALSE;
  246. }
  247. return TRUE;
  248. }
  249. /* We have to figure out the gp value, so that we can adjust the
  250. symbol value correctly. We look up the symbol _gp in the output
  251. BFD. If we can't find it, we're stuck. We cache it in the ELF
  252. target data. We don't need to adjust the symbol value for an
  253. external symbol if we are producing relocatable output. */
  254. static bfd_reloc_status_type
  255. score_elf_final_gp (bfd *output_bfd,
  256. asymbol *symbol,
  257. bfd_boolean relocatable,
  258. char **error_message,
  259. bfd_vma *pgp)
  260. {
  261. if (bfd_is_und_section (symbol->section)
  262. && ! relocatable)
  263. {
  264. *pgp = 0;
  265. return bfd_reloc_undefined;
  266. }
  267. *pgp = _bfd_get_gp_value (output_bfd);
  268. if (*pgp == 0
  269. && (! relocatable
  270. || (symbol->flags & BSF_SECTION_SYM) != 0))
  271. {
  272. if (relocatable)
  273. {
  274. /* Make up a value. */
  275. *pgp = symbol->section->output_section->vma + 0x4000;
  276. _bfd_set_gp_value (output_bfd, *pgp);
  277. }
  278. else if (!score_elf_assign_gp (output_bfd, pgp))
  279. {
  280. *error_message =
  281. (char *) _("GP relative relocation when _gp not defined");
  282. return bfd_reloc_dangerous;
  283. }
  284. }
  285. return bfd_reloc_ok;
  286. }
  287. static bfd_reloc_status_type
  288. score_elf_gprel15_with_gp (bfd *abfd,
  289. asymbol *symbol,
  290. arelent *reloc_entry,
  291. asection *input_section,
  292. bfd_boolean relocateable,
  293. void * data,
  294. bfd_vma gp ATTRIBUTE_UNUSED)
  295. {
  296. bfd_vma relocation;
  297. unsigned long insn;
  298. if (bfd_is_com_section (symbol->section))
  299. relocation = 0;
  300. else
  301. relocation = symbol->value;
  302. relocation += symbol->section->output_section->vma;
  303. relocation += symbol->section->output_offset;
  304. if (reloc_entry->address > input_section->size)
  305. return bfd_reloc_outofrange;
  306. insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  307. if (((reloc_entry->addend & 0xffffc000) != 0)
  308. && ((reloc_entry->addend & 0xffffc000) != 0xffffc000))
  309. return bfd_reloc_overflow;
  310. insn = (insn & ~0x7fff) | (reloc_entry->addend & 0x7fff);
  311. bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
  312. if (relocateable)
  313. reloc_entry->address += input_section->output_offset;
  314. return bfd_reloc_ok;
  315. }
  316. static bfd_reloc_status_type
  317. gprel32_with_gp (bfd *abfd, asymbol *symbol, arelent *reloc_entry,
  318. asection *input_section, bfd_boolean relocatable,
  319. void *data, bfd_vma gp)
  320. {
  321. bfd_vma relocation;
  322. bfd_vma val;
  323. if (bfd_is_com_section (symbol->section))
  324. relocation = 0;
  325. else
  326. relocation = symbol->value;
  327. relocation += symbol->section->output_section->vma;
  328. relocation += symbol->section->output_offset;
  329. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  330. return bfd_reloc_outofrange;
  331. /* Set val to the offset into the section or symbol. */
  332. val = reloc_entry->addend;
  333. if (reloc_entry->howto->partial_inplace)
  334. val += bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  335. /* Adjust val for the final section location and GP value. If we
  336. are producing relocatable output, we don't want to do this for
  337. an external symbol. */
  338. if (! relocatable
  339. || (symbol->flags & BSF_SECTION_SYM) != 0)
  340. val += relocation - gp;
  341. if (reloc_entry->howto->partial_inplace)
  342. bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
  343. else
  344. reloc_entry->addend = val;
  345. if (relocatable)
  346. reloc_entry->address += input_section->output_offset;
  347. return bfd_reloc_ok;
  348. }
  349. static bfd_reloc_status_type
  350. score_elf_gprel15_reloc (bfd *abfd,
  351. arelent *reloc_entry,
  352. asymbol *symbol,
  353. void * data,
  354. asection *input_section,
  355. bfd *output_bfd,
  356. char **error_message)
  357. {
  358. bfd_boolean relocateable;
  359. bfd_reloc_status_type ret;
  360. bfd_vma gp;
  361. if (output_bfd != NULL
  362. && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
  363. {
  364. reloc_entry->address += input_section->output_offset;
  365. return bfd_reloc_ok;
  366. }
  367. if (output_bfd != NULL)
  368. relocateable = TRUE;
  369. else
  370. {
  371. relocateable = FALSE;
  372. output_bfd = symbol->section->output_section->owner;
  373. }
  374. ret = score_elf_final_gp (output_bfd, symbol, relocateable, error_message, &gp);
  375. if (ret != bfd_reloc_ok)
  376. return ret;
  377. return score_elf_gprel15_with_gp (abfd, symbol, reloc_entry,
  378. input_section, relocateable, data, gp);
  379. }
  380. /* Do a R_SCORE_GPREL32 relocation. This is a 32 bit value which must
  381. become the offset from the gp register. */
  382. static bfd_reloc_status_type
  383. score_elf_gprel32_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  384. void *data, asection *input_section, bfd *output_bfd,
  385. char **error_message)
  386. {
  387. bfd_boolean relocatable;
  388. bfd_reloc_status_type ret;
  389. bfd_vma gp;
  390. /* R_SCORE_GPREL32 relocations are defined for local symbols only. */
  391. if (output_bfd != NULL
  392. && (symbol->flags & BSF_SECTION_SYM) == 0
  393. && (symbol->flags & BSF_LOCAL) != 0)
  394. {
  395. *error_message = (char *)
  396. _("32bits gp relative relocation occurs for an external symbol");
  397. return bfd_reloc_outofrange;
  398. }
  399. if (output_bfd != NULL)
  400. relocatable = TRUE;
  401. else
  402. {
  403. relocatable = FALSE;
  404. output_bfd = symbol->section->output_section->owner;
  405. }
  406. ret = score_elf_final_gp (output_bfd, symbol, relocatable, error_message, &gp);
  407. if (ret != bfd_reloc_ok)
  408. return ret;
  409. gp = 0;
  410. return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
  411. relocatable, data, gp);
  412. }
  413. /* A howto special_function for R_SCORE_GOT15 relocations. This is just
  414. like any other 16-bit relocation when applied to global symbols, but is
  415. treated in the same as R_SCORE_HI16 when applied to local symbols. */
  416. static bfd_reloc_status_type
  417. score_elf_got15_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  418. void *data, asection *input_section,
  419. bfd *output_bfd, char **error_message)
  420. {
  421. if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
  422. || bfd_is_und_section (bfd_get_section (symbol))
  423. || bfd_is_com_section (bfd_get_section (symbol)))
  424. /* The relocation is against a global symbol. */
  425. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  426. input_section, output_bfd,
  427. error_message);
  428. return score_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
  429. input_section, output_bfd, error_message);
  430. }
  431. static bfd_reloc_status_type
  432. score_elf_got_lo16_reloc (bfd *abfd,
  433. arelent *reloc_entry,
  434. asymbol *symbol ATTRIBUTE_UNUSED,
  435. void * data,
  436. asection *input_section,
  437. bfd *output_bfd ATTRIBUTE_UNUSED,
  438. char **error_message ATTRIBUTE_UNUSED)
  439. {
  440. bfd_vma addend = 0, offset = 0;
  441. signed long val;
  442. signed long hi16_offset, hi16_value, uvalue;
  443. hi16_value = bfd_get_32 (abfd, hi16_rel_addr);
  444. hi16_offset = ((((hi16_value >> 16) & 0x3) << 15) | (hi16_value & 0x7fff)) >> 1;
  445. addend = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  446. offset = ((((addend >> 16) & 0x3) << 15) | (addend & 0x7fff)) >> 1;
  447. val = reloc_entry->addend;
  448. if (reloc_entry->address > input_section->size)
  449. return bfd_reloc_outofrange;
  450. uvalue = ((hi16_offset << 16) | (offset & 0xffff)) + val;
  451. if ((uvalue > -0x8000) && (uvalue < 0x7fff))
  452. hi16_offset = 0;
  453. else
  454. hi16_offset = (uvalue >> 16) & 0x7fff;
  455. hi16_value = (hi16_value & ~0x37fff) | (hi16_offset & 0x7fff) | ((hi16_offset << 1) & 0x30000);
  456. bfd_put_32 (abfd, hi16_value, hi16_rel_addr);
  457. offset = (uvalue & 0xffff) << 1;
  458. addend = (addend & ~0x37fff) | (offset & 0x7fff) | ((offset << 1) & 0x30000);
  459. bfd_put_32 (abfd, addend, (bfd_byte *) data + reloc_entry->address);
  460. return bfd_reloc_ok;
  461. }
  462. static reloc_howto_type elf32_score_howto_table[] =
  463. {
  464. /* No relocation. */
  465. HOWTO (R_SCORE_NONE, /* type */
  466. 0, /* rightshift */
  467. 3, /* size (0 = byte, 1 = short, 2 = long) */
  468. 0, /* bitsize */
  469. FALSE, /* pc_relative */
  470. 0, /* bitpos */
  471. complain_overflow_dont,/* complain_on_overflow */
  472. bfd_elf_generic_reloc, /* special_function */
  473. "R_SCORE_NONE", /* name */
  474. FALSE, /* partial_inplace */
  475. 0, /* src_mask */
  476. 0, /* dst_mask */
  477. FALSE), /* pcrel_offset */
  478. /* R_SCORE_HI16 */
  479. HOWTO (R_SCORE_HI16, /* type */
  480. 0, /* rightshift */
  481. 2, /* size (0 = byte, 1 = short, 2 = long) */
  482. 16, /* bitsize */
  483. FALSE, /* pc_relative */
  484. 1, /* bitpos */
  485. complain_overflow_dont,/* complain_on_overflow */
  486. score_elf_hi16_reloc, /* special_function */
  487. "R_SCORE_HI16", /* name */
  488. TRUE, /* partial_inplace */
  489. 0x37fff, /* src_mask */
  490. 0x37fff, /* dst_mask */
  491. FALSE), /* pcrel_offset */
  492. /* R_SCORE_LO16 */
  493. HOWTO (R_SCORE_LO16, /* type */
  494. 0, /* rightshift */
  495. 2, /* size (0 = byte, 1 = short, 2 = long) */
  496. 16, /* bitsize */
  497. FALSE, /* pc_relative */
  498. 1, /* bitpos */
  499. complain_overflow_dont,/* complain_on_overflow */
  500. score_elf_lo16_reloc, /* special_function */
  501. "R_SCORE_LO16", /* name */
  502. TRUE, /* partial_inplace */
  503. 0x37fff, /* src_mask */
  504. 0x37fff, /* dst_mask */
  505. FALSE), /* pcrel_offset */
  506. /* R_SCORE_BCMP */
  507. HOWTO (R_SCORE_BCMP, /* type */
  508. 0, /* rightshift */
  509. 2, /* size (0 = byte, 1 = short, 2 = long) */
  510. 16, /* bitsize */
  511. FALSE, /* pc_relative */
  512. 1, /* bitpos */
  513. complain_overflow_dont,/* complain_on_overflow */
  514. bfd_elf_generic_reloc, /* special_function */
  515. "R_SCORE_BCMP", /* name */
  516. TRUE, /* partial_inplace */
  517. 0x0000ffff, /* src_mask */
  518. 0x0000ffff, /* dst_mask */
  519. FALSE), /* pcrel_offset */
  520. HOWTO (R_SCORE_24, /* type */
  521. 1, /* rightshift */
  522. 2, /* size (0 = byte, 1 = short, 2 = long) */
  523. 24, /* bitsize */
  524. FALSE, /* pc_relative */
  525. 1, /* bitpos */
  526. complain_overflow_dont,/* complain_on_overflow */
  527. bfd_elf_generic_reloc, /* special_function */
  528. "R_SCORE_24", /* name */
  529. FALSE, /* partial_inplace */
  530. 0x3ff7fff, /* src_mask */
  531. 0x3ff7fff, /* dst_mask */
  532. FALSE), /* pcrel_offset */
  533. /*R_SCORE_PC19 */
  534. HOWTO (R_SCORE_PC19, /* type */
  535. 1, /* rightshift */
  536. 2, /* size (0 = byte, 1 = short, 2 = long) */
  537. 19, /* bitsize */
  538. TRUE, /* pc_relative */
  539. 1, /* bitpos */
  540. complain_overflow_dont,/* complain_on_overflow */
  541. bfd_elf_generic_reloc, /* special_function */
  542. "R_SCORE_PC19", /* name */
  543. FALSE, /* partial_inplace */
  544. 0x3ff03fe, /* src_mask */
  545. 0x3ff03fe, /* dst_mask */
  546. FALSE), /* pcrel_offset */
  547. /*R_SCORE16_11 */
  548. HOWTO (R_SCORE16_11, /* type */
  549. 1, /* rightshift */
  550. 1, /* size (0 = byte, 1 = short, 2 = long) */
  551. 11, /* bitsize */
  552. FALSE, /* pc_relative */
  553. 1, /* bitpos */
  554. complain_overflow_dont,/* complain_on_overflow */
  555. bfd_elf_generic_reloc, /* special_function */
  556. "R_SCORE16_11", /* name */
  557. FALSE, /* partial_inplace */
  558. 0x000000ffe, /* src_mask */
  559. 0x000000ffe, /* dst_mask */
  560. FALSE), /* pcrel_offset */
  561. /* R_SCORE16_PC8 */
  562. HOWTO (R_SCORE16_PC8, /* type */
  563. 1, /* rightshift */
  564. 1, /* size (0 = byte, 1 = short, 2 = long) */
  565. 8, /* bitsize */
  566. TRUE, /* pc_relative */
  567. 0, /* bitpos */
  568. complain_overflow_dont,/* complain_on_overflow */
  569. bfd_elf_generic_reloc, /* special_function */
  570. "R_SCORE16_PC8", /* name */
  571. FALSE, /* partial_inplace */
  572. 0x000000ff, /* src_mask */
  573. 0x000000ff, /* dst_mask */
  574. FALSE), /* pcrel_offset */
  575. /* 32 bit absolute */
  576. HOWTO (R_SCORE_ABS32, /* type 8 */
  577. 0, /* rightshift */
  578. 2, /* size (0 = byte, 1 = short, 2 = long) */
  579. 32, /* bitsize */
  580. FALSE, /* pc_relative */
  581. 0, /* bitpos */
  582. complain_overflow_bitfield, /* complain_on_overflow */
  583. bfd_elf_generic_reloc, /* special_function */
  584. "R_SCORE_ABS32", /* name */
  585. FALSE, /* partial_inplace */
  586. 0xffffffff, /* src_mask */
  587. 0xffffffff, /* dst_mask */
  588. FALSE), /* pcrel_offset */
  589. /* 16 bit absolute */
  590. HOWTO (R_SCORE_ABS16, /* type 11 */
  591. 0, /* rightshift */
  592. 1, /* size (0 = byte, 1 = short, 2 = long) */
  593. 16, /* bitsize */
  594. FALSE, /* pc_relative */
  595. 0, /* bitpos */
  596. complain_overflow_bitfield, /* complain_on_overflow */
  597. bfd_elf_generic_reloc, /* special_function */
  598. "R_SCORE_ABS16", /* name */
  599. FALSE, /* partial_inplace */
  600. 0x0000ffff, /* src_mask */
  601. 0x0000ffff, /* dst_mask */
  602. FALSE), /* pcrel_offset */
  603. /* R_SCORE_DUMMY2 */
  604. HOWTO (R_SCORE_DUMMY2, /* type */
  605. 0, /* rightshift */
  606. 2, /* size (0 = byte, 1 = short, 2 = long) */
  607. 16, /* bitsize */
  608. FALSE, /* pc_relative */
  609. 0, /* bitpos */
  610. complain_overflow_dont,/* complain_on_overflow */
  611. bfd_elf_generic_reloc, /* special_function */
  612. "R_SCORE_DUMMY2", /* name */
  613. TRUE, /* partial_inplace */
  614. 0x00007fff, /* src_mask */
  615. 0x00007fff, /* dst_mask */
  616. FALSE), /* pcrel_offset */
  617. /* R_SCORE_GP15 */
  618. HOWTO (R_SCORE_GP15, /* type */
  619. 0, /* rightshift */
  620. 2, /* size (0 = byte, 1 = short, 2 = long) */
  621. 16, /* bitsize */
  622. FALSE, /* pc_relative */
  623. 0, /* bitpos */
  624. complain_overflow_dont,/* complain_on_overflow */
  625. score_elf_gprel15_reloc,/* special_function */
  626. "R_SCORE_GP15", /* name */
  627. TRUE, /* partial_inplace */
  628. 0x00007fff, /* src_mask */
  629. 0x00007fff, /* dst_mask */
  630. FALSE), /* pcrel_offset */
  631. /* GNU extension to record C++ vtable hierarchy. */
  632. HOWTO (R_SCORE_GNU_VTINHERIT, /* type */
  633. 0, /* rightshift */
  634. 2, /* size (0 = byte, 1 = short, 2 = long) */
  635. 0, /* bitsize */
  636. FALSE, /* pc_relative */
  637. 0, /* bitpos */
  638. complain_overflow_dont,/* complain_on_overflow */
  639. NULL, /* special_function */
  640. "R_SCORE_GNU_VTINHERIT", /* name */
  641. FALSE, /* partial_inplace */
  642. 0, /* src_mask */
  643. 0, /* dst_mask */
  644. FALSE), /* pcrel_offset */
  645. /* GNU extension to record C++ vtable member usage */
  646. HOWTO (R_SCORE_GNU_VTENTRY, /* type */
  647. 0, /* rightshift */
  648. 2, /* size (0 = byte, 1 = short, 2 = long) */
  649. 0, /* bitsize */
  650. FALSE, /* pc_relative */
  651. 0, /* bitpos */
  652. complain_overflow_dont,/* complain_on_overflow */
  653. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  654. "R_SCORE_GNU_VTENTRY", /* name */
  655. FALSE, /* partial_inplace */
  656. 0, /* src_mask */
  657. 0, /* dst_mask */
  658. FALSE), /* pcrel_offset */
  659. /* Reference to global offset table. */
  660. HOWTO (R_SCORE_GOT15, /* type */
  661. 0, /* rightshift */
  662. 2, /* size (0 = byte, 1 = short, 2 = long) */
  663. 16, /* bitsize */
  664. FALSE, /* pc_relative */
  665. 0, /* bitpos */
  666. complain_overflow_signed, /* complain_on_overflow */
  667. score_elf_got15_reloc, /* special_function */
  668. "R_SCORE_GOT15", /* name */
  669. TRUE, /* partial_inplace */
  670. 0x00007fff, /* src_mask */
  671. 0x00007fff, /* dst_mask */
  672. FALSE), /* pcrel_offset */
  673. /* Low 16 bits of displacement in global offset table. */
  674. HOWTO (R_SCORE_GOT_LO16, /* type */
  675. 0, /* rightshift */
  676. 2, /* size (0 = byte, 1 = short, 2 = long) */
  677. 16, /* bitsize */
  678. FALSE, /* pc_relative */
  679. 1, /* bitpos */
  680. complain_overflow_dont,/* complain_on_overflow */
  681. score_elf_got_lo16_reloc, /* special_function */
  682. "R_SCORE_GOT_LO16", /* name */
  683. TRUE, /* partial_inplace */
  684. 0x37ffe, /* src_mask */
  685. 0x37ffe, /* dst_mask */
  686. FALSE), /* pcrel_offset */
  687. /* 15 bit call through global offset table. */
  688. HOWTO (R_SCORE_CALL15, /* type */
  689. 0, /* rightshift */
  690. 2, /* size (0 = byte, 1 = short, 2 = long) */
  691. 16, /* bitsize */
  692. FALSE, /* pc_relative */
  693. 0, /* bitpos */
  694. complain_overflow_signed, /* complain_on_overflow */
  695. bfd_elf_generic_reloc, /* special_function */
  696. "R_SCORE_CALL15", /* name */
  697. TRUE, /* partial_inplace */
  698. 0x00007fff, /* src_mask */
  699. 0x00007fff, /* dst_mask */
  700. FALSE), /* pcrel_offset */
  701. /* 32 bit GP relative reference. */
  702. HOWTO (R_SCORE_GPREL32, /* type */
  703. 0, /* rightshift */
  704. 2, /* size (0 = byte, 1 = short, 2 = long) */
  705. 32, /* bitsize */
  706. FALSE, /* pc_relative */
  707. 0, /* bitpos */
  708. complain_overflow_dont,/* complain_on_overflow */
  709. score_elf_gprel32_reloc, /* special_function */
  710. "R_SCORE_GPREL32", /* name */
  711. TRUE, /* partial_inplace */
  712. 0xffffffff, /* src_mask */
  713. 0xffffffff, /* dst_mask */
  714. FALSE), /* pcrel_offset */
  715. /* 32 bit symbol relative relocation. */
  716. HOWTO (R_SCORE_REL32, /* type */
  717. 0, /* rightshift */
  718. 2, /* size (0 = byte, 1 = short, 2 = long) */
  719. 32, /* bitsize */
  720. FALSE, /* pc_relative */
  721. 0, /* bitpos */
  722. complain_overflow_dont,/* complain_on_overflow */
  723. bfd_elf_generic_reloc, /* special_function */
  724. "R_SCORE_REL32", /* name */
  725. TRUE, /* partial_inplace */
  726. 0xffffffff, /* src_mask */
  727. 0xffffffff, /* dst_mask */
  728. FALSE), /* pcrel_offset */
  729. /* R_SCORE_DUMMY_HI16 */
  730. HOWTO (R_SCORE_DUMMY_HI16, /* type */
  731. 0, /* rightshift */
  732. 2, /* size (0 = byte, 1 = short, 2 = long) */
  733. 16, /* bitsize */
  734. FALSE, /* pc_relative */
  735. 1, /* bitpos */
  736. complain_overflow_dont,/* complain_on_overflow */
  737. score_elf_hi16_reloc, /* special_function */
  738. "R_SCORE_DUMMY_HI16", /* name */
  739. TRUE, /* partial_inplace */
  740. 0x37fff, /* src_mask */
  741. 0x37fff, /* dst_mask */
  742. FALSE), /* pcrel_offset */
  743. };
  744. struct score_reloc_map
  745. {
  746. bfd_reloc_code_real_type bfd_reloc_val;
  747. unsigned char elf_reloc_val;
  748. };
  749. static const struct score_reloc_map elf32_score_reloc_map[] =
  750. {
  751. {BFD_RELOC_NONE, R_SCORE_NONE},
  752. {BFD_RELOC_HI16_S, R_SCORE_HI16},
  753. {BFD_RELOC_LO16, R_SCORE_LO16},
  754. {BFD_RELOC_SCORE_BCMP, R_SCORE_BCMP},
  755. {BFD_RELOC_SCORE_JMP, R_SCORE_24},
  756. {BFD_RELOC_SCORE_BRANCH, R_SCORE_PC19},
  757. {BFD_RELOC_SCORE16_JMP, R_SCORE16_11},
  758. {BFD_RELOC_SCORE16_BRANCH, R_SCORE16_PC8},
  759. {BFD_RELOC_32, R_SCORE_ABS32},
  760. {BFD_RELOC_16, R_SCORE_ABS16},
  761. {BFD_RELOC_SCORE_DUMMY2, R_SCORE_DUMMY2},
  762. {BFD_RELOC_SCORE_GPREL15, R_SCORE_GP15},
  763. {BFD_RELOC_VTABLE_INHERIT, R_SCORE_GNU_VTINHERIT},
  764. {BFD_RELOC_VTABLE_ENTRY, R_SCORE_GNU_VTENTRY},
  765. {BFD_RELOC_SCORE_GOT15, R_SCORE_GOT15},
  766. {BFD_RELOC_SCORE_GOT_LO16, R_SCORE_GOT_LO16},
  767. {BFD_RELOC_SCORE_CALL15, R_SCORE_CALL15},
  768. {BFD_RELOC_GPREL32, R_SCORE_GPREL32},
  769. {BFD_RELOC_32_PCREL, R_SCORE_REL32},
  770. {BFD_RELOC_SCORE_DUMMY_HI16, R_SCORE_DUMMY_HI16},
  771. };
  772. static INLINE hashval_t
  773. score_elf_hash_bfd_vma (bfd_vma addr)
  774. {
  775. #ifdef BFD64
  776. return addr + (addr >> 32);
  777. #else
  778. return addr;
  779. #endif
  780. }
  781. /* got_entries only match if they're identical, except for gotidx, so
  782. use all fields to compute the hash, and compare the appropriate
  783. union members. */
  784. static hashval_t
  785. score_elf_got_entry_hash (const void *entry_)
  786. {
  787. const struct score_got_entry *entry = (struct score_got_entry *) entry_;
  788. return entry->symndx
  789. + (! entry->abfd ? score_elf_hash_bfd_vma (entry->d.address)
  790. : entry->abfd->id
  791. + (entry->symndx >= 0 ? score_elf_hash_bfd_vma (entry->d.addend)
  792. : entry->d.h->root.root.root.hash));
  793. }
  794. static int
  795. score_elf_got_entry_eq (const void *entry1, const void *entry2)
  796. {
  797. const struct score_got_entry *e1 = (struct score_got_entry *) entry1;
  798. const struct score_got_entry *e2 = (struct score_got_entry *) entry2;
  799. return e1->abfd == e2->abfd && e1->symndx == e2->symndx
  800. && (! e1->abfd ? e1->d.address == e2->d.address
  801. : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
  802. : e1->d.h == e2->d.h);
  803. }
  804. /* If H needs a GOT entry, assign it the highest available dynamic
  805. index. Otherwise, assign it the lowest available dynamic
  806. index. */
  807. static bfd_boolean
  808. score_elf_sort_hash_table_f (struct score_elf_link_hash_entry *h, void *data)
  809. {
  810. struct score_elf_hash_sort_data *hsd = data;
  811. /* Symbols without dynamic symbol table entries aren't interesting at all. */
  812. if (h->root.dynindx == -1)
  813. return TRUE;
  814. /* Global symbols that need GOT entries that are not explicitly
  815. referenced are marked with got offset 2. Those that are
  816. referenced get a 1, and those that don't need GOT entries get
  817. -1. */
  818. if (h->root.got.offset == 2)
  819. {
  820. if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
  821. hsd->low = (struct elf_link_hash_entry *) h;
  822. h->root.dynindx = hsd->max_unref_got_dynindx++;
  823. }
  824. else if (h->root.got.offset != 1)
  825. h->root.dynindx = hsd->max_non_got_dynindx++;
  826. else
  827. {
  828. h->root.dynindx = --hsd->min_got_dynindx;
  829. hsd->low = (struct elf_link_hash_entry *) h;
  830. }
  831. return TRUE;
  832. }
  833. static asection *
  834. score_elf_got_section (bfd *abfd, bfd_boolean maybe_excluded)
  835. {
  836. asection *sgot = bfd_get_linker_section (abfd, ".got");
  837. if (sgot == NULL || (! maybe_excluded && (sgot->flags & SEC_EXCLUDE) != 0))
  838. return NULL;
  839. return sgot;
  840. }
  841. /* Returns the GOT information associated with the link indicated by
  842. INFO. If SGOTP is non-NULL, it is filled in with the GOT section. */
  843. static struct score_got_info *
  844. score_elf_got_info (bfd *abfd, asection **sgotp)
  845. {
  846. asection *sgot;
  847. struct score_got_info *g;
  848. sgot = score_elf_got_section (abfd, TRUE);
  849. BFD_ASSERT (sgot != NULL);
  850. BFD_ASSERT (elf_section_data (sgot) != NULL);
  851. g = score_elf_section_data (sgot)->u.got_info;
  852. BFD_ASSERT (g != NULL);
  853. if (sgotp)
  854. *sgotp = sgot;
  855. return g;
  856. }
  857. /* Sort the dynamic symbol table so that symbols that need GOT entries
  858. appear towards the end. This reduces the amount of GOT space
  859. required. MAX_LOCAL is used to set the number of local symbols
  860. known to be in the dynamic symbol table. During
  861. s7_bfd_score_elf_size_dynamic_sections, this value is 1. Afterward, the
  862. section symbols are added and the count is higher. */
  863. static bfd_boolean
  864. score_elf_sort_hash_table (struct bfd_link_info *info,
  865. unsigned long max_local)
  866. {
  867. struct score_elf_hash_sort_data hsd;
  868. struct score_got_info *g;
  869. bfd *dynobj;
  870. dynobj = elf_hash_table (info)->dynobj;
  871. g = score_elf_got_info (dynobj, NULL);
  872. hsd.low = NULL;
  873. hsd.max_unref_got_dynindx =
  874. hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
  875. /* In the multi-got case, assigned_gotno of the master got_info
  876. indicate the number of entries that aren't referenced in the
  877. primary GOT, but that must have entries because there are
  878. dynamic relocations that reference it. Since they aren't
  879. referenced, we move them to the end of the GOT, so that they
  880. don't prevent other entries that are referenced from getting
  881. too large offsets. */
  882. - (g->next ? g->assigned_gotno : 0);
  883. hsd.max_non_got_dynindx = max_local;
  884. score_elf_link_hash_traverse (elf_hash_table (info),
  885. score_elf_sort_hash_table_f,
  886. &hsd);
  887. /* There should have been enough room in the symbol table to
  888. accommodate both the GOT and non-GOT symbols. */
  889. BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
  890. BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
  891. <= elf_hash_table (info)->dynsymcount);
  892. /* Now we know which dynamic symbol has the lowest dynamic symbol
  893. table index in the GOT. */
  894. g->global_gotsym = hsd.low;
  895. return TRUE;
  896. }
  897. /* Returns the first relocation of type r_type found, beginning with
  898. RELOCATION. RELEND is one-past-the-end of the relocation table. */
  899. static const Elf_Internal_Rela *
  900. score_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
  901. const Elf_Internal_Rela *relocation,
  902. const Elf_Internal_Rela *relend)
  903. {
  904. while (relocation < relend)
  905. {
  906. if (ELF32_R_TYPE (relocation->r_info) == r_type)
  907. return relocation;
  908. ++relocation;
  909. }
  910. /* We didn't find it. */
  911. bfd_set_error (bfd_error_bad_value);
  912. return NULL;
  913. }
  914. /* This function is called via qsort() to sort the dynamic relocation
  915. entries by increasing r_symndx value. */
  916. static int
  917. score_elf_sort_dynamic_relocs (const void *arg1, const void *arg2)
  918. {
  919. Elf_Internal_Rela int_reloc1;
  920. Elf_Internal_Rela int_reloc2;
  921. bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
  922. bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
  923. return (ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info));
  924. }
  925. /* Return whether a relocation is against a local symbol. */
  926. static bfd_boolean
  927. score_elf_local_relocation_p (bfd *input_bfd,
  928. const Elf_Internal_Rela *relocation,
  929. asection **local_sections,
  930. bfd_boolean check_forced)
  931. {
  932. unsigned long r_symndx;
  933. Elf_Internal_Shdr *symtab_hdr;
  934. struct score_elf_link_hash_entry *h;
  935. size_t extsymoff;
  936. r_symndx = ELF32_R_SYM (relocation->r_info);
  937. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  938. extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
  939. if (r_symndx < extsymoff)
  940. return TRUE;
  941. if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
  942. return TRUE;
  943. if (check_forced)
  944. {
  945. /* Look up the hash table to check whether the symbol was forced local. */
  946. h = (struct score_elf_link_hash_entry *)
  947. elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
  948. /* Find the real hash-table entry for this symbol. */
  949. while (h->root.root.type == bfd_link_hash_indirect
  950. || h->root.root.type == bfd_link_hash_warning)
  951. h = (struct score_elf_link_hash_entry *) h->root.root.u.i.link;
  952. if (h->root.forced_local)
  953. return TRUE;
  954. }
  955. return FALSE;
  956. }
  957. /* Returns the dynamic relocation section for DYNOBJ. */
  958. static asection *
  959. score_elf_rel_dyn_section (bfd *dynobj, bfd_boolean create_p)
  960. {
  961. static const char dname[] = ".rel.dyn";
  962. asection *sreloc;
  963. sreloc = bfd_get_linker_section (dynobj, dname);
  964. if (sreloc == NULL && create_p)
  965. {
  966. sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
  967. (SEC_ALLOC
  968. | SEC_LOAD
  969. | SEC_HAS_CONTENTS
  970. | SEC_IN_MEMORY
  971. | SEC_LINKER_CREATED
  972. | SEC_READONLY));
  973. if (sreloc == NULL
  974. || ! bfd_set_section_alignment (dynobj, sreloc,
  975. SCORE_ELF_LOG_FILE_ALIGN (dynobj)))
  976. return NULL;
  977. }
  978. return sreloc;
  979. }
  980. static void
  981. score_elf_allocate_dynamic_relocations (bfd *abfd, unsigned int n)
  982. {
  983. asection *s;
  984. s = score_elf_rel_dyn_section (abfd, FALSE);
  985. BFD_ASSERT (s != NULL);
  986. if (s->size == 0)
  987. {
  988. /* Make room for a null element. */
  989. s->size += SCORE_ELF_REL_SIZE (abfd);
  990. ++s->reloc_count;
  991. }
  992. s->size += n * SCORE_ELF_REL_SIZE (abfd);
  993. }
  994. /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
  995. is the original relocation, which is now being transformed into a
  996. dynamic relocation. The ADDENDP is adjusted if necessary; the
  997. caller should store the result in place of the original addend. */
  998. static bfd_boolean
  999. score_elf_create_dynamic_relocation (bfd *output_bfd,
  1000. struct bfd_link_info *info,
  1001. const Elf_Internal_Rela *rel,
  1002. struct score_elf_link_hash_entry *h,
  1003. bfd_vma symbol,
  1004. bfd_vma *addendp, asection *input_section)
  1005. {
  1006. Elf_Internal_Rela outrel[3];
  1007. asection *sreloc;
  1008. bfd *dynobj;
  1009. int r_type;
  1010. long indx;
  1011. bfd_boolean defined_p;
  1012. r_type = ELF32_R_TYPE (rel->r_info);
  1013. dynobj = elf_hash_table (info)->dynobj;
  1014. sreloc = score_elf_rel_dyn_section (dynobj, FALSE);
  1015. BFD_ASSERT (sreloc != NULL);
  1016. BFD_ASSERT (sreloc->contents != NULL);
  1017. BFD_ASSERT (sreloc->reloc_count * SCORE_ELF_REL_SIZE (output_bfd) < sreloc->size);
  1018. outrel[0].r_offset =
  1019. _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
  1020. outrel[1].r_offset =
  1021. _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
  1022. outrel[2].r_offset =
  1023. _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
  1024. if (outrel[0].r_offset == MINUS_ONE)
  1025. /* The relocation field has been deleted. */
  1026. return TRUE;
  1027. if (outrel[0].r_offset == MINUS_TWO)
  1028. {
  1029. /* The relocation field has been converted into a relative value of
  1030. some sort. Functions like _bfd_elf_write_section_eh_frame expect
  1031. the field to be fully relocated, so add in the symbol's value. */
  1032. *addendp += symbol;
  1033. return TRUE;
  1034. }
  1035. /* We must now calculate the dynamic symbol table index to use
  1036. in the relocation. */
  1037. if (h != NULL
  1038. && (! info->symbolic || !h->root.def_regular)
  1039. /* h->root.dynindx may be -1 if this symbol was marked to
  1040. become local. */
  1041. && h->root.dynindx != -1)
  1042. {
  1043. indx = h->root.dynindx;
  1044. /* ??? glibc's ld.so just adds the final GOT entry to the
  1045. relocation field. It therefore treats relocs against
  1046. defined symbols in the same way as relocs against
  1047. undefined symbols. */
  1048. defined_p = FALSE;
  1049. }
  1050. else
  1051. {
  1052. indx = 0;
  1053. defined_p = TRUE;
  1054. }
  1055. /* If the relocation was previously an absolute relocation and
  1056. this symbol will not be referred to by the relocation, we must
  1057. adjust it by the value we give it in the dynamic symbol table.
  1058. Otherwise leave the job up to the dynamic linker. */
  1059. if (defined_p && r_type != R_SCORE_REL32)
  1060. *addendp += symbol;
  1061. /* The relocation is always an REL32 relocation because we don't
  1062. know where the shared library will wind up at load-time. */
  1063. outrel[0].r_info = ELF32_R_INFO ((unsigned long) indx, R_SCORE_REL32);
  1064. /* For strict adherence to the ABI specification, we should
  1065. generate a R_SCORE_64 relocation record by itself before the
  1066. _REL32/_64 record as well, such that the addend is read in as
  1067. a 64-bit value (REL32 is a 32-bit relocation, after all).
  1068. However, since none of the existing ELF64 SCORE dynamic
  1069. loaders seems to care, we don't waste space with these
  1070. artificial relocations. If this turns out to not be true,
  1071. score_elf_allocate_dynamic_relocations() should be tweaked so
  1072. as to make room for a pair of dynamic relocations per
  1073. invocation if ABI_64_P, and here we should generate an
  1074. additional relocation record with R_SCORE_64 by itself for a
  1075. NULL symbol before this relocation record. */
  1076. outrel[1].r_info = ELF32_R_INFO (0, R_SCORE_NONE);
  1077. outrel[2].r_info = ELF32_R_INFO (0, R_SCORE_NONE);
  1078. /* Adjust the output offset of the relocation to reference the
  1079. correct location in the output file. */
  1080. outrel[0].r_offset += (input_section->output_section->vma
  1081. + input_section->output_offset);
  1082. outrel[1].r_offset += (input_section->output_section->vma
  1083. + input_section->output_offset);
  1084. outrel[2].r_offset += (input_section->output_section->vma
  1085. + input_section->output_offset);
  1086. /* Put the relocation back out. We have to use the special
  1087. relocation outputter in the 64-bit case since the 64-bit
  1088. relocation format is non-standard. */
  1089. bfd_elf32_swap_reloc_out
  1090. (output_bfd, &outrel[0],
  1091. (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
  1092. /* We've now added another relocation. */
  1093. ++sreloc->reloc_count;
  1094. /* Make sure the output section is writable. The dynamic linker
  1095. will be writing to it. */
  1096. elf_section_data (input_section->output_section)->this_hdr.sh_flags |= SHF_WRITE;
  1097. return TRUE;
  1098. }
  1099. static bfd_boolean
  1100. score_elf_create_got_section (bfd *abfd,
  1101. struct bfd_link_info *info,
  1102. bfd_boolean maybe_exclude)
  1103. {
  1104. flagword flags;
  1105. asection *s;
  1106. struct elf_link_hash_entry *h;
  1107. struct bfd_link_hash_entry *bh;
  1108. struct score_got_info *g;
  1109. bfd_size_type amt;
  1110. /* This function may be called more than once. */
  1111. s = score_elf_got_section (abfd, TRUE);
  1112. if (s)
  1113. {
  1114. if (! maybe_exclude)
  1115. s->flags &= ~SEC_EXCLUDE;
  1116. return TRUE;
  1117. }
  1118. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  1119. if (maybe_exclude)
  1120. flags |= SEC_EXCLUDE;
  1121. /* We have to use an alignment of 2**4 here because this is hardcoded
  1122. in the function stub generation and in the linker script. */
  1123. s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  1124. if (s == NULL
  1125. || ! bfd_set_section_alignment (abfd, s, 4))
  1126. return FALSE;
  1127. /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
  1128. linker script because we don't want to define the symbol if we
  1129. are not creating a global offset table. */
  1130. bh = NULL;
  1131. if (! (_bfd_generic_link_add_one_symbol
  1132. (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
  1133. 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
  1134. return FALSE;
  1135. h = (struct elf_link_hash_entry *) bh;
  1136. h->non_elf = 0;
  1137. h->def_regular = 1;
  1138. h->type = STT_OBJECT;
  1139. if (bfd_link_pic (info)
  1140. && ! bfd_elf_link_record_dynamic_symbol (info, h))
  1141. return FALSE;
  1142. amt = sizeof (struct score_got_info);
  1143. g = bfd_alloc (abfd, amt);
  1144. if (g == NULL)
  1145. return FALSE;
  1146. g->global_gotsym = NULL;
  1147. g->global_gotno = 0;
  1148. g->local_gotno = SCORE_RESERVED_GOTNO;
  1149. g->assigned_gotno = SCORE_RESERVED_GOTNO;
  1150. g->next = NULL;
  1151. g->got_entries = htab_try_create (1, score_elf_got_entry_hash,
  1152. score_elf_got_entry_eq, NULL);
  1153. if (g->got_entries == NULL)
  1154. return FALSE;
  1155. score_elf_section_data (s)->u.got_info = g;
  1156. score_elf_section_data (s)->elf.this_hdr.sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_SCORE_GPREL;
  1157. return TRUE;
  1158. }
  1159. /* Calculate the %high function. */
  1160. static bfd_vma
  1161. score_elf_high (bfd_vma value)
  1162. {
  1163. return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
  1164. }
  1165. /* Create a local GOT entry for VALUE. Return the index of the entry,
  1166. or -1 if it could not be created. */
  1167. static struct score_got_entry *
  1168. score_elf_create_local_got_entry (bfd *abfd,
  1169. bfd *ibfd ATTRIBUTE_UNUSED,
  1170. struct score_got_info *gg,
  1171. asection *sgot, bfd_vma value,
  1172. unsigned long r_symndx ATTRIBUTE_UNUSED,
  1173. struct score_elf_link_hash_entry *h ATTRIBUTE_UNUSED,
  1174. int r_type ATTRIBUTE_UNUSED)
  1175. {
  1176. struct score_got_entry entry, **loc;
  1177. struct score_got_info *g;
  1178. entry.abfd = NULL;
  1179. entry.symndx = -1;
  1180. entry.d.address = value;
  1181. g = gg;
  1182. loc = (struct score_got_entry **) htab_find_slot (g->got_entries, &entry, INSERT);
  1183. if (*loc)
  1184. return *loc;
  1185. entry.gotidx = SCORE_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
  1186. *loc = bfd_alloc (abfd, sizeof entry);
  1187. if (! *loc)
  1188. return NULL;
  1189. memcpy (*loc, &entry, sizeof entry);
  1190. if (g->assigned_gotno >= g->local_gotno)
  1191. {
  1192. (*loc)->gotidx = -1;
  1193. /* We didn't allocate enough space in the GOT. */
  1194. (*_bfd_error_handler)
  1195. (_("not enough GOT space for local GOT entries"));
  1196. bfd_set_error (bfd_error_bad_value);
  1197. return NULL;
  1198. }
  1199. bfd_put_32 (abfd, value, (sgot->contents + entry.gotidx));
  1200. return *loc;
  1201. }
  1202. /* Find a GOT entry whose higher-order 16 bits are the same as those
  1203. for value. Return the index into the GOT for this entry. */
  1204. static bfd_vma
  1205. score_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
  1206. bfd_vma value, bfd_boolean external)
  1207. {
  1208. asection *sgot;
  1209. struct score_got_info *g;
  1210. struct score_got_entry *entry;
  1211. if (!external)
  1212. {
  1213. /* Although the ABI says that it is "the high-order 16 bits" that we
  1214. want, it is really the %high value. The complete value is
  1215. calculated with a `addiu' of a LO16 relocation, just as with a
  1216. HI16/LO16 pair. */
  1217. value = score_elf_high (value) << 16;
  1218. }
  1219. g = score_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
  1220. entry = score_elf_create_local_got_entry (abfd, ibfd, g, sgot, value, 0, NULL,
  1221. R_SCORE_GOT15);
  1222. if (entry)
  1223. return entry->gotidx;
  1224. else
  1225. return MINUS_ONE;
  1226. }
  1227. void
  1228. s7_bfd_score_elf_hide_symbol (struct bfd_link_info *info,
  1229. struct elf_link_hash_entry *entry,
  1230. bfd_boolean force_local)
  1231. {
  1232. bfd *dynobj;
  1233. asection *got;
  1234. struct score_got_info *g;
  1235. struct score_elf_link_hash_entry *h;
  1236. h = (struct score_elf_link_hash_entry *) entry;
  1237. if (h->forced_local)
  1238. return;
  1239. h->forced_local = TRUE;
  1240. dynobj = elf_hash_table (info)->dynobj;
  1241. if (dynobj != NULL && force_local)
  1242. {
  1243. got = score_elf_got_section (dynobj, FALSE);
  1244. if (got == NULL)
  1245. return;
  1246. g = score_elf_section_data (got)->u.got_info;
  1247. if (g->next)
  1248. {
  1249. struct score_got_entry e;
  1250. struct score_got_info *gg = g;
  1251. /* Since we're turning what used to be a global symbol into a
  1252. local one, bump up the number of local entries of each GOT
  1253. that had an entry for it. This will automatically decrease
  1254. the number of global entries, since global_gotno is actually
  1255. the upper limit of global entries. */
  1256. e.abfd = dynobj;
  1257. e.symndx = -1;
  1258. e.d.h = h;
  1259. for (g = g->next; g != gg; g = g->next)
  1260. if (htab_find (g->got_entries, &e))
  1261. {
  1262. BFD_ASSERT (g->global_gotno > 0);
  1263. g->local_gotno++;
  1264. g->global_gotno--;
  1265. }
  1266. /* If this was a global symbol forced into the primary GOT, we
  1267. no longer need an entry for it. We can't release the entry
  1268. at this point, but we must at least stop counting it as one
  1269. of the symbols that required a forced got entry. */
  1270. if (h->root.got.offset == 2)
  1271. {
  1272. BFD_ASSERT (gg->assigned_gotno > 0);
  1273. gg->assigned_gotno--;
  1274. }
  1275. }
  1276. else if (g->global_gotno == 0 && g->global_gotsym == NULL)
  1277. /* If we haven't got through GOT allocation yet, just bump up the
  1278. number of local entries, as this symbol won't be counted as
  1279. global. */
  1280. g->local_gotno++;
  1281. else if (h->root.got.offset == 1)
  1282. {
  1283. /* If we're past non-multi-GOT allocation and this symbol had
  1284. been marked for a global got entry, give it a local entry
  1285. instead. */
  1286. BFD_ASSERT (g->global_gotno > 0);
  1287. g->local_gotno++;
  1288. g->global_gotno--;
  1289. }
  1290. }
  1291. _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
  1292. }
  1293. /* If H is a symbol that needs a global GOT entry, but has a dynamic
  1294. symbol table index lower than any we've seen to date, record it for
  1295. posterity. */
  1296. static bfd_boolean
  1297. score_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
  1298. bfd *abfd,
  1299. struct bfd_link_info *info,
  1300. struct score_got_info *g)
  1301. {
  1302. struct score_got_entry entry, **loc;
  1303. /* A global symbol in the GOT must also be in the dynamic symbol table. */
  1304. if (h->dynindx == -1)
  1305. {
  1306. switch (ELF_ST_VISIBILITY (h->other))
  1307. {
  1308. case STV_INTERNAL:
  1309. case STV_HIDDEN:
  1310. s7_bfd_score_elf_hide_symbol (info, h, TRUE);
  1311. break;
  1312. }
  1313. if (!bfd_elf_link_record_dynamic_symbol (info, h))
  1314. return FALSE;
  1315. }
  1316. entry.abfd = abfd;
  1317. entry.symndx = -1;
  1318. entry.d.h = (struct score_elf_link_hash_entry *) h;
  1319. loc = (struct score_got_entry **) htab_find_slot (g->got_entries, &entry, INSERT);
  1320. /* If we've already marked this entry as needing GOT space, we don't
  1321. need to do it again. */
  1322. if (*loc)
  1323. return TRUE;
  1324. *loc = bfd_alloc (abfd, sizeof entry);
  1325. if (! *loc)
  1326. return FALSE;
  1327. entry.gotidx = -1;
  1328. memcpy (*loc, &entry, sizeof (entry));
  1329. if (h->got.offset != MINUS_ONE)
  1330. return TRUE;
  1331. /* By setting this to a value other than -1, we are indicating that
  1332. there needs to be a GOT entry for H. Avoid using zero, as the
  1333. generic ELF copy_indirect_symbol tests for <= 0. */
  1334. h->got.offset = 1;
  1335. return TRUE;
  1336. }
  1337. /* Reserve space in G for a GOT entry containing the value of symbol
  1338. SYMNDX in input bfd ABDF, plus ADDEND. */
  1339. static bfd_boolean
  1340. score_elf_record_local_got_symbol (bfd *abfd,
  1341. long symndx,
  1342. bfd_vma addend,
  1343. struct score_got_info *g)
  1344. {
  1345. struct score_got_entry entry, **loc;
  1346. entry.abfd = abfd;
  1347. entry.symndx = symndx;
  1348. entry.d.addend = addend;
  1349. loc = (struct score_got_entry **) htab_find_slot (g->got_entries, &entry, INSERT);
  1350. if (*loc)
  1351. return TRUE;
  1352. entry.gotidx = g->local_gotno++;
  1353. *loc = bfd_alloc (abfd, sizeof(entry));
  1354. if (! *loc)
  1355. return FALSE;
  1356. memcpy (*loc, &entry, sizeof (entry));
  1357. return TRUE;
  1358. }
  1359. /* Returns the GOT offset at which the indicated address can be found.
  1360. If there is not yet a GOT entry for this value, create one.
  1361. Returns -1 if no satisfactory GOT offset can be found. */
  1362. static bfd_vma
  1363. score_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
  1364. bfd_vma value, unsigned long r_symndx,
  1365. struct score_elf_link_hash_entry *h, int r_type)
  1366. {
  1367. asection *sgot;
  1368. struct score_got_info *g;
  1369. struct score_got_entry *entry;
  1370. g = score_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
  1371. entry = score_elf_create_local_got_entry (abfd, ibfd, g, sgot, value,
  1372. r_symndx, h, r_type);
  1373. if (!entry)
  1374. return MINUS_ONE;
  1375. else
  1376. return entry->gotidx;
  1377. }
  1378. /* Returns the GOT index for the global symbol indicated by H. */
  1379. static bfd_vma
  1380. score_elf_global_got_index (bfd *abfd, struct elf_link_hash_entry *h)
  1381. {
  1382. bfd_vma got_index;
  1383. asection *sgot;
  1384. struct score_got_info *g;
  1385. long global_got_dynindx = 0;
  1386. g = score_elf_got_info (abfd, &sgot);
  1387. if (g->global_gotsym != NULL)
  1388. global_got_dynindx = g->global_gotsym->dynindx;
  1389. /* Once we determine the global GOT entry with the lowest dynamic
  1390. symbol table index, we must put all dynamic symbols with greater
  1391. indices into the GOT. That makes it easy to calculate the GOT
  1392. offset. */
  1393. BFD_ASSERT (h->dynindx >= global_got_dynindx);
  1394. got_index = ((h->dynindx - global_got_dynindx + g->local_gotno) * SCORE_ELF_GOT_SIZE (abfd));
  1395. BFD_ASSERT (got_index < sgot->size);
  1396. return got_index;
  1397. }
  1398. /* Returns the offset for the entry at the INDEXth position in the GOT. */
  1399. static bfd_vma
  1400. score_elf_got_offset_from_index (bfd *dynobj,
  1401. bfd *output_bfd,
  1402. bfd *input_bfd ATTRIBUTE_UNUSED,
  1403. bfd_vma got_index)
  1404. {
  1405. asection *sgot;
  1406. bfd_vma gp;
  1407. score_elf_got_info (dynobj, &sgot);
  1408. gp = _bfd_get_gp_value (output_bfd);
  1409. return sgot->output_section->vma + sgot->output_offset + got_index - gp;
  1410. }
  1411. /* Follow indirect and warning hash entries so that each got entry
  1412. points to the final symbol definition. P must point to a pointer
  1413. to the hash table we're traversing. Since this traversal may
  1414. modify the hash table, we set this pointer to NULL to indicate
  1415. we've made a potentially-destructive change to the hash table, so
  1416. the traversal must be restarted. */
  1417. static int
  1418. score_elf_resolve_final_got_entry (void **entryp, void *p)
  1419. {
  1420. struct score_got_entry *entry = (struct score_got_entry *) *entryp;
  1421. htab_t got_entries = *(htab_t *) p;
  1422. if (entry->abfd != NULL && entry->symndx == -1)
  1423. {
  1424. struct score_elf_link_hash_entry *h = entry->d.h;
  1425. while (h->root.root.type == bfd_link_hash_indirect
  1426. || h->root.root.type == bfd_link_hash_warning)
  1427. h = (struct score_elf_link_hash_entry *) h->root.root.u.i.link;
  1428. if (entry->d.h == h)
  1429. return 1;
  1430. entry->d.h = h;
  1431. /* If we can't find this entry with the new bfd hash, re-insert
  1432. it, and get the traversal restarted. */
  1433. if (! htab_find (got_entries, entry))
  1434. {
  1435. htab_clear_slot (got_entries, entryp);
  1436. entryp = htab_find_slot (got_entries, entry, INSERT);
  1437. if (! *entryp)
  1438. *entryp = entry;
  1439. /* Abort the traversal, since the whole table may have
  1440. moved, and leave it up to the parent to restart the
  1441. process. */
  1442. *(htab_t *) p = NULL;
  1443. return 0;
  1444. }
  1445. /* We might want to decrement the global_gotno count, but it's
  1446. either too early or too late for that at this point. */
  1447. }
  1448. return 1;
  1449. }
  1450. /* Turn indirect got entries in a got_entries table into their final locations. */
  1451. static void
  1452. score_elf_resolve_final_got_entries (struct score_got_info *g)
  1453. {
  1454. htab_t got_entries;
  1455. do
  1456. {
  1457. got_entries = g->got_entries;
  1458. htab_traverse (got_entries,
  1459. score_elf_resolve_final_got_entry,
  1460. &got_entries);
  1461. }
  1462. while (got_entries == NULL);
  1463. }
  1464. /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS. for -r */
  1465. static void
  1466. score_elf_add_to_rel (bfd *abfd,
  1467. bfd_byte *address,
  1468. reloc_howto_type *howto,
  1469. bfd_signed_vma increment)
  1470. {
  1471. bfd_signed_vma addend;
  1472. bfd_vma contents;
  1473. unsigned long offset;
  1474. unsigned long r_type = howto->type;
  1475. unsigned long hi16_addend, hi16_offset, hi16_value, uvalue;
  1476. contents = bfd_get_32 (abfd, address);
  1477. /* Get the (signed) value from the instruction. */
  1478. addend = contents & howto->src_mask;
  1479. if (addend & ((howto->src_mask + 1) >> 1))
  1480. {
  1481. bfd_signed_vma mask;
  1482. mask = -1;
  1483. mask &= ~howto->src_mask;
  1484. addend |= mask;
  1485. }
  1486. /* Add in the increment, (which is a byte value). */
  1487. switch (r_type)
  1488. {
  1489. case R_SCORE_PC19:
  1490. offset =
  1491. (((contents & howto->src_mask) & 0x3ff0000) >> 6) | ((contents & howto->src_mask) & 0x3ff);
  1492. offset += increment;
  1493. contents =
  1494. (contents & ~howto->
  1495. src_mask) | (((offset << 6) & howto->src_mask) & 0x3ff0000) | (offset & 0x3ff);
  1496. bfd_put_32 (abfd, contents, address);
  1497. break;
  1498. case R_SCORE_HI16:
  1499. break;
  1500. case R_SCORE_LO16:
  1501. hi16_addend = bfd_get_32 (abfd, address - 4);
  1502. hi16_offset = ((((hi16_addend >> 16) & 0x3) << 15) | (hi16_addend & 0x7fff)) >> 1;
  1503. offset = ((((contents >> 16) & 0x3) << 15) | (contents & 0x7fff)) >> 1;
  1504. offset = (hi16_offset << 16) | (offset & 0xffff);
  1505. uvalue = increment + offset;
  1506. hi16_offset = (uvalue >> 16) << 1;
  1507. hi16_value = (hi16_addend & (~(howto->dst_mask)))
  1508. | (hi16_offset & 0x7fff) | ((hi16_offset << 1) & 0x30000);
  1509. bfd_put_32 (abfd, hi16_value, address - 4);
  1510. offset = (uvalue & 0xffff) << 1;
  1511. contents = (contents & (~(howto->dst_mask))) | (offset & 0x7fff) | ((offset << 1) & 0x30000);
  1512. bfd_put_32 (abfd, contents, address);
  1513. break;
  1514. case R_SCORE_24:
  1515. offset =
  1516. (((contents & howto->src_mask) >> 1) & 0x1ff8000) | ((contents & howto->src_mask) & 0x7fff);
  1517. offset += increment;
  1518. contents =
  1519. (contents & ~howto->
  1520. src_mask) | (((offset << 1) & howto->src_mask) & 0x3ff0000) | (offset & 0x7fff);
  1521. bfd_put_32 (abfd, contents, address);
  1522. break;
  1523. case R_SCORE16_11:
  1524. contents = bfd_get_16 (abfd, address);
  1525. offset = contents & howto->src_mask;
  1526. offset += increment;
  1527. contents = (contents & ~howto->src_mask) | (offset & howto->src_mask);
  1528. bfd_put_16 (abfd, contents, address);
  1529. break;
  1530. case R_SCORE16_PC8:
  1531. contents = bfd_get_16 (abfd, address);
  1532. offset = (contents & howto->src_mask) + ((increment >> 1) & 0xff);
  1533. contents = (contents & (~howto->src_mask)) | (offset & howto->src_mask);
  1534. bfd_put_16 (abfd, contents, address);
  1535. break;
  1536. case R_SCORE_GOT15:
  1537. case R_SCORE_GOT_LO16:
  1538. break;
  1539. default:
  1540. addend += increment;
  1541. contents = (contents & ~howto->dst_mask) | (addend & howto->dst_mask);
  1542. bfd_put_32 (abfd, contents, address);
  1543. break;
  1544. }
  1545. }
  1546. /* Perform a relocation as part of a final link. */
  1547. static bfd_reloc_status_type
  1548. score_elf_final_link_relocate (reloc_howto_type *howto,
  1549. bfd *input_bfd,
  1550. bfd *output_bfd,
  1551. asection *input_section,
  1552. bfd_byte *contents,
  1553. Elf_Internal_Rela *rel,
  1554. Elf_Internal_Rela *relocs,
  1555. bfd_vma symbol,
  1556. struct bfd_link_info *info,
  1557. const char *sym_name ATTRIBUTE_UNUSED,
  1558. int sym_flags ATTRIBUTE_UNUSED,
  1559. struct score_elf_link_hash_entry *h,
  1560. Elf_Internal_Sym *local_syms,
  1561. asection **local_sections,
  1562. bfd_boolean gp_disp_p)
  1563. {
  1564. unsigned long r_type;
  1565. unsigned long r_symndx;
  1566. bfd_byte *hit_data = contents + rel->r_offset;
  1567. bfd_vma addend;
  1568. /* The final GP value to be used for the relocatable, executable, or
  1569. shared object file being produced. */
  1570. bfd_vma gp = MINUS_ONE;
  1571. /* The place (section offset or address) of the storage unit being relocated. */
  1572. bfd_vma rel_addr;
  1573. /* The value of GP used to create the relocatable object. */
  1574. bfd_vma gp0 = MINUS_ONE;
  1575. /* The offset into the global offset table at which the address of the relocation entry
  1576. symbol, adjusted by the addend, resides during execution. */
  1577. bfd_vma g = MINUS_ONE;
  1578. /* TRUE if the symbol referred to by this relocation is a local symbol. */
  1579. bfd_boolean local_p;
  1580. /* The eventual value we will relocate. */
  1581. bfd_vma value = symbol;
  1582. unsigned long hi16_addend, hi16_offset, hi16_value, uvalue, offset, abs_value = 0;
  1583. Elf_Internal_Sym *sym = 0;
  1584. asection *sec = NULL;
  1585. bfd_boolean merge_p = 0;
  1586. if (elf_gp (output_bfd) == 0)
  1587. {
  1588. struct bfd_link_hash_entry *bh;
  1589. asection *o;
  1590. bh = bfd_link_hash_lookup (info->hash, "_gp", 0, 0, 1);
  1591. if (bh != NULL && bh->type == bfd_link_hash_defined)
  1592. elf_gp (output_bfd) = (bh->u.def.value
  1593. + bh->u.def.section->output_section->vma
  1594. + bh->u.def.section->output_offset);
  1595. else if (bfd_link_relocatable (info))
  1596. {
  1597. bfd_vma lo = -1;
  1598. /* Find the GP-relative section with the lowest offset. */
  1599. for (o = output_bfd->sections; o != NULL; o = o->next)
  1600. if (o->vma < lo)
  1601. lo = o->vma;
  1602. /* And calculate GP relative to that. */
  1603. elf_gp (output_bfd) = lo + ELF_SCORE_GP_OFFSET (input_bfd);
  1604. }
  1605. else
  1606. {
  1607. /* If the relocate_section function needs to do a reloc
  1608. involving the GP value, it should make a reloc_dangerous
  1609. callback to warn that GP is not defined. */
  1610. }
  1611. }
  1612. /* Parse the relocation. */
  1613. r_symndx = ELF32_R_SYM (rel->r_info);
  1614. r_type = ELF32_R_TYPE (rel->r_info);
  1615. rel_addr = (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
  1616. /* For hidden symbol. */
  1617. local_p = score_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE);
  1618. if (local_p)
  1619. {
  1620. sym = local_syms + r_symndx;
  1621. sec = local_sections[r_symndx];
  1622. symbol = sec->output_section->vma + sec->output_offset;
  1623. if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
  1624. || (sec->flags & SEC_MERGE))
  1625. symbol += sym->st_value;
  1626. if ((sec->flags & SEC_MERGE)
  1627. && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  1628. merge_p = 1;
  1629. }
  1630. if (r_type == R_SCORE_GOT15)
  1631. {
  1632. const Elf_Internal_Rela *relend;
  1633. const Elf_Internal_Rela *lo16_rel;
  1634. const struct elf_backend_data *bed;
  1635. bfd_vma lo_value = 0;
  1636. bed = get_elf_backend_data (output_bfd);
  1637. relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
  1638. lo16_rel = score_elf_next_relocation (input_bfd, R_SCORE_GOT_LO16, rel, relend);
  1639. if ((local_p) && (lo16_rel != NULL))
  1640. {
  1641. bfd_vma tmp = 0;
  1642. tmp = bfd_get_32 (input_bfd, contents + lo16_rel->r_offset);
  1643. lo_value = (((tmp >> 16) & 0x3) << 14) | ((tmp & 0x7fff) >> 1);
  1644. if (merge_p)
  1645. {
  1646. asection *msec = sec;
  1647. lo_value = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, lo_value);
  1648. lo_value -= symbol;
  1649. lo_value += msec->output_section->vma + msec->output_offset;
  1650. }
  1651. }
  1652. addend = lo_value;
  1653. }
  1654. else
  1655. {
  1656. addend = (bfd_get_32 (input_bfd, hit_data) >> howto->bitpos) & howto->src_mask;
  1657. }
  1658. /* Figure out the value of the symbol. */
  1659. if (local_p && !merge_p)
  1660. {
  1661. if (r_type == R_SCORE_GOT15)
  1662. {
  1663. const Elf_Internal_Rela *relend;
  1664. const Elf_Internal_Rela *lo16_rel;
  1665. const struct elf_backend_data *bed;
  1666. bfd_vma lo_value = 0;
  1667. value = bfd_get_32 (input_bfd, contents + rel->r_offset);
  1668. addend = value & 0x7fff;
  1669. if ((addend & 0x4000) == 0x4000)
  1670. addend |= 0xffffc000;
  1671. bed = get_elf_backend_data (output_bfd);
  1672. relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
  1673. lo16_rel = score_elf_next_relocation (input_bfd, R_SCORE_GOT_LO16, rel, relend);
  1674. if ((local_p) && (lo16_rel != NULL))
  1675. {
  1676. bfd_vma tmp = 0;
  1677. tmp = bfd_get_32 (input_bfd, contents + lo16_rel->r_offset);
  1678. lo_value = (((tmp >> 16) & 0x3) << 14) | ((tmp & 0x7fff) >> 1);
  1679. }
  1680. addend <<= 16;
  1681. addend += lo_value;
  1682. }
  1683. }
  1684. local_p = score_elf_local_relocation_p (input_bfd, rel, local_sections, TRUE);
  1685. /* If we haven't already determined the GOT offset, or the GP value,
  1686. and we're going to need it, get it now. */
  1687. switch (r_type)
  1688. {
  1689. case R_SCORE_CALL15:
  1690. case R_SCORE_GOT15:
  1691. if (!local_p)
  1692. {
  1693. g = score_elf_global_got_index (elf_hash_table (info)->dynobj,
  1694. (struct elf_link_hash_entry *) h);
  1695. if ((! elf_hash_table(info)->dynamic_sections_created
  1696. || (bfd_link_pic (info)
  1697. && (info->symbolic || h->root.dynindx == -1)
  1698. && h->root.def_regular)))
  1699. {
  1700. /* This is a static link or a -Bsymbolic link. The
  1701. symbol is defined locally, or was forced to be local.
  1702. We must initialize this entry in the GOT. */
  1703. bfd *tmpbfd = elf_hash_table (info)->dynobj;
  1704. asection *sgot = score_elf_got_section (tmpbfd, FALSE);
  1705. bfd_put_32 (tmpbfd, value, sgot->contents + g);
  1706. }
  1707. }
  1708. else if (r_type == R_SCORE_GOT15 || r_type == R_SCORE_CALL15)
  1709. {
  1710. /* There's no need to create a local GOT entry here; the
  1711. calculation for a local GOT15 entry does not involve G. */
  1712. ;
  1713. }
  1714. else
  1715. {
  1716. g = score_elf_local_got_index (output_bfd, input_bfd, info,
  1717. symbol + addend, r_symndx, h, r_type);
  1718. if (g == MINUS_ONE)
  1719. return bfd_reloc_outofrange;
  1720. }
  1721. /* Convert GOT indices to actual offsets. */
  1722. g = score_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
  1723. output_bfd, input_bfd, g);
  1724. break;
  1725. case R_SCORE_HI16:
  1726. case R_SCORE_LO16:
  1727. case R_SCORE_GPREL32:
  1728. gp0 = _bfd_get_gp_value (input_bfd);
  1729. gp = _bfd_get_gp_value (output_bfd);
  1730. break;
  1731. case R_SCORE_GP15:
  1732. gp = _bfd_get_gp_value (output_bfd);
  1733. default:
  1734. break;
  1735. }
  1736. switch (r_type)
  1737. {
  1738. case R_SCORE_NONE:
  1739. return bfd_reloc_ok;
  1740. case R_SCORE_ABS32:
  1741. case R_SCORE_REL32:
  1742. if ((bfd_link_pic (info)
  1743. || (elf_hash_table (info)->dynamic_sections_created
  1744. && h != NULL
  1745. && h->root.def_dynamic
  1746. && !h->root.def_regular))
  1747. && r_symndx != STN_UNDEF
  1748. && (input_section->flags & SEC_ALLOC) != 0)
  1749. {
  1750. /* If we're creating a shared library, or this relocation is against a symbol
  1751. in a shared library, then we can't know where the symbol will end up.
  1752. So, we create a relocation record in the output, and leave the job up
  1753. to the dynamic linker. */
  1754. value = addend;
  1755. if (!score_elf_create_dynamic_relocation (output_bfd, info, rel, h,
  1756. symbol, &value,
  1757. input_section))
  1758. return bfd_reloc_undefined;
  1759. }
  1760. else if (r_symndx == STN_UNDEF)
  1761. /* r_symndx will be STN_UNDEF (zero) only for relocs against symbols
  1762. from removed linkonce sections, or sections discarded by
  1763. a linker script. */
  1764. value = 0;
  1765. else
  1766. {
  1767. if (r_type != R_SCORE_REL32)
  1768. value = symbol + addend;
  1769. else
  1770. value = addend;
  1771. }
  1772. value &= howto->dst_mask;
  1773. bfd_put_32 (input_bfd, value, hit_data);
  1774. return bfd_reloc_ok;
  1775. case R_SCORE_ABS16:
  1776. value += addend;
  1777. if ((long) value > 0x7fff || (long) value < -0x8000)
  1778. return bfd_reloc_overflow;
  1779. bfd_put_16 (input_bfd, value, hit_data);
  1780. return bfd_reloc_ok;
  1781. case R_SCORE_24:
  1782. addend = bfd_get_32 (input_bfd, hit_data);
  1783. offset = (((addend & howto->src_mask) >> 1) & 0x1ff8000) | ((addend & howto->src_mask) & 0x7fff);
  1784. if ((offset & 0x1000000) != 0)
  1785. offset |= 0xfe000000;
  1786. value += offset;
  1787. abs_value = value - rel_addr;
  1788. if ((abs_value & 0xfe000000) != 0)
  1789. return bfd_reloc_overflow;
  1790. addend = (addend & ~howto->src_mask)
  1791. | (((value << 1) & howto->src_mask) & 0x3ff0000) | (value & 0x7fff);
  1792. bfd_put_32 (input_bfd, addend, hit_data);
  1793. return bfd_reloc_ok;
  1794. case R_SCORE_PC19:
  1795. addend = bfd_get_32 (input_bfd, hit_data);
  1796. offset = (((addend & howto->src_mask) & 0x3ff0000) >> 6) | ((addend & howto->src_mask) & 0x3ff);
  1797. if ((offset & 0x80000) != 0)
  1798. offset |= 0xfff00000;
  1799. abs_value = value = value - rel_addr + offset;
  1800. /* exceed 20 bit : overflow. */
  1801. if ((abs_value & 0x80000000) == 0x80000000)
  1802. abs_value = 0xffffffff - value + 1;
  1803. if ((abs_value & 0xfff80000) != 0)
  1804. return bfd_reloc_overflow;
  1805. addend = (addend & ~howto->src_mask)
  1806. | (((value << 6) & howto->src_mask) & 0x3ff0000) | (value & 0x3ff);
  1807. bfd_put_32 (input_bfd, addend, hit_data);
  1808. return bfd_reloc_ok;
  1809. case R_SCORE16_11:
  1810. addend = bfd_get_16 (input_bfd, hit_data);
  1811. offset = addend & howto->src_mask;
  1812. if ((offset & 0x800) != 0) /* Offset is negative. */
  1813. offset |= 0xfffff000;
  1814. value += offset;
  1815. abs_value = value - rel_addr;
  1816. if ((abs_value & 0xfffff000) != 0)
  1817. return bfd_reloc_overflow;
  1818. addend = (addend & ~howto->src_mask) | (value & howto->src_mask);
  1819. bfd_put_16 (input_bfd, addend, hit_data);
  1820. return bfd_reloc_ok;
  1821. case R_SCORE16_PC8:
  1822. addend = bfd_get_16 (input_bfd, hit_data);
  1823. offset = (addend & howto->src_mask) << 1;
  1824. if ((offset & 0x100) != 0) /* Offset is negative. */
  1825. offset |= 0xfffffe00;
  1826. abs_value = value = value - rel_addr + offset;
  1827. /* Sign bit + exceed 9 bit. */
  1828. if (((value & 0xffffff00) != 0) && ((value & 0xffffff00) != 0xffffff00))
  1829. return bfd_reloc_overflow;
  1830. value >>= 1;
  1831. addend = (addend & ~howto->src_mask) | (value & howto->src_mask);
  1832. bfd_put_16 (input_bfd, addend, hit_data);
  1833. return bfd_reloc_ok;
  1834. case R_SCORE_HI16:
  1835. return bfd_reloc_ok;
  1836. case R_SCORE_LO16:
  1837. hi16_addend = bfd_get_32 (input_bfd, hit_data - 4);
  1838. hi16_offset = ((((hi16_addend >> 16) & 0x3) << 15) | (hi16_addend & 0x7fff)) >> 1;
  1839. addend = bfd_get_32 (input_bfd, hit_data);
  1840. offset = ((((addend >> 16) & 0x3) << 15) | (addend & 0x7fff)) >> 1;
  1841. offset = (hi16_offset << 16) | (offset & 0xffff);
  1842. if (!gp_disp_p)
  1843. uvalue = value + offset;
  1844. else
  1845. uvalue = offset + gp - rel_addr + 4;
  1846. hi16_offset = (uvalue >> 16) << 1;
  1847. hi16_value = (hi16_addend & (~(howto->dst_mask)))
  1848. | (hi16_offset & 0x7fff) | ((hi16_offset << 1) & 0x30000);
  1849. bfd_put_32 (input_bfd, hi16_value, hit_data - 4);
  1850. offset = (uvalue & 0xffff) << 1;
  1851. value = (addend & (~(howto->dst_mask))) | (offset & 0x7fff) | ((offset << 1) & 0x30000);
  1852. bfd_put_32 (input_bfd, value, hit_data);
  1853. return bfd_reloc_ok;
  1854. case R_SCORE_GP15:
  1855. addend = bfd_get_32 (input_bfd, hit_data);
  1856. offset = addend & 0x7fff;
  1857. if ((offset & 0x4000) == 0x4000)
  1858. offset |= 0xffffc000;
  1859. value = value + offset - gp;
  1860. if (((value & 0xffffc000) != 0) && ((value & 0xffffc000) != 0xffffc000))
  1861. return bfd_reloc_overflow;
  1862. value = (addend & ~howto->src_mask) | (value & howto->src_mask);
  1863. bfd_put_32 (input_bfd, value, hit_data);
  1864. return bfd_reloc_ok;
  1865. case R_SCORE_GOT15:
  1866. case R_SCORE_CALL15:
  1867. if (local_p)
  1868. {
  1869. bfd_boolean forced;
  1870. /* The special case is when the symbol is forced to be local. We need the
  1871. full address in the GOT since no R_SCORE_GOT_LO16 relocation follows. */
  1872. forced = ! score_elf_local_relocation_p (input_bfd, rel,
  1873. local_sections, FALSE);
  1874. value = score_elf_got16_entry (output_bfd, input_bfd, info,
  1875. symbol + addend, forced);
  1876. if (value == MINUS_ONE)
  1877. return bfd_reloc_outofrange;
  1878. value = score_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
  1879. output_bfd, input_bfd, value);
  1880. }
  1881. else
  1882. {
  1883. value = g;
  1884. }
  1885. if ((long) value > 0x3fff || (long) value < -0x4000)
  1886. return bfd_reloc_overflow;
  1887. addend = bfd_get_32 (input_bfd, hit_data);
  1888. value = (addend & ~howto->dst_mask) | (value & howto->dst_mask);
  1889. bfd_put_32 (input_bfd, value, hit_data);
  1890. return bfd_reloc_ok;
  1891. case R_SCORE_GPREL32:
  1892. value = (addend + symbol + gp0 - gp);
  1893. value &= howto->dst_mask;
  1894. bfd_put_32 (input_bfd, value, hit_data);
  1895. return bfd_reloc_ok;
  1896. case R_SCORE_GOT_LO16:
  1897. addend = bfd_get_32 (input_bfd, hit_data);
  1898. value = (((addend >> 16) & 0x3) << 14) | ((addend & 0x7fff) >> 1);
  1899. value += symbol;
  1900. value = (addend & (~(howto->dst_mask))) | ((value & 0x3fff) << 1)
  1901. | (((value >> 14) & 0x3) << 16);
  1902. bfd_put_32 (input_bfd, value, hit_data);
  1903. return bfd_reloc_ok;
  1904. case R_SCORE_DUMMY_HI16:
  1905. return bfd_reloc_ok;
  1906. case R_SCORE_GNU_VTINHERIT:
  1907. case R_SCORE_GNU_VTENTRY:
  1908. /* We don't do anything with these at present. */
  1909. return bfd_reloc_continue;
  1910. default:
  1911. return bfd_reloc_notsupported;
  1912. }
  1913. }
  1914. /* Score backend functions. */
  1915. void
  1916. s7_bfd_score_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  1917. arelent *bfd_reloc,
  1918. Elf_Internal_Rela *elf_reloc)
  1919. {
  1920. unsigned int r_type;
  1921. r_type = ELF32_R_TYPE (elf_reloc->r_info);
  1922. if (r_type >= ARRAY_SIZE (elf32_score_howto_table))
  1923. bfd_reloc->howto = NULL;
  1924. else
  1925. bfd_reloc->howto = &elf32_score_howto_table[r_type];
  1926. }
  1927. /* Relocate an score ELF section. */
  1928. bfd_boolean
  1929. s7_bfd_score_elf_relocate_section (bfd *output_bfd,
  1930. struct bfd_link_info *info,
  1931. bfd *input_bfd,
  1932. asection *input_section,
  1933. bfd_byte *contents,
  1934. Elf_Internal_Rela *relocs,
  1935. Elf_Internal_Sym *local_syms,
  1936. asection **local_sections)
  1937. {
  1938. Elf_Internal_Shdr *symtab_hdr;
  1939. Elf_Internal_Rela *rel;
  1940. Elf_Internal_Rela *relend;
  1941. const char *name;
  1942. unsigned long offset;
  1943. unsigned long hi16_addend, hi16_offset, hi16_value, uvalue;
  1944. size_t extsymoff;
  1945. bfd_boolean gp_disp_p = FALSE;
  1946. /* Sort dynsym. */
  1947. if (elf_hash_table (info)->dynamic_sections_created)
  1948. {
  1949. bfd_size_type dynsecsymcount = 0;
  1950. if (bfd_link_pic (info))
  1951. {
  1952. asection * p;
  1953. const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  1954. for (p = output_bfd->sections; p ; p = p->next)
  1955. if ((p->flags & SEC_EXCLUDE) == 0
  1956. && (p->flags & SEC_ALLOC) != 0
  1957. && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
  1958. ++ dynsecsymcount;
  1959. }
  1960. if (!score_elf_sort_hash_table (info, dynsecsymcount + 1))
  1961. return FALSE;
  1962. }
  1963. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1964. extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
  1965. rel = relocs;
  1966. relend = relocs + input_section->reloc_count;
  1967. for (; rel < relend; rel++)
  1968. {
  1969. int r_type;
  1970. reloc_howto_type *howto;
  1971. unsigned long r_symndx;
  1972. Elf_Internal_Sym *sym;
  1973. asection *sec;
  1974. struct score_elf_link_hash_entry *h;
  1975. bfd_vma relocation = 0;
  1976. bfd_reloc_status_type r;
  1977. arelent bfd_reloc;
  1978. r_symndx = ELF32_R_SYM (rel->r_info);
  1979. r_type = ELF32_R_TYPE (rel->r_info);
  1980. s7_bfd_score_info_to_howto (input_bfd, &bfd_reloc, (Elf_Internal_Rela *) rel);
  1981. howto = bfd_reloc.howto;
  1982. h = NULL;
  1983. sym = NULL;
  1984. sec = NULL;
  1985. if (r_symndx < extsymoff)
  1986. {
  1987. sym = local_syms + r_symndx;
  1988. sec = local_sections[r_symndx];
  1989. relocation = sec->output_section->vma + sec->output_offset;
  1990. name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
  1991. if (!bfd_link_relocatable (info))
  1992. {
  1993. if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
  1994. || (sec->flags & SEC_MERGE))
  1995. {
  1996. relocation += sym->st_value;
  1997. }
  1998. if ((sec->flags & SEC_MERGE)
  1999. && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  2000. {
  2001. asection *msec;
  2002. bfd_vma addend, value;
  2003. switch (r_type)
  2004. {
  2005. case R_SCORE_HI16:
  2006. break;
  2007. case R_SCORE_LO16:
  2008. hi16_addend = bfd_get_32 (input_bfd, contents + rel->r_offset - 4);
  2009. hi16_offset = ((((hi16_addend >> 16) & 0x3) << 15) | (hi16_addend & 0x7fff)) >> 1;
  2010. value = bfd_get_32 (input_bfd, contents + rel->r_offset);
  2011. offset = ((((value >> 16) & 0x3) << 15) | (value & 0x7fff)) >> 1;
  2012. addend = (hi16_offset << 16) | (offset & 0xffff);
  2013. msec = sec;
  2014. addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend);
  2015. addend -= relocation;
  2016. addend += msec->output_section->vma + msec->output_offset;
  2017. uvalue = addend;
  2018. hi16_offset = (uvalue >> 16) << 1;
  2019. hi16_value = (hi16_addend & (~(howto->dst_mask)))
  2020. | (hi16_offset & 0x7fff) | ((hi16_offset << 1) & 0x30000);
  2021. bfd_put_32 (input_bfd, hi16_value, contents + rel->r_offset - 4);
  2022. offset = (uvalue & 0xffff) << 1;
  2023. value = (value & (~(howto->dst_mask)))
  2024. | (offset & 0x7fff) | ((offset << 1) & 0x30000);
  2025. bfd_put_32 (input_bfd, value, contents + rel->r_offset);
  2026. break;
  2027. case R_SCORE_GOT_LO16:
  2028. value = bfd_get_32 (input_bfd, contents + rel->r_offset);
  2029. addend = (((value >> 16) & 0x3) << 14) | ((value & 0x7fff) >> 1);
  2030. msec = sec;
  2031. addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend) - relocation;
  2032. addend += msec->output_section->vma + msec->output_offset;
  2033. value = (value & (~(howto->dst_mask))) | ((addend & 0x3fff) << 1)
  2034. | (((addend >> 14) & 0x3) << 16);
  2035. bfd_put_32 (input_bfd, value, contents + rel->r_offset);
  2036. break;
  2037. default:
  2038. value = bfd_get_32 (input_bfd, contents + rel->r_offset);
  2039. /* Get the (signed) value from the instruction. */
  2040. addend = value & howto->src_mask;
  2041. if (addend & ((howto->src_mask + 1) >> 1))
  2042. {
  2043. bfd_signed_vma mask;
  2044. mask = -1;
  2045. mask &= ~howto->src_mask;
  2046. addend |= mask;
  2047. }
  2048. msec = sec;
  2049. addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend) - relocation;
  2050. addend += msec->output_section->vma + msec->output_offset;
  2051. value = (value & ~howto->dst_mask) | (addend & howto->dst_mask);
  2052. bfd_put_32 (input_bfd, value, contents + rel->r_offset);
  2053. break;
  2054. }
  2055. }
  2056. }
  2057. }
  2058. else
  2059. {
  2060. /* For global symbols we look up the symbol in the hash-table. */
  2061. h = ((struct score_elf_link_hash_entry *)
  2062. elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
  2063. if (info->wrap_hash != NULL
  2064. && (input_section->flags & SEC_DEBUGGING) != 0)
  2065. h = ((struct score_elf_link_hash_entry *)
  2066. unwrap_hash_lookup (info, input_bfd, &h->root.root));
  2067. /* Find the real hash-table entry for this symbol. */
  2068. while (h->root.root.type == bfd_link_hash_indirect
  2069. || h->root.root.type == bfd_link_hash_warning)
  2070. h = (struct score_elf_link_hash_entry *) h->root.root.u.i.link;
  2071. /* Record the name of this symbol, for our caller. */
  2072. name = h->root.root.root.string;
  2073. /* See if this is the special GP_DISP_LABEL symbol. Note that such a
  2074. symbol must always be a global symbol. */
  2075. if (strcmp (name, GP_DISP_LABEL) == 0)
  2076. {
  2077. /* Relocations against GP_DISP_LABEL are permitted only with
  2078. R_SCORE_HI16 and R_SCORE_LO16 relocations. */
  2079. if (r_type != R_SCORE_HI16 && r_type != R_SCORE_LO16)
  2080. return bfd_reloc_notsupported;
  2081. gp_disp_p = TRUE;
  2082. }
  2083. /* If this symbol is defined, calculate its address. Note that
  2084. GP_DISP_LABEL is a magic symbol, always implicitly defined by the
  2085. linker, so it's inappropriate to check to see whether or not
  2086. its defined. */
  2087. else if ((h->root.root.type == bfd_link_hash_defined
  2088. || h->root.root.type == bfd_link_hash_defweak)
  2089. && h->root.root.u.def.section)
  2090. {
  2091. sec = h->root.root.u.def.section;
  2092. if (sec->output_section)
  2093. relocation = (h->root.root.u.def.value
  2094. + sec->output_section->vma
  2095. + sec->output_offset);
  2096. else
  2097. {
  2098. relocation = h->root.root.u.def.value;
  2099. }
  2100. }
  2101. else if (h->root.root.type == bfd_link_hash_undefweak)
  2102. /* We allow relocations against undefined weak symbols, giving
  2103. it the value zero, so that you can undefined weak functions
  2104. and check to see if they exist by looking at their addresses. */
  2105. relocation = 0;
  2106. else if (info->unresolved_syms_in_objects == RM_IGNORE
  2107. && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
  2108. relocation = 0;
  2109. else if (strcmp (name, "_DYNAMIC_LINK") == 0)
  2110. {
  2111. /* If this is a dynamic link, we should have created a _DYNAMIC_LINK symbol
  2112. in s7_bfd_score_elf_create_dynamic_sections. Otherwise, we should define
  2113. the symbol with a value of 0. */
  2114. BFD_ASSERT (! bfd_link_pic (info));
  2115. BFD_ASSERT (bfd_get_section_by_name (output_bfd, ".dynamic") == NULL);
  2116. relocation = 0;
  2117. }
  2118. else if (!bfd_link_relocatable (info))
  2119. {
  2120. if (! ((*info->callbacks->undefined_symbol)
  2121. (info, h->root.root.root.string, input_bfd,
  2122. input_section, rel->r_offset,
  2123. (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
  2124. || ELF_ST_VISIBILITY (h->root.other))))
  2125. return bfd_reloc_undefined;
  2126. relocation = 0;
  2127. }
  2128. }
  2129. if (sec != NULL && discarded_section (sec))
  2130. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  2131. rel, 1, relend, howto, 0, contents);
  2132. if (bfd_link_relocatable (info))
  2133. {
  2134. /* This is a relocatable link. We don't have to change
  2135. anything, unless the reloc is against a section symbol,
  2136. in which case we have to adjust according to where the
  2137. section symbol winds up in the output section. */
  2138. if (r_symndx < symtab_hdr->sh_info)
  2139. {
  2140. sym = local_syms + r_symndx;
  2141. if (r_type == R_SCORE_GOT15)
  2142. {
  2143. const Elf_Internal_Rela *lo16_rel;
  2144. const struct elf_backend_data *bed;
  2145. bfd_vma lo_addend = 0, lo_value = 0;
  2146. bfd_vma addend, value;
  2147. value = bfd_get_32 (input_bfd, contents + rel->r_offset);
  2148. addend = value & 0x7fff;
  2149. if ((addend & 0x4000) == 0x4000)
  2150. addend |= 0xffffc000;
  2151. bed = get_elf_backend_data (output_bfd);
  2152. relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
  2153. lo16_rel = score_elf_next_relocation (input_bfd, R_SCORE_GOT_LO16, rel, relend);
  2154. if (lo16_rel != NULL)
  2155. {
  2156. lo_value = bfd_get_32 (input_bfd, contents + lo16_rel->r_offset);
  2157. lo_addend = (((lo_value >> 16) & 0x3) << 14) | ((lo_value & 0x7fff) >> 1);
  2158. }
  2159. addend <<= 16;
  2160. addend += lo_addend;
  2161. if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  2162. addend += local_sections[r_symndx]->output_offset;
  2163. lo_addend = addend & 0xffff;
  2164. lo_value = (lo_value & (~(howto->dst_mask))) | ((lo_addend & 0x3fff) << 1)
  2165. | (((lo_addend >> 14) & 0x3) << 16);
  2166. bfd_put_32 (input_bfd, lo_value, contents + lo16_rel->r_offset);
  2167. addend = addend >> 16;
  2168. value = (value & ~howto->src_mask) | (addend & howto->src_mask);
  2169. bfd_put_32 (input_bfd, value, contents + rel->r_offset);
  2170. }
  2171. else if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  2172. {
  2173. sec = local_sections[r_symndx];
  2174. score_elf_add_to_rel (input_bfd, contents + rel->r_offset,
  2175. howto, (bfd_signed_vma) (sec->output_offset + sym->st_value));
  2176. }
  2177. }
  2178. continue;
  2179. }
  2180. /* This is a final link. */
  2181. r = score_elf_final_link_relocate (howto, input_bfd, output_bfd,
  2182. input_section, contents, rel, relocs,
  2183. relocation, info, name,
  2184. (h ? ELF_ST_TYPE ((unsigned int) h->root.root.type) :
  2185. ELF_ST_TYPE ((unsigned int) sym->st_info)), h, local_syms,
  2186. local_sections, gp_disp_p);
  2187. if (r != bfd_reloc_ok)
  2188. {
  2189. const char *msg = (const char *)0;
  2190. switch (r)
  2191. {
  2192. case bfd_reloc_overflow:
  2193. /* If the overflowing reloc was to an undefined symbol,
  2194. we have already printed one error message and there
  2195. is no point complaining again. */
  2196. if (((!h) || (h->root.root.type != bfd_link_hash_undefined))
  2197. && (!((*info->callbacks->reloc_overflow)
  2198. (info, NULL, name, howto->name, (bfd_vma) 0,
  2199. input_bfd, input_section, rel->r_offset))))
  2200. return FALSE;
  2201. break;
  2202. case bfd_reloc_undefined:
  2203. if (!((*info->callbacks->undefined_symbol)
  2204. (info, name, input_bfd, input_section, rel->r_offset, TRUE)))
  2205. return FALSE;
  2206. break;
  2207. case bfd_reloc_outofrange:
  2208. msg = _("internal error: out of range error");
  2209. goto common_error;
  2210. case bfd_reloc_notsupported:
  2211. msg = _("internal error: unsupported relocation error");
  2212. goto common_error;
  2213. case bfd_reloc_dangerous:
  2214. msg = _("internal error: dangerous error");
  2215. goto common_error;
  2216. default:
  2217. msg = _("internal error: unknown error");
  2218. /* fall through */
  2219. common_error:
  2220. if (!((*info->callbacks->warning)
  2221. (info, msg, name, input_bfd, input_section, rel->r_offset)))
  2222. return FALSE;
  2223. break;
  2224. }
  2225. }
  2226. }
  2227. return TRUE;
  2228. }
  2229. /* Look through the relocs for a section during the first phase, and
  2230. allocate space in the global offset table. */
  2231. bfd_boolean
  2232. s7_bfd_score_elf_check_relocs (bfd *abfd,
  2233. struct bfd_link_info *info,
  2234. asection *sec,
  2235. const Elf_Internal_Rela *relocs)
  2236. {
  2237. const char *name;
  2238. bfd *dynobj;
  2239. Elf_Internal_Shdr *symtab_hdr;
  2240. struct elf_link_hash_entry **sym_hashes;
  2241. struct score_got_info *g;
  2242. size_t extsymoff;
  2243. const Elf_Internal_Rela *rel;
  2244. const Elf_Internal_Rela *rel_end;
  2245. asection *sgot;
  2246. asection *sreloc;
  2247. const struct elf_backend_data *bed;
  2248. if (bfd_link_relocatable (info))
  2249. return TRUE;
  2250. dynobj = elf_hash_table (info)->dynobj;
  2251. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2252. sym_hashes = elf_sym_hashes (abfd);
  2253. extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
  2254. name = bfd_get_section_name (abfd, sec);
  2255. if (dynobj == NULL)
  2256. {
  2257. sgot = NULL;
  2258. g = NULL;
  2259. }
  2260. else
  2261. {
  2262. sgot = score_elf_got_section (dynobj, FALSE);
  2263. if (sgot == NULL)
  2264. g = NULL;
  2265. else
  2266. {
  2267. BFD_ASSERT (score_elf_section_data (sgot) != NULL);
  2268. g = score_elf_section_data (sgot)->u.got_info;
  2269. BFD_ASSERT (g != NULL);
  2270. }
  2271. }
  2272. sreloc = NULL;
  2273. bed = get_elf_backend_data (abfd);
  2274. rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
  2275. for (rel = relocs; rel < rel_end; ++rel)
  2276. {
  2277. unsigned long r_symndx;
  2278. unsigned int r_type;
  2279. struct elf_link_hash_entry *h;
  2280. r_symndx = ELF32_R_SYM (rel->r_info);
  2281. r_type = ELF32_R_TYPE (rel->r_info);
  2282. if (r_symndx < extsymoff)
  2283. {
  2284. h = NULL;
  2285. }
  2286. else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
  2287. {
  2288. (*_bfd_error_handler) (_("%s: Malformed reloc detected for section %s"), abfd, name);
  2289. bfd_set_error (bfd_error_bad_value);
  2290. return FALSE;
  2291. }
  2292. else
  2293. {
  2294. h = sym_hashes[r_symndx - extsymoff];
  2295. /* This may be an indirect symbol created because of a version. */
  2296. if (h != NULL)
  2297. {
  2298. while (h->root.type == bfd_link_hash_indirect)
  2299. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2300. /* PR15323, ref flags aren't set for references in the
  2301. same object. */
  2302. h->root.non_ir_ref = 1;
  2303. }
  2304. }
  2305. /* Some relocs require a global offset table. */
  2306. if (dynobj == NULL || sgot == NULL)
  2307. {
  2308. switch (r_type)
  2309. {
  2310. case R_SCORE_GOT15:
  2311. case R_SCORE_CALL15:
  2312. if (dynobj == NULL)
  2313. elf_hash_table (info)->dynobj = dynobj = abfd;
  2314. if (!score_elf_create_got_section (dynobj, info, FALSE))
  2315. return FALSE;
  2316. g = score_elf_got_info (dynobj, &sgot);
  2317. break;
  2318. case R_SCORE_ABS32:
  2319. case R_SCORE_REL32:
  2320. if (dynobj == NULL
  2321. && (bfd_link_pic (info) || h != NULL)
  2322. && (sec->flags & SEC_ALLOC) != 0)
  2323. elf_hash_table (info)->dynobj = dynobj = abfd;
  2324. break;
  2325. default:
  2326. break;
  2327. }
  2328. }
  2329. if (!h && (r_type == R_SCORE_GOT_LO16))
  2330. {
  2331. if (! score_elf_record_local_got_symbol (abfd, r_symndx, rel->r_addend, g))
  2332. return FALSE;
  2333. }
  2334. switch (r_type)
  2335. {
  2336. case R_SCORE_CALL15:
  2337. if (h == NULL)
  2338. {
  2339. (*_bfd_error_handler)
  2340. (_("%B: CALL15 reloc at 0x%lx not against global symbol"),
  2341. abfd, (unsigned long) rel->r_offset);
  2342. bfd_set_error (bfd_error_bad_value);
  2343. return FALSE;
  2344. }
  2345. else
  2346. {
  2347. /* This symbol requires a global offset table entry. */
  2348. if (! score_elf_record_global_got_symbol (h, abfd, info, g))
  2349. return FALSE;
  2350. /* We need a stub, not a plt entry for the undefined function. But we record
  2351. it as if it needs plt. See _bfd_elf_adjust_dynamic_symbol. */
  2352. h->needs_plt = 1;
  2353. h->type = STT_FUNC;
  2354. }
  2355. break;
  2356. case R_SCORE_GOT15:
  2357. if (h && ! score_elf_record_global_got_symbol (h, abfd, info, g))
  2358. return FALSE;
  2359. break;
  2360. case R_SCORE_ABS32:
  2361. case R_SCORE_REL32:
  2362. if ((bfd_link_pic (info) || h != NULL)
  2363. && (sec->flags & SEC_ALLOC) != 0)
  2364. {
  2365. if (sreloc == NULL)
  2366. {
  2367. sreloc = score_elf_rel_dyn_section (dynobj, TRUE);
  2368. if (sreloc == NULL)
  2369. return FALSE;
  2370. }
  2371. #define SCORE_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
  2372. if (bfd_link_pic (info))
  2373. {
  2374. /* When creating a shared object, we must copy these reloc types into
  2375. the output file as R_SCORE_REL32 relocs. We make room for this reloc
  2376. in the .rel.dyn reloc section. */
  2377. score_elf_allocate_dynamic_relocations (dynobj, 1);
  2378. if ((sec->flags & SCORE_READONLY_SECTION)
  2379. == SCORE_READONLY_SECTION)
  2380. /* We tell the dynamic linker that there are
  2381. relocations against the text segment. */
  2382. info->flags |= DF_TEXTREL;
  2383. }
  2384. else
  2385. {
  2386. struct score_elf_link_hash_entry *hscore;
  2387. /* We only need to copy this reloc if the symbol is
  2388. defined in a dynamic object. */
  2389. hscore = (struct score_elf_link_hash_entry *) h;
  2390. ++hscore->possibly_dynamic_relocs;
  2391. if ((sec->flags & SCORE_READONLY_SECTION)
  2392. == SCORE_READONLY_SECTION)
  2393. /* We need it to tell the dynamic linker if there
  2394. are relocations against the text segment. */
  2395. hscore->readonly_reloc = TRUE;
  2396. }
  2397. /* Even though we don't directly need a GOT entry for this symbol,
  2398. a symbol must have a dynamic symbol table index greater that
  2399. DT_SCORE_GOTSYM if there are dynamic relocations against it. */
  2400. if (h != NULL)
  2401. {
  2402. if (dynobj == NULL)
  2403. elf_hash_table (info)->dynobj = dynobj = abfd;
  2404. if (! score_elf_create_got_section (dynobj, info, TRUE))
  2405. return FALSE;
  2406. g = score_elf_got_info (dynobj, &sgot);
  2407. if (! score_elf_record_global_got_symbol (h, abfd, info, g))
  2408. return FALSE;
  2409. }
  2410. }
  2411. break;
  2412. /* This relocation describes the C++ object vtable hierarchy.
  2413. Reconstruct it for later use during GC. */
  2414. case R_SCORE_GNU_VTINHERIT:
  2415. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  2416. return FALSE;
  2417. break;
  2418. /* This relocation describes which C++ vtable entries are actually
  2419. used. Record for later use during GC. */
  2420. case R_SCORE_GNU_VTENTRY:
  2421. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
  2422. return FALSE;
  2423. break;
  2424. default:
  2425. break;
  2426. }
  2427. /* We must not create a stub for a symbol that has relocations
  2428. related to taking the function's address. */
  2429. switch (r_type)
  2430. {
  2431. default:
  2432. if (h != NULL)
  2433. {
  2434. struct score_elf_link_hash_entry *sh;
  2435. sh = (struct score_elf_link_hash_entry *) h;
  2436. sh->no_fn_stub = TRUE;
  2437. }
  2438. break;
  2439. case R_SCORE_CALL15:
  2440. break;
  2441. }
  2442. }
  2443. return TRUE;
  2444. }
  2445. bfd_boolean
  2446. s7_bfd_score_elf_add_symbol_hook (bfd *abfd,
  2447. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2448. Elf_Internal_Sym *sym,
  2449. const char **namep ATTRIBUTE_UNUSED,
  2450. flagword *flagsp ATTRIBUTE_UNUSED,
  2451. asection **secp,
  2452. bfd_vma *valp)
  2453. {
  2454. switch (sym->st_shndx)
  2455. {
  2456. case SHN_COMMON:
  2457. if (sym->st_size > elf_gp_size (abfd))
  2458. break;
  2459. /* Fall through. */
  2460. case SHN_SCORE_SCOMMON:
  2461. *secp = bfd_make_section_old_way (abfd, ".scommon");
  2462. (*secp)->flags |= SEC_IS_COMMON;
  2463. *valp = sym->st_size;
  2464. break;
  2465. }
  2466. return TRUE;
  2467. }
  2468. void
  2469. s7_bfd_score_elf_symbol_processing (bfd *abfd, asymbol *asym)
  2470. {
  2471. elf_symbol_type *elfsym;
  2472. elfsym = (elf_symbol_type *) asym;
  2473. switch (elfsym->internal_elf_sym.st_shndx)
  2474. {
  2475. case SHN_COMMON:
  2476. if (asym->value > elf_gp_size (abfd))
  2477. break;
  2478. /* Fall through. */
  2479. case SHN_SCORE_SCOMMON:
  2480. if (score_elf_scom_section.name == NULL)
  2481. {
  2482. /* Initialize the small common section. */
  2483. score_elf_scom_section.name = ".scommon";
  2484. score_elf_scom_section.flags = SEC_IS_COMMON;
  2485. score_elf_scom_section.output_section = &score_elf_scom_section;
  2486. score_elf_scom_section.symbol = &score_elf_scom_symbol;
  2487. score_elf_scom_section.symbol_ptr_ptr = &score_elf_scom_symbol_ptr;
  2488. score_elf_scom_symbol.name = ".scommon";
  2489. score_elf_scom_symbol.flags = BSF_SECTION_SYM;
  2490. score_elf_scom_symbol.section = &score_elf_scom_section;
  2491. score_elf_scom_symbol_ptr = &score_elf_scom_symbol;
  2492. }
  2493. asym->section = &score_elf_scom_section;
  2494. asym->value = elfsym->internal_elf_sym.st_size;
  2495. break;
  2496. }
  2497. }
  2498. int
  2499. s7_bfd_score_elf_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2500. const char *name ATTRIBUTE_UNUSED,
  2501. Elf_Internal_Sym *sym,
  2502. asection *input_sec,
  2503. struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
  2504. {
  2505. /* If we see a common symbol, which implies a relocatable link, then
  2506. if a symbol was small common in an input file, mark it as small
  2507. common in the output file. */
  2508. if (sym->st_shndx == SHN_COMMON && strcmp (input_sec->name, ".scommon") == 0)
  2509. sym->st_shndx = SHN_SCORE_SCOMMON;
  2510. return 1;
  2511. }
  2512. bfd_boolean
  2513. s7_bfd_score_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
  2514. asection *sec,
  2515. int *retval)
  2516. {
  2517. if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
  2518. {
  2519. *retval = SHN_SCORE_SCOMMON;
  2520. return TRUE;
  2521. }
  2522. return FALSE;
  2523. }
  2524. /* Adjust a symbol defined by a dynamic object and referenced by a
  2525. regular object. The current definition is in some section of the
  2526. dynamic object, but we're not including those sections. We have to
  2527. change the definition to something the rest of the link can understand. */
  2528. bfd_boolean
  2529. s7_bfd_score_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  2530. struct elf_link_hash_entry *h)
  2531. {
  2532. bfd *dynobj;
  2533. struct score_elf_link_hash_entry *hscore;
  2534. asection *s;
  2535. dynobj = elf_hash_table (info)->dynobj;
  2536. /* Make sure we know what is going on here. */
  2537. BFD_ASSERT (dynobj != NULL
  2538. && (h->needs_plt
  2539. || h->u.weakdef != NULL
  2540. || (h->def_dynamic && h->ref_regular && !h->def_regular)));
  2541. /* If this symbol is defined in a dynamic object, we need to copy
  2542. any R_SCORE_ABS32 or R_SCORE_REL32 relocs against it into the output
  2543. file. */
  2544. hscore = (struct score_elf_link_hash_entry *) h;
  2545. if (!bfd_link_relocatable (info)
  2546. && hscore->possibly_dynamic_relocs != 0
  2547. && (h->root.type == bfd_link_hash_defweak || !h->def_regular))
  2548. {
  2549. score_elf_allocate_dynamic_relocations (dynobj, hscore->possibly_dynamic_relocs);
  2550. if (hscore->readonly_reloc)
  2551. /* We tell the dynamic linker that there are relocations
  2552. against the text segment. */
  2553. info->flags |= DF_TEXTREL;
  2554. }
  2555. /* For a function, create a stub, if allowed. */
  2556. if (!hscore->no_fn_stub && h->needs_plt)
  2557. {
  2558. if (!elf_hash_table (info)->dynamic_sections_created)
  2559. return TRUE;
  2560. /* If this symbol is not defined in a regular file, then set
  2561. the symbol to the stub location. This is required to make
  2562. function pointers compare as equal between the normal
  2563. executable and the shared library. */
  2564. if (!h->def_regular)
  2565. {
  2566. /* We need .stub section. */
  2567. s = bfd_get_linker_section (dynobj, SCORE_ELF_STUB_SECTION_NAME);
  2568. BFD_ASSERT (s != NULL);
  2569. h->root.u.def.section = s;
  2570. h->root.u.def.value = s->size;
  2571. /* XXX Write this stub address somewhere. */
  2572. h->plt.offset = s->size;
  2573. /* Make room for this stub code. */
  2574. s->size += SCORE_FUNCTION_STUB_SIZE;
  2575. /* The last half word of the stub will be filled with the index
  2576. of this symbol in .dynsym section. */
  2577. return TRUE;
  2578. }
  2579. }
  2580. else if ((h->type == STT_FUNC) && !h->needs_plt)
  2581. {
  2582. /* This will set the entry for this symbol in the GOT to 0, and
  2583. the dynamic linker will take care of this. */
  2584. h->root.u.def.value = 0;
  2585. return TRUE;
  2586. }
  2587. /* If this is a weak symbol, and there is a real definition, the
  2588. processor independent code will have arranged for us to see the
  2589. real definition first, and we can just use the same value. */
  2590. if (h->u.weakdef != NULL)
  2591. {
  2592. BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
  2593. || h->u.weakdef->root.type == bfd_link_hash_defweak);
  2594. h->root.u.def.section = h->u.weakdef->root.u.def.section;
  2595. h->root.u.def.value = h->u.weakdef->root.u.def.value;
  2596. return TRUE;
  2597. }
  2598. /* This is a reference to a symbol defined by a dynamic object which
  2599. is not a function. */
  2600. return TRUE;
  2601. }
  2602. /* This function is called after all the input files have been read,
  2603. and the input sections have been assigned to output sections. */
  2604. bfd_boolean
  2605. s7_bfd_score_elf_always_size_sections (bfd *output_bfd,
  2606. struct bfd_link_info *info)
  2607. {
  2608. bfd *dynobj;
  2609. asection *s;
  2610. struct score_got_info *g;
  2611. int i;
  2612. bfd_size_type loadable_size = 0;
  2613. bfd_size_type local_gotno;
  2614. bfd *sub;
  2615. dynobj = elf_hash_table (info)->dynobj;
  2616. if (dynobj == NULL)
  2617. /* Relocatable links don't have it. */
  2618. return TRUE;
  2619. g = score_elf_got_info (dynobj, &s);
  2620. if (s == NULL)
  2621. return TRUE;
  2622. /* Calculate the total loadable size of the output. That will give us the
  2623. maximum number of GOT_PAGE entries required. */
  2624. for (sub = info->input_bfds; sub; sub = sub->link.next)
  2625. {
  2626. asection *subsection;
  2627. for (subsection = sub->sections;
  2628. subsection;
  2629. subsection = subsection->next)
  2630. {
  2631. if ((subsection->flags & SEC_ALLOC) == 0)
  2632. continue;
  2633. loadable_size += ((subsection->size + 0xf)
  2634. &~ (bfd_size_type) 0xf);
  2635. }
  2636. }
  2637. /* There has to be a global GOT entry for every symbol with
  2638. a dynamic symbol table index of DT_SCORE_GOTSYM or
  2639. higher. Therefore, it make sense to put those symbols
  2640. that need GOT entries at the end of the symbol table. We
  2641. do that here. */
  2642. if (! score_elf_sort_hash_table (info, 1))
  2643. return FALSE;
  2644. if (g->global_gotsym != NULL)
  2645. i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
  2646. else
  2647. /* If there are no global symbols, or none requiring
  2648. relocations, then GLOBAL_GOTSYM will be NULL. */
  2649. i = 0;
  2650. /* In the worst case, we'll get one stub per dynamic symbol. */
  2651. loadable_size += SCORE_FUNCTION_STUB_SIZE * i;
  2652. /* Assume there are two loadable segments consisting of
  2653. contiguous sections. Is 5 enough? */
  2654. local_gotno = (loadable_size >> 16) + 5;
  2655. g->local_gotno += local_gotno;
  2656. s->size += g->local_gotno * SCORE_ELF_GOT_SIZE (output_bfd);
  2657. g->global_gotno = i;
  2658. s->size += i * SCORE_ELF_GOT_SIZE (output_bfd);
  2659. score_elf_resolve_final_got_entries (g);
  2660. if (s->size > SCORE_ELF_GOT_MAX_SIZE (output_bfd))
  2661. {
  2662. /* Fixme. Error message or Warning message should be issued here. */
  2663. }
  2664. return TRUE;
  2665. }
  2666. /* Set the sizes of the dynamic sections. */
  2667. bfd_boolean
  2668. s7_bfd_score_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
  2669. {
  2670. bfd *dynobj;
  2671. asection *s;
  2672. bfd_boolean reltext;
  2673. dynobj = elf_hash_table (info)->dynobj;
  2674. BFD_ASSERT (dynobj != NULL);
  2675. if (elf_hash_table (info)->dynamic_sections_created)
  2676. {
  2677. /* Set the contents of the .interp section to the interpreter. */
  2678. if (!bfd_link_pic (info) && !info->nointerp)
  2679. {
  2680. s = bfd_get_linker_section (dynobj, ".interp");
  2681. BFD_ASSERT (s != NULL);
  2682. s->size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
  2683. s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
  2684. }
  2685. }
  2686. /* The check_relocs and adjust_dynamic_symbol entry points have
  2687. determined the sizes of the various dynamic sections. Allocate
  2688. memory for them. */
  2689. reltext = FALSE;
  2690. for (s = dynobj->sections; s != NULL; s = s->next)
  2691. {
  2692. const char *name;
  2693. if ((s->flags & SEC_LINKER_CREATED) == 0)
  2694. continue;
  2695. /* It's OK to base decisions on the section name, because none
  2696. of the dynobj section names depend upon the input files. */
  2697. name = bfd_get_section_name (dynobj, s);
  2698. if (CONST_STRNEQ (name, ".rel"))
  2699. {
  2700. if (s->size == 0)
  2701. {
  2702. /* We only strip the section if the output section name
  2703. has the same name. Otherwise, there might be several
  2704. input sections for this output section. FIXME: This
  2705. code is probably not needed these days anyhow, since
  2706. the linker now does not create empty output sections. */
  2707. if (s->output_section != NULL
  2708. && strcmp (name,
  2709. bfd_get_section_name (s->output_section->owner,
  2710. s->output_section)) == 0)
  2711. s->flags |= SEC_EXCLUDE;
  2712. }
  2713. else
  2714. {
  2715. const char *outname;
  2716. asection *target;
  2717. /* If this relocation section applies to a read only
  2718. section, then we probably need a DT_TEXTREL entry.
  2719. If the relocation section is .rel.dyn, we always
  2720. assert a DT_TEXTREL entry rather than testing whether
  2721. there exists a relocation to a read only section or
  2722. not. */
  2723. outname = bfd_get_section_name (output_bfd, s->output_section);
  2724. target = bfd_get_section_by_name (output_bfd, outname + 4);
  2725. if ((target != NULL
  2726. && (target->flags & SEC_READONLY) != 0
  2727. && (target->flags & SEC_ALLOC) != 0) || strcmp (outname, ".rel.dyn") == 0)
  2728. reltext = TRUE;
  2729. /* We use the reloc_count field as a counter if we need
  2730. to copy relocs into the output file. */
  2731. if (strcmp (name, ".rel.dyn") != 0)
  2732. s->reloc_count = 0;
  2733. }
  2734. }
  2735. else if (CONST_STRNEQ (name, ".got"))
  2736. {
  2737. /* s7_bfd_score_elf_always_size_sections() has already done
  2738. most of the work, but some symbols may have been mapped
  2739. to versions that we must now resolve in the got_entries
  2740. hash tables. */
  2741. }
  2742. else if (strcmp (name, SCORE_ELF_STUB_SECTION_NAME) == 0)
  2743. {
  2744. /* IRIX rld assumes that the function stub isn't at the end
  2745. of .text section. So put a dummy. XXX */
  2746. s->size += SCORE_FUNCTION_STUB_SIZE;
  2747. }
  2748. else if (! CONST_STRNEQ (name, ".init"))
  2749. {
  2750. /* It's not one of our sections, so don't allocate space. */
  2751. continue;
  2752. }
  2753. /* Allocate memory for the section contents. */
  2754. s->contents = bfd_zalloc (dynobj, s->size);
  2755. if (s->contents == NULL && s->size != 0)
  2756. {
  2757. bfd_set_error (bfd_error_no_memory);
  2758. return FALSE;
  2759. }
  2760. }
  2761. if (elf_hash_table (info)->dynamic_sections_created)
  2762. {
  2763. /* Add some entries to the .dynamic section. We fill in the
  2764. values later, in s7_bfd_score_elf_finish_dynamic_sections, but we
  2765. must add the entries now so that we get the correct size for
  2766. the .dynamic section. The DT_DEBUG entry is filled in by the
  2767. dynamic linker and used by the debugger. */
  2768. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
  2769. return FALSE;
  2770. if (reltext)
  2771. info->flags |= DF_TEXTREL;
  2772. if ((info->flags & DF_TEXTREL) != 0)
  2773. {
  2774. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
  2775. return FALSE;
  2776. }
  2777. if (! SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
  2778. return FALSE;
  2779. if (score_elf_rel_dyn_section (dynobj, FALSE))
  2780. {
  2781. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
  2782. return FALSE;
  2783. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
  2784. return FALSE;
  2785. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
  2786. return FALSE;
  2787. }
  2788. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_BASE_ADDRESS, 0))
  2789. return FALSE;
  2790. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_LOCAL_GOTNO, 0))
  2791. return FALSE;
  2792. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_SYMTABNO, 0))
  2793. return FALSE;
  2794. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_UNREFEXTNO, 0))
  2795. return FALSE;
  2796. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_GOTSYM, 0))
  2797. return FALSE;
  2798. if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_HIPAGENO, 0))
  2799. return FALSE;
  2800. }
  2801. return TRUE;
  2802. }
  2803. bfd_boolean
  2804. s7_bfd_score_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  2805. {
  2806. struct elf_link_hash_entry *h;
  2807. struct bfd_link_hash_entry *bh;
  2808. flagword flags;
  2809. asection *s;
  2810. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  2811. | SEC_LINKER_CREATED | SEC_READONLY);
  2812. /* ABI requests the .dynamic section to be read only. */
  2813. s = bfd_get_linker_section (abfd, ".dynamic");
  2814. if (s != NULL)
  2815. {
  2816. if (!bfd_set_section_flags (abfd, s, flags))
  2817. return FALSE;
  2818. }
  2819. /* We need to create .got section. */
  2820. if (!score_elf_create_got_section (abfd, info, FALSE))
  2821. return FALSE;
  2822. if (!score_elf_rel_dyn_section (elf_hash_table (info)->dynobj, TRUE))
  2823. return FALSE;
  2824. /* Create .stub section. */
  2825. if (bfd_get_linker_section (abfd, SCORE_ELF_STUB_SECTION_NAME) == NULL)
  2826. {
  2827. s = bfd_make_section_anyway_with_flags (abfd, SCORE_ELF_STUB_SECTION_NAME,
  2828. flags | SEC_CODE);
  2829. if (s == NULL
  2830. || !bfd_set_section_alignment (abfd, s, 2))
  2831. return FALSE;
  2832. }
  2833. if (!bfd_link_pic (info))
  2834. {
  2835. const char *name;
  2836. name = "_DYNAMIC_LINK";
  2837. bh = NULL;
  2838. if (!(_bfd_generic_link_add_one_symbol
  2839. (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr,
  2840. (bfd_vma) 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
  2841. return FALSE;
  2842. h = (struct elf_link_hash_entry *) bh;
  2843. h->non_elf = 0;
  2844. h->def_regular = 1;
  2845. h->type = STT_SECTION;
  2846. if (!bfd_elf_link_record_dynamic_symbol (info, h))
  2847. return FALSE;
  2848. }
  2849. return TRUE;
  2850. }
  2851. /* Finish up dynamic symbol handling. We set the contents of various
  2852. dynamic sections here. */
  2853. bfd_boolean
  2854. s7_bfd_score_elf_finish_dynamic_symbol (bfd *output_bfd,
  2855. struct bfd_link_info *info,
  2856. struct elf_link_hash_entry *h,
  2857. Elf_Internal_Sym *sym)
  2858. {
  2859. bfd *dynobj;
  2860. asection *sgot;
  2861. struct score_got_info *g;
  2862. const char *name;
  2863. dynobj = elf_hash_table (info)->dynobj;
  2864. if (h->plt.offset != MINUS_ONE)
  2865. {
  2866. asection *s;
  2867. bfd_byte stub[SCORE_FUNCTION_STUB_SIZE];
  2868. /* This symbol has a stub. Set it up. */
  2869. BFD_ASSERT (h->dynindx != -1);
  2870. s = bfd_get_linker_section (dynobj, SCORE_ELF_STUB_SECTION_NAME);
  2871. BFD_ASSERT (s != NULL);
  2872. /* FIXME: Can h->dynindex be more than 64K? */
  2873. if (h->dynindx & 0xffff0000)
  2874. return FALSE;
  2875. /* Fill the stub. */
  2876. bfd_put_32 (output_bfd, STUB_LW, stub);
  2877. bfd_put_32 (output_bfd, STUB_MOVE, stub + 4);
  2878. bfd_put_32 (output_bfd, STUB_LI16 | (h->dynindx << 1), stub + 8);
  2879. bfd_put_32 (output_bfd, STUB_BRL, stub + 12);
  2880. BFD_ASSERT (h->plt.offset <= s->size);
  2881. memcpy (s->contents + h->plt.offset, stub, SCORE_FUNCTION_STUB_SIZE);
  2882. /* Mark the symbol as undefined. plt.offset != -1 occurs
  2883. only for the referenced symbol. */
  2884. sym->st_shndx = SHN_UNDEF;
  2885. /* The run-time linker uses the st_value field of the symbol
  2886. to reset the global offset table entry for this external
  2887. to its stub address when unlinking a shared object. */
  2888. sym->st_value = (s->output_section->vma + s->output_offset + h->plt.offset);
  2889. }
  2890. BFD_ASSERT (h->dynindx != -1 || h->forced_local);
  2891. sgot = score_elf_got_section (dynobj, FALSE);
  2892. BFD_ASSERT (sgot != NULL);
  2893. BFD_ASSERT (score_elf_section_data (sgot) != NULL);
  2894. g = score_elf_section_data (sgot)->u.got_info;
  2895. BFD_ASSERT (g != NULL);
  2896. /* Run through the global symbol table, creating GOT entries for all
  2897. the symbols that need them. */
  2898. if (g->global_gotsym != NULL && h->dynindx >= g->global_gotsym->dynindx)
  2899. {
  2900. bfd_vma offset;
  2901. bfd_vma value;
  2902. value = sym->st_value;
  2903. offset = score_elf_global_got_index (dynobj, h);
  2904. bfd_put_32 (output_bfd, value, sgot->contents + offset);
  2905. }
  2906. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  2907. name = h->root.root.string;
  2908. if (h == elf_hash_table (info)->hdynamic
  2909. || h == elf_hash_table (info)->hgot)
  2910. sym->st_shndx = SHN_ABS;
  2911. else if (strcmp (name, "_DYNAMIC_LINK") == 0)
  2912. {
  2913. sym->st_shndx = SHN_ABS;
  2914. sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  2915. sym->st_value = 1;
  2916. }
  2917. else if (strcmp (name, GP_DISP_LABEL) == 0)
  2918. {
  2919. sym->st_shndx = SHN_ABS;
  2920. sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  2921. sym->st_value = elf_gp (output_bfd);
  2922. }
  2923. return TRUE;
  2924. }
  2925. /* Finish up the dynamic sections. */
  2926. bfd_boolean
  2927. s7_bfd_score_elf_finish_dynamic_sections (bfd *output_bfd,
  2928. struct bfd_link_info *info)
  2929. {
  2930. bfd *dynobj;
  2931. asection *sdyn;
  2932. asection *sgot;
  2933. asection *s;
  2934. struct score_got_info *g;
  2935. dynobj = elf_hash_table (info)->dynobj;
  2936. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2937. sgot = score_elf_got_section (dynobj, FALSE);
  2938. if (sgot == NULL)
  2939. g = NULL;
  2940. else
  2941. {
  2942. BFD_ASSERT (score_elf_section_data (sgot) != NULL);
  2943. g = score_elf_section_data (sgot)->u.got_info;
  2944. BFD_ASSERT (g != NULL);
  2945. }
  2946. if (elf_hash_table (info)->dynamic_sections_created)
  2947. {
  2948. bfd_byte *b;
  2949. BFD_ASSERT (sdyn != NULL);
  2950. BFD_ASSERT (g != NULL);
  2951. for (b = sdyn->contents;
  2952. b < sdyn->contents + sdyn->size;
  2953. b += SCORE_ELF_DYN_SIZE (dynobj))
  2954. {
  2955. Elf_Internal_Dyn dyn;
  2956. const char *name;
  2957. size_t elemsize;
  2958. bfd_boolean swap_out_p;
  2959. /* Read in the current dynamic entry. */
  2960. (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
  2961. /* Assume that we're going to modify it and write it out. */
  2962. swap_out_p = TRUE;
  2963. switch (dyn.d_tag)
  2964. {
  2965. case DT_RELENT:
  2966. s = score_elf_rel_dyn_section (dynobj, FALSE);
  2967. BFD_ASSERT (s != NULL);
  2968. dyn.d_un.d_val = SCORE_ELF_REL_SIZE (dynobj);
  2969. break;
  2970. case DT_STRSZ:
  2971. /* Rewrite DT_STRSZ. */
  2972. dyn.d_un.d_val = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
  2973. break;
  2974. case DT_PLTGOT:
  2975. name = ".got";
  2976. s = bfd_get_section_by_name (output_bfd, name);
  2977. BFD_ASSERT (s != NULL);
  2978. dyn.d_un.d_ptr = s->vma;
  2979. break;
  2980. case DT_SCORE_BASE_ADDRESS:
  2981. s = output_bfd->sections;
  2982. BFD_ASSERT (s != NULL);
  2983. dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
  2984. break;
  2985. case DT_SCORE_LOCAL_GOTNO:
  2986. dyn.d_un.d_val = g->local_gotno;
  2987. break;
  2988. case DT_SCORE_UNREFEXTNO:
  2989. /* The index into the dynamic symbol table which is the
  2990. entry of the first external symbol that is not
  2991. referenced within the same object. */
  2992. dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
  2993. break;
  2994. case DT_SCORE_GOTSYM:
  2995. if (g->global_gotsym)
  2996. {
  2997. dyn.d_un.d_val = g->global_gotsym->dynindx;
  2998. break;
  2999. }
  3000. /* In case if we don't have global got symbols we default
  3001. to setting DT_SCORE_GOTSYM to the same value as
  3002. DT_SCORE_SYMTABNO, so we just fall through. */
  3003. case DT_SCORE_SYMTABNO:
  3004. name = ".dynsym";
  3005. elemsize = SCORE_ELF_SYM_SIZE (output_bfd);
  3006. s = bfd_get_section_by_name (output_bfd, name);
  3007. BFD_ASSERT (s != NULL);
  3008. dyn.d_un.d_val = s->size / elemsize;
  3009. break;
  3010. case DT_SCORE_HIPAGENO:
  3011. dyn.d_un.d_val = g->local_gotno - SCORE_RESERVED_GOTNO;
  3012. break;
  3013. default:
  3014. swap_out_p = FALSE;
  3015. break;
  3016. }
  3017. if (swap_out_p)
  3018. (*get_elf_backend_data (dynobj)->s->swap_dyn_out) (dynobj, &dyn, b);
  3019. }
  3020. }
  3021. /* The first entry of the global offset table will be filled at
  3022. runtime. The second entry will be used by some runtime loaders.
  3023. This isn't the case of IRIX rld. */
  3024. if (sgot != NULL && sgot->size > 0)
  3025. {
  3026. bfd_put_32 (output_bfd, 0, sgot->contents);
  3027. bfd_put_32 (output_bfd, 0x80000000, sgot->contents + SCORE_ELF_GOT_SIZE (output_bfd));
  3028. }
  3029. if (sgot != NULL)
  3030. elf_section_data (sgot->output_section)->this_hdr.sh_entsize
  3031. = SCORE_ELF_GOT_SIZE (output_bfd);
  3032. /* We need to sort the entries of the dynamic relocation section. */
  3033. s = score_elf_rel_dyn_section (dynobj, FALSE);
  3034. if (s != NULL && s->size > (bfd_vma)2 * SCORE_ELF_REL_SIZE (output_bfd))
  3035. {
  3036. reldyn_sorting_bfd = output_bfd;
  3037. qsort ((Elf32_External_Rel *) s->contents + 1, s->reloc_count - 1,
  3038. sizeof (Elf32_External_Rel), score_elf_sort_dynamic_relocs);
  3039. }
  3040. return TRUE;
  3041. }
  3042. /* This function set up the ELF section header for a BFD section in preparation for writing
  3043. it out. This is where the flags and type fields are set for unusual sections. */
  3044. bfd_boolean
  3045. s7_bfd_score_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
  3046. Elf_Internal_Shdr *hdr,
  3047. asection *sec)
  3048. {
  3049. const char *name;
  3050. name = bfd_get_section_name (abfd, sec);
  3051. if (strcmp (name, ".got") == 0
  3052. || strcmp (name, ".srdata") == 0
  3053. || strcmp (name, ".sdata") == 0
  3054. || strcmp (name, ".sbss") == 0)
  3055. hdr->sh_flags |= SHF_SCORE_GPREL;
  3056. return TRUE;
  3057. }
  3058. /* This function do additional processing on the ELF section header before writing
  3059. it out. This is used to set the flags and type fields for some sections. */
  3060. /* assign_file_positions_except_relocs() check section flag and if it is allocatable,
  3061. warning message will be issued. backend_fake_section is called before
  3062. assign_file_positions_except_relocs(); backend_section_processing after it. so, we
  3063. modify section flag there, but not backend_fake_section. */
  3064. bfd_boolean
  3065. s7_bfd_score_elf_section_processing (bfd *abfd ATTRIBUTE_UNUSED, Elf_Internal_Shdr *hdr)
  3066. {
  3067. if (hdr->bfd_section != NULL)
  3068. {
  3069. const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
  3070. if (strcmp (name, ".sdata") == 0)
  3071. {
  3072. hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_SCORE_GPREL;
  3073. hdr->sh_type = SHT_PROGBITS;
  3074. }
  3075. else if (strcmp (name, ".sbss") == 0)
  3076. {
  3077. hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_SCORE_GPREL;
  3078. hdr->sh_type = SHT_NOBITS;
  3079. }
  3080. else if (strcmp (name, ".srdata") == 0)
  3081. {
  3082. hdr->sh_flags |= SHF_ALLOC | SHF_SCORE_GPREL;
  3083. hdr->sh_type = SHT_PROGBITS;
  3084. }
  3085. }
  3086. return TRUE;
  3087. }
  3088. bfd_boolean
  3089. s7_bfd_score_elf_write_section (bfd *output_bfd, asection *sec, bfd_byte *contents)
  3090. {
  3091. bfd_byte *to, *from, *end;
  3092. int i;
  3093. if (strcmp (sec->name, ".pdr") != 0)
  3094. return FALSE;
  3095. if (score_elf_section_data (sec)->u.tdata == NULL)
  3096. return FALSE;
  3097. to = contents;
  3098. end = contents + sec->size;
  3099. for (from = contents, i = 0; from < end; from += PDR_SIZE, i++)
  3100. {
  3101. if ((score_elf_section_data (sec)->u.tdata)[i] == 1)
  3102. continue;
  3103. if (to != from)
  3104. memcpy (to, from, PDR_SIZE);
  3105. to += PDR_SIZE;
  3106. }
  3107. bfd_set_section_contents (output_bfd, sec->output_section, contents,
  3108. (file_ptr) sec->output_offset, sec->size);
  3109. return TRUE;
  3110. }
  3111. /* Copy data from a SCORE ELF indirect symbol to its direct symbol, hiding the old
  3112. indirect symbol. Process additional relocation information. */
  3113. void
  3114. s7_bfd_score_elf_copy_indirect_symbol (struct bfd_link_info *info,
  3115. struct elf_link_hash_entry *dir,
  3116. struct elf_link_hash_entry *ind)
  3117. {
  3118. struct score_elf_link_hash_entry *dirscore, *indscore;
  3119. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  3120. if (ind->root.type != bfd_link_hash_indirect)
  3121. return;
  3122. dirscore = (struct score_elf_link_hash_entry *) dir;
  3123. indscore = (struct score_elf_link_hash_entry *) ind;
  3124. dirscore->possibly_dynamic_relocs += indscore->possibly_dynamic_relocs;
  3125. if (indscore->readonly_reloc)
  3126. dirscore->readonly_reloc = TRUE;
  3127. if (indscore->no_fn_stub)
  3128. dirscore->no_fn_stub = TRUE;
  3129. }
  3130. /* Remove information about discarded functions from other sections which mention them. */
  3131. bfd_boolean
  3132. s7_bfd_score_elf_discard_info (bfd *abfd,
  3133. struct elf_reloc_cookie *cookie,
  3134. struct bfd_link_info *info)
  3135. {
  3136. asection *o;
  3137. bfd_boolean ret = FALSE;
  3138. unsigned char *tdata;
  3139. size_t i, skip;
  3140. o = bfd_get_section_by_name (abfd, ".pdr");
  3141. if ((!o) || (o->size == 0) || (o->size % PDR_SIZE != 0)
  3142. || (o->output_section != NULL && bfd_is_abs_section (o->output_section)))
  3143. return FALSE;
  3144. tdata = bfd_zmalloc (o->size / PDR_SIZE);
  3145. if (!tdata)
  3146. return FALSE;
  3147. cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, info->keep_memory);
  3148. if (!cookie->rels)
  3149. {
  3150. free (tdata);
  3151. return FALSE;
  3152. }
  3153. cookie->rel = cookie->rels;
  3154. cookie->relend = cookie->rels + o->reloc_count;
  3155. for (i = 0, skip = 0; i < o->size; i++)
  3156. {
  3157. if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
  3158. {
  3159. tdata[i] = 1;
  3160. skip++;
  3161. }
  3162. }
  3163. if (skip != 0)
  3164. {
  3165. score_elf_section_data (o)->u.tdata = tdata;
  3166. o->size -= skip * PDR_SIZE;
  3167. ret = TRUE;
  3168. }
  3169. else
  3170. free (tdata);
  3171. if (!info->keep_memory)
  3172. free (cookie->rels);
  3173. return ret;
  3174. }
  3175. /* Signal that discard_info() has removed the discarded relocations for this section. */
  3176. bfd_boolean
  3177. s7_bfd_score_elf_ignore_discarded_relocs (asection *sec)
  3178. {
  3179. if (strcmp (sec->name, ".pdr") == 0)
  3180. return TRUE;
  3181. return FALSE;
  3182. }
  3183. /* Return the section that should be marked against GC for a given
  3184. relocation. */
  3185. asection *
  3186. s7_bfd_score_elf_gc_mark_hook (asection *sec,
  3187. struct bfd_link_info *info,
  3188. Elf_Internal_Rela *rel,
  3189. struct elf_link_hash_entry *h,
  3190. Elf_Internal_Sym *sym)
  3191. {
  3192. if (h != NULL)
  3193. switch (ELF32_R_TYPE (rel->r_info))
  3194. {
  3195. case R_SCORE_GNU_VTINHERIT:
  3196. case R_SCORE_GNU_VTENTRY:
  3197. return NULL;
  3198. }
  3199. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  3200. }
  3201. /* Support for core dump NOTE sections. */
  3202. bfd_boolean
  3203. s7_bfd_score_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  3204. {
  3205. int offset;
  3206. unsigned int raw_size;
  3207. switch (note->descsz)
  3208. {
  3209. default:
  3210. return FALSE;
  3211. case 272: /* Linux/Score elf_prstatus */
  3212. /* pr_cursig */
  3213. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  3214. /* pr_pid */
  3215. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  3216. /* pr_reg */
  3217. offset = 72;
  3218. /* sizeof(elf_gregset_t) */
  3219. raw_size = 196;
  3220. break;
  3221. }
  3222. /* Make a ".reg/999" section. */
  3223. return _bfd_elfcore_make_pseudosection (abfd, ".reg", raw_size,
  3224. note->descpos + offset);
  3225. }
  3226. bfd_boolean
  3227. s7_bfd_score_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  3228. {
  3229. switch (note->descsz)
  3230. {
  3231. default:
  3232. return FALSE;
  3233. case 128: /* Linux/Score elf_prpsinfo. */
  3234. /* pr_fname */
  3235. elf_tdata (abfd)->core->program
  3236. = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
  3237. /* pr_psargs */
  3238. elf_tdata (abfd)->core->command
  3239. = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
  3240. break;
  3241. }
  3242. /* Note that for some reason, a spurious space is tacked
  3243. onto the end of the args in some (at least one anyway)
  3244. implementations, so strip it off if it exists. */
  3245. {
  3246. char *command = elf_tdata (abfd)->core->command;
  3247. int n = strlen (command);
  3248. if (0 < n && command[n - 1] == ' ')
  3249. command[n - 1] = '\0';
  3250. }
  3251. return TRUE;
  3252. }
  3253. /* Score BFD functions. */
  3254. reloc_howto_type *
  3255. s7_elf32_score_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, bfd_reloc_code_real_type code)
  3256. {
  3257. unsigned int i;
  3258. for (i = 0; i < ARRAY_SIZE (elf32_score_reloc_map); i++)
  3259. if (elf32_score_reloc_map[i].bfd_reloc_val == code)
  3260. return &elf32_score_howto_table[elf32_score_reloc_map[i].elf_reloc_val];
  3261. return NULL;
  3262. }
  3263. bfd_boolean
  3264. s7_elf32_score_print_private_bfd_data (bfd *abfd, void * ptr)
  3265. {
  3266. FILE *file = (FILE *) ptr;
  3267. BFD_ASSERT (abfd != NULL && ptr != NULL);
  3268. /* Print normal ELF private data. */
  3269. _bfd_elf_print_private_bfd_data (abfd, ptr);
  3270. /* xgettext:c-format */
  3271. fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
  3272. if (elf_elfheader (abfd)->e_flags & EF_SCORE_PIC)
  3273. {
  3274. fprintf (file, _(" [pic]"));
  3275. }
  3276. if (elf_elfheader (abfd)->e_flags & EF_SCORE_FIXDEP)
  3277. {
  3278. fprintf (file, _(" [fix dep]"));
  3279. }
  3280. fputc ('\n', file);
  3281. return TRUE;
  3282. }
  3283. bfd_boolean
  3284. s7_elf32_score_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
  3285. {
  3286. flagword in_flags;
  3287. flagword out_flags;
  3288. if (!_bfd_generic_verify_endian_match (ibfd, obfd))
  3289. return FALSE;
  3290. in_flags = elf_elfheader (ibfd)->e_flags;
  3291. out_flags = elf_elfheader (obfd)->e_flags;
  3292. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  3293. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  3294. return TRUE;
  3295. in_flags = elf_elfheader (ibfd)->e_flags;
  3296. out_flags = elf_elfheader (obfd)->e_flags;
  3297. if (! elf_flags_init (obfd))
  3298. {
  3299. elf_flags_init (obfd) = TRUE;
  3300. elf_elfheader (obfd)->e_flags = in_flags;
  3301. if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
  3302. && bfd_get_arch_info (obfd)->the_default)
  3303. {
  3304. return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
  3305. }
  3306. return TRUE;
  3307. }
  3308. if (((in_flags & EF_SCORE_PIC) != 0) != ((out_flags & EF_SCORE_PIC) != 0))
  3309. {
  3310. (*_bfd_error_handler) (_("%B: warning: linking PIC files with non-PIC files"), ibfd);
  3311. }
  3312. /* Maybe dependency fix compatibility should be checked here. */
  3313. return TRUE;
  3314. }
  3315. bfd_boolean
  3316. s7_elf32_score_new_section_hook (bfd *abfd, asection *sec)
  3317. {
  3318. struct _score_elf_section_data *sdata;
  3319. bfd_size_type amt = sizeof (*sdata);
  3320. sdata = bfd_zalloc (abfd, amt);
  3321. if (sdata == NULL)
  3322. return FALSE;
  3323. sec->used_by_bfd = sdata;
  3324. return _bfd_elf_new_section_hook (abfd, sec);
  3325. }
  3326. #define elf_backend_omit_section_dynsym \
  3327. ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)