elfnn-loongarch.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425
  1. /* LoongArch-specific support for NN-bit ELF.
  2. Copyright (C) 2021-2022 Free Software Foundation, Inc.
  3. Contributed by Loongson Ltd.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; see the file COPYING3. If not,
  15. see <http://www.gnu.org/licenses/>. */
  16. #include "ansidecl.h"
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #define ARCH_SIZE NN
  21. #include "elf-bfd.h"
  22. #include "objalloc.h"
  23. #include "elf/loongarch.h"
  24. #include "elfxx-loongarch.h"
  25. static bool
  26. loongarch_info_to_howto_rela (bfd *abfd, arelent *cache_ptr,
  27. Elf_Internal_Rela *dst)
  28. {
  29. cache_ptr->howto = loongarch_elf_rtype_to_howto (abfd,
  30. ELFNN_R_TYPE (dst->r_info));
  31. return cache_ptr->howto != NULL;
  32. }
  33. /* LoongArch ELF linker hash entry. */
  34. struct loongarch_elf_link_hash_entry
  35. {
  36. struct elf_link_hash_entry elf;
  37. #define GOT_UNKNOWN 0
  38. #define GOT_NORMAL 1
  39. #define GOT_TLS_GD 2
  40. #define GOT_TLS_IE 4
  41. #define GOT_TLS_LE 8
  42. char tls_type;
  43. };
  44. #define loongarch_elf_hash_entry(ent) \
  45. ((struct loongarch_elf_link_hash_entry *) (ent))
  46. struct _bfd_loongarch_elf_obj_tdata
  47. {
  48. struct elf_obj_tdata root;
  49. /* The tls_type for each local got entry. */
  50. char *local_got_tls_type;
  51. };
  52. #define _bfd_loongarch_elf_tdata(abfd) \
  53. ((struct _bfd_loongarch_elf_obj_tdata *) (abfd)->tdata.any)
  54. #define _bfd_loongarch_elf_local_got_tls_type(abfd) \
  55. (_bfd_loongarch_elf_tdata (abfd)->local_got_tls_type)
  56. #define _bfd_loongarch_elf_tls_type(abfd, h, symndx) \
  57. (*((h) != NULL \
  58. ? &loongarch_elf_hash_entry (h)->tls_type \
  59. : &_bfd_loongarch_elf_local_got_tls_type (abfd)[symndx]))
  60. #define is_loongarch_elf(bfd) \
  61. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  62. && elf_tdata (bfd) != NULL \
  63. && elf_object_id (bfd) == LARCH_ELF_DATA)
  64. struct loongarch_elf_link_hash_table
  65. {
  66. struct elf_link_hash_table elf;
  67. /* Short-cuts to get to dynamic linker sections. */
  68. asection *sdyntdata;
  69. /* Small local sym to section mapping cache. */
  70. struct sym_cache sym_cache;
  71. /* Used by local STT_GNU_IFUNC symbols. */
  72. htab_t loc_hash_table;
  73. void *loc_hash_memory;
  74. /* The max alignment of output sections. */
  75. bfd_vma max_alignment;
  76. };
  77. /* Get the LoongArch ELF linker hash table from a link_info structure. */
  78. #define loongarch_elf_hash_table(p) \
  79. (elf_hash_table_id (elf_hash_table (p)) == LARCH_ELF_DATA \
  80. ? ((struct loongarch_elf_link_hash_table *) ((p)->hash)) \
  81. : NULL)
  82. #define MINUS_ONE ((bfd_vma) 0 - 1)
  83. #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
  84. #define LARCH_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
  85. #define LARCH_ELF_WORD_BYTES (1 << LARCH_ELF_LOG_WORD_BYTES)
  86. #define PLT_HEADER_INSNS 8
  87. #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
  88. #define PLT_ENTRY_INSNS 4
  89. #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
  90. #define GOT_ENTRY_SIZE (LARCH_ELF_WORD_BYTES)
  91. #define GOTPLT_HEADER_SIZE (GOT_ENTRY_SIZE * 2)
  92. #define elf_backend_want_got_plt 1
  93. #define elf_backend_plt_readonly 1
  94. #define elf_backend_want_plt_sym 0
  95. #define elf_backend_plt_alignment 4
  96. #define elf_backend_can_gc_sections 1
  97. /* #define elf_backend_can_refcount 1 */
  98. #define elf_backend_want_got_sym 1
  99. #define elf_backend_got_header_size (GOT_ENTRY_SIZE * 1)
  100. #define elf_backend_want_dynrelro 1
  101. /* #define elf_backend_rela_normal 1
  102. #define elf_backend_default_execstack 0 */
  103. /* Generate a PLT header. */
  104. static bool
  105. loongarch_make_plt_header (bfd_vma got_plt_addr, bfd_vma plt_header_addr,
  106. uint32_t *entry)
  107. {
  108. bfd_vma pcrel = got_plt_addr - plt_header_addr;
  109. bfd_vma hi, lo;
  110. if (pcrel + 0x80000800 > 0xffffffff)
  111. {
  112. _bfd_error_handler (_("%#" PRIx64 " invaild imm"), (uint64_t) pcrel);
  113. bfd_set_error (bfd_error_bad_value);
  114. return false;
  115. }
  116. hi = ((pcrel + 0x800) >> 12) & 0xfffff;
  117. lo = pcrel & 0xfff;
  118. /* pcaddu12i $t2, %hi(%pcrel(.got.plt))
  119. sub.[wd] $t1, $t1, $t3
  120. ld.[wd] $t3, $t2, %lo(%pcrel(.got.plt)) # _dl_runtime_resolve
  121. addi.[wd] $t1, $t1, -(PLT_HEADER_SIZE + 12)
  122. addi.[wd] $t0, $t2, %lo(%pcrel(.got.plt))
  123. srli.[wd] $t1, $t1, log2(16 / GOT_ENTRY_SIZE)
  124. ld.[wd] $t0, $t0, GOT_ENTRY_SIZE
  125. jirl $r0, $t3, 0 */
  126. if (GOT_ENTRY_SIZE == 8)
  127. {
  128. entry[0] = 0x1c00000e | (hi & 0xfffff) << 5;
  129. entry[1] = 0x0011bdad;
  130. entry[2] = 0x28c001cf | (lo & 0xfff) << 10;
  131. entry[3] = 0x02c001ad | ((-(PLT_HEADER_SIZE + 12)) & 0xfff) << 10;
  132. entry[4] = 0x02c001cc | (lo & 0xfff) << 10;
  133. entry[5] = 0x004501ad | (4 - LARCH_ELF_LOG_WORD_BYTES) << 10;
  134. entry[6] = 0x28c0018c | GOT_ENTRY_SIZE << 10;
  135. entry[7] = 0x4c0001e0;
  136. }
  137. else
  138. {
  139. entry[0] = 0x1c00000e | (hi & 0xfffff) << 5;
  140. entry[1] = 0x00113dad;
  141. entry[2] = 0x288001cf | (lo & 0xfff) << 10;
  142. entry[3] = 0x028001ad | ((-(PLT_HEADER_SIZE + 12)) & 0xfff) << 10;
  143. entry[4] = 0x028001cc | (lo & 0xfff) << 10;
  144. entry[5] = 0x004481ad | (4 - LARCH_ELF_LOG_WORD_BYTES) << 10;
  145. entry[6] = 0x2880018c | GOT_ENTRY_SIZE << 10;
  146. entry[7] = 0x4c0001e0;
  147. }
  148. return true;
  149. }
  150. /* Generate a PLT entry. */
  151. static bool
  152. loongarch_make_plt_entry (bfd_vma got_plt_entry_addr, bfd_vma plt_entry_addr,
  153. uint32_t *entry)
  154. {
  155. bfd_vma pcrel = got_plt_entry_addr - plt_entry_addr;
  156. bfd_vma hi, lo;
  157. if (pcrel + 0x80000800 > 0xffffffff)
  158. {
  159. _bfd_error_handler (_("%#" PRIx64 " invaild imm"), (uint64_t) pcrel);
  160. bfd_set_error (bfd_error_bad_value);
  161. return false;
  162. }
  163. hi = ((pcrel + 0x800) >> 12) & 0xfffff;
  164. lo = pcrel & 0xfff;
  165. entry[0] = 0x1c00000f | (hi & 0xfffff) << 5;
  166. entry[1] = ((GOT_ENTRY_SIZE == 8 ? 0x28c001ef : 0x288001ef)
  167. | (lo & 0xfff) << 10);
  168. entry[2] = 0x4c0001ed; /* jirl $r13, $15, 0 */
  169. entry[3] = 0x03400000; /* nop */
  170. return true;
  171. }
  172. /* Create an entry in an LoongArch ELF linker hash table. */
  173. static struct bfd_hash_entry *
  174. link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
  175. const char *string)
  176. {
  177. struct loongarch_elf_link_hash_entry *eh;
  178. /* Allocate the structure if it has not already been allocated by a
  179. subclass. */
  180. if (entry == NULL)
  181. {
  182. entry = bfd_hash_allocate (table, sizeof (*eh));
  183. if (entry == NULL)
  184. return entry;
  185. }
  186. /* Call the allocation method of the superclass. */
  187. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  188. if (entry != NULL)
  189. {
  190. eh = (struct loongarch_elf_link_hash_entry *) entry;
  191. eh->tls_type = GOT_UNKNOWN;
  192. }
  193. return entry;
  194. }
  195. /* Compute a hash of a local hash entry. We use elf_link_hash_entry
  196. for local symbol so that we can handle local STT_GNU_IFUNC symbols
  197. as global symbol. We reuse indx and dynstr_index for local symbol
  198. hash since they aren't used by global symbols in this backend. */
  199. static hashval_t
  200. elfNN_loongarch_local_htab_hash (const void *ptr)
  201. {
  202. struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) ptr;
  203. return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
  204. }
  205. /* Compare local hash entries. */
  206. static int
  207. elfNN_loongarch_local_htab_eq (const void *ptr1, const void *ptr2)
  208. {
  209. struct elf_link_hash_entry *h1 = (struct elf_link_hash_entry *) ptr1;
  210. struct elf_link_hash_entry *h2 = (struct elf_link_hash_entry *) ptr2;
  211. return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
  212. }
  213. /* Find and/or create a hash entry for local symbol. */
  214. static struct elf_link_hash_entry *
  215. elfNN_loongarch_get_local_sym_hash (struct loongarch_elf_link_hash_table *htab,
  216. bfd *abfd, const Elf_Internal_Rela *rel,
  217. bool create)
  218. {
  219. struct loongarch_elf_link_hash_entry e, *ret;
  220. asection *sec = abfd->sections;
  221. hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id, ELFNN_R_SYM (rel->r_info));
  222. void **slot;
  223. e.elf.indx = sec->id;
  224. e.elf.dynstr_index = ELFNN_R_SYM (rel->r_info);
  225. slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
  226. create ? INSERT : NO_INSERT);
  227. if (!slot)
  228. return NULL;
  229. if (*slot)
  230. {
  231. ret = (struct loongarch_elf_link_hash_entry *) *slot;
  232. return &ret->elf;
  233. }
  234. ret = ((struct loongarch_elf_link_hash_entry *)
  235. objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
  236. sizeof (struct loongarch_elf_link_hash_entry)));
  237. if (ret)
  238. {
  239. memset (ret, 0, sizeof (*ret));
  240. ret->elf.indx = sec->id;
  241. ret->elf.pointer_equality_needed = 0;
  242. ret->elf.dynstr_index = ELFNN_R_SYM (rel->r_info);
  243. ret->elf.dynindx = -1;
  244. ret->elf.needs_plt = 0;
  245. ret->elf.plt.refcount = -1;
  246. ret->elf.got.refcount = -1;
  247. ret->elf.def_dynamic = 0;
  248. ret->elf.def_regular = 1;
  249. ret->elf.ref_dynamic = 0; /* This should be always 0 for local. */
  250. ret->elf.ref_regular = 0;
  251. ret->elf.forced_local = 1;
  252. ret->elf.root.type = bfd_link_hash_defined;
  253. *slot = ret;
  254. }
  255. return &ret->elf;
  256. }
  257. /* Destroy an LoongArch elf linker hash table. */
  258. static void
  259. elfNN_loongarch_link_hash_table_free (bfd *obfd)
  260. {
  261. struct loongarch_elf_link_hash_table *ret;
  262. ret = (struct loongarch_elf_link_hash_table *) obfd->link.hash;
  263. if (ret->loc_hash_table)
  264. htab_delete (ret->loc_hash_table);
  265. if (ret->loc_hash_memory)
  266. objalloc_free ((struct objalloc *) ret->loc_hash_memory);
  267. _bfd_elf_link_hash_table_free (obfd);
  268. }
  269. /* Create a LoongArch ELF linker hash table. */
  270. static struct bfd_link_hash_table *
  271. loongarch_elf_link_hash_table_create (bfd *abfd)
  272. {
  273. struct loongarch_elf_link_hash_table *ret;
  274. bfd_size_type amt = sizeof (struct loongarch_elf_link_hash_table);
  275. ret = (struct loongarch_elf_link_hash_table *) bfd_zmalloc (amt);
  276. if (ret == NULL)
  277. return NULL;
  278. if (!_bfd_elf_link_hash_table_init
  279. (&ret->elf, abfd, link_hash_newfunc,
  280. sizeof (struct loongarch_elf_link_hash_entry), LARCH_ELF_DATA))
  281. {
  282. free (ret);
  283. return NULL;
  284. }
  285. ret->max_alignment = MINUS_ONE;
  286. ret->loc_hash_table = htab_try_create (1024, elfNN_loongarch_local_htab_hash,
  287. elfNN_loongarch_local_htab_eq, NULL);
  288. ret->loc_hash_memory = objalloc_create ();
  289. if (!ret->loc_hash_table || !ret->loc_hash_memory)
  290. {
  291. elfNN_loongarch_link_hash_table_free (abfd);
  292. return NULL;
  293. }
  294. ret->elf.root.hash_table_free = elfNN_loongarch_link_hash_table_free;
  295. return &ret->elf.root;
  296. }
  297. /* Merge backend specific data from an object file to the output
  298. object file when linking. */
  299. static bool
  300. elfNN_loongarch_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  301. {
  302. bfd *obfd = info->output_bfd;
  303. flagword in_flags = elf_elfheader (ibfd)->e_flags;
  304. flagword out_flags = elf_elfheader (obfd)->e_flags;
  305. if (!is_loongarch_elf (ibfd) || !is_loongarch_elf (obfd))
  306. return true;
  307. if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
  308. {
  309. _bfd_error_handler (_("%pB: ABI is incompatible with that of "
  310. "the selected emulation:\n"
  311. " target emulation `%s' does not match `%s'"),
  312. ibfd, bfd_get_target (ibfd), bfd_get_target (obfd));
  313. return false;
  314. }
  315. if (!_bfd_elf_merge_object_attributes (ibfd, info))
  316. return false;
  317. if (!elf_flags_init (obfd))
  318. {
  319. elf_flags_init (obfd) = true;
  320. elf_elfheader (obfd)->e_flags = in_flags;
  321. return true;
  322. }
  323. /* Disallow linking different ABIs. */
  324. if (EF_LOONGARCH_ABI(out_flags ^ in_flags) & EF_LOONGARCH_ABI_MASK)
  325. {
  326. _bfd_error_handler (_("%pB: can't link different ABI object."), ibfd);
  327. goto fail;
  328. }
  329. return true;
  330. fail:
  331. bfd_set_error (bfd_error_bad_value);
  332. return false;
  333. }
  334. /* Create the .got section. */
  335. static bool
  336. loongarch_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
  337. {
  338. flagword flags;
  339. char *name;
  340. asection *s, *s_got;
  341. struct elf_link_hash_entry *h;
  342. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  343. struct elf_link_hash_table *htab = elf_hash_table (info);
  344. /* This function may be called more than once. */
  345. if (htab->sgot != NULL)
  346. return true;
  347. flags = bed->dynamic_sec_flags;
  348. name = bed->rela_plts_and_copies_p ? ".rela.got" : ".rel.got";
  349. s = bfd_make_section_anyway_with_flags (abfd, name, flags | SEC_READONLY);
  350. if (s == NULL || !bfd_set_section_alignment (s, bed->s->log_file_align))
  351. return false;
  352. htab->srelgot = s;
  353. s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  354. if (s == NULL || !bfd_set_section_alignment (s, bed->s->log_file_align))
  355. return false;
  356. htab->sgot = s;
  357. /* The first bit of the global offset table is the header. */
  358. s->size += bed->got_header_size;
  359. if (bed->want_got_plt)
  360. {
  361. s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
  362. if (s == NULL || !bfd_set_section_alignment (s, bed->s->log_file_align))
  363. return false;
  364. htab->sgotplt = s;
  365. /* Reserve room for the header. */
  366. s->size = GOTPLT_HEADER_SIZE;
  367. }
  368. if (bed->want_got_sym)
  369. {
  370. /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  371. section. We don't do this in the linker script because we don't want
  372. to define the symbol if we are not creating a global offset table. */
  373. h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
  374. "_GLOBAL_OFFSET_TABLE_");
  375. elf_hash_table (info)->hgot = h;
  376. if (h == NULL)
  377. return false;
  378. }
  379. return true;
  380. }
  381. /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
  382. .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
  383. hash table. */
  384. static bool
  385. loongarch_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
  386. {
  387. struct loongarch_elf_link_hash_table *htab;
  388. htab = loongarch_elf_hash_table (info);
  389. BFD_ASSERT (htab != NULL);
  390. if (!loongarch_elf_create_got_section (dynobj, info))
  391. return false;
  392. if (!_bfd_elf_create_dynamic_sections (dynobj, info))
  393. return false;
  394. if (!bfd_link_pic (info))
  395. htab->sdyntdata
  396. = bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
  397. SEC_ALLOC | SEC_THREAD_LOCAL);
  398. if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
  399. || (!bfd_link_pic (info) && (!htab->elf.srelbss || !htab->sdyntdata)))
  400. abort ();
  401. return true;
  402. }
  403. static bool
  404. loongarch_elf_record_tls_and_got_reference (bfd *abfd,
  405. struct bfd_link_info *info,
  406. struct elf_link_hash_entry *h,
  407. unsigned long symndx,
  408. char tls_type)
  409. {
  410. struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
  411. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  412. /* This is a global offset table entry for a local symbol. */
  413. if (elf_local_got_refcounts (abfd) == NULL)
  414. {
  415. bfd_size_type size =
  416. symtab_hdr->sh_info * (sizeof (bfd_vma) + sizeof (tls_type));
  417. if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
  418. return false;
  419. _bfd_loongarch_elf_local_got_tls_type (abfd) =
  420. (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
  421. }
  422. switch (tls_type)
  423. {
  424. case GOT_NORMAL:
  425. case GOT_TLS_GD:
  426. case GOT_TLS_IE:
  427. /* Need GOT. */
  428. if (htab->elf.sgot == NULL
  429. && !loongarch_elf_create_got_section (htab->elf.dynobj, info))
  430. return false;
  431. if (h)
  432. {
  433. if (h->got.refcount < 0)
  434. h->got.refcount = 0;
  435. h->got.refcount++;
  436. }
  437. else
  438. elf_local_got_refcounts (abfd)[symndx]++;
  439. break;
  440. case GOT_TLS_LE:
  441. /* No need for GOT. */
  442. break;
  443. default:
  444. _bfd_error_handler (_("Internal error: unreachable."));
  445. return false;
  446. }
  447. char *new_tls_type = &_bfd_loongarch_elf_tls_type (abfd, h, symndx);
  448. *new_tls_type |= tls_type;
  449. if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
  450. {
  451. _bfd_error_handler (_("%pB: `%s' accessed both as normal and "
  452. "thread local symbol"),
  453. abfd,
  454. h ? h->root.root.string : "<local>");
  455. return false;
  456. }
  457. return true;
  458. }
  459. /* Look through the relocs for a section during the first phase, and
  460. allocate space in the global offset table or procedure linkage
  461. table. */
  462. static bool
  463. loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
  464. asection *sec, const Elf_Internal_Rela *relocs)
  465. {
  466. struct loongarch_elf_link_hash_table *htab;
  467. Elf_Internal_Shdr *symtab_hdr;
  468. struct elf_link_hash_entry **sym_hashes;
  469. const Elf_Internal_Rela *rel;
  470. asection *sreloc = NULL;
  471. if (bfd_link_relocatable (info))
  472. return true;
  473. htab = loongarch_elf_hash_table (info);
  474. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  475. sym_hashes = elf_sym_hashes (abfd);
  476. if (htab->elf.dynobj == NULL)
  477. htab->elf.dynobj = abfd;
  478. for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
  479. {
  480. unsigned int r_type;
  481. unsigned int r_symndx;
  482. struct elf_link_hash_entry *h;
  483. Elf_Internal_Sym *isym = NULL;
  484. int need_dynreloc;
  485. int only_need_pcrel;
  486. r_symndx = ELFNN_R_SYM (rel->r_info);
  487. r_type = ELFNN_R_TYPE (rel->r_info);
  488. if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
  489. {
  490. _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd, r_symndx);
  491. return false;
  492. }
  493. if (r_symndx < symtab_hdr->sh_info)
  494. {
  495. /* A local symbol. */
  496. isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
  497. if (isym == NULL)
  498. return false;
  499. if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  500. {
  501. h = elfNN_loongarch_get_local_sym_hash (htab, abfd, rel, true);
  502. if (h == NULL)
  503. return false;
  504. h->type = STT_GNU_IFUNC;
  505. h->ref_regular = 1;
  506. }
  507. else
  508. h = NULL;
  509. }
  510. else
  511. {
  512. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  513. while (h->root.type == bfd_link_hash_indirect
  514. || h->root.type == bfd_link_hash_warning)
  515. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  516. }
  517. /* It is referenced by a non-shared object. */
  518. if (h != NULL)
  519. h->ref_regular = 1;
  520. if (h && h->type == STT_GNU_IFUNC)
  521. {
  522. if (htab->elf.dynobj == NULL)
  523. htab->elf.dynobj = abfd;
  524. /* Create the ifunc sections, iplt and ipltgot, for static
  525. executables. */
  526. if ((r_type == R_LARCH_64 || r_type == R_LARCH_32)
  527. && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
  528. return false;
  529. if (!htab->elf.splt
  530. && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
  531. /* If '.plt' not represent, create '.iplt' to deal with ifunc. */
  532. return false;
  533. if (h->plt.refcount < 0)
  534. h->plt.refcount = 0;
  535. h->plt.refcount++;
  536. h->needs_plt = 1;
  537. elf_tdata (info->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
  538. }
  539. need_dynreloc = 0;
  540. only_need_pcrel = 0;
  541. switch (r_type)
  542. {
  543. case R_LARCH_SOP_PUSH_GPREL:
  544. if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
  545. r_symndx,
  546. GOT_NORMAL))
  547. return false;
  548. break;
  549. case R_LARCH_SOP_PUSH_TLS_GD:
  550. if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
  551. r_symndx,
  552. GOT_TLS_GD))
  553. return false;
  554. break;
  555. case R_LARCH_SOP_PUSH_TLS_GOT:
  556. if (bfd_link_pic (info))
  557. /* May fail for lazy-bind. */
  558. info->flags |= DF_STATIC_TLS;
  559. if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
  560. r_symndx,
  561. GOT_TLS_IE))
  562. return false;
  563. break;
  564. case R_LARCH_SOP_PUSH_TLS_TPREL:
  565. if (!bfd_link_executable (info))
  566. return false;
  567. info->flags |= DF_STATIC_TLS;
  568. if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
  569. r_symndx,
  570. GOT_TLS_LE))
  571. return false;
  572. break;
  573. case R_LARCH_SOP_PUSH_ABSOLUTE:
  574. if (h != NULL)
  575. /* If this reloc is in a read-only section, we might
  576. need a copy reloc. We can't check reliably at this
  577. stage whether the section is read-only, as input
  578. sections have not yet been mapped to output sections.
  579. Tentatively set the flag for now, and correct in
  580. adjust_dynamic_symbol. */
  581. h->non_got_ref = 1;
  582. break;
  583. case R_LARCH_SOP_PUSH_PCREL:
  584. if (h != NULL)
  585. {
  586. h->non_got_ref = 1;
  587. /* We try to create PLT stub for all non-local function. */
  588. if (h->plt.refcount < 0)
  589. h->plt.refcount = 0;
  590. h->plt.refcount++;
  591. }
  592. break;
  593. case R_LARCH_SOP_PUSH_PLT_PCREL:
  594. /* This symbol requires a procedure linkage table entry. We
  595. actually build the entry in adjust_dynamic_symbol,
  596. because this might be a case of linking PIC code without
  597. linking in any dynamic objects, in which case we don't
  598. need to generate a procedure linkage table after all. */
  599. if (h != NULL)
  600. {
  601. h->needs_plt = 1;
  602. if (h->plt.refcount < 0)
  603. h->plt.refcount = 0;
  604. h->plt.refcount++;
  605. }
  606. break;
  607. case R_LARCH_TLS_DTPREL32:
  608. case R_LARCH_TLS_DTPREL64:
  609. need_dynreloc = 1;
  610. only_need_pcrel = 1;
  611. break;
  612. case R_LARCH_JUMP_SLOT:
  613. case R_LARCH_32:
  614. case R_LARCH_64:
  615. need_dynreloc = 1;
  616. /* If resolved symbol is defined in this object,
  617. 1. Under pie, the symbol is known. We convert it
  618. into R_LARCH_RELATIVE and need load-addr still.
  619. 2. Under pde, the symbol is known and we can discard R_LARCH_NN.
  620. 3. Under dll, R_LARCH_NN can't be changed normally, since
  621. its defination could be covered by the one in executable.
  622. For symbolic, we convert it into R_LARCH_RELATIVE.
  623. Thus, only under pde, it needs pcrel only. We discard it. */
  624. only_need_pcrel = bfd_link_pde (info);
  625. if (h != NULL)
  626. h->non_got_ref = 1;
  627. if (h != NULL
  628. && (!bfd_link_pic (info)
  629. || h->type == STT_GNU_IFUNC))
  630. {
  631. /* This reloc might not bind locally. */
  632. h->non_got_ref = 1;
  633. h->pointer_equality_needed = 1;
  634. if (!h->def_regular
  635. || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
  636. {
  637. /* We may need a .plt entry if the symbol is a function
  638. defined in a shared lib or is a function referenced
  639. from the code or read-only section. */
  640. h->plt.refcount += 1;
  641. }
  642. }
  643. break;
  644. case R_LARCH_GNU_VTINHERIT:
  645. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  646. return false;
  647. break;
  648. case R_LARCH_GNU_VTENTRY:
  649. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  650. return false;
  651. break;
  652. default:
  653. break;
  654. }
  655. /* Record some info for sizing and allocating dynamic entry. */
  656. if (need_dynreloc && (sec->flags & SEC_ALLOC))
  657. {
  658. /* When creating a shared object, we must copy these
  659. relocs into the output file. We create a reloc
  660. section in dynobj and make room for the reloc. */
  661. struct elf_dyn_relocs *p;
  662. struct elf_dyn_relocs **head;
  663. if (sreloc == NULL)
  664. {
  665. sreloc
  666. = _bfd_elf_make_dynamic_reloc_section (sec, htab->elf.dynobj,
  667. LARCH_ELF_LOG_WORD_BYTES,
  668. abfd, /*rela?*/ true);
  669. if (sreloc == NULL)
  670. return false;
  671. }
  672. /* If this is a global symbol, we count the number of
  673. relocations we need for this symbol. */
  674. if (h != NULL)
  675. head = &h->dyn_relocs;
  676. else
  677. {
  678. /* Track dynamic relocs needed for local syms too.
  679. We really need local syms available to do this
  680. easily. Oh well. */
  681. asection *s;
  682. void *vpp;
  683. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  684. if (s == NULL)
  685. s = sec;
  686. vpp = &elf_section_data (s)->local_dynrel;
  687. head = (struct elf_dyn_relocs **) vpp;
  688. }
  689. p = *head;
  690. if (p == NULL || p->sec != sec)
  691. {
  692. bfd_size_type amt = sizeof *p;
  693. p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj, amt);
  694. if (p == NULL)
  695. return false;
  696. p->next = *head;
  697. *head = p;
  698. p->sec = sec;
  699. p->count = 0;
  700. p->pc_count = 0;
  701. }
  702. p->count++;
  703. p->pc_count += only_need_pcrel;
  704. }
  705. }
  706. return true;
  707. }
  708. /* Find dynamic relocs for H that apply to read-only sections. */
  709. static asection *
  710. readonly_dynrelocs (struct elf_link_hash_entry *h)
  711. {
  712. struct elf_dyn_relocs *p;
  713. for (p = h->dyn_relocs; p != NULL; p = p->next)
  714. {
  715. asection *s = p->sec->output_section;
  716. if (s != NULL && (s->flags & SEC_READONLY) != 0)
  717. return p->sec;
  718. }
  719. return NULL;
  720. }
  721. /* Adjust a symbol defined by a dynamic object and referenced by a
  722. regular object. The current definition is in some section of the
  723. dynamic object, but we're not including those sections. We have to
  724. change the definition to something the rest of the link can
  725. understand. */
  726. static bool
  727. loongarch_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  728. struct elf_link_hash_entry *h)
  729. {
  730. struct loongarch_elf_link_hash_table *htab;
  731. struct loongarch_elf_link_hash_entry *eh;
  732. bfd *dynobj;
  733. asection *s, *srel;
  734. htab = loongarch_elf_hash_table (info);
  735. BFD_ASSERT (htab != NULL);
  736. dynobj = htab->elf.dynobj;
  737. /* Make sure we know what is going on here. */
  738. BFD_ASSERT (dynobj != NULL
  739. && (h->needs_plt || h->type == STT_GNU_IFUNC || h->is_weakalias
  740. || (h->def_dynamic && h->ref_regular && !h->def_regular)));
  741. /* If this is a function, put it in the procedure linkage table. We
  742. will fill in the contents of the procedure linkage table later
  743. (although we could actually do it here). */
  744. if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
  745. {
  746. if (h->plt.refcount < 0
  747. || (h->type != STT_GNU_IFUNC
  748. && (SYMBOL_REFERENCES_LOCAL (info, h)
  749. || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  750. && h->root.type == bfd_link_hash_undefweak))))
  751. {
  752. /* This case can occur if we saw a R_LARCH_SOP_PUSH_PLT_PCREL reloc
  753. in an input file, but the symbol was never referred to by a
  754. dynamic object, or if all references were garbage collected.
  755. In such a case, we don't actually need to build a PLT entry. */
  756. h->plt.offset = MINUS_ONE;
  757. h->needs_plt = 0;
  758. }
  759. else
  760. h->needs_plt = 1;
  761. return true;
  762. }
  763. else
  764. h->plt.offset = MINUS_ONE;
  765. /* If this is a weak symbol, and there is a real definition, the
  766. processor independent code will have arranged for us to see the
  767. real definition first, and we can just use the same value. */
  768. if (h->is_weakalias)
  769. {
  770. struct elf_link_hash_entry *def = weakdef (h);
  771. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  772. h->root.u.def.section = def->root.u.def.section;
  773. h->root.u.def.value = def->root.u.def.value;
  774. return true;
  775. }
  776. /* This is a reference to a symbol defined by a dynamic object which
  777. is not a function. */
  778. /* If we are creating a shared library, we must presume that the
  779. only references to the symbol are via the global offset table.
  780. For such cases we need not do anything here; the relocations will
  781. be handled correctly by relocate_section. */
  782. if (bfd_link_dll (info))
  783. return true;
  784. /* If there are no references to this symbol that do not use the
  785. GOT, we don't need to generate a copy reloc. */
  786. if (!h->non_got_ref)
  787. return true;
  788. /* If -z nocopyreloc was given, we won't generate them either. */
  789. if (info->nocopyreloc)
  790. {
  791. h->non_got_ref = 0;
  792. return true;
  793. }
  794. /* If we don't find any dynamic relocs in read-only sections, then
  795. we'll be keeping the dynamic relocs and avoiding the copy reloc. */
  796. if (!readonly_dynrelocs (h))
  797. {
  798. h->non_got_ref = 0;
  799. return true;
  800. }
  801. /* We must allocate the symbol in our .dynbss section, which will
  802. become part of the .bss section of the executable. There will be
  803. an entry for this symbol in the .dynsym section. The dynamic
  804. object will contain position independent code, so all references
  805. from the dynamic object to this symbol will go through the global
  806. offset table. The dynamic linker will use the .dynsym entry to
  807. determine the address it must put in the global offset table, so
  808. both the dynamic object and the regular object will refer to the
  809. same memory location for the variable. */
  810. /* We must generate a R_LARCH_COPY reloc to tell the dynamic linker
  811. to copy the initial value out of the dynamic object and into the
  812. runtime process image. We need to remember the offset into the
  813. .rel.bss section we are going to use. */
  814. eh = (struct loongarch_elf_link_hash_entry *) h;
  815. if (eh->tls_type & ~GOT_NORMAL)
  816. {
  817. s = htab->sdyntdata;
  818. srel = htab->elf.srelbss;
  819. }
  820. else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
  821. {
  822. s = htab->elf.sdynrelro;
  823. srel = htab->elf.sreldynrelro;
  824. }
  825. else
  826. {
  827. s = htab->elf.sdynbss;
  828. srel = htab->elf.srelbss;
  829. }
  830. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  831. {
  832. srel->size += sizeof (ElfNN_External_Rela);
  833. h->needs_copy = 1;
  834. }
  835. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  836. }
  837. /* Allocate space in .plt, .got and associated reloc sections for
  838. dynamic relocs. */
  839. static bool
  840. allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  841. {
  842. struct bfd_link_info *info;
  843. struct loongarch_elf_link_hash_table *htab;
  844. struct elf_dyn_relocs *p;
  845. if (h->root.type == bfd_link_hash_indirect)
  846. return true;
  847. if (h->type == STT_GNU_IFUNC
  848. && h->def_regular)
  849. return true;
  850. info = (struct bfd_link_info *) inf;
  851. htab = loongarch_elf_hash_table (info);
  852. BFD_ASSERT (htab != NULL);
  853. do
  854. {
  855. asection *plt, *gotplt, *relplt;
  856. if (!h->needs_plt)
  857. break;
  858. h->needs_plt = 0;
  859. if (htab->elf.splt)
  860. {
  861. if (h->dynindx == -1 && !h->forced_local
  862. && !bfd_elf_link_record_dynamic_symbol (info, h))
  863. return false;
  864. if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h)
  865. && h->type != STT_GNU_IFUNC)
  866. break;
  867. plt = htab->elf.splt;
  868. gotplt = htab->elf.sgotplt;
  869. relplt = htab->elf.srelplt;
  870. }
  871. else if (htab->elf.iplt)
  872. {
  873. /* .iplt only for IFUNC. */
  874. if (h->type != STT_GNU_IFUNC)
  875. break;
  876. plt = htab->elf.iplt;
  877. gotplt = htab->elf.igotplt;
  878. relplt = htab->elf.irelplt;
  879. }
  880. else
  881. break;
  882. if (plt->size == 0)
  883. plt->size = PLT_HEADER_SIZE;
  884. h->plt.offset = plt->size;
  885. plt->size += PLT_ENTRY_SIZE;
  886. gotplt->size += GOT_ENTRY_SIZE;
  887. relplt->size += sizeof (ElfNN_External_Rela);
  888. /* If this symbol is not defined in a regular file, and we are
  889. not generating a shared library, then set the symbol to this
  890. location in the .plt. This is required to make function
  891. pointers compare as equal between the normal executable and
  892. the shared library. */
  893. if (!bfd_link_pic(info)
  894. && !h->def_regular)
  895. {
  896. h->root.u.def.section = plt;
  897. h->root.u.def.value = h->plt.offset;
  898. }
  899. h->needs_plt = 1;
  900. }
  901. while (0);
  902. if (!h->needs_plt)
  903. h->plt.offset = MINUS_ONE;
  904. if (0 < h->got.refcount)
  905. {
  906. asection *s;
  907. bool dyn;
  908. int tls_type = loongarch_elf_hash_entry (h)->tls_type;
  909. /* Make sure this symbol is output as a dynamic symbol.
  910. Undefined weak syms won't yet be marked as dynamic. */
  911. if (h->dynindx == -1 && !h->forced_local)
  912. {
  913. if (SYMBOL_REFERENCES_LOCAL (info, h)
  914. && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  915. && h->start_stop)
  916. {
  917. /* The pr21964-4. do nothing. */
  918. }
  919. else
  920. {
  921. if( !bfd_elf_link_record_dynamic_symbol (info, h))
  922. return false;
  923. }
  924. }
  925. s = htab->elf.sgot;
  926. h->got.offset = s->size;
  927. dyn = htab->elf.dynamic_sections_created;
  928. if (tls_type & (GOT_TLS_GD | GOT_TLS_IE))
  929. {
  930. /* TLS_GD needs two dynamic relocs and two GOT slots. */
  931. if (tls_type & GOT_TLS_GD)
  932. {
  933. s->size += 2 * GOT_ENTRY_SIZE;
  934. htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela);
  935. }
  936. /* TLS_IE needs one dynamic reloc and one GOT slot. */
  937. if (tls_type & GOT_TLS_IE)
  938. {
  939. s->size += GOT_ENTRY_SIZE;
  940. htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
  941. }
  942. }
  943. else
  944. {
  945. s->size += GOT_ENTRY_SIZE;
  946. if ((WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
  947. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  948. || h->type == STT_GNU_IFUNC)
  949. htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
  950. }
  951. }
  952. else
  953. h->got.offset = MINUS_ONE;
  954. if (h->dyn_relocs == NULL)
  955. return true;
  956. if (SYMBOL_REFERENCES_LOCAL (info, h))
  957. {
  958. struct elf_dyn_relocs **pp;
  959. for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
  960. {
  961. p->count -= p->pc_count;
  962. p->pc_count = 0;
  963. if (p->count == 0)
  964. *pp = p->next;
  965. else
  966. pp = &p->next;
  967. }
  968. }
  969. if (h->root.type == bfd_link_hash_undefweak)
  970. {
  971. if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  972. h->dyn_relocs = NULL;
  973. else if (h->dynindx == -1 && !h->forced_local
  974. /* Make sure this symbol is output as a dynamic symbol.
  975. Undefined weak syms won't yet be marked as dynamic. */
  976. && !bfd_elf_link_record_dynamic_symbol (info, h))
  977. return false;
  978. }
  979. for (p = h->dyn_relocs; p != NULL; p = p->next)
  980. {
  981. asection *sreloc = elf_section_data (p->sec)->sreloc;
  982. sreloc->size += p->count * sizeof (ElfNN_External_Rela);
  983. }
  984. return true;
  985. }
  986. /* Allocate space in .plt, .got and associated reloc sections for
  987. ifunc dynamic relocs. */
  988. static bool
  989. elfNN_loongarch_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
  990. void *inf)
  991. {
  992. struct bfd_link_info *info;
  993. /* An example of a bfd_link_hash_indirect symbol is versioned
  994. symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
  995. -> __gxx_personality_v0(bfd_link_hash_defined)
  996. There is no need to process bfd_link_hash_indirect symbols here
  997. because we will also be presented with the concrete instance of
  998. the symbol and loongarch_elf_copy_indirect_symbol () will have been
  999. called to copy all relevant data from the generic to the concrete
  1000. symbol instance. */
  1001. if (h->root.type == bfd_link_hash_indirect)
  1002. return true;
  1003. if (h->root.type == bfd_link_hash_warning)
  1004. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1005. info = (struct bfd_link_info *) inf;
  1006. /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
  1007. here if it is defined and referenced in a non-shared object. */
  1008. if (h->type == STT_GNU_IFUNC
  1009. && h->def_regular)
  1010. return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
  1011. &h->dyn_relocs,
  1012. PLT_ENTRY_SIZE,
  1013. PLT_HEADER_SIZE,
  1014. GOT_ENTRY_SIZE,
  1015. false);
  1016. return true;
  1017. }
  1018. /* Allocate space in .plt, .got and associated reloc sections for
  1019. ifunc dynamic relocs. */
  1020. static bool
  1021. elfNN_loongarch_allocate_local_dynrelocs (void **slot, void *inf)
  1022. {
  1023. struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) *slot;
  1024. if (h->type != STT_GNU_IFUNC
  1025. || !h->def_regular
  1026. || !h->ref_regular
  1027. || !h->forced_local
  1028. || h->root.type != bfd_link_hash_defined)
  1029. abort ();
  1030. return elfNN_loongarch_allocate_ifunc_dynrelocs (h, inf);
  1031. }
  1032. /* Set DF_TEXTREL if we find any dynamic relocs that apply to
  1033. read-only sections. */
  1034. static bool
  1035. maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
  1036. {
  1037. asection *sec;
  1038. if (h->root.type == bfd_link_hash_indirect)
  1039. return true;
  1040. sec = readonly_dynrelocs (h);
  1041. if (sec != NULL)
  1042. {
  1043. struct bfd_link_info *info = (struct bfd_link_info *) info_p;
  1044. info->flags |= DF_TEXTREL;
  1045. info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' in "
  1046. "read-only section `%pA'\n"),
  1047. sec->owner, h->root.root.string, sec);
  1048. /* Not an error, just cut short the traversal. */
  1049. return false;
  1050. }
  1051. return true;
  1052. }
  1053. static bool
  1054. loongarch_elf_size_dynamic_sections (bfd *output_bfd,
  1055. struct bfd_link_info *info)
  1056. {
  1057. struct loongarch_elf_link_hash_table *htab;
  1058. bfd *dynobj;
  1059. asection *s;
  1060. bfd *ibfd;
  1061. htab = loongarch_elf_hash_table (info);
  1062. BFD_ASSERT (htab != NULL);
  1063. dynobj = htab->elf.dynobj;
  1064. BFD_ASSERT (dynobj != NULL);
  1065. if (htab->elf.dynamic_sections_created)
  1066. {
  1067. /* Set the contents of the .interp section to the interpreter. */
  1068. if (bfd_link_executable (info) && !info->nointerp)
  1069. {
  1070. const char *interpreter;
  1071. flagword flags = elf_elfheader (output_bfd)->e_flags;
  1072. s = bfd_get_linker_section (dynobj, ".interp");
  1073. BFD_ASSERT (s != NULL);
  1074. if (EF_LOONGARCH_IS_ILP32 (flags))
  1075. interpreter = "/lib32/ld.so.1";
  1076. else if (EF_LOONGARCH_IS_LP64 (flags))
  1077. interpreter = "/lib64/ld.so.1";
  1078. else
  1079. interpreter = "/lib/ld.so.1";
  1080. s->contents = (unsigned char *) interpreter;
  1081. s->size = strlen (interpreter) + 1;
  1082. }
  1083. }
  1084. /* Set up .got offsets for local syms, and space for local dynamic
  1085. relocs. */
  1086. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  1087. {
  1088. bfd_signed_vma *local_got;
  1089. bfd_signed_vma *end_local_got;
  1090. char *local_tls_type;
  1091. bfd_size_type locsymcount;
  1092. Elf_Internal_Shdr *symtab_hdr;
  1093. asection *srel;
  1094. if (!is_loongarch_elf (ibfd))
  1095. continue;
  1096. for (s = ibfd->sections; s != NULL; s = s->next)
  1097. {
  1098. struct elf_dyn_relocs *p;
  1099. for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
  1100. {
  1101. p->count -= p->pc_count;
  1102. if (!bfd_is_abs_section (p->sec)
  1103. && bfd_is_abs_section (p->sec->output_section))
  1104. {
  1105. /* Input section has been discarded, either because
  1106. it is a copy of a linkonce section or due to
  1107. linker script /DISCARD/, so we'll be discarding
  1108. the relocs too. */
  1109. }
  1110. else if (0 < p->count)
  1111. {
  1112. srel = elf_section_data (p->sec)->sreloc;
  1113. srel->size += p->count * sizeof (ElfNN_External_Rela);
  1114. if ((p->sec->output_section->flags & SEC_READONLY) != 0)
  1115. info->flags |= DF_TEXTREL;
  1116. }
  1117. }
  1118. }
  1119. local_got = elf_local_got_refcounts (ibfd);
  1120. if (!local_got)
  1121. continue;
  1122. symtab_hdr = &elf_symtab_hdr (ibfd);
  1123. locsymcount = symtab_hdr->sh_info;
  1124. end_local_got = local_got + locsymcount;
  1125. local_tls_type = _bfd_loongarch_elf_local_got_tls_type (ibfd);
  1126. s = htab->elf.sgot;
  1127. srel = htab->elf.srelgot;
  1128. for (; local_got < end_local_got; ++local_got, ++local_tls_type)
  1129. {
  1130. if (0 < *local_got)
  1131. {
  1132. *local_got = s->size;
  1133. s->size += GOT_ENTRY_SIZE;
  1134. if (*local_tls_type & GOT_TLS_GD)
  1135. s->size += GOT_ENTRY_SIZE;
  1136. /* If R_LARCH_RELATIVE. */
  1137. if (bfd_link_pic (info)
  1138. /* Or R_LARCH_TLS_DTPRELNN or R_LARCH_TLS_TPRELNN. */
  1139. || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
  1140. srel->size += sizeof (ElfNN_External_Rela);
  1141. }
  1142. else
  1143. *local_got = MINUS_ONE;
  1144. }
  1145. }
  1146. /* Allocate global sym .plt and .got entries, and space for global
  1147. sym dynamic relocs. */
  1148. elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
  1149. /* Allocate global ifunc sym .plt and .got entries, and space for global
  1150. ifunc sym dynamic relocs. */
  1151. elf_link_hash_traverse (&htab->elf, elfNN_loongarch_allocate_ifunc_dynrelocs, info);
  1152. /* Allocate .plt and .got entries, and space for local ifunc symbols. */
  1153. htab_traverse (htab->loc_hash_table,
  1154. (void *) elfNN_loongarch_allocate_local_dynrelocs, info);
  1155. /* Don't allocate .got.plt section if there are no PLT. */
  1156. if (htab->elf.sgotplt && htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE
  1157. && (htab->elf.splt == NULL || htab->elf.splt->size == 0))
  1158. htab->elf.sgotplt->size = 0;
  1159. /* The check_relocs and adjust_dynamic_symbol entry points have
  1160. determined the sizes of the various dynamic sections. Allocate
  1161. memory for them. */
  1162. for (s = dynobj->sections; s != NULL; s = s->next)
  1163. {
  1164. if ((s->flags & SEC_LINKER_CREATED) == 0)
  1165. continue;
  1166. if (s == htab->elf.splt || s == htab->elf.iplt || s == htab->elf.sgot
  1167. || s == htab->elf.sgotplt || s == htab->elf.igotplt
  1168. || s == htab->elf.sdynbss || s == htab->elf.sdynrelro)
  1169. {
  1170. /* Strip this section if we don't need it; see the
  1171. comment below. */
  1172. }
  1173. else if (strncmp (s->name, ".rela", 5) == 0)
  1174. {
  1175. if (s->size != 0)
  1176. {
  1177. /* We use the reloc_count field as a counter if we need
  1178. to copy relocs into the output file. */
  1179. s->reloc_count = 0;
  1180. }
  1181. }
  1182. else
  1183. {
  1184. /* It's not one of our sections. */
  1185. continue;
  1186. }
  1187. if (s->size == 0)
  1188. {
  1189. /* If we don't need this section, strip it from the
  1190. output file. This is mostly to handle .rela.bss and
  1191. .rela.plt. We must create both sections in
  1192. create_dynamic_sections, because they must be created
  1193. before the linker maps input sections to output
  1194. sections. The linker does that before
  1195. adjust_dynamic_symbol is called, and it is that
  1196. function which decides whether anything needs to go
  1197. into these sections. */
  1198. s->flags |= SEC_EXCLUDE;
  1199. continue;
  1200. }
  1201. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  1202. continue;
  1203. /* Allocate memory for the section contents. Zero the memory
  1204. for the benefit of .rela.plt, which has 4 unused entries
  1205. at the beginning, and we don't want garbage. */
  1206. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  1207. if (s->contents == NULL)
  1208. return false;
  1209. }
  1210. if (elf_hash_table (info)->dynamic_sections_created)
  1211. {
  1212. /* Add some entries to the .dynamic section. We fill in the
  1213. values later, in loongarch_elf_finish_dynamic_sections, but we
  1214. must add the entries now so that we get the correct size for
  1215. the .dynamic section. The DT_DEBUG entry is filled in by the
  1216. dynamic linker and used by the debugger. */
  1217. #define add_dynamic_entry(TAG, VAL) _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  1218. if (bfd_link_executable (info))
  1219. {
  1220. if (!add_dynamic_entry (DT_DEBUG, 0))
  1221. return false;
  1222. }
  1223. if (htab->elf.srelplt->size != 0)
  1224. {
  1225. if (!add_dynamic_entry (DT_PLTGOT, 0)
  1226. || !add_dynamic_entry (DT_PLTRELSZ, 0)
  1227. || !add_dynamic_entry (DT_PLTREL, DT_RELA)
  1228. || !add_dynamic_entry (DT_JMPREL, 0))
  1229. return false;
  1230. }
  1231. if (!add_dynamic_entry (DT_RELA, 0)
  1232. || !add_dynamic_entry (DT_RELASZ, 0)
  1233. || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
  1234. return false;
  1235. /* If any dynamic relocs apply to a read-only section,
  1236. then we need a DT_TEXTREL entry. */
  1237. if ((info->flags & DF_TEXTREL) == 0)
  1238. elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
  1239. if (info->flags & DF_TEXTREL)
  1240. {
  1241. if (!add_dynamic_entry (DT_TEXTREL, 0))
  1242. return false;
  1243. /* Clear the DF_TEXTREL flag. It will be set again if we
  1244. write out an actual text relocation; we may not, because
  1245. at this point we do not know whether e.g. any .eh_frame
  1246. absolute relocations have been converted to PC-relative. */
  1247. info->flags &= ~DF_TEXTREL;
  1248. }
  1249. }
  1250. #undef add_dynamic_entry
  1251. return true;
  1252. }
  1253. #define LARCH_LD_STACK_DEPTH 16
  1254. static int64_t larch_opc_stack[LARCH_LD_STACK_DEPTH];
  1255. static size_t larch_stack_top = 0;
  1256. static bfd_reloc_status_type
  1257. loongarch_push (int64_t val)
  1258. {
  1259. if (LARCH_LD_STACK_DEPTH <= larch_stack_top)
  1260. return bfd_reloc_outofrange;
  1261. larch_opc_stack[larch_stack_top++] = val;
  1262. return bfd_reloc_ok;
  1263. }
  1264. static bfd_reloc_status_type
  1265. loongarch_pop (int64_t *val)
  1266. {
  1267. if (larch_stack_top == 0)
  1268. return bfd_reloc_outofrange;
  1269. BFD_ASSERT (val);
  1270. *val = larch_opc_stack[--larch_stack_top];
  1271. return bfd_reloc_ok;
  1272. }
  1273. static bfd_reloc_status_type
  1274. loongarch_top (int64_t *val)
  1275. {
  1276. if (larch_stack_top == 0)
  1277. return bfd_reloc_outofrange;
  1278. BFD_ASSERT (val);
  1279. *val = larch_opc_stack[larch_stack_top - 1];
  1280. return bfd_reloc_ok;
  1281. }
  1282. static void
  1283. loongarch_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
  1284. {
  1285. const struct elf_backend_data *bed;
  1286. bfd_byte *loc;
  1287. bed = get_elf_backend_data (abfd);
  1288. loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
  1289. bed->s->swap_reloca_out (abfd, rel, loc);
  1290. }
  1291. /* Check rel->r_offset in range of contents. */
  1292. static bfd_reloc_status_type
  1293. loongarch_check_offset (const Elf_Internal_Rela *rel,
  1294. const asection *input_section)
  1295. {
  1296. if (0 == strcmp(input_section->name, ".text")
  1297. && rel->r_offset > input_section->size)
  1298. return bfd_reloc_overflow;
  1299. return bfd_reloc_ok;
  1300. }
  1301. #define LARCH_RELOC_PERFORM_3OP(op1, op2, op3) \
  1302. ({ \
  1303. bfd_reloc_status_type ret = loongarch_pop (&op2); \
  1304. if (ret == bfd_reloc_ok) \
  1305. { \
  1306. ret = loongarch_pop (&op1); \
  1307. if (ret == bfd_reloc_ok) \
  1308. ret = loongarch_push (op3); \
  1309. } \
  1310. ret; \
  1311. })
  1312. static bfd_reloc_status_type
  1313. loongarch_reloc_rewrite_imm_insn (const Elf_Internal_Rela *rel,
  1314. const asection *input_section ATTRIBUTE_UNUSED,
  1315. reloc_howto_type *howto, bfd *input_bfd,
  1316. bfd_byte *contents, bfd_vma reloc_val)
  1317. {
  1318. int bits = bfd_get_reloc_size (howto) * 8;
  1319. uint32_t insn = bfd_get (bits, input_bfd, contents + rel->r_offset);
  1320. if (!loongarch_adjust_reloc_bitsfield(howto, &reloc_val))
  1321. return bfd_reloc_overflow;
  1322. insn = (insn & (uint32_t)howto->src_mask)
  1323. | ((insn & (~(uint32_t)howto->dst_mask)) | reloc_val);
  1324. bfd_put (bits, input_bfd, insn, contents + rel->r_offset);
  1325. return bfd_reloc_ok;
  1326. }
  1327. /* Emplace a static relocation. */
  1328. static bfd_reloc_status_type
  1329. perform_relocation (const Elf_Internal_Rela *rel, asection *input_section,
  1330. reloc_howto_type *howto, bfd_vma value,
  1331. bfd *input_bfd, bfd_byte *contents)
  1332. {
  1333. uint32_t insn1;
  1334. int64_t opr1, opr2, opr3;
  1335. bfd_reloc_status_type r = bfd_reloc_ok;
  1336. int bits = bfd_get_reloc_size (howto) * 8;
  1337. switch (ELFNN_R_TYPE (rel->r_info))
  1338. {
  1339. case R_LARCH_SOP_PUSH_PCREL:
  1340. case R_LARCH_SOP_PUSH_ABSOLUTE:
  1341. case R_LARCH_SOP_PUSH_GPREL:
  1342. case R_LARCH_SOP_PUSH_TLS_TPREL:
  1343. case R_LARCH_SOP_PUSH_TLS_GOT:
  1344. case R_LARCH_SOP_PUSH_TLS_GD:
  1345. case R_LARCH_SOP_PUSH_PLT_PCREL:
  1346. r = loongarch_push (value);
  1347. break;
  1348. case R_LARCH_SOP_PUSH_DUP:
  1349. r = loongarch_pop (&opr1);
  1350. if (r == bfd_reloc_ok)
  1351. {
  1352. r = loongarch_push (opr1);
  1353. if (r == bfd_reloc_ok)
  1354. r = loongarch_push (opr1);
  1355. }
  1356. break;
  1357. case R_LARCH_SOP_ASSERT:
  1358. r = loongarch_pop (&opr1);
  1359. if (r != bfd_reloc_ok || !opr1)
  1360. r = bfd_reloc_notsupported;
  1361. break;
  1362. case R_LARCH_SOP_NOT:
  1363. r = loongarch_pop (&opr1);
  1364. if (r == bfd_reloc_ok)
  1365. r = loongarch_push (!opr1);
  1366. break;
  1367. case R_LARCH_SOP_SUB:
  1368. r = LARCH_RELOC_PERFORM_3OP (opr1, opr2, opr1 - opr2);
  1369. break;
  1370. case R_LARCH_SOP_SL:
  1371. r = LARCH_RELOC_PERFORM_3OP (opr1, opr2, opr1 << opr2);
  1372. break;
  1373. case R_LARCH_SOP_SR:
  1374. r = LARCH_RELOC_PERFORM_3OP (opr1, opr2, opr1 >> opr2);
  1375. break;
  1376. case R_LARCH_SOP_AND:
  1377. r = LARCH_RELOC_PERFORM_3OP (opr1, opr2, opr1 & opr2);
  1378. break;
  1379. case R_LARCH_SOP_ADD:
  1380. r = LARCH_RELOC_PERFORM_3OP (opr1, opr2, opr1 + opr2);
  1381. break;
  1382. case R_LARCH_SOP_IF_ELSE:
  1383. r = loongarch_pop (&opr3);
  1384. if (r == bfd_reloc_ok)
  1385. {
  1386. r = loongarch_pop (&opr2);
  1387. if (r == bfd_reloc_ok)
  1388. {
  1389. r = loongarch_pop (&opr1);
  1390. if (r == bfd_reloc_ok)
  1391. r = loongarch_push (opr1 ? opr2 : opr3);
  1392. }
  1393. }
  1394. break;
  1395. case R_LARCH_SOP_POP_32_S_10_5:
  1396. case R_LARCH_SOP_POP_32_S_10_12:
  1397. case R_LARCH_SOP_POP_32_S_10_16:
  1398. case R_LARCH_SOP_POP_32_S_10_16_S2:
  1399. case R_LARCH_SOP_POP_32_S_5_20:
  1400. case R_LARCH_SOP_POP_32_U_10_12:
  1401. case R_LARCH_SOP_POP_32_U:
  1402. r = loongarch_pop (&opr1);
  1403. if (r != bfd_reloc_ok)
  1404. break;
  1405. r = loongarch_check_offset (rel, input_section);
  1406. if (r != bfd_reloc_ok)
  1407. break;
  1408. r = loongarch_reloc_rewrite_imm_insn (rel, input_section,
  1409. howto, input_bfd,
  1410. contents, (bfd_vma)opr1);
  1411. break;
  1412. case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
  1413. {
  1414. r = loongarch_pop (&opr1);
  1415. if (r != bfd_reloc_ok)
  1416. break;
  1417. if ((opr1 & 0x3) != 0)
  1418. {
  1419. r = bfd_reloc_overflow;
  1420. break;
  1421. }
  1422. uint32_t imm = opr1 >> howto->rightshift;
  1423. if ((imm & (~0xfffffU)) && ((imm & (~0xfffffU)) != (~0xfffffU)))
  1424. {
  1425. r = bfd_reloc_overflow;
  1426. break;
  1427. }
  1428. r = loongarch_check_offset (rel, input_section);
  1429. if (r != bfd_reloc_ok)
  1430. break;
  1431. insn1 = bfd_get (bits, input_bfd, contents + rel->r_offset);
  1432. insn1 = (insn1 & howto->src_mask)
  1433. | ((imm & 0xffffU) << 10)
  1434. | ((imm & 0x1f0000U) >> 16);
  1435. bfd_put (bits, input_bfd, insn1, contents + rel->r_offset);
  1436. break;
  1437. }
  1438. case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
  1439. {
  1440. r = loongarch_pop (&opr1);
  1441. if (r != bfd_reloc_ok)
  1442. break;
  1443. if ((opr1 & 0x3) != 0)
  1444. {
  1445. r = bfd_reloc_overflow;
  1446. break;
  1447. }
  1448. uint32_t imm = opr1 >> howto->rightshift;
  1449. if ((imm & (~0x1ffffffU)) && (imm & (~0x1ffffffU)) != (~0x1ffffffU))
  1450. {
  1451. r = bfd_reloc_overflow;
  1452. break;
  1453. }
  1454. r = loongarch_check_offset (rel, input_section);
  1455. if (r != bfd_reloc_ok)
  1456. break;
  1457. insn1 = bfd_get (bits, input_bfd, contents + rel->r_offset);
  1458. insn1 = ((insn1 & howto->src_mask)
  1459. | ((imm & 0xffffU) << 10)
  1460. | ((imm & 0x3ff0000U) >> 16));
  1461. bfd_put (bits, input_bfd, insn1, contents + rel->r_offset);
  1462. break;
  1463. }
  1464. case R_LARCH_TLS_DTPREL32:
  1465. case R_LARCH_32:
  1466. case R_LARCH_TLS_DTPREL64:
  1467. case R_LARCH_64:
  1468. r = loongarch_check_offset (rel, input_section);
  1469. if (r != bfd_reloc_ok)
  1470. break;
  1471. bfd_put (bits, input_bfd, value, contents + rel->r_offset);
  1472. break;
  1473. case R_LARCH_ADD8:
  1474. case R_LARCH_ADD16:
  1475. case R_LARCH_ADD24:
  1476. case R_LARCH_ADD32:
  1477. case R_LARCH_ADD64:
  1478. r = loongarch_check_offset (rel, input_section);
  1479. if (r != bfd_reloc_ok)
  1480. break;
  1481. opr1 = bfd_get (bits, input_bfd, contents + rel->r_offset);
  1482. bfd_put (bits, input_bfd, opr1 + value, contents + rel->r_offset);
  1483. break;
  1484. case R_LARCH_SUB8:
  1485. case R_LARCH_SUB16:
  1486. case R_LARCH_SUB24:
  1487. case R_LARCH_SUB32:
  1488. case R_LARCH_SUB64:
  1489. r = loongarch_check_offset (rel, input_section);
  1490. if (r != bfd_reloc_ok)
  1491. break;
  1492. opr1 = bfd_get (bits, input_bfd, contents + rel->r_offset);
  1493. bfd_put (bits, input_bfd, opr1 - value, contents + rel->r_offset);
  1494. break;
  1495. default:
  1496. r = bfd_reloc_notsupported;
  1497. }
  1498. return r;
  1499. }
  1500. #define LARCH_RECENT_RELOC_QUEUE_LENGTH 72
  1501. static struct
  1502. {
  1503. bfd *bfd;
  1504. asection *section;
  1505. bfd_vma r_offset;
  1506. int r_type;
  1507. bfd_vma relocation;
  1508. Elf_Internal_Sym *sym;
  1509. struct elf_link_hash_entry *h;
  1510. bfd_vma addend;
  1511. int64_t top_then;
  1512. } larch_reloc_queue[LARCH_RECENT_RELOC_QUEUE_LENGTH];
  1513. static size_t larch_reloc_queue_head = 0;
  1514. static size_t larch_reloc_queue_tail = 0;
  1515. static const char *
  1516. loongarch_sym_name (bfd *input_bfd, struct elf_link_hash_entry *h,
  1517. Elf_Internal_Sym *sym)
  1518. {
  1519. const char *ret = NULL;
  1520. if (sym)
  1521. ret = bfd_elf_string_from_elf_section (input_bfd,
  1522. elf_symtab_hdr (input_bfd).sh_link,
  1523. sym->st_name);
  1524. else if (h)
  1525. ret = h->root.root.string;
  1526. if (ret == NULL || *ret == '\0')
  1527. ret = "<nameless>";
  1528. return ret;
  1529. }
  1530. static void
  1531. loongarch_record_one_reloc (bfd *abfd, asection *section, int r_type,
  1532. bfd_vma r_offset, Elf_Internal_Sym *sym,
  1533. struct elf_link_hash_entry *h, bfd_vma addend)
  1534. {
  1535. if ((larch_reloc_queue_head == 0
  1536. && larch_reloc_queue_tail == LARCH_RECENT_RELOC_QUEUE_LENGTH - 1)
  1537. || larch_reloc_queue_head == larch_reloc_queue_tail + 1)
  1538. larch_reloc_queue_head =
  1539. (larch_reloc_queue_head + 1) % LARCH_RECENT_RELOC_QUEUE_LENGTH;
  1540. larch_reloc_queue[larch_reloc_queue_tail].bfd = abfd;
  1541. larch_reloc_queue[larch_reloc_queue_tail].section = section;
  1542. larch_reloc_queue[larch_reloc_queue_tail].r_offset = r_offset;
  1543. larch_reloc_queue[larch_reloc_queue_tail].r_type = r_type;
  1544. larch_reloc_queue[larch_reloc_queue_tail].sym = sym;
  1545. larch_reloc_queue[larch_reloc_queue_tail].h = h;
  1546. larch_reloc_queue[larch_reloc_queue_tail].addend = addend;
  1547. loongarch_top (&larch_reloc_queue[larch_reloc_queue_tail].top_then);
  1548. larch_reloc_queue_tail =
  1549. (larch_reloc_queue_tail + 1) % LARCH_RECENT_RELOC_QUEUE_LENGTH;
  1550. }
  1551. static void
  1552. loongarch_dump_reloc_record (void (*p) (const char *fmt, ...))
  1553. {
  1554. size_t i = larch_reloc_queue_head;
  1555. bfd *a_bfd = NULL;
  1556. asection *section = NULL;
  1557. bfd_vma r_offset = 0;
  1558. int inited = 0;
  1559. p ("Dump relocate record:\n");
  1560. p ("stack top\t\trelocation name\t\tsymbol");
  1561. while (i != larch_reloc_queue_tail)
  1562. {
  1563. if (a_bfd != larch_reloc_queue[i].bfd
  1564. || section != larch_reloc_queue[i].section
  1565. || r_offset != larch_reloc_queue[i].r_offset)
  1566. {
  1567. a_bfd = larch_reloc_queue[i].bfd;
  1568. section = larch_reloc_queue[i].section;
  1569. r_offset = larch_reloc_queue[i].r_offset;
  1570. p ("\nat %pB(%pA+0x%v):\n", larch_reloc_queue[i].bfd,
  1571. larch_reloc_queue[i].section, larch_reloc_queue[i].r_offset);
  1572. }
  1573. if (!inited)
  1574. inited = 1, p ("...\n");
  1575. reloc_howto_type *howto =
  1576. loongarch_elf_rtype_to_howto (larch_reloc_queue[i].bfd,
  1577. larch_reloc_queue[i].r_type);
  1578. p ("0x%V %s\t`%s'", (bfd_vma) larch_reloc_queue[i].top_then,
  1579. howto ? howto->name : "<unknown reloc>",
  1580. loongarch_sym_name (larch_reloc_queue[i].bfd, larch_reloc_queue[i].h,
  1581. larch_reloc_queue[i].sym));
  1582. long addend = larch_reloc_queue[i].addend;
  1583. if (addend < 0)
  1584. p (" - %ld", -addend);
  1585. else if (0 < addend)
  1586. p (" + %ld(0x%v)", addend, larch_reloc_queue[i].addend);
  1587. p ("\n");
  1588. i = (i + 1) % LARCH_RECENT_RELOC_QUEUE_LENGTH;
  1589. }
  1590. p ("\n"
  1591. "-- Record dump end --\n\n");
  1592. }
  1593. static bool
  1594. loongarch_reloc_is_fatal (struct bfd_link_info *info,
  1595. bfd *input_bfd,
  1596. asection *input_section,
  1597. Elf_Internal_Rela *rel,
  1598. reloc_howto_type *howto,
  1599. bfd_reloc_status_type rtype,
  1600. bool is_undefweak,
  1601. const char *name,
  1602. const char *msg)
  1603. {
  1604. bool fatal = true;
  1605. switch (rtype)
  1606. {
  1607. /* 'dangerous' means we do it but can't promise it's ok
  1608. 'unsupport' means out of ability of relocation type
  1609. 'undefined' means we can't deal with the undefined symbol. */
  1610. case bfd_reloc_undefined:
  1611. info->callbacks->undefined_symbol (info, name, input_bfd, input_section,
  1612. rel->r_offset, true);
  1613. info->callbacks->info ("%X%pB(%pA+0x%v): error: %s against %s`%s':\n%s\n",
  1614. input_bfd, input_section, rel->r_offset,
  1615. howto->name,
  1616. is_undefweak ? "[undefweak] " : "", name, msg);
  1617. break;
  1618. case bfd_reloc_dangerous:
  1619. info->callbacks->info ("%pB(%pA+0x%v): warning: %s against %s`%s':\n%s\n",
  1620. input_bfd, input_section, rel->r_offset,
  1621. howto->name,
  1622. is_undefweak ? "[undefweak] " : "", name, msg);
  1623. fatal = false;
  1624. break;
  1625. case bfd_reloc_notsupported:
  1626. info->callbacks->info ("%X%pB(%pA+0x%v): error: %s against %s`%s':\n%s\n",
  1627. input_bfd, input_section, rel->r_offset,
  1628. howto->name,
  1629. is_undefweak ? "[undefweak] " : "", name, msg);
  1630. break;
  1631. default:
  1632. break;
  1633. }
  1634. return fatal;
  1635. }
  1636. static int
  1637. loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
  1638. bfd *input_bfd, asection *input_section,
  1639. bfd_byte *contents, Elf_Internal_Rela *relocs,
  1640. Elf_Internal_Sym *local_syms,
  1641. asection **local_sections)
  1642. {
  1643. Elf_Internal_Rela *rel;
  1644. Elf_Internal_Rela *relend;
  1645. bool fatal = false;
  1646. asection *sreloc = elf_section_data (input_section)->sreloc;
  1647. struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
  1648. Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
  1649. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
  1650. bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
  1651. bool is_pic = bfd_link_pic (info);
  1652. bool is_dyn = elf_hash_table (info)->dynamic_sections_created;
  1653. asection *plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
  1654. asection *got = htab->elf.sgot;
  1655. relend = relocs + input_section->reloc_count;
  1656. for (rel = relocs; rel < relend; rel++)
  1657. {
  1658. int r_type = ELFNN_R_TYPE (rel->r_info);
  1659. unsigned long r_symndx = ELFNN_R_SYM (rel->r_info);
  1660. bfd_vma pc = sec_addr (input_section) + rel->r_offset;
  1661. reloc_howto_type *howto = NULL;
  1662. asection *sec = NULL;
  1663. Elf_Internal_Sym *sym = NULL;
  1664. struct elf_link_hash_entry *h = NULL;
  1665. const char *name;
  1666. bfd_reloc_status_type r = bfd_reloc_ok;
  1667. bool is_ie, is_undefweak, unresolved_reloc, defined_local;
  1668. bool resolved_local, resolved_dynly, resolved_to_const;
  1669. char tls_type;
  1670. bfd_vma relocation;
  1671. bfd_vma off, ie_off;
  1672. int i, j;
  1673. howto = loongarch_elf_rtype_to_howto (input_bfd, r_type);
  1674. if (howto == NULL || r_type == R_LARCH_GNU_VTINHERIT
  1675. || r_type == R_LARCH_GNU_VTENTRY)
  1676. continue;
  1677. /* This is a final link. */
  1678. if (r_symndx < symtab_hdr->sh_info)
  1679. {
  1680. is_undefweak = false;
  1681. unresolved_reloc = false;
  1682. sym = local_syms + r_symndx;
  1683. sec = local_sections[r_symndx];
  1684. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1685. /* Relocate against local STT_GNU_IFUNC symbol. */
  1686. if (!bfd_link_relocatable (info)
  1687. && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  1688. {
  1689. h = elfNN_loongarch_get_local_sym_hash (htab, input_bfd, rel,
  1690. false);
  1691. if (h == NULL)
  1692. abort ();
  1693. /* Set STT_GNU_IFUNC symbol value. */
  1694. h->root.u.def.value = sym->st_value;
  1695. h->root.u.def.section = sec;
  1696. }
  1697. defined_local = true;
  1698. resolved_local = true;
  1699. resolved_dynly = false;
  1700. resolved_to_const = false;
  1701. if (bfd_link_relocatable (info)
  1702. && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  1703. rel->r_addend += sec->output_offset;
  1704. }
  1705. else
  1706. {
  1707. bool warned, ignored;
  1708. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1709. r_symndx, symtab_hdr, sym_hashes,
  1710. h, sec, relocation,
  1711. unresolved_reloc, warned, ignored);
  1712. /* Here means symbol isn't local symbol only and 'h != NULL'. */
  1713. /* The 'unresolved_syms_in_objects' specify how to deal with undefined
  1714. symbol. And 'dynamic_undefined_weak' specify what to do when
  1715. meeting undefweak. */
  1716. if ((is_undefweak = h->root.type == bfd_link_hash_undefweak))
  1717. {
  1718. defined_local = false;
  1719. resolved_local = false;
  1720. resolved_to_const = (!is_dyn || h->dynindx == -1
  1721. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
  1722. resolved_dynly = !resolved_local && !resolved_to_const;
  1723. }
  1724. else if (warned)
  1725. {
  1726. /* Symbol undefined offen means failed already. I don't know why
  1727. 'warned' here but I guess it want to continue relocating as if
  1728. no error occures to find other errors as more as possible. */
  1729. /* To avoid generating warning messages about truncated
  1730. relocations, set the relocation's address to be the same as
  1731. the start of this section. */
  1732. relocation = (input_section->output_section
  1733. ? input_section->output_section->vma
  1734. : 0);
  1735. defined_local = relocation != 0;
  1736. resolved_local = defined_local;
  1737. resolved_to_const = !resolved_local;
  1738. resolved_dynly = false;
  1739. }
  1740. else
  1741. {
  1742. defined_local = !unresolved_reloc && !ignored;
  1743. resolved_local =
  1744. defined_local && SYMBOL_REFERENCES_LOCAL (info, h);
  1745. resolved_dynly = !resolved_local;
  1746. resolved_to_const = !resolved_local && !resolved_dynly;
  1747. }
  1748. }
  1749. name = loongarch_sym_name (input_bfd, h, sym);
  1750. if (sec != NULL && discarded_section (sec))
  1751. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, rel,
  1752. 1, relend, howto, 0, contents);
  1753. if (bfd_link_relocatable (info))
  1754. continue;
  1755. /* The r_symndx will be STN_UNDEF (zero) only for relocs against symbols
  1756. from removed linkonce sections, or sections discarded by a linker
  1757. script. Also for R_*_SOP_PUSH_ABSOLUTE and PCREL to specify const. */
  1758. if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
  1759. {
  1760. defined_local = false;
  1761. resolved_local = false;
  1762. resolved_dynly = false;
  1763. resolved_to_const = true;
  1764. }
  1765. /* The ifunc without reference does not generate plt. */
  1766. if (h && h->type == STT_GNU_IFUNC && h->plt.offset != MINUS_ONE)
  1767. {
  1768. defined_local = true;
  1769. resolved_local = true;
  1770. resolved_dynly = false;
  1771. resolved_to_const = false;
  1772. relocation = sec_addr (plt) + h->plt.offset;
  1773. }
  1774. unresolved_reloc = resolved_dynly;
  1775. BFD_ASSERT (resolved_local + resolved_dynly + resolved_to_const == 1);
  1776. /* BFD_ASSERT (!resolved_dynly || (h && h->dynindx != -1));. */
  1777. BFD_ASSERT (!resolved_local || defined_local);
  1778. is_ie = false;
  1779. switch (r_type)
  1780. {
  1781. case R_LARCH_MARK_PCREL:
  1782. case R_LARCH_MARK_LA:
  1783. case R_LARCH_NONE:
  1784. r = bfd_reloc_continue;
  1785. unresolved_reloc = false;
  1786. break;
  1787. case R_LARCH_32:
  1788. case R_LARCH_64:
  1789. if (resolved_dynly || (is_pic && resolved_local))
  1790. {
  1791. Elf_Internal_Rela outrel;
  1792. /* When generating a shared object, these relocations are copied
  1793. into the output file to be resolved at run time. */
  1794. outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
  1795. input_section,
  1796. rel->r_offset);
  1797. unresolved_reloc = (!((bfd_vma) -2 <= outrel.r_offset)
  1798. && (input_section->flags & SEC_ALLOC));
  1799. outrel.r_offset += sec_addr (input_section);
  1800. /* A pointer point to a local ifunc symbol. */
  1801. if(h
  1802. && h->type == STT_GNU_IFUNC
  1803. && (h->dynindx == -1
  1804. || h->forced_local
  1805. || bfd_link_executable(info)))
  1806. {
  1807. outrel.r_info = ELFNN_R_INFO (0, R_LARCH_IRELATIVE);
  1808. outrel.r_addend = (h->root.u.def.value
  1809. + h->root.u.def.section->output_section->vma
  1810. + h->root.u.def.section->output_offset);
  1811. /* Dynamic relocations are stored in
  1812. 1. .rela.ifunc section in PIC object.
  1813. 2. .rela.got section in dynamic executable.
  1814. 3. .rela.iplt section in static executable. */
  1815. if (bfd_link_pic (info))
  1816. sreloc = htab->elf.irelifunc;
  1817. else if (htab->elf.splt != NULL)
  1818. sreloc = htab->elf.srelgot;
  1819. else
  1820. sreloc = htab->elf.irelplt;
  1821. }
  1822. else if (resolved_dynly)
  1823. {
  1824. outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
  1825. outrel.r_addend = rel->r_addend;
  1826. }
  1827. else
  1828. {
  1829. outrel.r_info = ELFNN_R_INFO (0, R_LARCH_RELATIVE);
  1830. outrel.r_addend = relocation + rel->r_addend;
  1831. }
  1832. if (unresolved_reloc)
  1833. loongarch_elf_append_rela (output_bfd, sreloc, &outrel);
  1834. }
  1835. relocation += rel->r_addend;
  1836. break;
  1837. case R_LARCH_ADD8:
  1838. case R_LARCH_ADD16:
  1839. case R_LARCH_ADD24:
  1840. case R_LARCH_ADD32:
  1841. case R_LARCH_ADD64:
  1842. case R_LARCH_SUB8:
  1843. case R_LARCH_SUB16:
  1844. case R_LARCH_SUB24:
  1845. case R_LARCH_SUB32:
  1846. case R_LARCH_SUB64:
  1847. if (resolved_dynly)
  1848. fatal = (loongarch_reloc_is_fatal
  1849. (info, input_bfd, input_section, rel, howto,
  1850. bfd_reloc_undefined, is_undefweak, name,
  1851. "Can't be resolved dynamically. "
  1852. "If this procedure is hand-written assembly,\n"
  1853. "there must be something like '.dword sym1 - sym2' "
  1854. "to generate these relocs\n"
  1855. "and we can't get known link-time address of "
  1856. "these symbols."));
  1857. else
  1858. relocation += rel->r_addend;
  1859. break;
  1860. case R_LARCH_TLS_DTPREL32:
  1861. case R_LARCH_TLS_DTPREL64:
  1862. if (resolved_dynly)
  1863. {
  1864. Elf_Internal_Rela outrel;
  1865. outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
  1866. input_section,
  1867. rel->r_offset);
  1868. unresolved_reloc = (!((bfd_vma) -2 <= outrel.r_offset)
  1869. && (input_section->flags & SEC_ALLOC));
  1870. outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
  1871. outrel.r_offset += sec_addr (input_section);
  1872. outrel.r_addend = rel->r_addend;
  1873. if (unresolved_reloc)
  1874. loongarch_elf_append_rela (output_bfd, sreloc, &outrel);
  1875. break;
  1876. }
  1877. if (resolved_to_const)
  1878. fatal = loongarch_reloc_is_fatal (info, input_bfd, input_section,
  1879. rel, howto,
  1880. bfd_reloc_notsupported,
  1881. is_undefweak, name,
  1882. "Internal:");
  1883. if (resolved_local)
  1884. {
  1885. if (!elf_hash_table (info)->tls_sec)
  1886. {
  1887. fatal = loongarch_reloc_is_fatal (info, input_bfd,
  1888. input_section, rel, howto, bfd_reloc_notsupported,
  1889. is_undefweak, name, "TLS section not be created");
  1890. }
  1891. else
  1892. relocation -= elf_hash_table (info)->tls_sec->vma;
  1893. }
  1894. else
  1895. {
  1896. fatal = loongarch_reloc_is_fatal (info, input_bfd,
  1897. input_section, rel, howto, bfd_reloc_undefined,
  1898. is_undefweak, name,
  1899. "TLS LE just can be resolved local only.");
  1900. }
  1901. break;
  1902. case R_LARCH_SOP_PUSH_TLS_TPREL:
  1903. if (resolved_local)
  1904. {
  1905. if (!elf_hash_table (info)->tls_sec)
  1906. fatal = (loongarch_reloc_is_fatal
  1907. (info, input_bfd, input_section, rel, howto,
  1908. bfd_reloc_notsupported, is_undefweak, name,
  1909. "TLS section not be created"));
  1910. else
  1911. relocation -= elf_hash_table (info)->tls_sec->vma;
  1912. }
  1913. else
  1914. fatal = (loongarch_reloc_is_fatal
  1915. (info, input_bfd, input_section, rel, howto,
  1916. bfd_reloc_undefined, is_undefweak, name,
  1917. "TLS LE just can be resolved local only."));
  1918. break;
  1919. case R_LARCH_SOP_PUSH_ABSOLUTE:
  1920. if (is_undefweak)
  1921. {
  1922. if (resolved_dynly)
  1923. fatal = (loongarch_reloc_is_fatal
  1924. (info, input_bfd, input_section, rel, howto,
  1925. bfd_reloc_dangerous, is_undefweak, name,
  1926. "Someone require us to resolve undefweak "
  1927. "symbol dynamically. \n"
  1928. "But this reloc can't be done. "
  1929. "I think I can't throw error "
  1930. "for this\n"
  1931. "so I resolved it to 0. "
  1932. "I suggest to re-compile with '-fpic'."));
  1933. relocation = 0;
  1934. unresolved_reloc = false;
  1935. break;
  1936. }
  1937. if (resolved_to_const)
  1938. {
  1939. relocation += rel->r_addend;
  1940. break;
  1941. }
  1942. if (is_pic)
  1943. {
  1944. fatal = (loongarch_reloc_is_fatal
  1945. (info, input_bfd, input_section, rel, howto,
  1946. bfd_reloc_notsupported, is_undefweak, name,
  1947. "Under PIC we don't know load address. Re-compile "
  1948. "with '-fpic'?"));
  1949. break;
  1950. }
  1951. if (resolved_dynly)
  1952. {
  1953. if (!(plt && h && h->plt.offset != MINUS_ONE))
  1954. {
  1955. fatal = (loongarch_reloc_is_fatal
  1956. (info, input_bfd, input_section, rel, howto,
  1957. bfd_reloc_undefined, is_undefweak, name,
  1958. "Can't be resolved dynamically. Try to re-compile "
  1959. "with '-fpic'?"));
  1960. break;
  1961. }
  1962. if (rel->r_addend != 0)
  1963. {
  1964. fatal = (loongarch_reloc_is_fatal
  1965. (info, input_bfd, input_section, rel, howto,
  1966. bfd_reloc_notsupported, is_undefweak, name,
  1967. "Shouldn't be with r_addend."));
  1968. break;
  1969. }
  1970. relocation = sec_addr (plt) + h->plt.offset;
  1971. unresolved_reloc = false;
  1972. break;
  1973. }
  1974. if (resolved_local)
  1975. {
  1976. relocation += rel->r_addend;
  1977. break;
  1978. }
  1979. break;
  1980. case R_LARCH_SOP_PUSH_PCREL:
  1981. case R_LARCH_SOP_PUSH_PLT_PCREL:
  1982. unresolved_reloc = false;
  1983. if (resolved_to_const)
  1984. {
  1985. relocation += rel->r_addend;
  1986. break;
  1987. }
  1988. else if (is_undefweak)
  1989. {
  1990. i = 0, j = 0;
  1991. relocation = 0;
  1992. if (resolved_dynly)
  1993. {
  1994. if (h && h->plt.offset != MINUS_ONE)
  1995. i = 1, j = 2;
  1996. else
  1997. fatal = (loongarch_reloc_is_fatal
  1998. (info, input_bfd, input_section, rel, howto,
  1999. bfd_reloc_dangerous, is_undefweak, name,
  2000. "Undefweak need to be resolved dynamically, "
  2001. "but PLT stub doesn't represent."));
  2002. }
  2003. }
  2004. else
  2005. {
  2006. if (!(defined_local || (h && h->plt.offset != MINUS_ONE)))
  2007. {
  2008. fatal = (loongarch_reloc_is_fatal
  2009. (info, input_bfd, input_section, rel, howto,
  2010. bfd_reloc_undefined, is_undefweak, name,
  2011. "PLT stub does not represent and "
  2012. "symbol not defined."));
  2013. break;
  2014. }
  2015. if (resolved_local)
  2016. i = 0, j = 2;
  2017. else /* if (resolved_dynly) */
  2018. {
  2019. if (!(h && h->plt.offset != MINUS_ONE))
  2020. fatal = (loongarch_reloc_is_fatal
  2021. (info, input_bfd, input_section, rel, howto,
  2022. bfd_reloc_dangerous, is_undefweak, name,
  2023. "Internal: PLT stub doesn't represent. "
  2024. "Resolve it with pcrel"));
  2025. i = 1, j = 3;
  2026. }
  2027. }
  2028. for (; i < j; i++)
  2029. {
  2030. if ((i & 1) == 0 && defined_local)
  2031. {
  2032. relocation -= pc;
  2033. relocation += rel->r_addend;
  2034. break;
  2035. }
  2036. if ((i & 1) && h && h->plt.offset != MINUS_ONE)
  2037. {
  2038. if (rel->r_addend != 0)
  2039. {
  2040. fatal = (loongarch_reloc_is_fatal
  2041. (info, input_bfd, input_section, rel, howto,
  2042. bfd_reloc_notsupported, is_undefweak, name,
  2043. "PLT shouldn't be with r_addend."));
  2044. break;
  2045. }
  2046. relocation = sec_addr (plt) + h->plt.offset - pc;
  2047. break;
  2048. }
  2049. }
  2050. break;
  2051. case R_LARCH_SOP_PUSH_GPREL:
  2052. unresolved_reloc = false;
  2053. if (rel->r_addend != 0)
  2054. {
  2055. fatal = (loongarch_reloc_is_fatal
  2056. (info, input_bfd, input_section, rel, howto,
  2057. bfd_reloc_notsupported, is_undefweak, name,
  2058. "Shouldn't be with r_addend."));
  2059. break;
  2060. }
  2061. if (h != NULL)
  2062. {
  2063. off = h->got.offset;
  2064. if (off == MINUS_ONE
  2065. && h->type != STT_GNU_IFUNC)
  2066. {
  2067. fatal = (loongarch_reloc_is_fatal
  2068. (info, input_bfd, input_section, rel, howto,
  2069. bfd_reloc_notsupported, is_undefweak, name,
  2070. "Internal: GOT entry doesn't represent."));
  2071. break;
  2072. }
  2073. /* Hidden symbol not has .got entry, only .got.plt entry
  2074. so gprel is (plt - got). */
  2075. if (off == MINUS_ONE
  2076. && h->type == STT_GNU_IFUNC)
  2077. {
  2078. if (h->plt.offset == (bfd_vma) -1)
  2079. {
  2080. abort();
  2081. }
  2082. bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE;
  2083. off = plt_index * GOT_ENTRY_SIZE;
  2084. if (htab->elf.splt != NULL)
  2085. {
  2086. /* Section .plt header is 2 times of plt entry. */
  2087. off = sec_addr(htab->elf.sgotplt) + off
  2088. - sec_addr(htab->elf.sgot);
  2089. }
  2090. else
  2091. {
  2092. /* Section iplt not has plt header. */
  2093. off = sec_addr(htab->elf.igotplt) + off
  2094. - sec_addr(htab->elf.sgot);
  2095. }
  2096. }
  2097. if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (is_dyn, is_pic, h)
  2098. || (is_pic && SYMBOL_REFERENCES_LOCAL (info, h)))
  2099. {
  2100. /* This is actually a static link, or it is a
  2101. -Bsymbolic link and the symbol is defined
  2102. locally, or the symbol was forced to be local
  2103. because of a version file. We must initialize
  2104. this entry in the global offset table. Since the
  2105. offset must always be a multiple of the word size,
  2106. we use the least significant bit to record whether
  2107. we have initialized it already.
  2108. When doing a dynamic link, we create a .rela.got
  2109. relocation entry to initialize the value. This
  2110. is done in the finish_dynamic_symbol routine. */
  2111. if (resolved_dynly)
  2112. {
  2113. fatal = (loongarch_reloc_is_fatal
  2114. (info, input_bfd, input_section, rel, howto,
  2115. bfd_reloc_dangerous, is_undefweak, name,
  2116. "Internal: here shouldn't dynamic."));
  2117. }
  2118. if (!(defined_local || resolved_to_const))
  2119. {
  2120. fatal = (loongarch_reloc_is_fatal
  2121. (info, input_bfd, input_section, rel, howto,
  2122. bfd_reloc_undefined, is_undefweak, name,
  2123. "Internal: "));
  2124. break;
  2125. }
  2126. if ((off & 1) != 0)
  2127. off &= ~1;
  2128. else
  2129. {
  2130. /* The pr21964-4. Create relocate entry. */
  2131. if (is_pic && h->start_stop)
  2132. {
  2133. asection *s;
  2134. Elf_Internal_Rela outrel;
  2135. /* We need to generate a R_LARCH_RELATIVE reloc
  2136. for the dynamic linker. */
  2137. s = htab->elf.srelgot;
  2138. if (!s)
  2139. {
  2140. fatal = loongarch_reloc_is_fatal (info, input_bfd,
  2141. input_section, rel, howto,
  2142. bfd_reloc_notsupported, is_undefweak, name,
  2143. "Internal: '.rel.got' not represent");
  2144. break;
  2145. }
  2146. outrel.r_offset = sec_addr (got) + off;
  2147. outrel.r_info = ELFNN_R_INFO (0, R_LARCH_RELATIVE);
  2148. outrel.r_addend = relocation; /* Link-time addr. */
  2149. loongarch_elf_append_rela (output_bfd, s, &outrel);
  2150. }
  2151. bfd_put_NN (output_bfd, relocation, got->contents + off);
  2152. h->got.offset |= 1;
  2153. }
  2154. }
  2155. }
  2156. else
  2157. {
  2158. if (!local_got_offsets)
  2159. {
  2160. fatal = (loongarch_reloc_is_fatal
  2161. (info, input_bfd, input_section, rel, howto,
  2162. bfd_reloc_notsupported, is_undefweak, name,
  2163. "Internal: local got offsets not reporesent."));
  2164. break;
  2165. }
  2166. off = local_got_offsets[r_symndx];
  2167. if (off == MINUS_ONE)
  2168. {
  2169. fatal = (loongarch_reloc_is_fatal
  2170. (info, input_bfd, input_section, rel, howto,
  2171. bfd_reloc_notsupported, is_undefweak, name,
  2172. "Internal: GOT entry doesn't represent."));
  2173. break;
  2174. }
  2175. /* The offset must always be a multiple of the word size.
  2176. So, we can use the least significant bit to record
  2177. whether we have already processed this entry. */
  2178. if ((off & 1) != 0)
  2179. off &= ~1;
  2180. else
  2181. {
  2182. if (is_pic)
  2183. {
  2184. asection *s;
  2185. Elf_Internal_Rela outrel;
  2186. /* We need to generate a R_LARCH_RELATIVE reloc
  2187. for the dynamic linker. */
  2188. s = htab->elf.srelgot;
  2189. if (!s)
  2190. {
  2191. fatal = (loongarch_reloc_is_fatal
  2192. (info, input_bfd, input_section, rel, howto,
  2193. bfd_reloc_notsupported, is_undefweak, name,
  2194. "Internal: '.rel.got' not represent"));
  2195. break;
  2196. }
  2197. outrel.r_offset = sec_addr (got) + off;
  2198. outrel.r_info = ELFNN_R_INFO (0, R_LARCH_RELATIVE);
  2199. outrel.r_addend = relocation; /* Link-time addr. */
  2200. loongarch_elf_append_rela (output_bfd, s, &outrel);
  2201. }
  2202. bfd_put_NN (output_bfd, relocation, got->contents + off);
  2203. local_got_offsets[r_symndx] |= 1;
  2204. }
  2205. }
  2206. relocation = off;
  2207. break;
  2208. case R_LARCH_SOP_PUSH_TLS_GOT:
  2209. case R_LARCH_SOP_PUSH_TLS_GD:
  2210. if (r_type == R_LARCH_SOP_PUSH_TLS_GOT)
  2211. is_ie = true;
  2212. unresolved_reloc = false;
  2213. if (rel->r_addend != 0)
  2214. {
  2215. fatal = (loongarch_reloc_is_fatal
  2216. (info, input_bfd, input_section, rel, howto,
  2217. bfd_reloc_notsupported, is_undefweak, name,
  2218. "Shouldn't be with r_addend."));
  2219. break;
  2220. }
  2221. if (resolved_to_const && is_undefweak && h->dynindx != -1)
  2222. {
  2223. /* What if undefweak? Let rtld make a decision. */
  2224. resolved_to_const = resolved_local = false;
  2225. resolved_dynly = true;
  2226. }
  2227. if (resolved_to_const)
  2228. {
  2229. fatal = (loongarch_reloc_is_fatal
  2230. (info, input_bfd, input_section, rel, howto,
  2231. bfd_reloc_notsupported, is_undefweak, name,
  2232. "Internal: Shouldn't be resolved to const."));
  2233. break;
  2234. }
  2235. if (h != NULL)
  2236. {
  2237. off = h->got.offset;
  2238. h->got.offset |= 1;
  2239. }
  2240. else
  2241. {
  2242. off = local_got_offsets[r_symndx];
  2243. local_got_offsets[r_symndx] |= 1;
  2244. }
  2245. if (off == MINUS_ONE)
  2246. {
  2247. fatal = (loongarch_reloc_is_fatal
  2248. (info, input_bfd, input_section, rel, howto,
  2249. bfd_reloc_notsupported, is_undefweak, name,
  2250. "Internal: TLS GOT entry doesn't represent."));
  2251. break;
  2252. }
  2253. tls_type = _bfd_loongarch_elf_tls_type (input_bfd, h, r_symndx);
  2254. /* If this symbol is referenced by both GD and IE TLS, the IE
  2255. reference's GOT slot follows the GD reference's slots. */
  2256. ie_off = 0;
  2257. if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
  2258. ie_off = 2 * GOT_ENTRY_SIZE;
  2259. if ((off & 1) != 0)
  2260. off &= ~1;
  2261. else
  2262. {
  2263. bfd_vma tls_block_off = 0;
  2264. Elf_Internal_Rela outrel;
  2265. if (resolved_local)
  2266. {
  2267. if (!elf_hash_table (info)->tls_sec)
  2268. {
  2269. fatal = (loongarch_reloc_is_fatal
  2270. (info, input_bfd, input_section, rel, howto,
  2271. bfd_reloc_notsupported, is_undefweak, name,
  2272. "Internal: TLS sec not represent."));
  2273. break;
  2274. }
  2275. tls_block_off =
  2276. relocation - elf_hash_table (info)->tls_sec->vma;
  2277. }
  2278. if (tls_type & GOT_TLS_GD)
  2279. {
  2280. outrel.r_offset = sec_addr (got) + off;
  2281. outrel.r_addend = 0;
  2282. bfd_put_NN (output_bfd, 0, got->contents + off);
  2283. if (resolved_local && bfd_link_executable (info))
  2284. bfd_put_NN (output_bfd, 1, got->contents + off);
  2285. else if (resolved_local /* && !bfd_link_executable (info) */)
  2286. {
  2287. outrel.r_info = ELFNN_R_INFO (0, R_LARCH_TLS_DTPMODNN);
  2288. loongarch_elf_append_rela (output_bfd, htab->elf.srelgot,
  2289. &outrel);
  2290. }
  2291. else /* if (resolved_dynly) */
  2292. {
  2293. outrel.r_info =
  2294. ELFNN_R_INFO (h->dynindx, R_LARCH_TLS_DTPMODNN);
  2295. loongarch_elf_append_rela (output_bfd, htab->elf.srelgot,
  2296. &outrel);
  2297. }
  2298. outrel.r_offset += GOT_ENTRY_SIZE;
  2299. bfd_put_NN (output_bfd, tls_block_off,
  2300. got->contents + off + GOT_ENTRY_SIZE);
  2301. if (resolved_local)
  2302. /* DTPREL known. */;
  2303. else /* if (resolved_dynly) */
  2304. {
  2305. outrel.r_info =
  2306. ELFNN_R_INFO (h->dynindx, R_LARCH_TLS_DTPRELNN);
  2307. loongarch_elf_append_rela (output_bfd, htab->elf.srelgot,
  2308. &outrel);
  2309. }
  2310. }
  2311. if (tls_type & GOT_TLS_IE)
  2312. {
  2313. outrel.r_offset = sec_addr (got) + off + ie_off;
  2314. bfd_put_NN (output_bfd, tls_block_off,
  2315. got->contents + off + ie_off);
  2316. if (resolved_local && bfd_link_executable (info))
  2317. /* TPREL known. */;
  2318. else if (resolved_local /* && !bfd_link_executable (info) */)
  2319. {
  2320. outrel.r_info = ELFNN_R_INFO (0, R_LARCH_TLS_TPRELNN);
  2321. outrel.r_addend = tls_block_off;
  2322. loongarch_elf_append_rela (output_bfd, htab->elf.srelgot,
  2323. &outrel);
  2324. }
  2325. else /* if (resolved_dynly) */
  2326. {
  2327. /* Static linking has no .dynsym table. */
  2328. if (!htab->elf.dynamic_sections_created)
  2329. {
  2330. outrel.r_info =
  2331. ELFNN_R_INFO (0, R_LARCH_TLS_TPRELNN);
  2332. outrel.r_addend = 0;
  2333. }
  2334. else
  2335. {
  2336. outrel.r_info =
  2337. ELFNN_R_INFO (h->dynindx, R_LARCH_TLS_TPRELNN);
  2338. outrel.r_addend = 0;
  2339. }
  2340. loongarch_elf_append_rela (output_bfd, htab->elf.srelgot,
  2341. &outrel);
  2342. }
  2343. }
  2344. }
  2345. relocation = off + (is_ie ? ie_off : 0);
  2346. break;
  2347. default:
  2348. break;
  2349. }
  2350. if (fatal)
  2351. break;
  2352. do
  2353. {
  2354. /* 'unresolved_reloc' means we haven't done it yet.
  2355. We need help of dynamic linker to fix this memory location up. */
  2356. if (!unresolved_reloc)
  2357. break;
  2358. if (_bfd_elf_section_offset (output_bfd, info, input_section,
  2359. rel->r_offset) == MINUS_ONE)
  2360. /* WHY? May because it's invalid so skip checking.
  2361. But why dynamic reloc a invalid section? */
  2362. break;
  2363. if (input_section->output_section->flags & SEC_DEBUGGING)
  2364. {
  2365. fatal = (loongarch_reloc_is_fatal
  2366. (info, input_bfd, input_section, rel, howto,
  2367. bfd_reloc_dangerous, is_undefweak, name,
  2368. "Seems dynamic linker not process "
  2369. "sections 'SEC_DEBUGGING'."));
  2370. }
  2371. if (!is_dyn)
  2372. break;
  2373. if ((info->flags & DF_TEXTREL) == 0)
  2374. if (input_section->output_section->flags & SEC_READONLY)
  2375. info->flags |= DF_TEXTREL;
  2376. }
  2377. while (0);
  2378. if (fatal)
  2379. break;
  2380. loongarch_record_one_reloc (input_bfd, input_section, r_type,
  2381. rel->r_offset, sym, h, rel->r_addend);
  2382. if (r != bfd_reloc_continue)
  2383. r = perform_relocation (rel, input_section, howto, relocation,
  2384. input_bfd, contents);
  2385. switch (r)
  2386. {
  2387. case bfd_reloc_dangerous:
  2388. case bfd_reloc_continue:
  2389. case bfd_reloc_ok:
  2390. continue;
  2391. case bfd_reloc_overflow:
  2392. /* Overflow value can't be filled in. */
  2393. loongarch_dump_reloc_record (info->callbacks->info);
  2394. info->callbacks->reloc_overflow
  2395. (info, h ? &h->root : NULL, name, howto->name, rel->r_addend,
  2396. input_bfd, input_section, rel->r_offset);
  2397. break;
  2398. case bfd_reloc_outofrange:
  2399. /* Stack state incorrect. */
  2400. loongarch_dump_reloc_record (info->callbacks->info);
  2401. info->callbacks->info
  2402. ("%X%H: Internal stack state is incorrect.\n"
  2403. "Want to push to full stack or pop from empty stack?\n",
  2404. input_bfd, input_section, rel->r_offset);
  2405. break;
  2406. case bfd_reloc_notsupported:
  2407. info->callbacks->info ("%X%H: Unknown relocation type.\n", input_bfd,
  2408. input_section, rel->r_offset);
  2409. break;
  2410. default:
  2411. info->callbacks->info ("%X%H: Internal: unknown error.\n", input_bfd,
  2412. input_section, rel->r_offset);
  2413. break;
  2414. }
  2415. fatal = true;
  2416. break;
  2417. }
  2418. return !fatal;
  2419. }
  2420. /* Finish up dynamic symbol handling. We set the contents of various
  2421. dynamic sections here. */
  2422. static bool
  2423. loongarch_elf_finish_dynamic_symbol (bfd *output_bfd,
  2424. struct bfd_link_info *info,
  2425. struct elf_link_hash_entry *h,
  2426. Elf_Internal_Sym *sym)
  2427. {
  2428. struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
  2429. const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  2430. if (h->plt.offset != MINUS_ONE)
  2431. {
  2432. size_t i, plt_idx;
  2433. asection *plt, *gotplt, *relplt;
  2434. bfd_vma got_address;
  2435. uint32_t plt_entry[PLT_ENTRY_INSNS];
  2436. bfd_byte *loc;
  2437. Elf_Internal_Rela rela;
  2438. if (htab->elf.splt)
  2439. {
  2440. BFD_ASSERT ((h->type == STT_GNU_IFUNC
  2441. && SYMBOL_REFERENCES_LOCAL (info, h))
  2442. || h->dynindx != -1);
  2443. plt = htab->elf.splt;
  2444. gotplt = htab->elf.sgotplt;
  2445. relplt = htab->elf.srelplt;
  2446. plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
  2447. got_address =
  2448. sec_addr (gotplt) + GOTPLT_HEADER_SIZE + plt_idx * GOT_ENTRY_SIZE;
  2449. }
  2450. else /* if (htab->elf.iplt) */
  2451. {
  2452. BFD_ASSERT (h->type == STT_GNU_IFUNC
  2453. && SYMBOL_REFERENCES_LOCAL (info, h));
  2454. plt = htab->elf.iplt;
  2455. gotplt = htab->elf.igotplt;
  2456. relplt = htab->elf.irelplt;
  2457. plt_idx = h->plt.offset / PLT_ENTRY_SIZE;
  2458. got_address = sec_addr (gotplt) + plt_idx * GOT_ENTRY_SIZE;
  2459. }
  2460. /* Find out where the .plt entry should go. */
  2461. loc = plt->contents + h->plt.offset;
  2462. /* Fill in the PLT entry itself. */
  2463. if (!loongarch_make_plt_entry (got_address,
  2464. sec_addr (plt) + h->plt.offset,
  2465. plt_entry))
  2466. return false;
  2467. for (i = 0; i < PLT_ENTRY_INSNS; i++)
  2468. bfd_put_32 (output_bfd, plt_entry[i], loc + 4 * i);
  2469. /* Fill in the initial value of the .got.plt entry. */
  2470. loc = gotplt->contents + (got_address - sec_addr (gotplt));
  2471. bfd_put_NN (output_bfd, sec_addr (plt), loc);
  2472. rela.r_offset = got_address;
  2473. /* TRUE if this is a PLT reference to a local IFUNC. */
  2474. if (PLT_LOCAL_IFUNC_P(info, h))
  2475. {
  2476. rela.r_info = ELFNN_R_INFO (0, R_LARCH_IRELATIVE);
  2477. rela.r_addend = (h->root.u.def.value
  2478. + h->root.u.def.section->output_section->vma
  2479. + h->root.u.def.section->output_offset);
  2480. }
  2481. else
  2482. {
  2483. /* Fill in the entry in the .rela.plt section. */
  2484. rela.r_info = ELFNN_R_INFO (h->dynindx, R_LARCH_JUMP_SLOT);
  2485. rela.r_addend = 0;
  2486. }
  2487. loc = relplt->contents + plt_idx * sizeof (ElfNN_External_Rela);
  2488. bed->s->swap_reloca_out (output_bfd, &rela, loc);
  2489. if (!h->def_regular)
  2490. {
  2491. /* Mark the symbol as undefined, rather than as defined in
  2492. the .plt section. Leave the value alone. */
  2493. sym->st_shndx = SHN_UNDEF;
  2494. /* If the symbol is weak, we do need to clear the value.
  2495. Otherwise, the PLT entry would provide a definition for
  2496. the symbol even if the symbol wasn't defined anywhere,
  2497. and so the symbol would never be NULL. */
  2498. if (!h->ref_regular_nonweak)
  2499. sym->st_value = 0;
  2500. }
  2501. }
  2502. if (h->got.offset != MINUS_ONE
  2503. /* TLS got entry have been handled in elf_relocate_section. */
  2504. && !(loongarch_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE))
  2505. /* have allocated got entry but not allocated rela before. */
  2506. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  2507. {
  2508. asection *sgot, *srela;
  2509. Elf_Internal_Rela rela;
  2510. bfd_vma off = h->got.offset & ~(bfd_vma) 1;
  2511. /* This symbol has an entry in the GOT. Set it up. */
  2512. sgot = htab->elf.sgot;
  2513. srela = htab->elf.srelgot;
  2514. BFD_ASSERT (sgot && srela);
  2515. rela.r_offset = sec_addr (sgot) + off;
  2516. if (h->def_regular
  2517. && h->type == STT_GNU_IFUNC)
  2518. {
  2519. if(h->plt.offset == MINUS_ONE)
  2520. {
  2521. if (htab->elf.splt == NULL)
  2522. srela = htab->elf.irelplt;
  2523. if (SYMBOL_REFERENCES_LOCAL (info, h))
  2524. {
  2525. asection *sec = h->root.u.def.section;
  2526. rela.r_info = ELFNN_R_INFO (0, R_LARCH_IRELATIVE);
  2527. rela.r_addend = h->root.u.def.value + sec->output_section->vma
  2528. + sec->output_offset;
  2529. bfd_put_NN (output_bfd, 0, sgot->contents + off);
  2530. }
  2531. else
  2532. {
  2533. BFD_ASSERT ((h->got.offset & 1) == 0);
  2534. BFD_ASSERT (h->dynindx != -1);
  2535. rela.r_info = ELFNN_R_INFO (h->dynindx, R_LARCH_NN);
  2536. rela.r_addend = 0;
  2537. bfd_put_NN (output_bfd, (bfd_vma) 0, sgot->contents + off);
  2538. }
  2539. }
  2540. else if(bfd_link_pic (info))
  2541. {
  2542. rela.r_info = ELFNN_R_INFO (h->dynindx, R_LARCH_NN);
  2543. rela.r_addend = 0;
  2544. bfd_put_NN (output_bfd, rela.r_addend, sgot->contents + off);
  2545. }
  2546. else
  2547. {
  2548. asection *plt;
  2549. /* For non-shared object, we can't use .got.plt, which
  2550. contains the real function address if we need pointer
  2551. equality. We load the GOT entry with the PLT entry. */
  2552. plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
  2553. bfd_put_NN (output_bfd,
  2554. (plt->output_section->vma
  2555. + plt->output_offset
  2556. + h->plt.offset),
  2557. sgot->contents + off);
  2558. return true;
  2559. }
  2560. }
  2561. else if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
  2562. {
  2563. BFD_ASSERT (h->got.offset & 1 /* Has been filled in addr. */);
  2564. asection *sec = h->root.u.def.section;
  2565. rela.r_info = ELFNN_R_INFO (0, R_LARCH_RELATIVE);
  2566. rela.r_addend = (h->root.u.def.value + sec->output_section->vma
  2567. + sec->output_offset);
  2568. }
  2569. else
  2570. {
  2571. BFD_ASSERT ((h->got.offset & 1) == 0);
  2572. BFD_ASSERT (h->dynindx != -1);
  2573. rela.r_info = ELFNN_R_INFO (h->dynindx, R_LARCH_NN);
  2574. rela.r_addend = 0;
  2575. }
  2576. loongarch_elf_append_rela (output_bfd, srela, &rela);
  2577. }
  2578. if (h->needs_copy)
  2579. {
  2580. Elf_Internal_Rela rela;
  2581. asection *s;
  2582. /* This symbols needs a copy reloc. Set it up. */
  2583. BFD_ASSERT (h->dynindx != -1);
  2584. rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value;
  2585. rela.r_info = ELFNN_R_INFO (h->dynindx, R_LARCH_COPY);
  2586. rela.r_addend = 0;
  2587. if (h->root.u.def.section == htab->elf.sdynrelro)
  2588. s = htab->elf.sreldynrelro;
  2589. else
  2590. s = htab->elf.srelbss;
  2591. loongarch_elf_append_rela (output_bfd, s, &rela);
  2592. }
  2593. /* Mark some specially defined symbols as absolute. */
  2594. if (h == htab->elf.hdynamic || h == htab->elf.hgot || h == htab->elf.hplt)
  2595. sym->st_shndx = SHN_ABS;
  2596. return true;
  2597. }
  2598. /* Finish up the dynamic sections. */
  2599. static bool
  2600. loongarch_finish_dyn (bfd *output_bfd, struct bfd_link_info *info, bfd *dynobj,
  2601. asection *sdyn)
  2602. {
  2603. struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
  2604. const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  2605. size_t dynsize = bed->s->sizeof_dyn, skipped_size = 0;
  2606. bfd_byte *dyncon, *dynconend;
  2607. dynconend = sdyn->contents + sdyn->size;
  2608. for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
  2609. {
  2610. Elf_Internal_Dyn dyn;
  2611. asection *s;
  2612. int skipped = 0;
  2613. bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
  2614. switch (dyn.d_tag)
  2615. {
  2616. case DT_PLTGOT:
  2617. s = htab->elf.sgotplt;
  2618. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2619. break;
  2620. case DT_JMPREL:
  2621. s = htab->elf.srelplt;
  2622. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2623. break;
  2624. case DT_PLTRELSZ:
  2625. s = htab->elf.srelplt;
  2626. dyn.d_un.d_val = s->size;
  2627. break;
  2628. case DT_TEXTREL:
  2629. if ((info->flags & DF_TEXTREL) == 0)
  2630. skipped = 1;
  2631. break;
  2632. case DT_FLAGS:
  2633. if ((info->flags & DF_TEXTREL) == 0)
  2634. dyn.d_un.d_val &= ~DF_TEXTREL;
  2635. break;
  2636. }
  2637. if (skipped)
  2638. skipped_size += dynsize;
  2639. else
  2640. bed->s->swap_dyn_out (output_bfd, &dyn, dyncon - skipped_size);
  2641. }
  2642. /* Wipe out any trailing entries if we shifted down a dynamic tag. */
  2643. memset (dyncon - skipped_size, 0, skipped_size);
  2644. return true;
  2645. }
  2646. /* Finish up local dynamic symbol handling. We set the contents of
  2647. various dynamic sections here. */
  2648. static bool
  2649. elfNN_loongarch_finish_local_dynamic_symbol (void **slot, void *inf)
  2650. {
  2651. struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) *slot;
  2652. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  2653. return loongarch_elf_finish_dynamic_symbol (info->output_bfd, info, h, NULL);
  2654. }
  2655. static bool
  2656. loongarch_elf_finish_dynamic_sections (bfd *output_bfd,
  2657. struct bfd_link_info *info)
  2658. {
  2659. bfd *dynobj;
  2660. asection *sdyn, *plt, *gotplt = NULL;
  2661. struct loongarch_elf_link_hash_table *htab;
  2662. htab = loongarch_elf_hash_table (info);
  2663. BFD_ASSERT (htab);
  2664. dynobj = htab->elf.dynobj;
  2665. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2666. if (elf_hash_table (info)->dynamic_sections_created)
  2667. {
  2668. BFD_ASSERT (htab->elf.splt && sdyn);
  2669. if (!loongarch_finish_dyn (output_bfd, info, dynobj, sdyn))
  2670. return false;
  2671. }
  2672. plt = htab->elf.splt;
  2673. gotplt = htab->elf.sgotplt;
  2674. if (plt && 0 < plt->size)
  2675. {
  2676. size_t i;
  2677. uint32_t plt_header[PLT_HEADER_INSNS];
  2678. if (!loongarch_make_plt_header (sec_addr (gotplt), sec_addr (plt),
  2679. plt_header))
  2680. return false;
  2681. for (i = 0; i < PLT_HEADER_INSNS; i++)
  2682. bfd_put_32 (output_bfd, plt_header[i], plt->contents + 4 * i);
  2683. elf_section_data (plt->output_section)->this_hdr.sh_entsize =
  2684. PLT_ENTRY_SIZE;
  2685. }
  2686. if (htab->elf.sgotplt)
  2687. {
  2688. asection *output_section = htab->elf.sgotplt->output_section;
  2689. if (bfd_is_abs_section (output_section))
  2690. {
  2691. _bfd_error_handler (_("discarded output section: `%pA'"),
  2692. htab->elf.sgotplt);
  2693. return false;
  2694. }
  2695. if (0 < htab->elf.sgotplt->size)
  2696. {
  2697. /* Write the first two entries in .got.plt, needed for the dynamic
  2698. linker. */
  2699. bfd_put_NN (output_bfd, MINUS_ONE, htab->elf.sgotplt->contents);
  2700. bfd_put_NN (output_bfd, (bfd_vma) 0,
  2701. htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
  2702. }
  2703. elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
  2704. }
  2705. if (htab->elf.sgot)
  2706. {
  2707. asection *output_section = htab->elf.sgot->output_section;
  2708. if (0 < htab->elf.sgot->size)
  2709. {
  2710. /* Set the first entry in the global offset table to the address of
  2711. the dynamic section. */
  2712. bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
  2713. bfd_put_NN (output_bfd, val, htab->elf.sgot->contents);
  2714. }
  2715. elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
  2716. }
  2717. /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
  2718. htab_traverse (htab->loc_hash_table,
  2719. (void *) elfNN_loongarch_finish_local_dynamic_symbol, info);
  2720. return true;
  2721. }
  2722. /* Return address for Ith PLT stub in section PLT, for relocation REL
  2723. or (bfd_vma) -1 if it should not be included. */
  2724. static bfd_vma
  2725. loongarch_elf_plt_sym_val (bfd_vma i, const asection *plt,
  2726. const arelent *rel ATTRIBUTE_UNUSED)
  2727. {
  2728. return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
  2729. }
  2730. static enum elf_reloc_type_class
  2731. loongarch_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2732. const asection *rel_sec ATTRIBUTE_UNUSED,
  2733. const Elf_Internal_Rela *rela)
  2734. {
  2735. struct loongarch_elf_link_hash_table *htab;
  2736. htab = loongarch_elf_hash_table (info);
  2737. if (htab->elf.dynsym != NULL && htab->elf.dynsym->contents != NULL)
  2738. {
  2739. /* Check relocation against STT_GNU_IFUNC symbol if there are
  2740. dynamic symbols. */
  2741. bfd *abfd = info->output_bfd;
  2742. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2743. unsigned long r_symndx = ELFNN_R_SYM (rela->r_info);
  2744. if (r_symndx != STN_UNDEF)
  2745. {
  2746. Elf_Internal_Sym sym;
  2747. if (!bed->s->swap_symbol_in (abfd,
  2748. htab->elf.dynsym->contents
  2749. + r_symndx * bed->s->sizeof_sym,
  2750. 0, &sym))
  2751. {
  2752. /* xgettext:c-format */
  2753. _bfd_error_handler (_("%pB symbol number %lu references"
  2754. " nonexistent SHT_SYMTAB_SHNDX section"),
  2755. abfd, r_symndx);
  2756. /* Ideally an error class should be returned here. */
  2757. }
  2758. else if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
  2759. return reloc_class_ifunc;
  2760. }
  2761. }
  2762. switch (ELFNN_R_TYPE (rela->r_info))
  2763. {
  2764. case R_LARCH_IRELATIVE:
  2765. return reloc_class_ifunc;
  2766. case R_LARCH_RELATIVE:
  2767. return reloc_class_relative;
  2768. case R_LARCH_JUMP_SLOT:
  2769. return reloc_class_plt;
  2770. case R_LARCH_COPY:
  2771. return reloc_class_copy;
  2772. default:
  2773. return reloc_class_normal;
  2774. }
  2775. }
  2776. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  2777. static void
  2778. loongarch_elf_copy_indirect_symbol (struct bfd_link_info *info,
  2779. struct elf_link_hash_entry *dir,
  2780. struct elf_link_hash_entry *ind)
  2781. {
  2782. struct elf_link_hash_entry *edir, *eind;
  2783. edir = dir;
  2784. eind = ind;
  2785. if (eind->dyn_relocs != NULL)
  2786. {
  2787. if (edir->dyn_relocs != NULL)
  2788. {
  2789. struct elf_dyn_relocs **pp;
  2790. struct elf_dyn_relocs *p;
  2791. /* Add reloc counts against the indirect sym to the direct sym
  2792. list. Merge any entries against the same section. */
  2793. for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
  2794. {
  2795. struct elf_dyn_relocs *q;
  2796. for (q = edir->dyn_relocs; q != NULL; q = q->next)
  2797. if (q->sec == p->sec)
  2798. {
  2799. q->pc_count += p->pc_count;
  2800. q->count += p->count;
  2801. *pp = p->next;
  2802. break;
  2803. }
  2804. if (q == NULL)
  2805. pp = &p->next;
  2806. }
  2807. *pp = edir->dyn_relocs;
  2808. }
  2809. edir->dyn_relocs = eind->dyn_relocs;
  2810. eind->dyn_relocs = NULL;
  2811. }
  2812. if (ind->root.type == bfd_link_hash_indirect && dir->got.refcount < 0)
  2813. {
  2814. loongarch_elf_hash_entry(edir)->tls_type
  2815. = loongarch_elf_hash_entry(eind)->tls_type;
  2816. loongarch_elf_hash_entry(eind)->tls_type = GOT_UNKNOWN;
  2817. }
  2818. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  2819. }
  2820. #define PRSTATUS_SIZE 0x1d8
  2821. #define PRSTATUS_OFFSET_PR_CURSIG 0xc
  2822. #define PRSTATUS_OFFSET_PR_PID 0x20
  2823. #define ELF_GREGSET_T_SIZE 0x168
  2824. #define PRSTATUS_OFFSET_PR_REG 0x70
  2825. /* Support for core dump NOTE sections. */
  2826. static bool
  2827. loongarch_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  2828. {
  2829. switch (note->descsz)
  2830. {
  2831. default:
  2832. return false;
  2833. /* The sizeof (struct elf_prstatus) on Linux/LoongArch. */
  2834. case PRSTATUS_SIZE:
  2835. /* pr_cursig */
  2836. elf_tdata (abfd)->core->signal =
  2837. bfd_get_16 (abfd, note->descdata + PRSTATUS_OFFSET_PR_CURSIG);
  2838. /* pr_pid */
  2839. elf_tdata (abfd)->core->lwpid =
  2840. bfd_get_32 (abfd, note->descdata + PRSTATUS_OFFSET_PR_PID);
  2841. break;
  2842. }
  2843. /* Make a ".reg/999" section. */
  2844. return _bfd_elfcore_make_pseudosection (abfd, ".reg", ELF_GREGSET_T_SIZE,
  2845. note->descpos
  2846. + PRSTATUS_OFFSET_PR_REG);
  2847. }
  2848. #define PRPSINFO_SIZE 0x88
  2849. #define PRPSINFO_OFFSET_PR_PID 0x18
  2850. #define PRPSINFO_OFFSET_PR_FNAME 0x28
  2851. #define PRPSINFO_SIZEOF_PR_FNAME 0x10
  2852. #define PRPSINFO_OFFSET_PR_PS_ARGS 0x38
  2853. #define PRPSINFO_SIZEOF_PR_PS_ARGS 0x50
  2854. static bool
  2855. loongarch_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  2856. {
  2857. switch (note->descsz)
  2858. {
  2859. default:
  2860. return false;
  2861. /* The sizeof (prpsinfo_t) on Linux/LoongArch. */
  2862. case PRPSINFO_SIZE:
  2863. /* pr_pid */
  2864. elf_tdata (abfd)->core->pid =
  2865. bfd_get_32 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PID);
  2866. /* pr_fname */
  2867. elf_tdata (abfd)->core->program =
  2868. _bfd_elfcore_strndup (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME,
  2869. PRPSINFO_SIZEOF_PR_FNAME);
  2870. /* pr_psargs */
  2871. elf_tdata (abfd)->core->command =
  2872. _bfd_elfcore_strndup (abfd, note->descdata + PRPSINFO_OFFSET_PR_PS_ARGS,
  2873. PRPSINFO_SIZEOF_PR_PS_ARGS);
  2874. break;
  2875. }
  2876. /* Note that for some reason, a spurious space is tacked
  2877. onto the end of the args in some (at least one anyway)
  2878. implementations, so strip it off if it exists. */
  2879. {
  2880. char *command = elf_tdata (abfd)->core->command;
  2881. int n = strlen (command);
  2882. if (0 < n && command[n - 1] == ' ')
  2883. command[n - 1] = '\0';
  2884. }
  2885. return true;
  2886. }
  2887. /* Set the right mach type. */
  2888. static bool
  2889. loongarch_elf_object_p (bfd *abfd)
  2890. {
  2891. /* There are only two mach types in LoongArch currently. */
  2892. if (strcmp (abfd->xvec->name, "elf64-loongarch") == 0)
  2893. bfd_default_set_arch_mach (abfd, bfd_arch_loongarch, bfd_mach_loongarch64);
  2894. else
  2895. bfd_default_set_arch_mach (abfd, bfd_arch_loongarch, bfd_mach_loongarch32);
  2896. return true;
  2897. }
  2898. static asection *
  2899. loongarch_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
  2900. Elf_Internal_Rela *rel,
  2901. struct elf_link_hash_entry *h,
  2902. Elf_Internal_Sym *sym)
  2903. {
  2904. if (h != NULL)
  2905. switch (ELFNN_R_TYPE (rel->r_info))
  2906. {
  2907. case R_LARCH_GNU_VTINHERIT:
  2908. case R_LARCH_GNU_VTENTRY:
  2909. return NULL;
  2910. }
  2911. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  2912. }
  2913. #define TARGET_LITTLE_SYM loongarch_elfNN_vec
  2914. #define TARGET_LITTLE_NAME "elfNN-loongarch"
  2915. #define ELF_ARCH bfd_arch_loongarch
  2916. #define ELF_TARGET_ID LARCH_ELF_DATA
  2917. #define ELF_MACHINE_CODE EM_LOONGARCH
  2918. #define ELF_MAXPAGESIZE 0x4000
  2919. #define bfd_elfNN_bfd_reloc_type_lookup loongarch_reloc_type_lookup
  2920. #define bfd_elfNN_bfd_link_hash_table_create \
  2921. loongarch_elf_link_hash_table_create
  2922. #define bfd_elfNN_bfd_reloc_name_lookup loongarch_reloc_name_lookup
  2923. #define elf_info_to_howto_rel NULL /* Fall through to elf_info_to_howto. */
  2924. #define elf_info_to_howto loongarch_info_to_howto_rela
  2925. #define bfd_elfNN_bfd_merge_private_bfd_data \
  2926. elfNN_loongarch_merge_private_bfd_data
  2927. #define elf_backend_reloc_type_class loongarch_reloc_type_class
  2928. #define elf_backend_copy_indirect_symbol loongarch_elf_copy_indirect_symbol
  2929. #define elf_backend_create_dynamic_sections \
  2930. loongarch_elf_create_dynamic_sections
  2931. #define elf_backend_check_relocs loongarch_elf_check_relocs
  2932. #define elf_backend_adjust_dynamic_symbol loongarch_elf_adjust_dynamic_symbol
  2933. #define elf_backend_size_dynamic_sections loongarch_elf_size_dynamic_sections
  2934. #define elf_backend_relocate_section loongarch_elf_relocate_section
  2935. #define elf_backend_finish_dynamic_symbol loongarch_elf_finish_dynamic_symbol
  2936. #define elf_backend_finish_dynamic_sections \
  2937. loongarch_elf_finish_dynamic_sections
  2938. #define elf_backend_object_p loongarch_elf_object_p
  2939. #define elf_backend_gc_mark_hook loongarch_elf_gc_mark_hook
  2940. #define elf_backend_plt_sym_val loongarch_elf_plt_sym_val
  2941. #define elf_backend_grok_prstatus loongarch_elf_grok_prstatus
  2942. #define elf_backend_grok_psinfo loongarch_elf_grok_psinfo
  2943. #include "elfNN-target.h"