ogelf.pas 119 KB

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