ogelf.pas 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains the binary elf writer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ogelf;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,
  25. { assembler }
  26. aasmbase,assemble,
  27. { ELF definitions }
  28. elfbase,
  29. { output }
  30. ogbase,
  31. owbase;
  32. type
  33. {$ifdef cpu64bitaddr}
  34. TElfsechdr = TElf64sechdr;
  35. {$else cpu64bitaddr}
  36. TElfsechdr = TElf32sechdr;
  37. {$endif cpu64bitaddr}
  38. TElfObjSection = class(TObjSection)
  39. public
  40. shstridx,
  41. shtype,
  42. shflags,
  43. shlink,
  44. shinfo,
  45. shentsize : longint;
  46. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);override;
  47. constructor create_ext(aobjdata:TObjData;const Aname:string;Ashtype,Ashflags:longint;Aalign:longint;Aentsize:longint);
  48. constructor create_reloc(aobjdata:TObjData;const Aname:string;allocflag:boolean);
  49. procedure writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);override;
  50. end;
  51. TElfSymtabKind = (esk_obj,esk_exe,esk_dyn);
  52. TElfSymtab = class(TElfObjSection)
  53. public
  54. kind: TElfSymtabKind;
  55. fstrsec: TObjSection;
  56. symidx: longint;
  57. tlsbase: aword;
  58. constructor create(aObjData:TObjData;aKind:TElfSymtabKind);reintroduce;
  59. procedure writeSymbol(objsym:TObjSymbol;nameidx:longword=0);
  60. procedure writeInternalSymbol(avalue:aword;astridx:longword;ainfo:byte;ashndx:word);
  61. end;
  62. TElfObjData = class(TObjData)
  63. public
  64. ident: TElfIdent;
  65. flags: longword;
  66. {$ifdef mips}
  67. gp_value: longword;
  68. {$endif mips}
  69. constructor create(const n:string);override;
  70. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  71. procedure CreateDebugSections;override;
  72. procedure writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);override;
  73. end;
  74. TElfObjectOutput = class(tObjOutput)
  75. private
  76. symtabsect: TElfSymtab;
  77. shstrtabsect: TElfObjSection;
  78. procedure createrelocsection(s:TElfObjSection;data:TObjData);
  79. procedure createshstrtab(data:TObjData);
  80. procedure createsymtab(data: TObjData);
  81. procedure writesectionheader(s:TElfObjSection);
  82. procedure section_write_symbol(p:TObject;arg:pointer);
  83. procedure section_count_sections(p:TObject;arg:pointer);
  84. procedure section_create_relocsec(p:TObject;arg:pointer);
  85. procedure section_write_sechdr(p:TObject;arg:pointer);
  86. protected
  87. function writedata(data:TObjData):boolean;override;
  88. public
  89. constructor Create(AWriter:TObjectWriter);override;
  90. end;
  91. TElfAssembler = class(tinternalassembler)
  92. constructor create(info: pasminfo; smart:boolean);override;
  93. end;
  94. PSectionRec=^TSectionRec;
  95. TSectionRec=record
  96. sec: TObjSection;
  97. relocpos: aword;
  98. relocs: longint;
  99. relentsize: longint;
  100. end;
  101. TElfsecheaderarray=array of TElfsechdr;
  102. TObjSymbolClass=class of TObjSymbol;
  103. TElfObjInput=class(TObjInput)
  104. private
  105. FSecTbl: PSectionRec;
  106. FSymTbl: PPointer;
  107. FLoaded: PBoolean;
  108. shdrs: TElfsecheaderarray;
  109. nsects: longword;
  110. shoffset: aword;
  111. shstrndx: longword;
  112. symtabndx: longword;
  113. shstrtab: PChar;
  114. strtab: PChar;
  115. shstrtablen: longword;
  116. strtablen: longword;
  117. symtaboffset: aword;
  118. syms: longword;
  119. localsyms: longword;
  120. symversions: PWord;
  121. dynobj: boolean;
  122. CObjSymbol: TObjSymbolClass;
  123. verdefs: TFPHashObjectList;
  124. function LoadHeader(out objdata:TObjData):boolean;
  125. procedure LoadSection(const shdr:TElfsechdr;index:longint;objdata:TObjData);
  126. procedure LoadRelocations(const secrec:TSectionRec);
  127. procedure LoadSymbols(objdata:TObjData;count,locals:longword);
  128. procedure LoadDynamic(const shdr:TElfsechdr;objdata:TObjData);
  129. public
  130. constructor Create;override;
  131. destructor Destroy;override;
  132. function ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;override;
  133. class function CanReadObjData(AReader:TObjectreader):boolean;override;
  134. function CreateSection(const shdr:TElfsechdr;index:longint;objdata:TObjData;
  135. out secname:string):TElfObjSection;
  136. function ReadBytes(offs:longint;out buf;len:longint):boolean;
  137. end;
  138. TElfVersionDef = class(TFPHashObject)
  139. public
  140. index: longword;
  141. end;
  142. TElfDynamicObjData = class(TElfObjData)
  143. private
  144. FVersionDefs: TFPHashObjectList;
  145. public
  146. soname_strofs: longword;
  147. vernaux_count: longword;
  148. constructor create(const n:string);override;
  149. destructor destroy;override;
  150. property versiondefs:TFPHashObjectList read FVersionDefs;
  151. end;
  152. TVersionedObjSymbol = class(TObjSymbol)
  153. private
  154. FVersion: TElfVersionDef;
  155. public
  156. property version: TElfVersionDef read FVersion write FVersion;
  157. end;
  158. TRelocNameProc=function(reltyp:byte):string;
  159. TEncodeRelocProc=function(objrel:TObjRelocation):byte;
  160. TLoadRelocProc=procedure(objrel:TObjRelocation);
  161. TLoadSectionProc=function(objinput:TElfObjInput;objdata:TObjData;const shdr:TElfsechdr;shindex:longint):boolean;
  162. TEncodeFlagsProc=function:longword;
  163. TDynamicReloc=(
  164. dr_relative,
  165. dr_glob_dat,
  166. dr_jump_slot,
  167. dr_copy,
  168. dr_irelative
  169. );
  170. TElfTarget=record
  171. max_page_size: longword;
  172. exe_image_base: longword;
  173. machine_code: word;
  174. relocs_use_addend: boolean;
  175. dyn_reloc_codes: array[TDynamicReloc] of byte;
  176. relocname: TRelocNameProc;
  177. encodereloc: TEncodeRelocProc;
  178. loadreloc: TLoadRelocProc;
  179. loadsection: TLoadSectionProc;
  180. encodeflags: TEncodeFlagsProc;
  181. end;
  182. TElfExeSection=class(TExeSection)
  183. public
  184. secshidx : longword; { index of the section header }
  185. shstridx,
  186. shtype,
  187. shflags,
  188. shlink,
  189. shinfo,
  190. shentsize : longword;
  191. procedure AddObjSection(objsec:TObjSection;ignoreprops:boolean=false);override;
  192. end;
  193. TElfSegment=class
  194. public
  195. ptype: longword;
  196. pflags: longword;
  197. DataPos: aword;
  198. DataSize: aword;
  199. MemPos: aword;
  200. MemSize: aword;
  201. align: longword;
  202. //physaddr: aword;
  203. FSectionList: TFPObjectList;
  204. constructor Create(atype,aflags,aalign:longword);
  205. destructor Destroy; override;
  206. procedure Add(exesec:TExeSection);
  207. end;
  208. TElfExeOutput=class(TExeOutput)
  209. private
  210. segmentlist: TFPObjectList;
  211. textseg,
  212. dataseg,
  213. noteseg,
  214. phdrseg: TElfSegment;
  215. shstrtabsect: TElfObjSection;
  216. symtab: TElfSymtab;
  217. shoffset: aword;
  218. gotwritten: boolean;
  219. { dynamic linking }
  220. dynsymnames: Plongword;
  221. dynsymtable: TElfSymtab;
  222. interpobjsec: TObjSection;
  223. FInterpreter: pshortstring;
  224. verneedcount,
  225. verdefcount: longword;
  226. symversec,
  227. verdefsec,
  228. verneedsec,
  229. hashobjsec: TElfObjSection;
  230. neededlist: TFPHashList;
  231. dyncopysyms: TFPObjectList;
  232. preinitarraysec,
  233. initarraysec,
  234. finiarraysec: TObjSection;
  235. function AttachSection(objsec:TObjSection):TElfExeSection;
  236. function CreateSegment(atype,aflags,aalign:longword):TElfSegment;
  237. procedure WriteHeader;
  238. procedure WriteDynamicSymbolsHash;
  239. procedure WriteVersionSections;
  240. procedure WriteDynamicTags;
  241. procedure FinishDynamicTags;
  242. procedure exesection_write_header(p:TObject;arg:Pointer);
  243. procedure segment_write_header(p:TObject;arg:Pointer);
  244. procedure mempos_segment(seg:TElfSegment);
  245. procedure datapos_segment(seg:TElfSegment);
  246. procedure MapSectionsToSegments;
  247. procedure WriteStaticSymtable;
  248. procedure WriteShstrtab;
  249. procedure FixupSectionLinks;
  250. procedure InitDynlink;
  251. procedure OrderOrphanSections;
  252. protected
  253. dynamiclink: boolean;
  254. hastextrelocs: boolean;
  255. gotsymbol: TObjSymbol;
  256. dynsymlist: TFPObjectList;
  257. dynamicsec,
  258. gotobjsec: TObjSection;
  259. dynbssobjsec,
  260. pltobjsec,
  261. gotpltobjsec,
  262. pltrelocsec,
  263. ipltrelocsec,
  264. dynrelocsec: TElfObjSection;
  265. dynreloclist: TFPObjectList;
  266. tlsseg: TElfSegment;
  267. relative_reloc_count: longint;
  268. gotsize: aword;
  269. dynrelsize: aword;
  270. procedure PrepareGOT;virtual;
  271. function AllocGOTSlot(objsym: TObjSymbol):boolean;virtual;
  272. procedure CreateGOTSection;virtual;
  273. procedure make_dynamic_if_undefweak(exesym:TExeSymbol);
  274. procedure WriteDynRelocEntry(dataofs:aword;typ:byte;symidx:aword;addend:aword);
  275. procedure CreatePLT;virtual;
  276. procedure WriteFirstPLTEntry;virtual;abstract;
  277. procedure WritePLTEntry(exesym:TExeSymbol);virtual;
  278. procedure WriteIndirectPLTEntry(exesym:TExeSymbol);virtual;
  279. procedure WriteTargetDynamicTags;virtual;
  280. procedure GOTRelocPass1(objsec:TObjSection;var idx:longint);virtual;abstract;
  281. procedure ReportNonDSOReloc(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  282. procedure ReportRelocOverflow(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  283. procedure WriteDynTag(aTag:longword;aValue:longword);
  284. procedure WriteDynTag(aTag:longword;aSection:TObjSection;aOffs:aword=0);
  285. procedure Do_Mempos;virtual;
  286. public
  287. constructor Create;override;
  288. destructor Destroy;override;
  289. procedure Load_Start;override;
  290. procedure Load_DynamicObject(ObjData:TObjData;asneeded:boolean);override;
  291. procedure Order_Start;override;
  292. procedure Order_end;override;
  293. procedure AfterUnusedSectionRemoval;override;
  294. procedure MemPos_Start;override;
  295. procedure MemPos_ExeSection(const aname:string);override;
  296. procedure DataPos_Start;override;
  297. procedure DataPos_ExeSection(const aname:string);override;
  298. function writeData:boolean;override;
  299. procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);override;
  300. property interpreter:pshortstring read FInterpreter write FInterpreter;
  301. end;
  302. var
  303. ElfExeOutputClass: TExeOutputClass;
  304. ElfTarget: TElfTarget;
  305. const
  306. { Bits of TObjSymbol.refs field }
  307. symref_plt = 1;
  308. symref_from_text = 2;
  309. implementation
  310. uses
  311. SysUtils,
  312. verbose,
  313. export,expunix,
  314. cutils,globals,fmodule,owar;
  315. const
  316. symbolresize = 200*18;
  317. {$ifdef cpu64bitaddr}
  318. const
  319. ELFCLASS = ELFCLASS64;
  320. type
  321. telfheader = telf64header;
  322. telfreloc = telf64reloc;
  323. telfsymbol = telf64symbol;
  324. telfproghdr = telf64proghdr;
  325. telfdyn = telf64dyn;
  326. function ELF_R_INFO(sym:longword;typ:byte):qword;inline;
  327. begin
  328. result:=(qword(sym) shl 32) or typ;
  329. end;
  330. {$else cpu64bitaddr}
  331. const
  332. ELFCLASS = ELFCLASS32;
  333. type
  334. telfheader = telf32header;
  335. telfreloc = telf32reloc;
  336. telfsymbol = telf32symbol;
  337. telfproghdr = telf32proghdr;
  338. telfdyn = telf32dyn;
  339. function ELF_R_INFO(sym:longword;typ:byte):longword;inline;
  340. begin
  341. result:=(sym shl 8) or typ;
  342. end;
  343. {$endif cpu64bitaddr}
  344. {****************************************************************************
  345. Helpers
  346. ****************************************************************************}
  347. procedure encodesechdrflags(aoptions:TObjSectionOptions;out AshType:longint;out Ashflags:longint);
  348. begin
  349. { Section Type }
  350. AshType:=SHT_PROGBITS;
  351. if oso_strings in aoptions then
  352. AshType:=SHT_STRTAB
  353. else if not(oso_data in aoptions) then
  354. AshType:=SHT_NOBITS;
  355. { Section Flags }
  356. Ashflags:=0;
  357. if oso_load in aoptions then
  358. Ashflags:=Ashflags or SHF_ALLOC;
  359. if oso_executable in aoptions then
  360. Ashflags:=Ashflags or SHF_EXECINSTR;
  361. if oso_write in aoptions then
  362. Ashflags:=Ashflags or SHF_WRITE;
  363. end;
  364. procedure decodesechdrflags(AshType:longint;Ashflags:longint;out aoptions:TObjSectionOptions);
  365. begin
  366. aoptions:=[];
  367. { Section Type }
  368. if AshType<>SHT_NOBITS then
  369. include(aoptions,oso_data);
  370. if AshType=SHT_STRTAB then
  371. include(aoptions,oso_strings);
  372. { Section Flags }
  373. if Ashflags and SHF_ALLOC<>0 then
  374. include(aoptions,oso_load);
  375. if Ashflags and SHF_WRITE<>0 then
  376. include(aoptions,oso_write);
  377. if Ashflags and SHF_EXECINSTR<>0 then
  378. include(aoptions,oso_executable);
  379. end;
  380. {****************************************************************************
  381. TElfObjSection
  382. ****************************************************************************}
  383. constructor TElfObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);
  384. begin
  385. inherited create(AList,Aname,Aalign,aoptions);
  386. index:=0;
  387. shstridx:=0;
  388. encodesechdrflags(aoptions,shtype,shflags);
  389. shlink:=0;
  390. shinfo:=0;
  391. if name='.stab' then
  392. shentsize:=sizeof(TObjStabEntry);
  393. end;
  394. constructor TElfObjSection.create_ext(aobjdata:TObjData;const Aname:string;Ashtype,Ashflags:longint;Aalign:longint;Aentsize:longint);
  395. var
  396. aoptions : TObjSectionOptions;
  397. begin
  398. decodesechdrflags(Ashtype,Ashflags,aoptions);
  399. inherited create(aobjdata.ObjSectionList,Aname,Aalign,aoptions);
  400. objdata:=aobjdata;
  401. index:=0;
  402. shstridx:=0;
  403. shtype:=AshType;
  404. shflags:=AshFlags;
  405. shentsize:=Aentsize;
  406. end;
  407. const
  408. relsec_prefix:array[boolean] of string[5] = ('.rel','.rela');
  409. relsec_shtype:array[boolean] of longword = (SHT_REL,SHT_RELA);
  410. constructor TElfObjSection.create_reloc(aobjdata:TObjData;const Aname:string;allocflag:boolean);
  411. begin
  412. create_ext(aobjdata,
  413. relsec_prefix[ElfTarget.relocs_use_addend]+aname,
  414. relsec_shtype[ElfTarget.relocs_use_addend],
  415. SHF_ALLOC*ord(allocflag),
  416. sizeof(pint),
  417. (2+ord(ElfTarget.relocs_use_addend))*sizeof(pint));
  418. end;
  419. procedure TElfObjSection.writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);
  420. var
  421. reloc: TObjRelocation;
  422. begin
  423. reloc:=TObjRelocation.CreateSection(Size,aTarget,reltype);
  424. reloc.size:=len;
  425. ObjRelocations.Add(reloc);
  426. if reltype=RELOC_RELATIVE then
  427. { ARM does not require this adjustment, other CPUs must be checked }
  428. {$if defined(i386) or defined(x86_64)}
  429. dec(offset,len)
  430. {$endif i386 or x86_64}
  431. else if reltype<>RELOC_ABSOLUTE then
  432. InternalError(2012062401);
  433. if ElfTarget.relocs_use_addend then
  434. begin
  435. reloc.orgsize:=offset;
  436. offset:=0;
  437. end;
  438. write(offset,len);
  439. end;
  440. {****************************************************************************
  441. TElfObjData
  442. ****************************************************************************}
  443. constructor TElfObjData.create(const n:string);
  444. begin
  445. inherited create(n);
  446. CObjSection:=TElfObjSection;
  447. end;
  448. function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  449. const
  450. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  451. { TODO: sec_rodata is still writable }
  452. '.text','.data','.data','.rodata','.bss','.threadvar',
  453. '.pdata',
  454. '.text', { darwin stubs }
  455. '__DATA,__nl_symbol_ptr',
  456. '__DATA,__la_symbol_ptr',
  457. '__DATA,__mod_init_func',
  458. '__DATA,__mod_term_func',
  459. '.stab','.stabstr',
  460. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  461. '.eh_frame',
  462. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  463. '.fpc',
  464. '.toc',
  465. '.init',
  466. '.fini',
  467. '.objc_class',
  468. '.objc_meta_class',
  469. '.objc_cat_cls_meth',
  470. '.objc_cat_inst_meth',
  471. '.objc_protocol',
  472. '.objc_string_object',
  473. '.objc_cls_meth',
  474. '.objc_inst_meth',
  475. '.objc_cls_refs',
  476. '.objc_message_refs',
  477. '.objc_symbols',
  478. '.objc_category',
  479. '.objc_class_vars',
  480. '.objc_instance_vars',
  481. '.objc_module_info',
  482. '.objc_class_names',
  483. '.objc_meth_var_types',
  484. '.objc_meth_var_names',
  485. '.objc_selector_strs',
  486. '.objc_protocol_ext',
  487. '.objc_class_ext',
  488. '.objc_property',
  489. '.objc_image_info',
  490. '.objc_cstring_object',
  491. '.objc_sel_fixup',
  492. '__DATA,__objc_data',
  493. '__DATA,__objc_const',
  494. '.objc_superrefs',
  495. '__DATA, __datacoal_nt,coalesced',
  496. '.objc_classlist',
  497. '.objc_nlclasslist',
  498. '.objc_catlist',
  499. '.obcj_nlcatlist',
  500. '.objc_protolist',
  501. '.stack',
  502. '.heap'
  503. );
  504. var
  505. sep : string[3];
  506. secname : string;
  507. begin
  508. { section type user gives the user full controll on the section name }
  509. if atype=sec_user then
  510. result:=aname
  511. else
  512. begin
  513. secname:=secnames[atype];
  514. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  515. begin
  516. result:=secname+'.'+aname;
  517. exit;
  518. end;
  519. if create_smartlink_sections and (aname<>'') then
  520. begin
  521. case aorder of
  522. secorder_begin :
  523. sep:='.b_';
  524. secorder_end :
  525. sep:='.z_';
  526. else
  527. sep:='.n_';
  528. end;
  529. result:=secname+sep+aname
  530. end
  531. else
  532. result:=secname;
  533. end;
  534. end;
  535. procedure TElfObjData.CreateDebugSections;
  536. begin
  537. if target_dbg.id=dbg_stabs then
  538. begin
  539. stabssec:=createsection(sec_stab);
  540. stabstrsec:=createsection(sec_stabstr);
  541. end;
  542. end;
  543. procedure TElfObjData.writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);
  544. var
  545. symaddr : aint;
  546. objreloc: TObjRelocation;
  547. begin
  548. if CurrObjSec=nil then
  549. internalerror(200403292);
  550. objreloc:=nil;
  551. if assigned(p) then
  552. begin
  553. { real address of the symbol }
  554. symaddr:=p.address;
  555. { Local ObjSymbols can be resolved already or need a section reloc }
  556. if (p.bind=AB_LOCAL) and
  557. (reltype in [RELOC_RELATIVE,RELOC_ABSOLUTE{$ifdef x86_64},RELOC_ABSOLUTE32{$endif x86_64}{$ifdef arm},RELOC_RELATIVE_24,RELOC_RELATIVE_CALL{$endif arm}]) then
  558. begin
  559. {$ifdef ARM}
  560. if (reltype in [RELOC_RELATIVE_24,RELOC_RELATIVE_CALL]) and
  561. (p.objsection=CurrObjSec) then
  562. begin
  563. data:=aint((data and $ff000000) or (((((data and $ffffff) shl 2)+(symaddr-CurrObjSec.Size)) shr 2) and $FFFFFF)); // TODO: Check overflow
  564. end
  565. else
  566. {$endif ARM}
  567. { For a reltype relocation in the same section the
  568. value can be calculated }
  569. if (p.objsection=CurrObjSec) and
  570. (reltype=RELOC_RELATIVE) then
  571. inc(data,symaddr-len-CurrObjSec.Size)
  572. else
  573. begin
  574. objreloc:=TObjRelocation.CreateSection(CurrObjSec.Size,p.objsection,reltype);
  575. CurrObjSec.ObjRelocations.Add(objreloc);
  576. inc(data,symaddr);
  577. end;
  578. end
  579. else
  580. begin
  581. objreloc:=TObjRelocation.CreateSymbol(CurrObjSec.Size,p,reltype);
  582. CurrObjSec.ObjRelocations.Add(objreloc);
  583. { If target is a local label and it isn't handled above,
  584. patch its type in order to get it written to symtable.
  585. This may happen e.g. when taking address of Pascal label in PIC mode. }
  586. if (p.bind=AB_LOCAL) and (p.typ=AT_LABEL) then
  587. p.typ:=AT_ADDR;
  588. end;
  589. end;
  590. if assigned(objreloc) then
  591. begin
  592. objreloc.size:=len;
  593. if reltype in [RELOC_RELATIVE{$ifdef x86},RELOC_PLT32{$endif}{$ifdef x86_64},RELOC_GOTPCREL{$endif}] then
  594. dec(data,len);
  595. if ElfTarget.relocs_use_addend then
  596. begin
  597. objreloc.orgsize:=aword(data);
  598. data:=0;
  599. end;
  600. end;
  601. CurrObjSec.write(data,len);
  602. end;
  603. {****************************************************************************
  604. TElfDynamicObjData
  605. ****************************************************************************}
  606. constructor TElfDynamicObjData.create(const n:string);
  607. begin
  608. inherited Create(n);
  609. FVersionDefs:=TFPHashObjectList.create(true);
  610. { Default symversions with indices 0 and 1 }
  611. TElfVersionDef.create(FVersionDefs,'*local*');
  612. TElfVersionDef.create(FVersionDefs,'*global*');
  613. end;
  614. destructor TElfDynamicObjData.destroy;
  615. begin
  616. FVersionDefs.free;
  617. inherited Destroy;
  618. end;
  619. {****************************************************************************
  620. TElfSymtab
  621. ****************************************************************************}
  622. const
  623. symsecnames: array[boolean] of string[8] = ('.symtab','.dynsym');
  624. strsecnames: array[boolean] of string[8] = ('.strtab','.dynstr');
  625. symsectypes: array[boolean] of longword = (SHT_SYMTAB,SHT_DYNSYM);
  626. symsecattrs: array[boolean] of longword = (0,SHF_ALLOC);
  627. constructor TElfSymtab.create(aObjData:TObjData;aKind:TElfSymtabKind);
  628. var
  629. dyn:boolean;
  630. begin
  631. dyn:=(aKind=esk_dyn);
  632. create_ext(aObjData,symsecnames[dyn],symsectypes[dyn],symsecattrs[dyn],sizeof(pint),sizeof(TElfSymbol));
  633. fstrsec:=TElfObjSection.create_ext(aObjData,strsecnames[dyn],SHT_STRTAB,symsecattrs[dyn],1,0);
  634. fstrsec.writezeros(1);
  635. writezeros(sizeof(TElfSymbol));
  636. symidx:=1;
  637. shinfo:=1;
  638. kind:=aKind;
  639. end;
  640. procedure TElfSymtab.writeInternalSymbol(avalue:aword;astridx:longword;ainfo:byte;ashndx:word);
  641. var
  642. elfsym:TElfSymbol;
  643. begin
  644. fillchar(elfsym,sizeof(elfsym),0);
  645. elfsym.st_value:=avalue;
  646. elfsym.st_name:=astridx;
  647. elfsym.st_info:=ainfo;
  648. elfsym.st_shndx:=ashndx;
  649. inc(symidx);
  650. inc(shinfo);
  651. MaybeSwapElfSymbol(elfsym);
  652. write(elfsym,sizeof(elfsym));
  653. end;
  654. procedure TElfSymtab.writeSymbol(objsym:TObjSymbol;nameidx:longword);
  655. var
  656. elfsym:TElfSymbol;
  657. begin
  658. fillchar(elfsym,sizeof(elfsym),0);
  659. if nameidx=0 then
  660. elfsym.st_name:=fstrsec.writestr(objsym.name)
  661. else
  662. elfsym.st_name:=nameidx;
  663. elfsym.st_size:=objsym.size;
  664. elfsym.st_value:=objsym.address;
  665. {$ifdef ARM}
  666. if objsym.ThumbFunc then
  667. inc(elfsym.st_value);
  668. {$endif ARM}
  669. case objsym.bind of
  670. AB_LOCAL :
  671. begin
  672. elfsym.st_info:=STB_LOCAL shl 4;
  673. inc(shinfo);
  674. end;
  675. AB_COMMON :
  676. begin
  677. elfsym.st_value:=size_2_align(objsym.size);
  678. elfsym.st_info:=STB_GLOBAL shl 4;
  679. elfsym.st_shndx:=SHN_COMMON;
  680. end;
  681. AB_EXTERNAL :
  682. elfsym.st_info:=STB_GLOBAL shl 4;
  683. AB_WEAK_EXTERNAL :
  684. elfsym.st_info:=STB_WEAK shl 4;
  685. AB_GLOBAL :
  686. elfsym.st_info:=STB_GLOBAL shl 4;
  687. else
  688. InternalError(2012111801);
  689. end;
  690. { External symbols must be NOTYPE in relocatable files }
  691. if (objsym.bind<>AB_EXTERNAL) or (kind<>esk_obj) then
  692. begin
  693. case objsym.typ of
  694. AT_FUNCTION :
  695. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  696. AT_DATA,
  697. AT_METADATA:
  698. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  699. AT_TLS:
  700. elfsym.st_info:=elfsym.st_info or STT_TLS;
  701. AT_GNU_IFUNC:
  702. elfsym.st_info:=elfsym.st_info or STT_GNU_IFUNC;
  703. { other types are implicitly mapped to STT_NOTYPE }
  704. end;
  705. end;
  706. if objsym.bind<>AB_COMMON then
  707. begin
  708. if kind<>esk_obj then
  709. begin
  710. if assigned(objsym.objsection) and assigned(objsym.objsection.ExeSection) then
  711. begin
  712. if (objsym.typ=AT_TLS) then
  713. elfsym.st_value:=elfsym.st_value-tlsbase
  714. else if (oso_plt in objsym.objsection.SecOptions) then
  715. elfsym.st_value:=0
  716. else
  717. elfsym.st_shndx:=TElfExeSection(objsym.objsection.ExeSection).secshidx;
  718. end;
  719. end
  720. else
  721. begin
  722. if assigned(objsym.objsection) then
  723. elfsym.st_shndx:=objsym.objsection.index
  724. else
  725. elfsym.st_shndx:=SHN_UNDEF;
  726. objsym.symidx:=symidx;
  727. end;
  728. end;
  729. inc(symidx);
  730. MaybeSwapElfSymbol(elfsym);
  731. write(elfsym,sizeof(TElfSymbol));
  732. end;
  733. {****************************************************************************
  734. TElfObjectOutput
  735. ****************************************************************************}
  736. constructor TElfObjectOutput.create(AWriter:TObjectWriter);
  737. begin
  738. inherited Create(AWriter);
  739. CObjData:=TElfObjData;
  740. end;
  741. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection;data:TObjData);
  742. var
  743. i : longint;
  744. rel : telfreloc;
  745. objreloc : TObjRelocation;
  746. relsym : longint;
  747. relocsect : TElfObjSection;
  748. begin
  749. { create the reloc section }
  750. relocsect:=TElfObjSection.create_reloc(data,s.name,false);
  751. relocsect.shlink:=symtabsect.index;
  752. relocsect.shinfo:=s.index;
  753. { add the relocations }
  754. for i:=0 to s.Objrelocations.count-1 do
  755. begin
  756. objreloc:=TObjRelocation(s.Objrelocations[i]);
  757. { Symbol }
  758. if assigned(objreloc.symbol) then
  759. begin
  760. if objreloc.symbol.symidx=-1 then
  761. begin
  762. writeln(objreloc.symbol.Name);
  763. internalerror(200603012);
  764. end;
  765. relsym:=objreloc.symbol.symidx;
  766. end
  767. else
  768. begin
  769. if objreloc.objsection<>nil then
  770. relsym:=objreloc.objsection.secsymidx
  771. else
  772. relsym:=SHN_UNDEF;
  773. end;
  774. rel.address:=objreloc.dataoffset;
  775. rel.info:=ELF_R_INFO(relsym,ElfTarget.encodereloc(objreloc));
  776. {$push}{$r-}
  777. rel.addend:=objreloc.orgsize;
  778. {$pop}
  779. { write reloc }
  780. { ElfXX_Rel is essentially ElfXX_Rela without the addend field. }
  781. MaybeSwapElfReloc(rel);
  782. relocsect.write(rel,relocsect.shentsize);
  783. end;
  784. end;
  785. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  786. begin
  787. { Must not write symbols for internal sections like .symtab }
  788. { TODO: maybe use inclusive list of section types instead }
  789. if (TElfObjSection(p).shtype in [SHT_SYMTAB,SHT_STRTAB,SHT_REL,SHT_RELA]) then
  790. exit;
  791. TObjSection(p).secsymidx:=symtabsect.symidx;
  792. symtabsect.writeInternalSymbol(0,0,STT_SECTION,TObjSection(p).index);
  793. end;
  794. procedure TElfObjectOutput.createsymtab(data: TObjData);
  795. var
  796. i : longint;
  797. objsym : TObjSymbol;
  798. begin
  799. with data do
  800. begin
  801. { section symbols }
  802. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  803. { First the Local Symbols, this is required by ELF. The localsyms
  804. count stored in shinfo is used to skip the local symbols
  805. when traversing the symtab }
  806. for i:=0 to ObjSymbolList.Count-1 do
  807. begin
  808. objsym:=TObjSymbol(ObjSymbolList[i]);
  809. if (objsym.bind=AB_LOCAL) and (objsym.typ<>AT_LABEL) then
  810. symtabsect.WriteSymbol(objsym);
  811. end;
  812. { Global Symbols }
  813. for i:=0 to ObjSymbolList.Count-1 do
  814. begin
  815. objsym:=TObjSymbol(ObjSymbolList[i]);
  816. if (objsym.bind<>AB_LOCAL) then
  817. symtabsect.WriteSymbol(objsym);
  818. end;
  819. { update the .symtab section header }
  820. symtabsect.shlink:=symtabsect.fstrsec.index;
  821. end;
  822. end;
  823. procedure TElfObjectOutput.createshstrtab(data: TObjData);
  824. var
  825. i,prefixlen:longint;
  826. objsec,target:TElfObjSection;
  827. begin
  828. shstrtabsect.writezeros(1);
  829. prefixlen:=length('.rel')+ord(ElfTarget.relocs_use_addend);
  830. for i:=0 to data.ObjSectionList.Count-1 do
  831. begin
  832. objsec:=TElfObjSection(data.ObjSectionList[i]);
  833. { Alias section names into names of corresponding reloc sections,
  834. this is allowed by ELF specs and saves good half of .shstrtab space. }
  835. if objsec.shtype=relsec_shtype[ElfTarget.relocs_use_addend] then
  836. begin
  837. target:=TElfObjSection(data.ObjSectionList[objsec.shinfo-1]);
  838. if (target.ObjRelocations.Count=0) or
  839. (target.shstridx<prefixlen) then
  840. InternalError(2012101204);
  841. objsec.shstridx:=target.shstridx-prefixlen;
  842. end
  843. else
  844. begin
  845. if objsec.ObjRelocations.Count<>0 then
  846. shstrtabsect.write(relsec_prefix[true][1],prefixlen);
  847. objsec.shstridx:=shstrtabsect.writestr(objsec.name);
  848. end;
  849. end;
  850. end;
  851. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  852. var
  853. sechdr : telfsechdr;
  854. begin
  855. fillchar(sechdr,sizeof(sechdr),0);
  856. sechdr.sh_name:=s.shstridx;
  857. sechdr.sh_type:=s.shtype;
  858. sechdr.sh_flags:=s.shflags;
  859. sechdr.sh_offset:=s.datapos;
  860. sechdr.sh_size:=s.Size;
  861. sechdr.sh_link:=s.shlink;
  862. sechdr.sh_info:=s.shinfo;
  863. sechdr.sh_addralign:=s.secalign;
  864. sechdr.sh_entsize:=s.shentsize;
  865. MaybeSwapSecHeader(sechdr);
  866. writer.write(sechdr,sizeof(sechdr));
  867. end;
  868. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  869. begin
  870. TElfObjSection(p).index:=pword(arg)^;
  871. inc(pword(arg)^);
  872. end;
  873. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  874. begin
  875. if (TElfObjSection(p).ObjRelocations.count>0) then
  876. createrelocsection(TElfObjSection(p),TObjData(arg));
  877. end;
  878. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  879. begin
  880. writesectionheader(TElfObjSection(p));
  881. end;
  882. function TElfObjectOutput.writedata(data:TObjData):boolean;
  883. var
  884. header : telfheader;
  885. shoffset,
  886. datapos : aword;
  887. nsections : word;
  888. begin
  889. result:=false;
  890. with data do
  891. begin
  892. { default sections }
  893. symtabsect:=TElfSymtab.create(data,esk_obj);
  894. shstrtabsect:=TElfObjSection.create_ext(data,'.shstrtab',SHT_STRTAB,0,1,0);
  895. { "no executable stack" marker }
  896. { TODO: used by OpenBSD/NetBSD as well? }
  897. if (target_info.system in (systems_linux + systems_android + systems_freebsd + systems_dragonfly)) and
  898. not(cs_executable_stack in current_settings.moduleswitches) then
  899. TElfObjSection.create_ext(data,'.note.GNU-stack',SHT_PROGBITS,0,1,0);
  900. { symbol for filename }
  901. symtabsect.fstrsec.writestr(ExtractFileName(current_module.mainsource));
  902. symtabsect.writeInternalSymbol(0,1,STT_FILE,SHN_ABS);
  903. { calc amount of sections we have }
  904. nsections:=1;
  905. { also create the index in the section header table }
  906. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  907. { create .symtab and .strtab }
  908. createsymtab(data);
  909. { Create the relocation sections, this needs valid secidx and symidx }
  910. ObjSectionList.ForEachCall(@section_create_relocsec,data);
  911. { recalc nsections to incude the reloc sections }
  912. nsections:=1;
  913. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  914. { create .shstrtab }
  915. createshstrtab(data);
  916. { Calculate the filepositions }
  917. datapos:=$40; { elfheader + alignment }
  918. { section data }
  919. layoutsections(datapos);
  920. { section headers }
  921. shoffset:=align(datapos,dword(Sizeof(AInt)));
  922. inc(datapos,(nsections+1)*sizeof(telfsechdr));
  923. { Write ELF Header }
  924. fillchar(header,sizeof(header),0);
  925. header.e_ident[EI_MAG0]:=ELFMAG0; { = #127'ELF' }
  926. header.e_ident[EI_MAG1]:=ELFMAG1;
  927. header.e_ident[EI_MAG2]:=ELFMAG2;
  928. header.e_ident[EI_MAG3]:=ELFMAG3;
  929. header.e_ident[EI_CLASS]:=ELFCLASS;
  930. if target_info.endian=endian_big then
  931. header.e_ident[EI_DATA]:=ELFDATA2MSB
  932. else
  933. header.e_ident[EI_DATA]:=ELFDATA2LSB;
  934. header.e_ident[EI_VERSION]:=1;
  935. if target_info.system in systems_openbsd then
  936. header.e_ident[EI_OSABI]:=ELFOSABI_OPENBSD
  937. else if target_info.system in systems_freebsd then
  938. header.e_ident[EI_OSABI]:=ELFOSABI_FREEBSD
  939. else if target_info.system in systems_dragonfly then
  940. header.e_ident[EI_OSABI]:=ELFOSABI_NONE;
  941. header.e_type:=ET_REL;
  942. header.e_machine:=ElfTarget.machine_code;
  943. header.e_version:=1;
  944. header.e_shoff:=shoffset;
  945. header.e_shstrndx:=shstrtabsect.index;
  946. header.e_shnum:=nsections;
  947. header.e_ehsize:=sizeof(telfheader);
  948. header.e_shentsize:=sizeof(telfsechdr);
  949. if assigned(ElfTarget.encodeflags) then
  950. header.e_flags:=ElfTarget.encodeflags();
  951. MaybeSwapHeader(header);
  952. writer.write(header,sizeof(header));
  953. writer.writezeros($40-sizeof(header)); { align }
  954. { Sections }
  955. WriteSectionContent(data);
  956. { Align header }
  957. Writer.Writezeros(Align(Writer.Size,Sizeof(AInt))-Writer.Size);
  958. { section headers, start with an empty header for sh_undef }
  959. writer.writezeros(sizeof(telfsechdr));
  960. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  961. end;
  962. result:=true;
  963. end;
  964. {****************************************************************************
  965. TELFAssembler
  966. ****************************************************************************}
  967. constructor TElfAssembler.Create(info: pasminfo; smart:boolean);
  968. begin
  969. inherited;
  970. CObjOutput:=TElfObjectOutput;
  971. CInternalAr:=tarobjectwriter;
  972. end;
  973. {****************************************************************************
  974. TELFObjectInput
  975. ****************************************************************************}
  976. constructor TElfObjInput.Create;
  977. begin
  978. inherited Create;
  979. CObjData:=TElfObjData;
  980. CObjSymbol:=TObjSymbol;
  981. end;
  982. destructor TElfObjInput.Destroy;
  983. begin
  984. if Assigned(FSymTbl) then
  985. FreeMem(FSymTbl);
  986. if Assigned(FSecTbl) then
  987. FreeMem(FSecTbl);
  988. if Assigned(strtab) then
  989. FreeMem(strtab);
  990. if Assigned(shstrtab) then
  991. FreeMem(shstrtab);
  992. if Assigned(symversions) then
  993. FreeMem(symversions);
  994. inherited Destroy;
  995. end;
  996. procedure TElfObjInput.LoadRelocations(const secrec:TSectionRec);
  997. var
  998. i: longint;
  999. rel: TElfReloc;
  1000. reltyp: byte;
  1001. relsym: longint;
  1002. objrel: TObjRelocation;
  1003. p: TObjSymbol;
  1004. begin
  1005. FReader.Seek(secrec.relocpos);
  1006. if secrec.sec=nil then
  1007. InternalError(2012060203);
  1008. if (secrec.relentsize=3*sizeof(pint)) then
  1009. with secrec.sec do SecOptions:=SecOptions+[oso_rela_relocs];
  1010. for i:=0 to secrec.relocs-1 do
  1011. begin
  1012. FReader.Read(rel,secrec.relentsize);
  1013. MaybeSwapElfReloc(rel);
  1014. reltyp:=rel.info and $FF;
  1015. {$ifdef cpu64bitaddr}
  1016. relsym:=rel.info shr 32;
  1017. {$else cpu64bitaddr}
  1018. relsym:=(rel.info shr 8) and $FFFFFF;
  1019. {$endif cpu64bitaddr}
  1020. if relsym>=syms then
  1021. InternalError(2012060204);
  1022. p:=TObjSymbol(FSymTbl[relsym]);
  1023. { Some relocations (e.g. R_ARM_V4BX) don't use a symbol at all }
  1024. if assigned(p) or (relsym=0) then
  1025. begin
  1026. objrel:=TObjRelocation.CreateRaw(rel.address-secrec.sec.mempos,p,reltyp);
  1027. if (secrec.relentsize=3*sizeof(pint)) then
  1028. objrel.orgsize:=rel.addend;
  1029. { perform target-specific actions }
  1030. if Assigned(ElfTarget.loadreloc) then
  1031. ElfTarget.loadreloc(objrel);
  1032. secrec.sec.ObjRelocations.add(objrel);
  1033. end
  1034. else
  1035. begin
  1036. InputError('Unable to resolve symbol of relocation');
  1037. exit;
  1038. end;
  1039. end;
  1040. end;
  1041. procedure TElfObjInput.LoadSymbols(objdata:TObjData;count,locals:longword);
  1042. var
  1043. i: longint;
  1044. sym: TElfSymbol;
  1045. bind: TAsmSymBind;
  1046. typ: TAsmSymType;
  1047. objsym: TObjSymbol;
  1048. ver: word;
  1049. begin
  1050. FSymTbl:=AllocMem(count*sizeof(Pointer));
  1051. for i:=1 to count-1 do
  1052. begin
  1053. FReader.Read(sym,sizeof(TElfSymbol));
  1054. MaybeSwapElfSymbol(sym);
  1055. if sym.st_name>=strtablen then
  1056. InternalError(2012060205);
  1057. if sym.st_shndx=SHN_ABS then { ignore absolute symbols (should we really do it???) }
  1058. Continue
  1059. else if sym.st_shndx=SHN_COMMON then
  1060. bind:=AB_COMMON
  1061. else if (sym.st_shndx>=nsects) then
  1062. InternalError(2012060206)
  1063. else
  1064. case (sym.st_info shr 4) of
  1065. STB_LOCAL:
  1066. bind:=AB_LOCAL;
  1067. STB_GLOBAL:
  1068. if sym.st_shndx=SHN_UNDEF then
  1069. bind:=AB_EXTERNAL
  1070. else
  1071. bind:=AB_GLOBAL;
  1072. STB_WEAK:
  1073. bind:=AB_WEAK_EXTERNAL;
  1074. else
  1075. InternalError(2012060207);
  1076. end;
  1077. { Ignore section symbol if we didn't create the corresponding objsection
  1078. (examples are SHT_GROUP or .note.GNU-stack sections). }
  1079. if (sym.st_shndx>0) and (sym.st_shndx<SHN_LORESERVE) and
  1080. (FSecTbl[sym.st_shndx].sec=nil) and
  1081. (not dynobj) then
  1082. if ((sym.st_info and $0F)=STT_SECTION) then
  1083. Continue
  1084. else
  1085. begin
  1086. writeln(objdata.name,' ',i);
  1087. InternalError(2012110701)
  1088. end;
  1089. case (sym.st_info and $0F) of
  1090. STT_NOTYPE:
  1091. typ:=AT_NONE;
  1092. STT_OBJECT:
  1093. typ:=AT_DATA;
  1094. STT_FUNC:
  1095. typ:=AT_FUNCTION;
  1096. STT_SECTION:
  1097. typ:=AT_SECTION;
  1098. STT_FILE:
  1099. continue;
  1100. STT_TLS:
  1101. typ:=AT_TLS;
  1102. STT_GNU_IFUNC:
  1103. typ:=AT_GNU_IFUNC;
  1104. else
  1105. writeln(objdata.name,' ',sym.st_info and $0F);
  1106. InternalError(2012060208);
  1107. end;
  1108. { If reading DSO, we're interested only in global symbols defined there.
  1109. Symbols with non-current version should also be ignored. }
  1110. ver:=0;
  1111. if dynobj then
  1112. begin
  1113. if assigned(symversions) then
  1114. begin
  1115. ver:=symversions[i];
  1116. if (ver=VER_NDX_LOCAL) or (ver>VERSYM_VERSION) then
  1117. continue;
  1118. end;
  1119. if (bind=AB_LOCAL) or (sym.st_shndx=SHN_UNDEF) then
  1120. continue;
  1121. if ver>=verdefs.count then
  1122. InternalError(2012120505);
  1123. end;
  1124. { validity of name and objsection has been checked above }
  1125. { !! all AT_SECTION symbols have duplicate (null) name,
  1126. therefore TObjSection.CreateSymbol cannot be used here }
  1127. objsym:=CObjSymbol.Create(objdata.ObjSymbolList,string(PChar(@strtab[sym.st_name])));
  1128. objsym.bind:=bind;
  1129. objsym.typ:=typ;
  1130. if bind<>AB_COMMON then
  1131. objsym.objsection:=FSecTbl[sym.st_shndx].sec;
  1132. objsym.offset:=sym.st_value;
  1133. objsym.size:=sym.st_size;
  1134. FSymTbl[i]:=objsym;
  1135. if (ver>VER_NDX_GLOBAL) then
  1136. TVersionedObjSymbol(objsym).version:=TElfVersionDef(verdefs[ver]);
  1137. end;
  1138. end;
  1139. function TElfObjInput.CreateSection(const shdr:TElfsechdr;index:longint;objdata:tobjdata;
  1140. out secname:string):TElfObjSection;
  1141. begin
  1142. secname:=string(PChar(@shstrtab[shdr.sh_name]));
  1143. result:=TElfObjSection.create_ext(objdata,secname,
  1144. shdr.sh_type,shdr.sh_flags,shdr.sh_addralign,shdr.sh_entsize);
  1145. result.index:=index;
  1146. result.DataPos:=shdr.sh_offset;
  1147. result.MemPos:=shdr.sh_addr;
  1148. result.Size:=shdr.sh_size;
  1149. FSecTbl[index].sec:=result;
  1150. end;
  1151. function TElfObjInput.ReadBytes(offs:longint;out buf;len:longint):boolean;
  1152. begin
  1153. FReader.Seek(offs);
  1154. result:=FReader.Read(buf,len);
  1155. end;
  1156. procedure TElfObjInput.LoadSection(const shdr:TElfsechdr;index:longint;objdata:tobjdata);
  1157. var
  1158. sec: TElfObjSection;
  1159. sym: TElfSymbol;
  1160. secname: string;
  1161. begin
  1162. if shdr.sh_name>=shstrtablen then
  1163. InternalError(2012060210);
  1164. case shdr.sh_type of
  1165. SHT_NULL:
  1166. {ignore};
  1167. { SHT_STRTAB may appear for .stabstr and other debug sections.
  1168. .shstrtab and .strtab are processed separately and don't appear here. }
  1169. SHT_PROGBITS,SHT_NOBITS,SHT_NOTE,SHT_STRTAB,
  1170. SHT_INIT_ARRAY,SHT_FINI_ARRAY,SHT_PREINIT_ARRAY:
  1171. begin
  1172. sec:=CreateSection(shdr,index,objdata,secname);
  1173. if (Length(secname)>3) and (secname[2] in ['d','f','n','s']) then
  1174. begin
  1175. if (Pos('.stub',secname)=1) or
  1176. (Pos('.fpc',secname)=1) then
  1177. sec.SecOptions:=[oso_keep]
  1178. { ELF does not have any flags specific to debug sections,
  1179. but reserves names starting with '.debug' for this purpose }
  1180. else if (Pos('.debug',secname)=1) or
  1181. (secname='.stab') or
  1182. (secname='.stabstr') then
  1183. sec.SecOptions:=[oso_debug]
  1184. else if (secname='.note.GNU-stack') and (shdr.sh_type=SHT_PROGBITS) then
  1185. begin
  1186. if (shdr.sh_flags and SHF_EXECINSTR)=0 then
  1187. objdata.ExecStack:=False;
  1188. end;
  1189. end;
  1190. if (shdr.sh_type=SHT_NOTE) and (shdr.sh_size<>0) then
  1191. sec.SecOptions:=[oso_keep];
  1192. end;
  1193. SHT_REL,SHT_RELA:
  1194. begin
  1195. if shdr.sh_info>=nsects then
  1196. InternalError(2012060211);
  1197. if shdr.sh_entsize<>longword((2+ord(shdr.sh_type=SHT_RELA))*sizeof(pint)) then
  1198. InternalError(2012060212);
  1199. with FSecTbl[shdr.sh_info] do
  1200. begin
  1201. relocpos:=shdr.sh_offset;
  1202. relocs:=shdr.sh_size div shdr.sh_entsize;
  1203. relentsize:=shdr.sh_entsize;
  1204. end;
  1205. end;
  1206. SHT_GROUP:
  1207. if (shdr.sh_size>=2*sizeof(longword)) and
  1208. (shdr.sh_entsize=sizeof(longword)) and
  1209. ((shdr.sh_size mod shdr.sh_entsize)=0) then
  1210. begin
  1211. { Groups are identified by name of symbol pointed to by
  1212. sh_link and sh_info, not by sh_name. This symbol
  1213. may as well be STT_SECTION symbol of this section,
  1214. in which case we end up using sh_name. }
  1215. if dynobj then
  1216. InternalError(2012110801);
  1217. if (shdr.sh_link<>symtabndx) then
  1218. InternalError(2012110703);
  1219. if (shdr.sh_info>=syms) then
  1220. InternalError(2012110704);
  1221. FReader.Seek(symtaboffset+shdr.sh_info*sizeof(TElfSymbol));
  1222. FReader.Read(sym,sizeof(TElfSymbol));
  1223. MaybeSwapElfSymbol(sym);
  1224. if sym.st_name>=strtablen then
  1225. InternalError(2012110705);
  1226. if (sym.st_shndx=index) and (sym.st_info=((STB_LOCAL shl 4) or STT_SECTION)) then
  1227. secname:=string(PChar(@shstrtab[shdr.sh_name]))
  1228. else
  1229. secname:=string(PChar(@strtab[sym.st_name]));
  1230. { Postpone further processing until all sections are loaded,
  1231. we'll need to access correct section header.
  1232. Since ABI requires SHT_GROUP sections to come first in the file,
  1233. we assume that group number x has header index x+1.
  1234. If we ever encounter files where this is not true, we'll have
  1235. to maintain a separate index. }
  1236. objdata.CreateSectionGroup(secname);
  1237. if (index<>objdata.GroupsList.Count) then
  1238. InternalError(2012110802);
  1239. end
  1240. else
  1241. InternalError(2012110706);
  1242. SHT_GNU_ATTRIBUTES:
  1243. { TODO: must not be ignored };
  1244. else
  1245. if not (assigned(ElfTarget.loadsection) and
  1246. ElfTarget.loadsection(self,objdata,shdr,index)) then
  1247. InternalError(2012072603);
  1248. end;
  1249. FLoaded[index]:=True;
  1250. end;
  1251. function TElfObjInput.LoadHeader(out objdata:TObjData):boolean;
  1252. var
  1253. header:TElfHeader;
  1254. begin
  1255. result:=false;
  1256. if not FReader.read(header,sizeof(header)) then
  1257. begin
  1258. InputError('Can''t read ELF header');
  1259. exit;
  1260. end;
  1261. if (header.e_ident[EI_MAG0]<>ELFMAG0) or (header.e_ident[EI_MAG1]<>ELFMAG1) or
  1262. (header.e_ident[EI_MAG2]<>ELFMAG2) or (header.e_ident[EI_MAG3]<>ELFMAG3) then
  1263. begin
  1264. InputError('Illegal ELF magic');
  1265. exit;
  1266. end;
  1267. if (header.e_ident[EI_VERSION]<>1) then
  1268. begin
  1269. InputError('Unknown ELF file version');
  1270. exit;
  1271. end;
  1272. if (header.e_ident[EI_CLASS]<>ELFCLASS) then
  1273. begin
  1274. InputError('Wrong ELF file class (32/64 bit mismatch)');
  1275. exit;
  1276. end;
  1277. if (header.e_ident[EI_DATA]<>1+ord(target_info.endian=endian_big)) then
  1278. begin
  1279. InputError('ELF endianness does not match target');
  1280. exit;
  1281. end;
  1282. MaybeSwapHeader(header);
  1283. if (header.e_version<>1) then
  1284. begin
  1285. InputError('Unknown ELF data version');
  1286. exit;
  1287. end;
  1288. if (header.e_machine<>ElfTarget.machine_code) then
  1289. begin
  1290. InputError('ELF file is for different CPU');
  1291. exit;
  1292. end;
  1293. if (header.e_type<>ET_REL) and (header.e_type<>ET_DYN) then
  1294. begin
  1295. InputError('Not a relocatable or dynamic ELF file');
  1296. exit;
  1297. end;
  1298. if header.e_shentsize<>sizeof(TElfsechdr) then
  1299. InternalError(2012062701);
  1300. nsects:=header.e_shnum;
  1301. dynobj:=(header.e_type=ET_DYN);
  1302. shoffset:=header.e_shoff;
  1303. shstrndx:=header.e_shstrndx;
  1304. if dynobj then
  1305. begin
  1306. objdata:=TElfDynamicObjData.Create(InputFilename);
  1307. verdefs:=TElfDynamicObjData(objdata).versiondefs;
  1308. CObjSymbol:=TVersionedObjSymbol;
  1309. end
  1310. else
  1311. objdata:=CObjData.Create(InputFilename);
  1312. TElfObjData(objdata).ident:=header.e_ident;
  1313. TElfObjData(objdata).flags:=header.e_flags;
  1314. result:=true;
  1315. end;
  1316. procedure TElfObjInput.LoadDynamic(const shdr:TElfsechdr;objdata:TObjData);
  1317. var
  1318. dt: TElfDyn;
  1319. i: longint;
  1320. begin
  1321. if (shdr.sh_entsize<>sizeof(TElfDyn)) then
  1322. InternalError(2012071403);
  1323. FReader.Seek(shdr.sh_offset);
  1324. for i:=0 to (shdr.sh_size div shdr.sh_entsize)-1 do
  1325. begin
  1326. FReader.Read(dt,sizeof(TElfDyn));
  1327. MaybeSwapElfDyn(dt);
  1328. case dt.d_tag of
  1329. DT_NULL:
  1330. break;
  1331. DT_SONAME:
  1332. TElfObjData(objdata).FName:=string(PChar(@strtab[dt.d_ptr]));
  1333. DT_NEEDED:
  1334. ;
  1335. end;
  1336. end;
  1337. end;
  1338. function TElfObjInput.ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;
  1339. var
  1340. i,j,strndx,dynndx,
  1341. versymndx,verdefndx,verneedndx: longint;
  1342. objsec: TObjSection;
  1343. grp: TObjSectionGroup;
  1344. tmp: longword;
  1345. count: longint;
  1346. vd: TElfverdef;
  1347. vda: TElfverdaux;
  1348. vdoffset: aword;
  1349. begin
  1350. FReader:=AReader;
  1351. InputFileName:=AReader.FileName;
  1352. result:=false;
  1353. strndx:=0;
  1354. if not LoadHeader(objData) then
  1355. exit;
  1356. FSecTbl:=AllocMem(nsects*sizeof(TSectionRec));
  1357. FLoaded:=AllocMem(nsects*sizeof(boolean));
  1358. SetLength(shdrs,nsects);
  1359. FReader.Seek(shoffset);
  1360. if not FReader.Read(shdrs[0],nsects*sizeof(TElfsechdr)) then
  1361. begin
  1362. InputError('Can''t read ELF section headers');
  1363. exit;
  1364. end;
  1365. if source_info.endian<>target_info.endian then
  1366. for i:=0 to nsects-1 do
  1367. MaybeSwapSecHeader(shdrs[i]);
  1368. { First, load the .shstrtab section }
  1369. if shstrndx>=nsects then
  1370. InternalError(2012060201);
  1371. if shdrs[shstrndx].sh_type<>SHT_STRTAB then
  1372. InternalError(2012060202);
  1373. shstrtablen:=shdrs[shstrndx].sh_size;
  1374. GetMem(shstrtab,shstrtablen);
  1375. FReader.seek(shdrs[shstrndx].sh_offset);
  1376. FReader.read(shstrtab^,shstrtablen);
  1377. FLoaded[shstrndx]:=True;
  1378. { Locate the symtable, it is typically at the end so loop backwards.
  1379. Load the strings, postpone symtable itself until done with sections.
  1380. Note that is is legal to have no symtable.
  1381. For DSO, locate .dynsym instead, this one is near the beginning, but
  1382. overall number of sections won't be big. }
  1383. symtabndx:=0;
  1384. for i:=nsects-1 downto 1 do
  1385. begin
  1386. if (shdrs[i].sh_type<>symsectypes[dynobj]) then
  1387. continue;
  1388. if (shdrs[i].sh_entsize<>sizeof(TElfSymbol)) then
  1389. InternalError(2012060213);
  1390. if shdrs[i].sh_link>=nsects then
  1391. InternalError(2012062702);
  1392. strndx:=shdrs[i].sh_link;
  1393. if shdrs[strndx].sh_type<>SHT_STRTAB then
  1394. InternalError(2012062703);
  1395. strtablen:=shdrs[strndx].sh_size;
  1396. GetMem(strtab,strtablen);
  1397. FReader.seek(shdrs[strndx].sh_offset);
  1398. FReader.read(strtab^,strtablen);
  1399. symtaboffset:=shdrs[i].sh_offset;
  1400. syms:=shdrs[i].sh_size div sizeof(TElfSymbol);
  1401. localsyms:=shdrs[i].sh_info;
  1402. FLoaded[i]:=True;
  1403. FLoaded[strndx]:=True;
  1404. symtabndx:=i;
  1405. break;
  1406. end;
  1407. if dynobj then
  1408. begin
  1409. if symtabndx=0 then
  1410. InternalError(2012110707);
  1411. { Locate .dynamic and version sections. Expect a single one of a kind. }
  1412. dynndx:=0;
  1413. versymndx:=0;
  1414. verdefndx:=0;
  1415. verneedndx:=0;
  1416. for i:=nsects-1 downto 0 do
  1417. begin
  1418. case shdrs[i].sh_type of
  1419. SHT_DYNAMIC:
  1420. begin
  1421. if dynndx<>0 then
  1422. InternalError(2012102001);
  1423. dynndx:=i;
  1424. if (shdrs[dynndx].sh_link<>strndx) then
  1425. InternalError(2012071402);
  1426. LoadDynamic(shdrs[dynndx],objdata);
  1427. end;
  1428. SHT_GNU_versym:
  1429. begin
  1430. if versymndx<>0 then
  1431. InternalError(2012102002);
  1432. versymndx:=i;
  1433. if shdrs[i].sh_entsize<>sizeof(word) then
  1434. InternalError(2012102003);
  1435. if shdrs[i].sh_link<>symtabndx then
  1436. InternalError(2012102004);
  1437. if shdrs[i].sh_size<>syms*sizeof(word) then
  1438. InternalError(2012102005);
  1439. GetMem(symversions,shdrs[i].sh_size);
  1440. FReader.seek(shdrs[i].sh_offset);
  1441. FReader.read(symversions^,shdrs[i].sh_size);
  1442. if source_info.endian<>target_info.endian then
  1443. for j:=0 to syms-1 do
  1444. symversions[j]:=SwapEndian(symversions[j]);
  1445. end;
  1446. SHT_GNU_verdef:
  1447. begin
  1448. if verdefndx<>0 then
  1449. InternalError(2012102006);
  1450. verdefndx:=i;
  1451. if shdrs[i].sh_link<>strndx then
  1452. InternalError(2012120501);
  1453. vdoffset:=shdrs[i].sh_offset;
  1454. { TODO: can we rely on sh_info, or must read until vd_next=0? }
  1455. for j:=1 to shdrs[i].sh_info do
  1456. begin
  1457. FReader.seek(vdoffset);
  1458. FReader.Read(vd,sizeof(TElfverdef));
  1459. MaybeSwapElfverdef(vd);
  1460. if vd.vd_version<>VER_DEF_CURRENT then
  1461. InternalError(2012120502);
  1462. FReader.seek(vdoffset+vd.vd_aux);
  1463. vdoffset:=vdoffset+vd.vd_next;
  1464. { First verdaux entry holds name of version (unless VER_FLG_BASE flag is set),
  1465. subsequent one(s) point to parent(s). For our purposes, version hierarchy
  1466. looks irrelevant. }
  1467. FReader.Read(vda,sizeof(TElfverdaux));
  1468. MaybeSwapElfverdaux(vda);
  1469. if vda.vda_name>=strtablen then
  1470. InternalError(2012120503);
  1471. if (vd.vd_flags and VER_FLG_BASE)<>0 then
  1472. continue;
  1473. { Assuming verdef indices assigned continuously starting from 2,
  1474. at least BFD produces files that way. }
  1475. if verdefs.count<>vd.vd_ndx then
  1476. InternalError(2012120504);
  1477. TElfVersionDef.Create(verdefs,string(PChar(@strtab[vda.vda_name])));
  1478. end;
  1479. end;
  1480. SHT_GNU_verneed:
  1481. begin
  1482. if verneedndx<>0 then
  1483. InternalError(2012102007);
  1484. verneedndx:=i;
  1485. //sh_link->.dynstr
  1486. //sh_info->number of entries
  1487. end;
  1488. end;
  1489. end;
  1490. if dynndx=0 then
  1491. InternalError(2012071401);
  1492. { load the symtable }
  1493. FReader.Seek(symtaboffset+sizeof(TElfSymbol));
  1494. LoadSymbols(objdata,syms,localsyms);
  1495. result:=True;
  1496. exit;
  1497. end;
  1498. { assume stack is executable until proven otherwise }
  1499. objdata.ExecStack:=True;
  1500. { Process section headers }
  1501. for i:=1 to nsects-1 do
  1502. if not FLoaded[i] then
  1503. LoadSection(shdrs[i],i,objdata);
  1504. { load the content }
  1505. ReadSectionContent(objdata);
  1506. { load the symtable }
  1507. FReader.Seek(symtaboffset+sizeof(TElfSymbol));
  1508. LoadSymbols(objdata,syms,localsyms);
  1509. { finish relocations }
  1510. for i:=0 to objdata.ObjSectionList.Count-1 do
  1511. begin
  1512. objsec:=TObjSection(objdata.ObjsectionList[i]);
  1513. { skip debug sections }
  1514. if (oso_debug in objsec.SecOptions) and
  1515. (cs_link_strip in current_settings.globalswitches) and
  1516. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  1517. continue;
  1518. if FSecTbl[objsec.index].relocpos>0 then
  1519. LoadRelocations(FSecTbl[objsec.index]);
  1520. end;
  1521. { finish processing section groups, if any }
  1522. if Assigned(objdata.GroupsList) then
  1523. begin
  1524. for i:=0 to objdata.GroupsList.Count-1 do
  1525. begin
  1526. grp:=TObjSectionGroup(objData.GroupsList[i]);
  1527. FReader.Seek(shdrs[i+1].sh_offset);
  1528. { first dword is flags }
  1529. FReader.Read(tmp,sizeof(longword));
  1530. if source_info.endian<>target_info.endian then
  1531. tmp:=SwapEndian(tmp);
  1532. if (tmp and GRP_COMDAT)<>0 then
  1533. grp.IsComdat:=true;
  1534. count:=(shdrs[i+1].sh_size div sizeof(longword))-1;
  1535. SetLength(grp.members,count);
  1536. for j:=0 to count-1 do
  1537. begin
  1538. FReader.Read(tmp,sizeof(longword));
  1539. if source_info.endian<>target_info.endian then
  1540. tmp:=SwapEndian(tmp);
  1541. if (tmp>=nsects) then
  1542. InternalError(2012110805);
  1543. objsec:=FSecTbl[tmp].sec;
  1544. if (objsec=nil) then
  1545. InternalError(2012110806);
  1546. if (TElfObjSection(objsec).shflags and SHF_GROUP)=0 then
  1547. InternalError(2012110807);
  1548. grp.members[j]:=objsec;
  1549. objsec.Group:=grp;
  1550. end;
  1551. end;
  1552. end;
  1553. result:=True;
  1554. end;
  1555. class function TElfObjInput.CanReadObjData(AReader:TObjectreader):boolean;
  1556. var
  1557. header: TElfHeader;
  1558. begin
  1559. result:=false;
  1560. if AReader.Read(header,sizeof(header)) then
  1561. begin;
  1562. if (header.e_ident[EI_MAG0]=ELFMAG0) and (header.e_ident[EI_MAG1]=ELFMAG1) and
  1563. (header.e_ident[EI_MAG2]=ELFMAG2) and (header.e_ident[EI_MAG3]=ELFMAG3) then
  1564. { TODO: check additional fields }
  1565. result:=true;
  1566. end;
  1567. AReader.Seek(0);
  1568. end;
  1569. {*****************************************************************************
  1570. TElfExeOutput
  1571. *****************************************************************************}
  1572. constructor TElfExeOutput.Create;
  1573. begin
  1574. inherited Create;
  1575. CObjData:=TElfObjData;
  1576. CExeSection:=TElfExeSection;
  1577. {$ifdef cpu64}
  1578. MaxMemPos:=Qword($FFFFFFFFFFFFFFFF);
  1579. //MaxMemPos:=$7EFFFFFF; { As specified by SysV AMD64 ABI for small memory model }
  1580. {$else cpu64}
  1581. MaxMemPos:=$7FFFFFFF;
  1582. {$endif cpu64}
  1583. SectionMemAlign:=$20;
  1584. SectionDataAlign:=$20;
  1585. segmentlist:=TFPObjectList.Create(True);
  1586. neededlist:=TFPHashList.Create;
  1587. end;
  1588. destructor TElfExeOutput.Destroy;
  1589. begin
  1590. dyncopysyms.Free;
  1591. neededlist.Free;
  1592. segmentlist.Free;
  1593. dynsymlist.Free;
  1594. dynreloclist.Free;
  1595. if assigned(dynsymnames) then
  1596. FreeMem(dynsymnames);
  1597. stringdispose(FInterpreter);
  1598. inherited Destroy;
  1599. end;
  1600. function TElfExeOutput.AttachSection(objsec:TObjSection):TElfExeSection;
  1601. begin
  1602. objsec.SecOptions:=[oso_keep];
  1603. result:=TElfExeSection(FindExeSection(objsec.name));
  1604. if result=nil then
  1605. result:=TElfExeSection.Create(ExeSectionList,objsec.name);
  1606. result.AddObjSection(objsec);
  1607. end;
  1608. function TElfExeOutput.CreateSegment(atype,aflags,aalign:longword):TElfSegment;
  1609. begin
  1610. result:=TElfSegment.Create(atype,aflags,aalign);
  1611. segmentlist.add(result);
  1612. end;
  1613. procedure TElfExeOutput.WriteHeader;
  1614. var
  1615. header: TElfHeader;
  1616. begin
  1617. FillChar(header,sizeof(header),0);
  1618. header.e_ident[EI_MAG0]:=ELFMAG0; { = #127'ELF' }
  1619. header.e_ident[EI_MAG1]:=ELFMAG1;
  1620. header.e_ident[EI_MAG2]:=ELFMAG2;
  1621. header.e_ident[EI_MAG3]:=ELFMAG3;
  1622. header.e_ident[EI_CLASS]:=ELFCLASS;
  1623. if target_info.endian=endian_big then
  1624. header.e_ident[EI_DATA]:=ELFDATA2MSB
  1625. else
  1626. header.e_ident[EI_DATA]:=ELFDATA2LSB;
  1627. header.e_ident[EI_VERSION]:=1;
  1628. if target_info.system in systems_openbsd then
  1629. header.e_ident[EI_OSABI]:=ELFOSABI_OPENBSD
  1630. else if target_info.system in systems_freebsd then
  1631. header.e_ident[EI_OSABI]:=ELFOSABI_FREEBSD
  1632. else if target_info.system in systems_dragonfly then
  1633. header.e_ident[EI_OSABI]:=ELFOSABI_NONE;
  1634. if IsSharedLibrary then
  1635. header.e_type:=ET_DYN
  1636. else
  1637. header.e_type:=ET_EXEC;
  1638. header.e_machine:=ElfTarget.machine_code;
  1639. header.e_version:=1;
  1640. header.e_phoff:=sizeof(TElfHeader);
  1641. header.e_shoff:=shoffset;
  1642. header.e_shstrndx:=ExeSectionList.IndexOf(shstrtabsect.ExeSection)+1;
  1643. header.e_shnum:=ExeSectionList.Count+1;
  1644. header.e_phnum:=segmentlist.count;
  1645. header.e_ehsize:=sizeof(telfheader);
  1646. if assigned(ElfTarget.encodeflags) then
  1647. header.e_flags:=ElfTarget.encodeflags();
  1648. if assigned(EntrySym) then
  1649. header.e_entry:=EntrySym.Address;
  1650. header.e_shentsize:=sizeof(telfsechdr);
  1651. header.e_phentsize:=sizeof(telfproghdr);
  1652. MaybeSwapHeader(header);
  1653. FWriter.Write(header,sizeof(header));
  1654. end;
  1655. procedure TElfExeOutput.exesection_write_header(p:TObject;arg:Pointer);
  1656. var
  1657. shdr: TElfsechdr;
  1658. exesec: TElfExeSection absolute p;
  1659. begin
  1660. FillChar(shdr,sizeof(shdr),0);
  1661. shdr.sh_name:=exesec.shstridx;
  1662. if (ExeWriteMode=ewm_dbgonly) and
  1663. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  1664. shdr.sh_type:=SHT_NOBITS
  1665. else
  1666. shdr.sh_type:=exesec.shtype;
  1667. shdr.sh_flags:=exesec.shflags;
  1668. if (oso_load in exesec.SecOptions) then
  1669. shdr.sh_addr:=exesec.MemPos;
  1670. shdr.sh_offset:=exesec.DataPos;
  1671. shdr.sh_size:=exesec.Size;
  1672. shdr.sh_link:=exesec.shlink;
  1673. shdr.sh_info:=exesec.shinfo;
  1674. shdr.sh_addralign:=exesec.SecAlign;
  1675. shdr.sh_entsize:=exesec.shentsize;
  1676. MaybeSwapSecHeader(shdr);
  1677. FWriter.Write(shdr,sizeof(shdr));
  1678. end;
  1679. procedure TElfExeOutput.segment_write_header(p:TObject;arg:Pointer);
  1680. var
  1681. phdr: TElfproghdr;
  1682. seg: TElfSegment absolute p;
  1683. begin
  1684. FillChar(phdr,sizeof(phdr),0);
  1685. phdr.p_type:=seg.ptype;
  1686. phdr.p_flags:=seg.pflags;
  1687. phdr.p_align:=seg.align;
  1688. phdr.p_offset:=seg.DataPos;
  1689. phdr.p_filesz:=seg.DataSize;
  1690. phdr.p_memsz:=seg.MemSize;
  1691. phdr.p_vaddr:=seg.MemPos;
  1692. phdr.p_paddr:=seg.MemPos;
  1693. MaybeSwapHeader(phdr);
  1694. FWriter.Write(phdr,sizeof(phdr));
  1695. end;
  1696. procedure TElfExeOutput.WriteStaticSymtable;
  1697. var
  1698. i: longint;
  1699. sec: TElfExeSection;
  1700. exesym: TExeSymbol;
  1701. begin
  1702. if assigned(tlsseg) then
  1703. symtab.tlsbase:=tlsseg.MemPos;
  1704. for i:=0 to ExeSectionList.Count-1 do
  1705. begin
  1706. sec:=TElfExeSection(ExeSectionList[i]);
  1707. { Must not write symbols for internal sections like .symtab }
  1708. if (sec.shtype in [SHT_SYMTAB,SHT_STRTAB,SHT_REL,SHT_RELA]) then
  1709. continue;
  1710. sec.secsymidx:=symtab.symidx;
  1711. symtab.writeInternalSymbol(sec.mempos,0,STT_SECTION,sec.secshidx);
  1712. end;
  1713. { local symbols first }
  1714. for i:=0 to ExeSymbolList.Count-1 do
  1715. begin
  1716. exesym:=TExeSymbol(ExeSymbolList[i]);
  1717. if (exesym.objsymbol.bind=AB_LOCAL) and (exesym.objsymbol.typ<>AT_LABEL) then
  1718. symtab.WriteSymbol(exesym.objsymbol);
  1719. end;
  1720. { Global Symbols }
  1721. for i:=0 to ExeSymbolList.Count-1 do
  1722. begin
  1723. exesym:=TExeSymbol(ExeSymbolList[i]);
  1724. if (exesym.objsymbol.bind<>AB_LOCAL) then
  1725. symtab.WriteSymbol(exesym.objsymbol);
  1726. end;
  1727. { update exe section properties }
  1728. symtab.ExeSection.size:=symtab.size;
  1729. TElfExeSection(symtab.ExeSection).shinfo:=symtab.shinfo;
  1730. TElfExeSection(symtab.ExeSection).shlink:=ExeSectionList.IndexOf(symtab.fstrsec.ExeSection)+1;
  1731. symtab.fstrsec.ExeSection.Size:=symtab.fstrsec.size;
  1732. end;
  1733. procedure TElfExeOutput.MapSectionsToSegments;
  1734. var
  1735. seg: TElfSegment;
  1736. exesec: TExeSection;
  1737. i: longint;
  1738. begin
  1739. if (not IsSharedLibrary) and assigned(interpobjsec) then
  1740. begin
  1741. phdrseg:=CreateSegment(PT_PHDR,PF_R or PF_X,sizeof(pint));
  1742. seg:=CreateSegment(PT_INTERP,PF_R,1);
  1743. seg.Add(interpobjsec.ExeSection);
  1744. end;
  1745. textseg:=CreateSegment(PT_LOAD,PF_X or PF_R,ElfTarget.max_page_size);
  1746. dataseg:=CreateSegment(PT_LOAD,PF_R or PF_W,ElfTarget.max_page_size);
  1747. for i:=0 to ExeSectionList.Count-1 do
  1748. begin
  1749. exesec:=TExeSection(ExeSectionList[i]);
  1750. if (oso_load in exesec.SecOptions) then
  1751. begin
  1752. if (TElfExeSection(exesec).shflags and SHF_TLS)<>0 then
  1753. begin
  1754. if tlsseg=nil then
  1755. tlsseg:=CreateSegment(PT_TLS,PF_R,sizeof(pint));
  1756. tlsseg.add(exesec);
  1757. end;
  1758. { TODO: at least on Linux, ld seems to drop .note.ABI-tag for static executables.
  1759. (Logic is as follows: there is no .note.ABI-tag section in ld script, so it
  1760. is processed as orphan section. As such, it is placed after .interp.
  1761. For static executables .interp is dropped, and it looks like there's nowhere to
  1762. place .note.ABI-tag in this case)
  1763. Always including it doesn't harm though (except increasing file size). }
  1764. if TElfExeSection(exesec).shtype=SHT_NOTE then
  1765. begin
  1766. if noteseg=nil then
  1767. noteseg:=CreateSegment(PT_NOTE,PF_R,4);
  1768. noteseg.Add(exesec);
  1769. Include(exesec.SecOptions,oso_debug_copy);
  1770. end;
  1771. if (oso_executable in exesec.SecOptions) or
  1772. not (oso_write in exesec.SecOptions) then
  1773. textseg.add(exesec)
  1774. else
  1775. dataseg.add(exesec);
  1776. end;
  1777. end;
  1778. if dynamiclink then
  1779. begin
  1780. seg:=CreateSegment(PT_DYNAMIC,PF_R or PF_W,sizeof(pint));
  1781. seg.add(dynamicsec.ExeSection);
  1782. end;
  1783. { stack flags }
  1784. CreateSegment(PT_GNU_STACK,PF_R or PF_W or (PF_X*ord(ExecStack)),sizeof(pint));
  1785. end;
  1786. procedure TElfExeOutput.make_dynamic_if_undefweak(exesym:TExeSymbol);
  1787. begin
  1788. if (exesym.dynindex=0) and (exesym.state=symstate_undefweak) and
  1789. not (cs_link_staticflag in current_settings.globalswitches) then
  1790. exesym.dynindex:=dynsymlist.add(exesym)+1;
  1791. end;
  1792. function TElfExeOutput.AllocGOTSlot(objsym:TObjSymbol):boolean;
  1793. var
  1794. exesym: TExeSymbol;
  1795. begin
  1796. result:=false;
  1797. exesym:=objsym.exesymbol;
  1798. { Although local symbols should not be accessed through GOT,
  1799. this isn't strictly forbidden. In this case we need to fake up
  1800. the exesym to store the GOT offset in it.
  1801. TODO: name collision; maybe use a different symbol list object? }
  1802. if exesym=nil then
  1803. begin
  1804. exesym:=TExeSymbol.Create(ExeSymbolList,objsym.name+'*local*');
  1805. exesym.objsymbol:=objsym;
  1806. objsym.exesymbol:=exesym;
  1807. end;
  1808. if exesym.GotOffset>0 then
  1809. exit;
  1810. gotobjsec.alloc(sizeof(pint));
  1811. exesym.GotOffset:=gotobjsec.size;
  1812. make_dynamic_if_undefweak(exesym);
  1813. { In shared library, every GOT entry needs a RELATIVE dynamic reloc,
  1814. imported/exported symbols need GLOB_DAT instead. For executables,
  1815. only the latter applies. }
  1816. if IsSharedLibrary or (exesym.dynindex>0) then
  1817. dynrelocsec.alloc(dynrelocsec.shentsize);
  1818. result:=true;
  1819. end;
  1820. procedure TElfExeOutput.PrepareGOT;
  1821. var
  1822. i,j,k: longint;
  1823. objsec: TElfObjSection;
  1824. exesec: TExeSection;
  1825. begin
  1826. for i:=0 to ExeSectionList.Count-1 do
  1827. begin
  1828. exesec:=TExeSection(ExeSectionList[i]);
  1829. for j:=0 to exesec.ObjSectionlist.count-1 do
  1830. begin
  1831. objsec:=TElfObjSection(exesec.ObjSectionlist[j]);
  1832. { ignore linker-generated and debug sections }
  1833. if (objsec.objdata=internalobjdata) or (oso_debug in objsec.SecOptions) then
  1834. continue;
  1835. if not objsec.Used then
  1836. internalerror(2012060901);
  1837. k:=0;
  1838. while k<objsec.ObjRelocations.Count do
  1839. begin
  1840. GOTRelocPass1(objsec,k);
  1841. inc(k);
  1842. end;
  1843. end;
  1844. end;
  1845. { remember sizes for sanity checking }
  1846. gotsize:=gotobjsec.size;
  1847. if assigned(dynrelocsec) then
  1848. dynrelsize:=dynrelocsec.size
  1849. else
  1850. dynrelsize:=0;
  1851. end;
  1852. procedure TElfExeOutput.CreateGOTSection;
  1853. begin
  1854. gotpltobjsec:=TElfObjSection.create_ext(internalObjData,'.got.plt',
  1855. SHT_PROGBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(pint));
  1856. gotobjsec:=TElfObjSection.create_ext(internalObjData,'.got',
  1857. SHT_PROGBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(pint));
  1858. gotobjsec.SecOptions:=[oso_keep];
  1859. { GOT symbol and reserved .got.plt entries }
  1860. internalObjData.SetSection(gotpltobjsec);
  1861. gotsymbol:=internalObjData.SymbolDefine('_GLOBAL_OFFSET_TABLE_',AB_GLOBAL,AT_DATA);
  1862. gotpltobjsec.writeZeros(3*sizeof(pint));
  1863. end;
  1864. procedure TElfExeOutput.Load_Start;
  1865. begin
  1866. inherited Load_Start;
  1867. dynsymlist:=TFPObjectList.Create(False);
  1868. CreateGOTSection;
  1869. end;
  1870. procedure TElfExeOutput.Load_DynamicObject(objdata:TObjData;asneeded:boolean);
  1871. var
  1872. i: longint;
  1873. exesym: TExeSymbol;
  1874. objsym: TObjSymbol;
  1875. needed: boolean;
  1876. begin
  1877. Comment(v_debug,'Dynamic object: '+objdata.name);
  1878. needed:=false;
  1879. for i:=0 to UnresolvedExeSymbols.Count-1 do
  1880. begin
  1881. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  1882. if not (exesym.State in [symstate_undefined,symstate_undefweak]) then
  1883. continue;
  1884. objsym:=TObjSymbol(objdata.ObjSymbolList.Find(exesym.name));
  1885. if assigned(objsym) then
  1886. begin
  1887. exesym.State:=symstate_defined;
  1888. exesym.dynindex:=dynsymlist.Add(exesym)+1;
  1889. { The original binding, value and section of external symbol
  1890. must be preserved, therefore resolving directly to .so symbol
  1891. hurts more than it helps. Copy type and size, and store .so
  1892. symbol in objsym.indsymbol for later use. }
  1893. exesym.ObjSymbol.typ:=objsym.typ;
  1894. if objsym.typ<>AT_FUNCTION then
  1895. exesym.ObjSymbol.size:=objsym.size;
  1896. exesym.ObjSymbol.indsymbol:=objsym;
  1897. objsym.ExeSymbol:=exesym;
  1898. needed:=true;
  1899. end;
  1900. end;
  1901. if (needed or (not asneeded)) and
  1902. (neededlist.Find(objdata.name)=nil) then
  1903. neededlist.Add(objdata.name,objdata);
  1904. end;
  1905. procedure TElfExeOutput.Order_Start;
  1906. begin
  1907. inherited Order_Start;
  1908. dynamiclink:=IsSharedLibrary or (dynsymlist.count>0) or
  1909. (
  1910. (UnresolvedExeSymbols.Count>0) and
  1911. not (cs_link_staticflag in current_settings.globalswitches)
  1912. );
  1913. if dynamiclink then
  1914. InitDynlink;
  1915. if dynamiclink or (IndirectObjSymbols.Count>0) then
  1916. CreatePLT;
  1917. end;
  1918. procedure TElfExeOutput.Order_end;
  1919. procedure set_oso_keep(const s:string;out firstsec:TObjSection);
  1920. var
  1921. exesec:TExeSection;
  1922. objsec:TObjSection;
  1923. i:longint;
  1924. sz: aword;
  1925. begin
  1926. firstsec:=nil;
  1927. sz:=0;
  1928. exesec:=TExeSection(ExeSectionList.Find(s));
  1929. if assigned(exesec) then
  1930. begin
  1931. for i:=0 to exesec.ObjSectionList.Count-1 do
  1932. begin
  1933. objsec:=TObjSection(exesec.ObjSectionList[i]);
  1934. { ignore sections used for symbol definition }
  1935. if oso_data in objsec.SecOptions then
  1936. begin
  1937. if firstsec=nil then
  1938. firstsec:=objsec;
  1939. objsec.SecOptions:=[oso_keep];
  1940. inc(sz,objsec.size);
  1941. end;
  1942. end;
  1943. exesec.size:=sz;
  1944. end;
  1945. end;
  1946. var
  1947. dummy: TObjSection;
  1948. begin
  1949. OrderOrphanSections;
  1950. inherited Order_end;
  1951. set_oso_keep('.init',dummy);
  1952. set_oso_keep('.fini',dummy);
  1953. set_oso_keep('.jcr',dummy);
  1954. set_oso_keep('.ctors',dummy);
  1955. set_oso_keep('.dtors',dummy);
  1956. set_oso_keep('.preinit_array',preinitarraysec);
  1957. if assigned(preinitarraysec) and IsSharedLibrary then
  1958. Comment(v_error,'.preinit_array section is not allowed in shared libraries');
  1959. set_oso_keep('.init_array',initarraysec);
  1960. set_oso_keep('.fini_array',finiarraysec);
  1961. set_oso_keep('.eh_frame',dummy);
  1962. { let .dynamic reference other dynamic sections so they aren't marked
  1963. for removal as unused }
  1964. if dynamiclink then
  1965. WriteDynamicTags;
  1966. end;
  1967. procedure TElfExeOutput.OrderOrphanSections;
  1968. var
  1969. i,j:longint;
  1970. objdata:TObjData;
  1971. objsec:TObjSection;
  1972. exesec:TExeSection;
  1973. opts:TObjSectionOptions;
  1974. s:string;
  1975. newsections:TFPHashObjectList;
  1976. allsections:TFPList;
  1977. inserts:array[0..6] of TExeSection;
  1978. idx,inspos:longint;
  1979. begin
  1980. newsections:=TFPHashObjectList.Create(false);
  1981. allsections:=TFPList.Create;
  1982. { copy existing sections }
  1983. for i:=0 to ExeSectionList.Count-1 do
  1984. allsections.add(ExeSectionList[i]);
  1985. inserts[0]:=FindExeSection('.comment');
  1986. inserts[1]:=nil;
  1987. inserts[2]:=FindExeSection('.interp');
  1988. inserts[3]:=FindExeSection('.bss');
  1989. inserts[4]:=FindExeSection('.data');
  1990. inserts[5]:=FindExeSection('.rodata');
  1991. inserts[6]:=FindExeSection('.text');
  1992. for i:=0 to ObjDataList.Count-1 do
  1993. begin
  1994. ObjData:=TObjData(ObjDataList[i]);
  1995. for j:=0 to ObjData.ObjSectionList.Count-1 do
  1996. begin
  1997. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  1998. if objsec.Used then
  1999. continue;
  2000. s:=objsec.name;
  2001. exesec:=TExeSection(newsections.Find(s));
  2002. if assigned(exesec) then
  2003. begin
  2004. exesec.AddObjSection(objsec);
  2005. continue;
  2006. end;
  2007. opts:=objsec.SecOptions*[oso_data,oso_load,oso_write,oso_executable];
  2008. if (objsec.SecOptions*[oso_load,oso_debug]=[]) then
  2009. { non-alloc, after .comment
  2010. GNU ld places .comment between stabs and dwarf debug info }
  2011. inspos:=0
  2012. else if not (oso_load in objsec.SecOptions) then
  2013. inspos:=1 { debugging, skip }
  2014. else if (oso_load in objsec.SecOptions) and
  2015. (TElfObjSection(objsec).shtype=SHT_NOTE) then
  2016. inspos:=2 { after .interp }
  2017. else if (opts=[oso_load,oso_write]) then
  2018. inspos:=3 { after .bss }
  2019. else if (opts=[oso_data,oso_load,oso_write]) then
  2020. inspos:=4 { after .data }
  2021. else if (opts=[oso_data,oso_load]) then
  2022. inspos:=5 { rodata, relocs=??? }
  2023. else if (opts=[oso_data,oso_load,oso_executable]) then
  2024. inspos:=6 { text }
  2025. else
  2026. begin
  2027. Comment(v_debug,'Orphan section '+objsec.fullname+' has attributes that are not handled!');
  2028. continue;
  2029. end;
  2030. if (inserts[inspos]=nil) then
  2031. begin
  2032. Comment(v_debug,'Orphan section '+objsec.fullname+': nowhere to insert, ignored');
  2033. continue;
  2034. end;
  2035. idx:=allsections.IndexOf(inserts[inspos]);
  2036. exesec:=CExeSection.Create(newsections,s);
  2037. allsections.Insert(idx+1,exesec);
  2038. inserts[inspos]:=exesec;
  2039. exesec.AddObjSection(objsec);
  2040. end;
  2041. end;
  2042. { Now replace the ExeSectionList with content of allsections }
  2043. if (newsections.count<>0) then
  2044. ReplaceExeSectionList(allsections);
  2045. newsections.Free;
  2046. allsections.Free;
  2047. end;
  2048. procedure TElfExeOutput.AfterUnusedSectionRemoval;
  2049. var
  2050. i:longint;
  2051. exesym:TExeSymbol;
  2052. objsym:TObjSymbol;
  2053. objsec: TObjSection;
  2054. begin
  2055. { Unused section removal sets Used property of referenced exesymbols.
  2056. Remaining ones can be removed. }
  2057. for i:=0 to dynsymlist.count-1 do
  2058. begin
  2059. exesym:=TExeSymbol(dynsymlist[i]);
  2060. if assigned(exesym.ObjSymbol.ObjSection) then // an exported symbol
  2061. continue;
  2062. if not exesym.used then
  2063. begin
  2064. dynsymlist[i]:=nil;
  2065. exesym.dynindex:=0;
  2066. end;
  2067. end;
  2068. dynsymlist.Pack;
  2069. { reindex }
  2070. for i:=0 to dynsymlist.count-1 do
  2071. TExeSymbol(dynsymlist[i]).dynindex:=i+1;
  2072. { Drop unresolved symbols that aren't referenced, assign dynamic
  2073. indices to remaining ones, but not if linking with -Xt.
  2074. TODO: behavior of .so with -Xt ? }
  2075. if (cs_link_staticflag in current_settings.globalswitches) then
  2076. UnresolvedExeSymbols.Clear
  2077. else
  2078. for i:=0 to UnresolvedExeSymbols.Count-1 do
  2079. begin
  2080. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2081. if exesym.used then
  2082. begin
  2083. if exesym.dynindex<>0 then
  2084. InternalError(2012062301);
  2085. { Weak-referenced symbols are changed into dynamic ones
  2086. only if referenced through GOT or PLT (this is BFD-compatible) }
  2087. if exesym.state<>symstate_undefweak then
  2088. exesym.dynindex:=dynsymlist.add(exesym)+1;
  2089. end
  2090. else
  2091. UnresolvedExeSymbols[i]:=nil;
  2092. end;
  2093. UnresolvedExeSymbols.Pack;
  2094. { Scan relocations to determine size of GOT, dynamic reloc section, etc. }
  2095. PrepareGOT;
  2096. { Write required PLT entries }
  2097. for i:=0 to dynsymlist.Count-1 do
  2098. begin
  2099. exesym:=TExeSymbol(dynsymlist[i]);
  2100. if assigned(exesym.ObjSymbol.objsection) then // an exported symbol
  2101. continue;
  2102. if ((exesym.ObjSymbol.refs and symref_plt)<>0) or
  2103. ((exesym.ObjSymbol.typ in [AT_FUNCTION,AT_GNU_IFUNC]) and (not IsSharedLibrary)) then
  2104. begin
  2105. make_dynamic_if_undefweak(exesym);
  2106. { This symbol has a valid address to which relocations are resolved,
  2107. but it remains (weak)external when written to dynamic symtable. }
  2108. objsym:=internalobjdata.CreateSymbol(exesym.name);
  2109. objsym.typ:=AT_FUNCTION;
  2110. objsym.bind:=exesym.ObjSymbol.bind; { AB_EXTERNAL or AB_WEAK_EXTERNAL }
  2111. objsym.indsymbol:=exesym.ObjSymbol.indsymbol;
  2112. objsym.offset:=pltobjsec.size;
  2113. objsym.objsection:=pltobjsec;
  2114. objsym.exesymbol:=exesym;
  2115. exesym.ObjSymbol:=objsym;
  2116. WritePLTEntry(exesym);
  2117. end
  2118. else if ((exesym.ObjSymbol.refs and symref_from_text)<>0) and
  2119. (exesym.ObjSymbol.typ<>AT_FUNCTION) and (not IsSharedLibrary) and
  2120. (exesym.state<>symstate_undefweak) then
  2121. begin
  2122. if exesym.ObjSymbol.size=0 then
  2123. Comment(v_error,'Dynamic variable '+exesym.name+' has zero size');
  2124. internalobjdata.setSection(dynbssobjsec);
  2125. internalobjdata.allocalign(size_2_align(exesym.ObjSymbol.size));
  2126. objsym:=internalobjdata.SymbolDefine(exesym.name,AB_GLOBAL,AT_DATA);
  2127. objsym.size:=exesym.ObjSymbol.size;
  2128. objsym.indsymbol:=exesym.ObjSymbol.indsymbol;
  2129. exesym.ObjSymbol:=objsym;
  2130. objsym.exesymbol:=exesym;
  2131. dynbssobjsec.alloc(objsym.size);
  2132. { allocate space for R_xx_COPY relocation for this symbol;
  2133. we'll create it later, to be consistent with "-z combreloc" semantics }
  2134. dyncopysyms.add(objsym);
  2135. dynrelocsec.alloc(dynrelocsec.shentsize);
  2136. inc(dynrelsize,dynrelocsec.shentsize);
  2137. end;
  2138. end;
  2139. { Handle indirect symbols }
  2140. for i:=0 to IndirectObjSymbols.Count-1 do
  2141. begin
  2142. objsym:=TObjSymbol(IndirectObjSymbols[i]);
  2143. objsec:=objsym.ExeSymbol.ObjSymbol.objsection;
  2144. objsym.bind:=AB_EXTERNAL; { cheat FixupSymbols }
  2145. if (oso_plt in objsec.SecOptions) then
  2146. continue;
  2147. WriteIndirectPLTEntry(objsym.ExeSymbol);
  2148. end;
  2149. FixupSymbols;
  2150. if dynamiclink then
  2151. begin
  2152. WriteVersionSections;
  2153. WriteDynamicSymbolsHash;
  2154. end;
  2155. { Create .shstrtab section, which is needed in both exe and .dbg files }
  2156. shstrtabsect:=TElfObjSection.Create_ext(internalObjData,'.shstrtab',SHT_STRTAB,0,1,0);
  2157. shstrtabsect.SecOptions:=[oso_debug_copy];
  2158. AttachSection(shstrtabsect);
  2159. { Create the static symtable (.symtab and .strtab) }
  2160. if (cs_link_separate_dbg_file in current_settings.globalswitches) or
  2161. not(cs_link_strip in current_settings.globalswitches) then
  2162. begin
  2163. symtab:=TElfSymtab.Create(internalObjData,esk_exe);
  2164. symtab.SecOptions:=[oso_debug];
  2165. symtab.fstrsec.SecOptions:=[oso_debug];
  2166. AttachSection(symtab);
  2167. AttachSection(symtab.fstrsec);
  2168. end;
  2169. { Re-enable sections which end up to contain some data
  2170. (.got, .rel[a].dyn, .rel[a].plt (includes .rel[a].iplt) and .hash }
  2171. if gotobjsec.size<>0 then
  2172. gotobjsec.ExeSection.Disabled:=false;
  2173. if assigned(dynrelocsec) and
  2174. ((dynrelocsec.size<>0) or (dyncopysyms.count<>0)) then
  2175. dynrelocsec.ExeSection.Disabled:=false;
  2176. if assigned(pltrelocsec) and (pltrelocsec.size>0) then
  2177. pltrelocsec.ExeSection.Disabled:=false;
  2178. if assigned(ipltrelocsec) and (ipltrelocsec.size>0) then
  2179. ipltrelocsec.ExeSection.Disabled:=false;
  2180. if assigned(hashobjsec) then
  2181. hashobjsec.ExeSection.Disabled:=false;
  2182. if assigned(symversec) and (symversec.size<>0) then
  2183. symversec.ExeSection.Disabled:=false;
  2184. if assigned(verneedsec) and (verneedsec.size<>0) then
  2185. verneedsec.ExeSection.Disabled:=false;
  2186. if assigned(verdefsec) and (verdefsec.size<>0) then
  2187. verdefsec.ExeSection.Disabled:=false;
  2188. RemoveDisabledSections;
  2189. MapSectionsToSegments;
  2190. if dynamiclink then
  2191. FinishDynamicTags;
  2192. end;
  2193. procedure TElfExeOutput.WriteShStrtab;
  2194. var
  2195. i: longint;
  2196. exesec: TElfExeSection;
  2197. begin
  2198. { Remove any existing .shstrtab contents }
  2199. if (shstrtabsect.size>0) then
  2200. begin
  2201. shstrtabsect.ReleaseData;
  2202. shstrtabsect.Size:=0;
  2203. shstrtabsect.SecOptions:=[oso_data];
  2204. end;
  2205. shstrtabsect.writezeros(1);
  2206. for i:=0 to ExeSectionList.Count-1 do
  2207. begin
  2208. exesec:=TElfExeSection(ExeSectionList[i]);
  2209. exesec.shstridx:=shstrtabsect.writestr(exesec.Name);
  2210. exesec.secshidx:=i+1;
  2211. end;
  2212. end;
  2213. procedure TElfExeOutput.FixupSectionLinks;
  2214. var
  2215. dynstrndx,dynsymndx: longword;
  2216. begin
  2217. if dynamiclink then
  2218. begin
  2219. dynstrndx:=TElfExeSection(dynsymtable.fstrsec.ExeSection).secshidx;
  2220. dynsymndx:=TElfExeSection(dynsymtable.ExeSection).secshidx;
  2221. TElfExeSection(hashobjsec.ExeSection).shlink:=dynsymndx;
  2222. TElfExeSection(dynamicsec.ExeSection).shlink:=dynstrndx;
  2223. TElfExeSection(dynsymtable.ExeSection).shlink:=dynstrndx;
  2224. if assigned(pltrelocsec) then
  2225. begin
  2226. TElfExeSection(pltrelocsec.ExeSection).shlink:=dynsymndx;
  2227. TElfExeSection(pltrelocsec.ExeSection).shinfo:=TElfExeSection(pltobjsec.ExeSection).secshidx;
  2228. end;
  2229. if assigned(dynrelocsec) and assigned(dynrelocsec.ExeSection) then
  2230. TElfExeSection(dynrelocsec.ExeSection).shlink:=dynsymndx;
  2231. if symversec.size>0 then
  2232. TElfExeSection(symversec.ExeSection).shlink:=dynsymndx;
  2233. if verdefsec.size>0 then
  2234. TElfExeSection(verdefsec.ExeSection).shlink:=dynstrndx;
  2235. if verneedsec.size>0 then
  2236. TElfExeSection(verneedsec.ExeSection).shlink:=dynstrndx;
  2237. end
  2238. else if assigned(ipltrelocsec) then
  2239. TElfExeSection(ipltrelocsec.ExeSection).shinfo:=TElfExeSection(pltobjsec.ExeSection).secshidx;
  2240. end;
  2241. procedure TElfExeOutput.Do_Mempos;
  2242. var
  2243. i,j: longint;
  2244. seg: TElfSegment;
  2245. exesec: TElfExeSection;
  2246. objsec: TObjSection;
  2247. tempmempos: qword;
  2248. begin
  2249. if IsSharedLibrary then
  2250. CurrMemPos:=0
  2251. else
  2252. CurrMemPos:=ElfTarget.exe_image_base;
  2253. textseg.MemPos:=CurrMemPos;
  2254. if assigned(phdrseg) then
  2255. begin
  2256. phdrseg.Mempos:=CurrMemPos+sizeof(TElfHeader);
  2257. phdrseg.Memsize:=sizeof(TElfproghdr)*segmentlist.count;
  2258. end;
  2259. CurrMemPos:=CurrMemPos+sizeof(TElfHeader)+segmentlist.count*sizeof(TElfproghdr);
  2260. MemPos_Segment(textseg);
  2261. CurrMemPos:=Align(CurrMemPos,SectionDataAlign); {! Data,not MemAlign}
  2262. CurrMemPos:=CurrMemPos+ElfTarget.max_page_size;
  2263. dataseg.MemPos:=CurrMemPos;
  2264. MemPos_Segment(dataseg);
  2265. { Mempos of unmapped sections is forced to zero, but we have to set positions
  2266. of its objsections and update sizes }
  2267. for i:=0 to ExeSectionList.Count-1 do
  2268. begin
  2269. exesec:=TElfExeSection(ExeSectionList[i]);
  2270. if not (oso_load in exesec.SecOptions) then
  2271. begin
  2272. tempmempos:=0;
  2273. exesec.MemPos:=tempmempos;
  2274. for j:=0 to exesec.ObjSectionList.Count-1 do
  2275. begin
  2276. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2277. tempmempos:=objsec.setmempos(tempmempos);
  2278. end;
  2279. exesec.Size:=tempmempos;
  2280. end;
  2281. end;
  2282. { Update MemPos and MemSize of non-load segments,
  2283. in particular, TLS sizes are needed to resolve relocations }
  2284. for i:=0 to segmentlist.count-1 do
  2285. begin
  2286. seg:=TElfSegment(segmentlist[i]);
  2287. if (seg.ptype=PT_LOAD) or (seg.FSectionList.Count=0) then
  2288. continue;
  2289. seg.MemPos:=TExeSection(seg.FSectionList.First).MemPos;
  2290. for j:=0 to seg.FSectionList.Count-1 do
  2291. begin
  2292. exesec:=TElfExeSection(seg.FSectionList[j]);
  2293. seg.MemSize:=exesec.MemPos+exesec.Size-seg.MemPos;
  2294. end;
  2295. end;
  2296. end;
  2297. procedure TElfExeOutput.MemPos_Start;
  2298. var
  2299. i: longint;
  2300. begin
  2301. { Assign section indices and fill .shstrtab
  2302. List of sections cannot be modified after this point. }
  2303. WriteShStrtab;
  2304. { fixup sh_link/sh_info members of various dynamic sections }
  2305. FixupSectionLinks;
  2306. { The actual layout }
  2307. Do_Mempos;
  2308. if (not gotwritten) then
  2309. begin
  2310. { Reset size of .got and .rel[a].dyn, they will be refilled while fixing up relocations.
  2311. For .got, consider already written reserved entries. }
  2312. if assigned(gotobjsec) then
  2313. gotobjsec.size:=gotobjsec.data.size;
  2314. if assigned(dynrelocsec) then
  2315. begin
  2316. dynrelocsec.size:=0;
  2317. { write actual .dynsym content (needs valid symbol addresses) }
  2318. if assigned(tlsseg) then
  2319. dynsymtable.tlsbase:=tlsseg.MemPos;
  2320. dynsymtable.size:=sizeof(TElfsymbol);
  2321. for i:=0 to dynsymlist.count-1 do
  2322. dynsymtable.writeSymbol(TExeSymbol(dynsymlist[i]).objsymbol,dynsymnames[i]);
  2323. end;
  2324. end;
  2325. end;
  2326. procedure TElfExeOutput.MemPos_Segment(seg:TElfSegment);
  2327. var
  2328. i: longint;
  2329. exesec: TElfExeSection;
  2330. begin
  2331. for i:=0 to seg.FSectionList.Count-1 do
  2332. begin
  2333. exesec:=TElfExeSection(seg.FSectionList[i]);
  2334. inherited MemPos_ExeSection(exesec);
  2335. { .tbss should not contribute to address space }
  2336. if (exesec.shtype=SHT_NOBITS) and ((exesec.shflags and SHF_TLS)<>0) then
  2337. CurrMemPos:=exesec.MemPos;
  2338. end;
  2339. { calculate size of the segment }
  2340. seg.MemSize:=CurrMemPos-seg.MemPos;
  2341. end;
  2342. procedure TElfExeOutput.MemPos_ExeSection(const aname:string);
  2343. begin
  2344. // Ignore. All layout is done in mempos_start
  2345. end;
  2346. procedure TElfExeOutput.DataPos_Start;
  2347. var
  2348. i,j: longint;
  2349. exesec: TExeSection;
  2350. seg: TElfSegment;
  2351. objreloc: TObjRelocation;
  2352. objsym: TObjSymbol;
  2353. begin
  2354. gotwritten:=true;
  2355. { If target does not support sorted relocations, it is expected to write the
  2356. entire .rel[a].dyn section during FixupRelocations, and leave dynreloclist empty.
  2357. Otherwise, only RELATIVE ones should be written, space for non-relative relocations
  2358. should remain. }
  2359. if assigned(dynrelocsec) then
  2360. begin
  2361. { Append R_xx_COPY relocations }
  2362. for i:=0 to dyncopysyms.count-1 do
  2363. begin
  2364. objsym:=TObjSymbol(dyncopysyms[i]);
  2365. dynreloclist.Add(TObjRelocation.CreateRaw(objsym.address,objsym,ElfTarget.dyn_reloc_codes[dr_copy]));
  2366. end;
  2367. dyncopysyms.Clear;
  2368. if (dynrelocsec.size+(dynreloclist.count*dynrelocsec.shentsize)<>dynrelsize) then
  2369. InternalError(2012110601);
  2370. { Write out non-RELATIVE dynamic relocations
  2371. TODO: additional sorting? }
  2372. for i:=0 to dynreloclist.count-1 do
  2373. begin
  2374. objreloc:=TObjRelocation(dynreloclist[i]);
  2375. WriteDynRelocEntry(objreloc.dataoffset,objreloc.ftype,objreloc.symbol.exesymbol.dynindex,0);
  2376. end;
  2377. end;
  2378. { sanity checks }
  2379. if assigned(gotobjsec) and (gotsize<>gotobjsec.size) then
  2380. InternalError(2012092501);
  2381. if assigned(dynrelocsec) and (dynrelsize<>dynrelocsec.size) then
  2382. InternalError(2012092502);
  2383. if (ExeWriteMode=ewm_dbgonly) or
  2384. (
  2385. (ExeWriteMode=ewm_exefull) and
  2386. not(cs_link_strip in current_settings.globalswitches)
  2387. ) then
  2388. WriteStaticSymtable;
  2389. { first handle primary segments }
  2390. textseg.DataPos:=0;
  2391. CurrDataPos:=sizeof(TElfHeader)+sizeof(TElfproghdr)*segmentlist.count;
  2392. if assigned(phdrseg) then
  2393. begin
  2394. phdrseg.DataPos:=sizeof(TElfHeader);
  2395. phdrseg.DataSize:=sizeof(TElfproghdr)*segmentlist.count;
  2396. end;
  2397. DataPos_Segment(textseg);
  2398. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  2399. dataseg.DataPos:=CurrDataPos;
  2400. DataPos_Segment(dataseg);
  2401. { then unmapped sections }
  2402. for i:=0 to ExeSectionList.Count-1 do
  2403. begin
  2404. exesec:=TExeSection(ExeSectionList[i]);
  2405. if not (oso_load in exesec.SecOptions) then
  2406. inherited DataPos_ExeSection(exesec);
  2407. end;
  2408. { finally, update size/position of non-load segments }
  2409. for i:=0 to segmentlist.count-1 do
  2410. begin
  2411. seg:=TElfSegment(segmentlist[i]);
  2412. if (seg.ptype=PT_LOAD) or (seg.FSectionList.Count=0) then
  2413. continue;
  2414. seg.DataPos:=TExeSection(seg.FSectionList.First).DataPos;
  2415. for j:=0 to seg.FSectionList.Count-1 do
  2416. begin
  2417. exesec:=TExeSection(seg.FSectionList[j]);
  2418. if oso_data in exesec.SecOptions then
  2419. seg.DataSize:=exesec.DataPos+exesec.Size-seg.DataPos;
  2420. end;
  2421. end;
  2422. { place section headers after the data }
  2423. shoffset:=CurrDataPos;
  2424. CurrDataPos:=CurrDataPos+ExeSectionList.Count*sizeof(TElfsechdr);
  2425. end;
  2426. procedure TElfExeOutput.DataPos_Segment(seg:TElfSegment);
  2427. var
  2428. i: longint;
  2429. exesec: TElfExeSection;
  2430. begin
  2431. for i:=0 to seg.FSectionList.Count-1 do
  2432. begin
  2433. exesec:=TElfExeSection(seg.FSectionList[i]);
  2434. { ELF needs DataPos set to 'would-be' value for sections that
  2435. don't have data, and for non-debug sections in .dbg file, too.
  2436. This slightly differs from generic approach. }
  2437. if not (oso_data in exesec.SecOptions) or
  2438. (
  2439. (ExeWriteMode=ewm_dbgonly) and
  2440. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[])
  2441. ) then
  2442. begin
  2443. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  2444. exesec.DataPos:=CurrDataPos;
  2445. end
  2446. else
  2447. inherited DataPos_ExeSection(exesec);
  2448. end;
  2449. { calculate size of the segment }
  2450. seg.DataSize:=CurrDataPos-seg.DataPos;
  2451. end;
  2452. procedure TElfExeOutput.DataPos_ExeSection(const aname:string);
  2453. begin
  2454. // Ignore. Work is done entirely in datapos_start.
  2455. end;
  2456. procedure TElfExeOutput.InitDynlink;
  2457. begin
  2458. if not IsSharedLibrary then
  2459. begin
  2460. interpobjsec:=internalObjData.createsection('.interp',1,[oso_data,oso_load,oso_keep]);
  2461. interpobjsec.writestr(interpreter^);
  2462. end;
  2463. hashobjsec:=TElfObjSection.create_ext(internalObjData,'.hash',
  2464. SHT_HASH,SHF_ALLOC,sizeof(pint),4);
  2465. hashobjsec.secoptions:=[oso_keep];
  2466. dynsymtable:=TElfSymtab.create(internalObjData,esk_dyn);
  2467. dynamicsec:=TElfObjSection.create_ext(internalObjData,'.dynamic',
  2468. SHT_DYNAMIC,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(TElfDyn));
  2469. dynamicsec.SecOptions:=[oso_keep];
  2470. dynrelocsec:=TElfObjSection.create_reloc(internalObjData,'.dyn',true);
  2471. dynrelocsec.SecOptions:=[oso_keep];
  2472. dynbssobjsec:=TElfObjSection.create_ext(internalObjData,'.dynbss',
  2473. SHT_NOBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint){16??},0);
  2474. dynbssobjsec.SecOptions:=[oso_keep];
  2475. dynreloclist:=TFPObjectList.Create(true);
  2476. symversec:=TElfObjSection.create_ext(internalObjData,'.gnu.version',
  2477. SHT_GNU_VERSYM,SHF_ALLOC,sizeof(word),sizeof(word));
  2478. symversec.SecOptions:=[oso_keep];
  2479. verdefsec:=TElfObjSection.create_ext(internalObjData,'.gnu.version_d',
  2480. SHT_GNU_VERDEF,SHF_ALLOC,sizeof(pint),0);
  2481. verdefsec.SecOptions:=[oso_keep];
  2482. verneedsec:=TElfObjSection.create_ext(internalObjData,'.gnu.version_r',
  2483. SHT_GNU_VERNEED,SHF_ALLOC,sizeof(pint),0);
  2484. verneedsec.SecOptions:=[oso_keep];
  2485. dyncopysyms:=TFPObjectList.Create(False);
  2486. end;
  2487. const
  2488. hashbuckets: array[0..15] of longint=(
  2489. 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
  2490. 16411, 32771);
  2491. {$push}{$r-,q-}
  2492. function elfhash(const name:string):longword;
  2493. var
  2494. g: longword;
  2495. i: longint;
  2496. begin
  2497. result:=0;
  2498. for i:=1 to length(name) do
  2499. begin
  2500. result:=(result shl 4)+ord(name[i]);
  2501. g:=result and $F0000000;
  2502. if g>0 then
  2503. result:=result xor (g shr 24);
  2504. result:=result and (not g);
  2505. end;
  2506. end;
  2507. {$pop}
  2508. procedure TElfExeOutput.WriteDynamicSymbolsHash;
  2509. var
  2510. nchains,nbuckets: longint;
  2511. i,j: longint;
  2512. hashdata: plongint;
  2513. sym: TExeSymbol;
  2514. begin
  2515. dynsymnames:=AllocMem(dynsymlist.count*sizeof(longword));
  2516. nchains:=dynsymlist.Count+1;
  2517. { determine suitable bucket count }
  2518. i:=high(hashbuckets);
  2519. while (i>=0) and (nchains<hashbuckets[i]) do
  2520. dec(i);
  2521. nbuckets:=hashbuckets[i];
  2522. hashdata:=AllocMem((2+nchains+nbuckets)*sizeof(longint));
  2523. hashdata[0]:=nbuckets;
  2524. hashdata[1]:=nchains;
  2525. { The contents of .dynsym can be written only after mempos pass
  2526. because it needs valid symbol virtual addresses and section indices.
  2527. Here we preset .dynsym size and write names, in order to get
  2528. correct size of .dynstr section. }
  2529. dynsymtable.size:=(dynsymlist.count+1)*sizeof(TElfsymbol);
  2530. for i:=0 to dynsymlist.Count-1 do
  2531. begin
  2532. sym:=TExeSymbol(dynsymlist[i]);
  2533. dynsymnames[i]:=dynsymtable.fstrsec.writestr(sym.objsymbol.name);
  2534. j:=(elfhash(sym.objsymbol.name) mod nbuckets)+2;
  2535. while hashdata[j]<>0 do
  2536. j:=2+nbuckets+hashdata[j];
  2537. hashdata[j]:=i+1;
  2538. end;
  2539. if source_info.endian<>target_info.endian then
  2540. for i:=0 to nchains+nbuckets+1 do
  2541. hashdata[i]:=swapendian(hashdata[i]);
  2542. hashobjsec.write(hashdata^,(2+nchains+nbuckets)*sizeof(longint));
  2543. freemem(hashdata);
  2544. end;
  2545. procedure TElfExeOutput.WriteVersionSections;
  2546. var
  2547. i,j: longint;
  2548. idx,auxidx: longword;
  2549. exesym: TExeSymbol;
  2550. dynobj: TElfDynamicObjData;
  2551. ver: TElfVersionDef;
  2552. vn: TElfverneed;
  2553. vna: TElfvernaux;
  2554. symversions: pword;
  2555. begin
  2556. symversions:=AllocMem((dynsymlist.count+1)*sizeof(word));
  2557. { Assign version indices }
  2558. idx:=VER_NDX_GLOBAL+1;
  2559. for i:=0 to dynsymlist.count-1 do
  2560. begin
  2561. exesym:=TExeSymbol(dynsymlist[i]);
  2562. if (exesym.objsymbol.indsymbol is TVersionedObjSymbol) then
  2563. ver:=TVersionedObjSymbol(exesym.objsymbol.indsymbol).version
  2564. else
  2565. ver:=nil;
  2566. if assigned(ver) then
  2567. begin
  2568. if ver.index=0 then
  2569. begin
  2570. ver.index:=idx;
  2571. inc(idx);
  2572. end;
  2573. symversions[i+1]:=ver.index;
  2574. end
  2575. else if exesym.state in [symstate_undefined,symstate_undefweak] then
  2576. symversions[i+1]:=VER_NDX_LOCAL
  2577. else
  2578. symversions[i+1]:=VER_NDX_GLOBAL;
  2579. end;
  2580. { Count entries to be written }
  2581. verneedcount:=0;
  2582. for i:=0 to neededlist.count-1 do
  2583. begin
  2584. dynobj:=TElfDynamicObjData(neededlist[i]);
  2585. dynobj.vernaux_count:=0;
  2586. for j:=2 to dynobj.versiondefs.count-1 do
  2587. begin
  2588. ver:=TElfVersionDef(dynobj.versiondefs[j]);
  2589. if ver.index>VER_NDX_GLOBAL then
  2590. inc(dynobj.vernaux_count);
  2591. end;
  2592. if (dynobj.vernaux_count>0) then
  2593. inc(verneedcount);
  2594. end;
  2595. { Now write }
  2596. idx:=0;
  2597. for i:=0 to neededlist.count-1 do
  2598. begin
  2599. dynobj:=TElfDynamicObjData(neededlist[i]);
  2600. if dynobj.vernaux_count=0 then
  2601. continue;
  2602. inc(idx);
  2603. vn.vn_version:=VER_NEED_CURRENT;
  2604. vn.vn_cnt:=dynobj.vernaux_count;
  2605. vn.vn_file:=dynobj.soname_strofs;
  2606. vn.vn_aux:=sizeof(TElfverneed);
  2607. vn.vn_next:=ord(idx<verneedcount)*(sizeof(TElfverneed)+vn.vn_cnt*sizeof(TElfvernaux));
  2608. MaybeSwapElfverneed(vn);
  2609. verneedsec.write(vn,sizeof(TElfverneed));
  2610. auxidx:=0;
  2611. for j:=2 to dynobj.versiondefs.count-1 do
  2612. begin
  2613. ver:=TElfVersionDef(dynobj.versiondefs[j]);
  2614. if ver.index<=VER_NDX_GLOBAL then
  2615. continue;
  2616. inc(auxidx);
  2617. vna.vna_hash:=elfhash(ver.name);
  2618. vna.vna_flags:=0; { BFD copies this from verdef.vd_flags?? }
  2619. vna.vna_other:=ver.index;
  2620. vna.vna_name:=dynsymtable.fstrsec.writestr(ver.name);
  2621. vna.vna_next:=ord(auxidx<dynobj.vernaux_count)*sizeof(TElfvernaux);
  2622. MaybeSwapElfvernaux(vna);
  2623. verneedsec.write(vna,sizeof(TElfvernaux));
  2624. end;
  2625. end;
  2626. TElfExeSection(verneedsec.ExeSection).shinfo:=verneedcount;
  2627. { If there are no needed versions, .gnu.version section is not needed }
  2628. if verneedcount>0 then
  2629. begin
  2630. if source_info.endian<>target_info.endian then
  2631. for i:=0 to dynsymlist.count+1 do
  2632. symversions[i]:=swapendian(symversions[i]);
  2633. symversec.write(symversions^,(dynsymlist.count+1)*sizeof(word));
  2634. end;
  2635. FreeMem(symversions);
  2636. end;
  2637. procedure TElfExeOutput.WriteDynRelocEntry(dataofs:aword;typ:byte;symidx:aword;addend:aword);
  2638. var
  2639. rel:telfreloc;
  2640. begin
  2641. rel.address:=dataofs;
  2642. rel.info:=ELF_R_INFO(symidx,typ);
  2643. {$push}{$r-}
  2644. rel.addend:=addend;
  2645. {$pop}
  2646. MaybeSwapElfReloc(rel);
  2647. dynrelocsec.write(rel,dynrelocsec.shentsize);
  2648. end;
  2649. procedure TElfExeOutput.WriteDynTag(aTag:longword;aValue:longword);
  2650. var
  2651. d: TElfDyn;
  2652. begin
  2653. d.d_tag:=aTag;
  2654. d.d_val:=aValue;
  2655. MaybeSwapElfDyn(d);
  2656. dynamicsec.write(d,sizeof(TElfDyn));
  2657. end;
  2658. procedure TElfExeOutput.WriteDynTag(aTag:longword;aSection:TObjSection;aOffs:aword);
  2659. var
  2660. d: TElfDyn;
  2661. begin
  2662. d.d_tag:=aTag;
  2663. if source_info.endian<>target_info.endian then
  2664. d.d_tag:=swapendian(d.d_tag);
  2665. dynamicsec.write(d.d_tag,sizeof(d.d_tag));
  2666. { TODO: ignores endianness! }
  2667. dynamicsec.writeReloc_internal(aSection,aOffs,sizeof(d.d_ptr),RELOC_ABSOLUTE);
  2668. end;
  2669. procedure TElfExeOutput.WriteTargetDynamicTags;
  2670. begin
  2671. { to be overridden by CPU-specific descendants }
  2672. end;
  2673. procedure TElfExeOutput.WriteDynamicTags;
  2674. var
  2675. s: aword;
  2676. i: longint;
  2677. sym: TExeSymbol;
  2678. hs:string;
  2679. dynobj: TElfDynamicObjData;
  2680. begin
  2681. for i:=0 to neededlist.Count-1 do
  2682. begin
  2683. dynobj:=TElfDynamicObjData(neededlist[i]);
  2684. s:=dynsymtable.fstrsec.writestr(dynobj.name);
  2685. dynobj.soname_strofs:=s;
  2686. WriteDynTag(DT_NEEDED,s);
  2687. end;
  2688. if IsSharedLibrary then
  2689. begin
  2690. s:=dynsymtable.fstrsec.writestr(ExtractFileName(current_module.sharedlibfilename));
  2691. WriteDynTag(DT_SONAME,s);
  2692. { TODO: names hardcoded here }
  2693. sym:=TExeSymbol(ExeSymbolList.Find('FPC_SHARED_LIB_START'));
  2694. if assigned(sym) then
  2695. WriteDynTag(DT_INIT,sym.objsymbol.objsection,sym.objsymbol.offset);
  2696. sym:=TExeSymbol(ExeSymbolList.Find('FPC_LIB_EXIT'));
  2697. if assigned(sym) then
  2698. WriteDynTag(DT_FINI,sym.objsymbol.objsection,sym.objsymbol.offset);
  2699. end;
  2700. { TODO: we need a dedicated parameter to pass runpath, instead of this hack
  2701. (-Xr is a different thing, it passes "-rpath-link"). }
  2702. if (ParaLinkOptions<>'') then
  2703. begin
  2704. hs:=ParaLinkOptions;
  2705. while (hs<>'') do
  2706. begin
  2707. if (GetToken(hs,' ')='-rpath') then
  2708. begin
  2709. s:=dynsymtable.fstrsec.writestr(GetToken(hs,' '));
  2710. WriteDynTag(DT_RPATH,s);
  2711. end;
  2712. end;
  2713. end;
  2714. if assigned(preinitarraysec) then
  2715. begin
  2716. WriteDynTag(DT_PREINIT_ARRAY,preinitarraysec,0);
  2717. WriteDynTag(DT_PREINIT_ARRAYSZ,preinitarraysec.exesection.size);
  2718. end;
  2719. if assigned(initarraysec) then
  2720. begin
  2721. WriteDynTag(DT_INIT_ARRAY,initarraysec,0);
  2722. WriteDynTag(DT_INIT_ARRAYSZ,initarraysec.exesection.size);
  2723. end;
  2724. if assigned(finiarraysec) then
  2725. begin
  2726. WriteDynTag(DT_FINI_ARRAY,finiarraysec,0);
  2727. WriteDynTag(DT_FINI_ARRAYSZ,finiarraysec.exesection.size);
  2728. end;
  2729. writeDynTag(DT_HASH,hashobjsec);
  2730. writeDynTag(DT_STRTAB,dynsymtable.fstrsec);
  2731. writeDynTag(DT_SYMTAB,dynsymtable);
  2732. writeDynTag(DT_SYMENT,sizeof(TElfSymbol));
  2733. if Assigned(gotpltobjsec) then
  2734. writeDynTag(DT_PLTGOT,gotpltobjsec);
  2735. end;
  2736. const
  2737. pltreltags: array[boolean] of longword=(DT_REL,DT_RELA);
  2738. relsztags: array[boolean] of longword=(DT_RELSZ,DT_RELASZ);
  2739. relenttags: array[boolean] of longword=(DT_RELENT,DT_RELAENT);
  2740. {$ifndef MIPS}
  2741. relcnttags: array[boolean] of longword=(DT_RELCOUNT,DT_RELACOUNT);
  2742. {$endif MIPS}
  2743. procedure TElfExeOutput.FinishDynamicTags;
  2744. var
  2745. rela: boolean;
  2746. begin
  2747. if assigned(dynsymtable) then
  2748. writeDynTag(DT_STRSZ,dynsymtable.fstrsec.size);
  2749. if hastextrelocs then
  2750. writeDynTag(DT_TEXTREL,0);
  2751. if Assigned(pltrelocsec) and (pltrelocsec.size>0) then
  2752. begin
  2753. writeDynTag(DT_PLTRELSZ,pltrelocsec.Size);
  2754. writeDynTag(DT_PLTREL,pltreltags[pltrelocsec.shtype=SHT_RELA]);
  2755. writeDynTag(DT_JMPREL,pltrelocsec);
  2756. end;
  2757. if Assigned(dynrelocsec) and (dynrelocsec.size>0) then
  2758. begin
  2759. rela:=(dynrelocsec.shtype=SHT_RELA);
  2760. writeDynTag(pltreltags[rela],dynrelocsec);
  2761. writeDynTag(relsztags[rela],dynrelocsec.Size);
  2762. writeDynTag(relenttags[rela],dynrelocsec.shentsize);
  2763. {$ifndef MIPS}
  2764. if (relative_reloc_count>0) then
  2765. writeDynTag(relcnttags[rela],relative_reloc_count);
  2766. {$endif MIPS}
  2767. end;
  2768. WriteTargetDynamicTags;
  2769. if (verdefcount>0) or (verneedcount>0) then
  2770. begin
  2771. if (verdefcount>0) then
  2772. begin
  2773. writeDynTag(DT_VERDEF,verdefsec);
  2774. writeDynTag(DT_VERDEFNUM,verdefcount);
  2775. end;
  2776. if (verneedcount>0) then
  2777. begin
  2778. writeDynTag(DT_VERNEED,verneedsec);
  2779. writeDynTag(DT_VERNEEDNUM,verneedcount);
  2780. end;
  2781. writeDynTag(DT_VERSYM,symversec);
  2782. end;
  2783. writeDynTag(DT_NULL,0);
  2784. end;
  2785. procedure TElfExeOutput.CreatePLT;
  2786. var
  2787. reloc: TObjRelocation;
  2788. begin
  2789. pltobjsec:=TElfObjSection.create_ext(internalObjData,'.plt',
  2790. SHT_PROGBITS,SHF_ALLOC or SHF_EXECINSTR,4,16);
  2791. pltobjsec.SecOptions:=[oso_keep,oso_plt];
  2792. pltrelocsec:=TElfObjSection.create_reloc(internalObjData,'.plt',true);
  2793. pltrelocsec.SecOptions:=[oso_keep];
  2794. ipltrelocsec:=TElfObjSection.create_reloc(internalObjData,'.iplt',true);
  2795. ipltrelocsec.SecOptions:=[oso_keep];
  2796. { reference .dynamic from .got.plt, this isn't necessary if linking statically }
  2797. { TODO: maybe move writing initial .got.plt entries here completely
  2798. (needs testing --- GOT symbol may get lost if .got.plt is empty)}
  2799. if dynamiclink then
  2800. begin
  2801. reloc:=TObjRelocation.CreateSection(0,dynamicsec,RELOC_ABSOLUTE);
  2802. reloc.size:=sizeof(pint);
  2803. gotpltobjsec.ObjRelocations.Add(reloc);
  2804. end;
  2805. { Initial PLT entry, CPU-specific }
  2806. WriteFirstPLTEntry;
  2807. end;
  2808. procedure TElfExeOutput.WritePLTEntry(exesym:TExeSymbol);
  2809. begin
  2810. // must be implemented by CPU-specific descendant
  2811. InternalError(2012092102);
  2812. end;
  2813. procedure TElfExeOutput.WriteIndirectPLTEntry(exesym:TExeSymbol);
  2814. begin
  2815. // must be implemented by CPU-specific descendant
  2816. InternalError(2012092101);
  2817. end;
  2818. function TElfExeOutput.WriteData:boolean;
  2819. begin
  2820. WriteHeader;
  2821. segmentlist.ForEachCall(@segment_write_header,nil);
  2822. WriteExeSectionContent;
  2823. FWriter.WriteZeros(sizeof(TElfsechdr));
  2824. ExeSectionList.ForEachCall(@exesection_write_header,nil);
  2825. result:=true;
  2826. end;
  2827. procedure TElfExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2828. var
  2829. exportlist: TCmdStrList;
  2830. sym: TExeSymbol;
  2831. begin
  2832. AllowUndefinedSymbols:=IsSharedLibrary;
  2833. { add exported symbols to dynamic list }
  2834. exportlist:=texportlibunix(exportlib).exportedsymnames;
  2835. if not exportlist.empty then
  2836. repeat
  2837. sym:=TExeSymbol(ExeSymbolList.Find(exportlist.getfirst));
  2838. if assigned(sym) then
  2839. begin
  2840. if assigned(sym.objsymbol.objsection) then
  2841. sym.objsymbol.objsection.SecOptions:=[oso_keep];
  2842. sym.dynindex:=dynsymlist.add(sym)+1
  2843. end
  2844. else
  2845. InternalError(2012071801);
  2846. until exportlist.empty;
  2847. end;
  2848. procedure TElfExeOutput.ReportNonDSOReloc(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  2849. begin
  2850. { TODO: include objsec properties into message }
  2851. Comment(v_error,'Relocation '+ElfTarget.RelocName(reltyp)+' against '''+objreloc.TargetName+''' cannot be used when linking a shared object; recompile with -Cg');
  2852. end;
  2853. procedure TElfExeOutput.ReportRelocOverflow(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  2854. begin
  2855. { TODO: include objsec properties into message }
  2856. Comment(v_error,'Relocation truncated to fit: '+ElfTarget.RelocName(reltyp)+' against '''+objreloc.TargetName+'''');
  2857. end;
  2858. {****************************************************************************
  2859. TElfExeSection
  2860. ****************************************************************************}
  2861. procedure TElfExeSection.AddObjSection(objsec:TObjSection;ignoreprops:boolean);
  2862. begin
  2863. inherited AddObjSection(objsec,ignoreprops);
  2864. if ignoreprops then
  2865. exit;
  2866. if (shtype=SHT_NULL) then
  2867. begin
  2868. shtype:=TElfObjSection(objsec).shtype;
  2869. shflags:=TElfObjSection(objsec).shflags;
  2870. shentsize:=TElfObjSection(objsec).shentsize;
  2871. end;
  2872. end;
  2873. {****************************************************************************
  2874. TElfSegment
  2875. ****************************************************************************}
  2876. constructor TElfSegment.Create(atype,aflags,aalign:longword);
  2877. begin
  2878. ptype:=atype;
  2879. pflags:=aflags;
  2880. align:=aalign;
  2881. FSectionList:=TFPObjectList.Create(false);
  2882. end;
  2883. destructor TElfSegment.Destroy;
  2884. begin
  2885. FSectionList.Free;
  2886. inherited Destroy;
  2887. end;
  2888. procedure TElfSegment.Add(exesec:TExeSection);
  2889. begin
  2890. FSectionList.Add(exesec);
  2891. end;
  2892. end.