ogelf.pas 119 KB

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