elf32-score7.c 114 KB

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