ogelf.pas 118 KB

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