elf.c 381 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546
  1. /* ELF executable support for BFD.
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  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 3 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., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. /*
  17. SECTION
  18. ELF backends
  19. BFD support for ELF formats is being worked on.
  20. Currently, the best supported back ends are for sparc and i386
  21. (running svr4 or Solaris 2).
  22. Documentation of the internals of the support code still needs
  23. to be written. The code is changing quickly enough that we
  24. haven't bothered yet. */
  25. /* For sparc64-cross-sparc32. */
  26. #define _SYSCALL32
  27. #include "sysdep.h"
  28. #include <limits.h>
  29. #include "bfd.h"
  30. #include "bfdlink.h"
  31. #include "libbfd.h"
  32. #define ARCH_SIZE 0
  33. #include "elf-bfd.h"
  34. #include "libiberty.h"
  35. #include "safe-ctype.h"
  36. #include "elf-linux-core.h"
  37. #ifdef CORE_HEADER
  38. #include CORE_HEADER
  39. #endif
  40. static int elf_sort_sections (const void *, const void *);
  41. static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
  42. static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
  43. struct bfd_link_info *);
  44. static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
  45. file_ptr offset, size_t align);
  46. /* Swap version information in and out. The version information is
  47. currently size independent. If that ever changes, this code will
  48. need to move into elfcode.h. */
  49. /* Swap in a Verdef structure. */
  50. void
  51. _bfd_elf_swap_verdef_in (bfd *abfd,
  52. const Elf_External_Verdef *src,
  53. Elf_Internal_Verdef *dst)
  54. {
  55. dst->vd_version = H_GET_16 (abfd, src->vd_version);
  56. dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
  57. dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
  58. dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
  59. dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
  60. dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
  61. dst->vd_next = H_GET_32 (abfd, src->vd_next);
  62. }
  63. /* Swap out a Verdef structure. */
  64. void
  65. _bfd_elf_swap_verdef_out (bfd *abfd,
  66. const Elf_Internal_Verdef *src,
  67. Elf_External_Verdef *dst)
  68. {
  69. H_PUT_16 (abfd, src->vd_version, dst->vd_version);
  70. H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
  71. H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
  72. H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
  73. H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
  74. H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
  75. H_PUT_32 (abfd, src->vd_next, dst->vd_next);
  76. }
  77. /* Swap in a Verdaux structure. */
  78. void
  79. _bfd_elf_swap_verdaux_in (bfd *abfd,
  80. const Elf_External_Verdaux *src,
  81. Elf_Internal_Verdaux *dst)
  82. {
  83. dst->vda_name = H_GET_32 (abfd, src->vda_name);
  84. dst->vda_next = H_GET_32 (abfd, src->vda_next);
  85. }
  86. /* Swap out a Verdaux structure. */
  87. void
  88. _bfd_elf_swap_verdaux_out (bfd *abfd,
  89. const Elf_Internal_Verdaux *src,
  90. Elf_External_Verdaux *dst)
  91. {
  92. H_PUT_32 (abfd, src->vda_name, dst->vda_name);
  93. H_PUT_32 (abfd, src->vda_next, dst->vda_next);
  94. }
  95. /* Swap in a Verneed structure. */
  96. void
  97. _bfd_elf_swap_verneed_in (bfd *abfd,
  98. const Elf_External_Verneed *src,
  99. Elf_Internal_Verneed *dst)
  100. {
  101. dst->vn_version = H_GET_16 (abfd, src->vn_version);
  102. dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
  103. dst->vn_file = H_GET_32 (abfd, src->vn_file);
  104. dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
  105. dst->vn_next = H_GET_32 (abfd, src->vn_next);
  106. }
  107. /* Swap out a Verneed structure. */
  108. void
  109. _bfd_elf_swap_verneed_out (bfd *abfd,
  110. const Elf_Internal_Verneed *src,
  111. Elf_External_Verneed *dst)
  112. {
  113. H_PUT_16 (abfd, src->vn_version, dst->vn_version);
  114. H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
  115. H_PUT_32 (abfd, src->vn_file, dst->vn_file);
  116. H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
  117. H_PUT_32 (abfd, src->vn_next, dst->vn_next);
  118. }
  119. /* Swap in a Vernaux structure. */
  120. void
  121. _bfd_elf_swap_vernaux_in (bfd *abfd,
  122. const Elf_External_Vernaux *src,
  123. Elf_Internal_Vernaux *dst)
  124. {
  125. dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
  126. dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
  127. dst->vna_other = H_GET_16 (abfd, src->vna_other);
  128. dst->vna_name = H_GET_32 (abfd, src->vna_name);
  129. dst->vna_next = H_GET_32 (abfd, src->vna_next);
  130. }
  131. /* Swap out a Vernaux structure. */
  132. void
  133. _bfd_elf_swap_vernaux_out (bfd *abfd,
  134. const Elf_Internal_Vernaux *src,
  135. Elf_External_Vernaux *dst)
  136. {
  137. H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
  138. H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
  139. H_PUT_16 (abfd, src->vna_other, dst->vna_other);
  140. H_PUT_32 (abfd, src->vna_name, dst->vna_name);
  141. H_PUT_32 (abfd, src->vna_next, dst->vna_next);
  142. }
  143. /* Swap in a Versym structure. */
  144. void
  145. _bfd_elf_swap_versym_in (bfd *abfd,
  146. const Elf_External_Versym *src,
  147. Elf_Internal_Versym *dst)
  148. {
  149. dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
  150. }
  151. /* Swap out a Versym structure. */
  152. void
  153. _bfd_elf_swap_versym_out (bfd *abfd,
  154. const Elf_Internal_Versym *src,
  155. Elf_External_Versym *dst)
  156. {
  157. H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
  158. }
  159. /* Standard ELF hash function. Do not change this function; you will
  160. cause invalid hash tables to be generated. */
  161. unsigned long
  162. bfd_elf_hash (const char *namearg)
  163. {
  164. const unsigned char *name = (const unsigned char *) namearg;
  165. unsigned long h = 0;
  166. unsigned long g;
  167. int ch;
  168. while ((ch = *name++) != '\0')
  169. {
  170. h = (h << 4) + ch;
  171. if ((g = (h & 0xf0000000)) != 0)
  172. {
  173. h ^= g >> 24;
  174. /* The ELF ABI says `h &= ~g', but this is equivalent in
  175. this case and on some machines one insn instead of two. */
  176. h ^= g;
  177. }
  178. }
  179. return h & 0xffffffff;
  180. }
  181. /* DT_GNU_HASH hash function. Do not change this function; you will
  182. cause invalid hash tables to be generated. */
  183. unsigned long
  184. bfd_elf_gnu_hash (const char *namearg)
  185. {
  186. const unsigned char *name = (const unsigned char *) namearg;
  187. unsigned long h = 5381;
  188. unsigned char ch;
  189. while ((ch = *name++) != '\0')
  190. h = (h << 5) + h + ch;
  191. return h & 0xffffffff;
  192. }
  193. /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
  194. the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
  195. bool
  196. bfd_elf_allocate_object (bfd *abfd,
  197. size_t object_size,
  198. enum elf_target_id object_id)
  199. {
  200. BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
  201. abfd->tdata.any = bfd_zalloc (abfd, object_size);
  202. if (abfd->tdata.any == NULL)
  203. return false;
  204. elf_object_id (abfd) = object_id;
  205. if (abfd->direction != read_direction)
  206. {
  207. struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
  208. if (o == NULL)
  209. return false;
  210. elf_tdata (abfd)->o = o;
  211. elf_program_header_size (abfd) = (bfd_size_type) -1;
  212. }
  213. return true;
  214. }
  215. bool
  216. bfd_elf_make_object (bfd *abfd)
  217. {
  218. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  219. return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
  220. bed->target_id);
  221. }
  222. bool
  223. bfd_elf_mkcorefile (bfd *abfd)
  224. {
  225. /* I think this can be done just like an object file. */
  226. if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
  227. return false;
  228. elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
  229. return elf_tdata (abfd)->core != NULL;
  230. }
  231. char *
  232. bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
  233. {
  234. Elf_Internal_Shdr **i_shdrp;
  235. bfd_byte *shstrtab = NULL;
  236. file_ptr offset;
  237. bfd_size_type shstrtabsize;
  238. i_shdrp = elf_elfsections (abfd);
  239. if (i_shdrp == 0
  240. || shindex >= elf_numsections (abfd)
  241. || i_shdrp[shindex] == 0)
  242. return NULL;
  243. shstrtab = i_shdrp[shindex]->contents;
  244. if (shstrtab == NULL)
  245. {
  246. /* No cached one, attempt to read, and cache what we read. */
  247. offset = i_shdrp[shindex]->sh_offset;
  248. shstrtabsize = i_shdrp[shindex]->sh_size;
  249. /* Allocate and clear an extra byte at the end, to prevent crashes
  250. in case the string table is not terminated. */
  251. if (shstrtabsize + 1 <= 1
  252. || bfd_seek (abfd, offset, SEEK_SET) != 0
  253. || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
  254. shstrtabsize)) == NULL)
  255. {
  256. /* Once we've failed to read it, make sure we don't keep
  257. trying. Otherwise, we'll keep allocating space for
  258. the string table over and over. */
  259. i_shdrp[shindex]->sh_size = 0;
  260. }
  261. else
  262. shstrtab[shstrtabsize] = '\0';
  263. i_shdrp[shindex]->contents = shstrtab;
  264. }
  265. return (char *) shstrtab;
  266. }
  267. char *
  268. bfd_elf_string_from_elf_section (bfd *abfd,
  269. unsigned int shindex,
  270. unsigned int strindex)
  271. {
  272. Elf_Internal_Shdr *hdr;
  273. if (strindex == 0)
  274. return "";
  275. if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
  276. return NULL;
  277. hdr = elf_elfsections (abfd)[shindex];
  278. if (hdr->contents == NULL)
  279. {
  280. if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
  281. {
  282. /* PR 17512: file: f057ec89. */
  283. /* xgettext:c-format */
  284. _bfd_error_handler (_("%pB: attempt to load strings from"
  285. " a non-string section (number %d)"),
  286. abfd, shindex);
  287. return NULL;
  288. }
  289. if (bfd_elf_get_str_section (abfd, shindex) == NULL)
  290. return NULL;
  291. }
  292. else
  293. {
  294. /* PR 24273: The string section's contents may have already
  295. been loaded elsewhere, eg because a corrupt file has the
  296. string section index in the ELF header pointing at a group
  297. section. So be paranoid, and test that the last byte of
  298. the section is zero. */
  299. if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
  300. return NULL;
  301. }
  302. if (strindex >= hdr->sh_size)
  303. {
  304. unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
  305. _bfd_error_handler
  306. /* xgettext:c-format */
  307. (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
  308. abfd, strindex, (uint64_t) hdr->sh_size,
  309. (shindex == shstrndx && strindex == hdr->sh_name
  310. ? ".shstrtab"
  311. : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
  312. return NULL;
  313. }
  314. return ((char *) hdr->contents) + strindex;
  315. }
  316. /* Read and convert symbols to internal format.
  317. SYMCOUNT specifies the number of symbols to read, starting from
  318. symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
  319. are non-NULL, they are used to store the internal symbols, external
  320. symbols, and symbol section index extensions, respectively.
  321. Returns a pointer to the internal symbol buffer (malloced if necessary)
  322. or NULL if there were no symbols or some kind of problem. */
  323. Elf_Internal_Sym *
  324. bfd_elf_get_elf_syms (bfd *ibfd,
  325. Elf_Internal_Shdr *symtab_hdr,
  326. size_t symcount,
  327. size_t symoffset,
  328. Elf_Internal_Sym *intsym_buf,
  329. void *extsym_buf,
  330. Elf_External_Sym_Shndx *extshndx_buf)
  331. {
  332. Elf_Internal_Shdr *shndx_hdr;
  333. void *alloc_ext;
  334. const bfd_byte *esym;
  335. Elf_External_Sym_Shndx *alloc_extshndx;
  336. Elf_External_Sym_Shndx *shndx;
  337. Elf_Internal_Sym *alloc_intsym;
  338. Elf_Internal_Sym *isym;
  339. Elf_Internal_Sym *isymend;
  340. const struct elf_backend_data *bed;
  341. size_t extsym_size;
  342. size_t amt;
  343. file_ptr pos;
  344. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  345. abort ();
  346. if (symcount == 0)
  347. return intsym_buf;
  348. /* Normal syms might have section extension entries. */
  349. shndx_hdr = NULL;
  350. if (elf_symtab_shndx_list (ibfd) != NULL)
  351. {
  352. elf_section_list * entry;
  353. Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
  354. /* Find an index section that is linked to this symtab section. */
  355. for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
  356. {
  357. /* PR 20063. */
  358. if (entry->hdr.sh_link >= elf_numsections (ibfd))
  359. continue;
  360. if (sections[entry->hdr.sh_link] == symtab_hdr)
  361. {
  362. shndx_hdr = & entry->hdr;
  363. break;
  364. };
  365. }
  366. if (shndx_hdr == NULL)
  367. {
  368. if (symtab_hdr == & elf_symtab_hdr (ibfd))
  369. /* Not really accurate, but this was how the old code used to work. */
  370. shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
  371. /* Otherwise we do nothing. The assumption is that
  372. the index table will not be needed. */
  373. }
  374. }
  375. /* Read the symbols. */
  376. alloc_ext = NULL;
  377. alloc_extshndx = NULL;
  378. alloc_intsym = NULL;
  379. bed = get_elf_backend_data (ibfd);
  380. extsym_size = bed->s->sizeof_sym;
  381. if (_bfd_mul_overflow (symcount, extsym_size, &amt))
  382. {
  383. bfd_set_error (bfd_error_file_too_big);
  384. intsym_buf = NULL;
  385. goto out;
  386. }
  387. pos = symtab_hdr->sh_offset + symoffset * extsym_size;
  388. if (extsym_buf == NULL)
  389. {
  390. alloc_ext = bfd_malloc (amt);
  391. extsym_buf = alloc_ext;
  392. }
  393. if (extsym_buf == NULL
  394. || bfd_seek (ibfd, pos, SEEK_SET) != 0
  395. || bfd_bread (extsym_buf, amt, ibfd) != amt)
  396. {
  397. intsym_buf = NULL;
  398. goto out;
  399. }
  400. if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
  401. extshndx_buf = NULL;
  402. else
  403. {
  404. if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
  405. {
  406. bfd_set_error (bfd_error_file_too_big);
  407. intsym_buf = NULL;
  408. goto out;
  409. }
  410. pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
  411. if (extshndx_buf == NULL)
  412. {
  413. alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
  414. extshndx_buf = alloc_extshndx;
  415. }
  416. if (extshndx_buf == NULL
  417. || bfd_seek (ibfd, pos, SEEK_SET) != 0
  418. || bfd_bread (extshndx_buf, amt, ibfd) != amt)
  419. {
  420. intsym_buf = NULL;
  421. goto out;
  422. }
  423. }
  424. if (intsym_buf == NULL)
  425. {
  426. if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
  427. {
  428. bfd_set_error (bfd_error_file_too_big);
  429. goto out;
  430. }
  431. alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
  432. intsym_buf = alloc_intsym;
  433. if (intsym_buf == NULL)
  434. goto out;
  435. }
  436. /* Convert the symbols to internal form. */
  437. isymend = intsym_buf + symcount;
  438. for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
  439. shndx = extshndx_buf;
  440. isym < isymend;
  441. esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
  442. if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
  443. {
  444. symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
  445. /* xgettext:c-format */
  446. _bfd_error_handler (_("%pB symbol number %lu references"
  447. " nonexistent SHT_SYMTAB_SHNDX section"),
  448. ibfd, (unsigned long) symoffset);
  449. free (alloc_intsym);
  450. intsym_buf = NULL;
  451. goto out;
  452. }
  453. out:
  454. free (alloc_ext);
  455. free (alloc_extshndx);
  456. return intsym_buf;
  457. }
  458. /* Look up a symbol name. */
  459. const char *
  460. bfd_elf_sym_name (bfd *abfd,
  461. Elf_Internal_Shdr *symtab_hdr,
  462. Elf_Internal_Sym *isym,
  463. asection *sym_sec)
  464. {
  465. const char *name;
  466. unsigned int iname = isym->st_name;
  467. unsigned int shindex = symtab_hdr->sh_link;
  468. if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
  469. /* Check for a bogus st_shndx to avoid crashing. */
  470. && isym->st_shndx < elf_numsections (abfd))
  471. {
  472. iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
  473. shindex = elf_elfheader (abfd)->e_shstrndx;
  474. }
  475. name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
  476. if (name == NULL)
  477. name = "(null)";
  478. else if (sym_sec && *name == '\0')
  479. name = bfd_section_name (sym_sec);
  480. return name;
  481. }
  482. /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
  483. sections. The first element is the flags, the rest are section
  484. pointers. */
  485. typedef union elf_internal_group {
  486. Elf_Internal_Shdr *shdr;
  487. unsigned int flags;
  488. } Elf_Internal_Group;
  489. /* Return the name of the group signature symbol. Why isn't the
  490. signature just a string? */
  491. static const char *
  492. group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
  493. {
  494. Elf_Internal_Shdr *hdr;
  495. unsigned char esym[sizeof (Elf64_External_Sym)];
  496. Elf_External_Sym_Shndx eshndx;
  497. Elf_Internal_Sym isym;
  498. /* First we need to ensure the symbol table is available. Make sure
  499. that it is a symbol table section. */
  500. if (ghdr->sh_link >= elf_numsections (abfd))
  501. return NULL;
  502. hdr = elf_elfsections (abfd) [ghdr->sh_link];
  503. if (hdr->sh_type != SHT_SYMTAB
  504. || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
  505. return NULL;
  506. /* Go read the symbol. */
  507. hdr = &elf_tdata (abfd)->symtab_hdr;
  508. if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
  509. &isym, esym, &eshndx) == NULL)
  510. return NULL;
  511. return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
  512. }
  513. /* Set next_in_group list pointer, and group name for NEWSECT. */
  514. static bool
  515. setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
  516. {
  517. unsigned int num_group = elf_tdata (abfd)->num_group;
  518. /* If num_group is zero, read in all SHT_GROUP sections. The count
  519. is set to -1 if there are no SHT_GROUP sections. */
  520. if (num_group == 0)
  521. {
  522. unsigned int i, shnum;
  523. /* First count the number of groups. If we have a SHT_GROUP
  524. section with just a flag word (ie. sh_size is 4), ignore it. */
  525. shnum = elf_numsections (abfd);
  526. num_group = 0;
  527. #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
  528. ( (shdr)->sh_type == SHT_GROUP \
  529. && (shdr)->sh_size >= minsize \
  530. && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
  531. && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
  532. for (i = 0; i < shnum; i++)
  533. {
  534. Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
  535. if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
  536. num_group += 1;
  537. }
  538. if (num_group == 0)
  539. {
  540. num_group = (unsigned) -1;
  541. elf_tdata (abfd)->num_group = num_group;
  542. elf_tdata (abfd)->group_sect_ptr = NULL;
  543. }
  544. else
  545. {
  546. /* We keep a list of elf section headers for group sections,
  547. so we can find them quickly. */
  548. size_t amt;
  549. elf_tdata (abfd)->num_group = num_group;
  550. amt = num_group * sizeof (Elf_Internal_Shdr *);
  551. elf_tdata (abfd)->group_sect_ptr
  552. = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
  553. if (elf_tdata (abfd)->group_sect_ptr == NULL)
  554. return false;
  555. num_group = 0;
  556. for (i = 0; i < shnum; i++)
  557. {
  558. Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
  559. if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
  560. {
  561. unsigned char *src;
  562. Elf_Internal_Group *dest;
  563. /* Make sure the group section has a BFD section
  564. attached to it. */
  565. if (!bfd_section_from_shdr (abfd, i))
  566. return false;
  567. /* Add to list of sections. */
  568. elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
  569. num_group += 1;
  570. /* Read the raw contents. */
  571. BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
  572. shdr->contents = NULL;
  573. if (_bfd_mul_overflow (shdr->sh_size,
  574. sizeof (*dest) / 4, &amt)
  575. || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
  576. || !(shdr->contents
  577. = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
  578. {
  579. _bfd_error_handler
  580. /* xgettext:c-format */
  581. (_("%pB: invalid size field in group section"
  582. " header: %#" PRIx64 ""),
  583. abfd, (uint64_t) shdr->sh_size);
  584. bfd_set_error (bfd_error_bad_value);
  585. -- num_group;
  586. continue;
  587. }
  588. /* Translate raw contents, a flag word followed by an
  589. array of elf section indices all in target byte order,
  590. to the flag word followed by an array of elf section
  591. pointers. */
  592. src = shdr->contents + shdr->sh_size;
  593. dest = (Elf_Internal_Group *) (shdr->contents + amt);
  594. while (1)
  595. {
  596. unsigned int idx;
  597. src -= 4;
  598. --dest;
  599. idx = H_GET_32 (abfd, src);
  600. if (src == shdr->contents)
  601. {
  602. dest->shdr = NULL;
  603. dest->flags = idx;
  604. if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
  605. shdr->bfd_section->flags
  606. |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
  607. break;
  608. }
  609. if (idx < shnum)
  610. {
  611. dest->shdr = elf_elfsections (abfd)[idx];
  612. /* PR binutils/23199: All sections in a
  613. section group should be marked with
  614. SHF_GROUP. But some tools generate
  615. broken objects without SHF_GROUP. Fix
  616. them up here. */
  617. dest->shdr->sh_flags |= SHF_GROUP;
  618. }
  619. if (idx >= shnum
  620. || dest->shdr->sh_type == SHT_GROUP)
  621. {
  622. _bfd_error_handler
  623. (_("%pB: invalid entry in SHT_GROUP section [%u]"),
  624. abfd, i);
  625. dest->shdr = NULL;
  626. }
  627. }
  628. }
  629. }
  630. /* PR 17510: Corrupt binaries might contain invalid groups. */
  631. if (num_group != (unsigned) elf_tdata (abfd)->num_group)
  632. {
  633. elf_tdata (abfd)->num_group = num_group;
  634. /* If all groups are invalid then fail. */
  635. if (num_group == 0)
  636. {
  637. elf_tdata (abfd)->group_sect_ptr = NULL;
  638. elf_tdata (abfd)->num_group = num_group = -1;
  639. _bfd_error_handler
  640. (_("%pB: no valid group sections found"), abfd);
  641. bfd_set_error (bfd_error_bad_value);
  642. }
  643. }
  644. }
  645. }
  646. if (num_group != (unsigned) -1)
  647. {
  648. unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
  649. unsigned int j;
  650. for (j = 0; j < num_group; j++)
  651. {
  652. /* Begin search from previous found group. */
  653. unsigned i = (j + search_offset) % num_group;
  654. Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
  655. Elf_Internal_Group *idx;
  656. bfd_size_type n_elt;
  657. if (shdr == NULL)
  658. continue;
  659. idx = (Elf_Internal_Group *) shdr->contents;
  660. if (idx == NULL || shdr->sh_size < 4)
  661. {
  662. /* See PR 21957 for a reproducer. */
  663. /* xgettext:c-format */
  664. _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
  665. abfd, shdr->bfd_section);
  666. elf_tdata (abfd)->group_sect_ptr[i] = NULL;
  667. bfd_set_error (bfd_error_bad_value);
  668. return false;
  669. }
  670. n_elt = shdr->sh_size / 4;
  671. /* Look through this group's sections to see if current
  672. section is a member. */
  673. while (--n_elt != 0)
  674. if ((++idx)->shdr == hdr)
  675. {
  676. asection *s = NULL;
  677. /* We are a member of this group. Go looking through
  678. other members to see if any others are linked via
  679. next_in_group. */
  680. idx = (Elf_Internal_Group *) shdr->contents;
  681. n_elt = shdr->sh_size / 4;
  682. while (--n_elt != 0)
  683. if ((++idx)->shdr != NULL
  684. && (s = idx->shdr->bfd_section) != NULL
  685. && elf_next_in_group (s) != NULL)
  686. break;
  687. if (n_elt != 0)
  688. {
  689. /* Snarf the group name from other member, and
  690. insert current section in circular list. */
  691. elf_group_name (newsect) = elf_group_name (s);
  692. elf_next_in_group (newsect) = elf_next_in_group (s);
  693. elf_next_in_group (s) = newsect;
  694. }
  695. else
  696. {
  697. const char *gname;
  698. gname = group_signature (abfd, shdr);
  699. if (gname == NULL)
  700. return false;
  701. elf_group_name (newsect) = gname;
  702. /* Start a circular list with one element. */
  703. elf_next_in_group (newsect) = newsect;
  704. }
  705. /* If the group section has been created, point to the
  706. new member. */
  707. if (shdr->bfd_section != NULL)
  708. elf_next_in_group (shdr->bfd_section) = newsect;
  709. elf_tdata (abfd)->group_search_offset = i;
  710. j = num_group - 1;
  711. break;
  712. }
  713. }
  714. }
  715. if (elf_group_name (newsect) == NULL)
  716. {
  717. /* xgettext:c-format */
  718. _bfd_error_handler (_("%pB: no group info for section '%pA'"),
  719. abfd, newsect);
  720. return false;
  721. }
  722. return true;
  723. }
  724. bool
  725. _bfd_elf_setup_sections (bfd *abfd)
  726. {
  727. unsigned int i;
  728. unsigned int num_group = elf_tdata (abfd)->num_group;
  729. bool result = true;
  730. asection *s;
  731. /* Process SHF_LINK_ORDER. */
  732. for (s = abfd->sections; s != NULL; s = s->next)
  733. {
  734. Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
  735. if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
  736. {
  737. unsigned int elfsec = this_hdr->sh_link;
  738. /* An sh_link value of 0 is now allowed. It indicates that linked
  739. to section has already been discarded, but that the current
  740. section has been retained for some other reason. This linking
  741. section is still a candidate for later garbage collection
  742. however. */
  743. if (elfsec == 0)
  744. {
  745. elf_linked_to_section (s) = NULL;
  746. }
  747. else
  748. {
  749. asection *linksec = NULL;
  750. if (elfsec < elf_numsections (abfd))
  751. {
  752. this_hdr = elf_elfsections (abfd)[elfsec];
  753. linksec = this_hdr->bfd_section;
  754. }
  755. /* PR 1991, 2008:
  756. Some strip/objcopy may leave an incorrect value in
  757. sh_link. We don't want to proceed. */
  758. if (linksec == NULL)
  759. {
  760. _bfd_error_handler
  761. /* xgettext:c-format */
  762. (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
  763. s->owner, elfsec, s);
  764. result = false;
  765. }
  766. elf_linked_to_section (s) = linksec;
  767. }
  768. }
  769. else if (this_hdr->sh_type == SHT_GROUP
  770. && elf_next_in_group (s) == NULL)
  771. {
  772. _bfd_error_handler
  773. /* xgettext:c-format */
  774. (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
  775. abfd, elf_section_data (s)->this_idx);
  776. result = false;
  777. }
  778. }
  779. /* Process section groups. */
  780. if (num_group == (unsigned) -1)
  781. return result;
  782. for (i = 0; i < num_group; i++)
  783. {
  784. Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
  785. Elf_Internal_Group *idx;
  786. unsigned int n_elt;
  787. /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
  788. if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
  789. {
  790. _bfd_error_handler
  791. /* xgettext:c-format */
  792. (_("%pB: section group entry number %u is corrupt"),
  793. abfd, i);
  794. result = false;
  795. continue;
  796. }
  797. idx = (Elf_Internal_Group *) shdr->contents;
  798. n_elt = shdr->sh_size / 4;
  799. while (--n_elt != 0)
  800. {
  801. ++ idx;
  802. if (idx->shdr == NULL)
  803. continue;
  804. else if (idx->shdr->bfd_section)
  805. elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
  806. else if (idx->shdr->sh_type != SHT_RELA
  807. && idx->shdr->sh_type != SHT_REL)
  808. {
  809. /* There are some unknown sections in the group. */
  810. _bfd_error_handler
  811. /* xgettext:c-format */
  812. (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
  813. abfd,
  814. idx->shdr->sh_type,
  815. bfd_elf_string_from_elf_section (abfd,
  816. (elf_elfheader (abfd)
  817. ->e_shstrndx),
  818. idx->shdr->sh_name),
  819. shdr->bfd_section);
  820. result = false;
  821. }
  822. }
  823. }
  824. return result;
  825. }
  826. bool
  827. bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
  828. {
  829. return elf_next_in_group (sec) != NULL;
  830. }
  831. const char *
  832. bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
  833. {
  834. if (elf_sec_group (sec) != NULL)
  835. return elf_group_name (sec);
  836. return NULL;
  837. }
  838. static char *
  839. convert_debug_to_zdebug (bfd *abfd, const char *name)
  840. {
  841. unsigned int len = strlen (name);
  842. char *new_name = bfd_alloc (abfd, len + 2);
  843. if (new_name == NULL)
  844. return NULL;
  845. new_name[0] = '.';
  846. new_name[1] = 'z';
  847. memcpy (new_name + 2, name + 1, len);
  848. return new_name;
  849. }
  850. static char *
  851. convert_zdebug_to_debug (bfd *abfd, const char *name)
  852. {
  853. unsigned int len = strlen (name);
  854. char *new_name = bfd_alloc (abfd, len);
  855. if (new_name == NULL)
  856. return NULL;
  857. new_name[0] = '.';
  858. memcpy (new_name + 1, name + 2, len - 1);
  859. return new_name;
  860. }
  861. /* This a copy of lto_section defined in GCC (lto-streamer.h). */
  862. struct lto_section
  863. {
  864. int16_t major_version;
  865. int16_t minor_version;
  866. unsigned char slim_object;
  867. /* Flags is a private field that is not defined publicly. */
  868. uint16_t flags;
  869. };
  870. /* Make a BFD section from an ELF section. We store a pointer to the
  871. BFD section in the bfd_section field of the header. */
  872. bool
  873. _bfd_elf_make_section_from_shdr (bfd *abfd,
  874. Elf_Internal_Shdr *hdr,
  875. const char *name,
  876. int shindex)
  877. {
  878. asection *newsect;
  879. flagword flags;
  880. const struct elf_backend_data *bed;
  881. unsigned int opb = bfd_octets_per_byte (abfd, NULL);
  882. if (hdr->bfd_section != NULL)
  883. return true;
  884. newsect = bfd_make_section_anyway (abfd, name);
  885. if (newsect == NULL)
  886. return false;
  887. hdr->bfd_section = newsect;
  888. elf_section_data (newsect)->this_hdr = *hdr;
  889. elf_section_data (newsect)->this_idx = shindex;
  890. /* Always use the real type/flags. */
  891. elf_section_type (newsect) = hdr->sh_type;
  892. elf_section_flags (newsect) = hdr->sh_flags;
  893. newsect->filepos = hdr->sh_offset;
  894. flags = SEC_NO_FLAGS;
  895. if (hdr->sh_type != SHT_NOBITS)
  896. flags |= SEC_HAS_CONTENTS;
  897. if (hdr->sh_type == SHT_GROUP)
  898. flags |= SEC_GROUP;
  899. if ((hdr->sh_flags & SHF_ALLOC) != 0)
  900. {
  901. flags |= SEC_ALLOC;
  902. if (hdr->sh_type != SHT_NOBITS)
  903. flags |= SEC_LOAD;
  904. }
  905. if ((hdr->sh_flags & SHF_WRITE) == 0)
  906. flags |= SEC_READONLY;
  907. if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
  908. flags |= SEC_CODE;
  909. else if ((flags & SEC_LOAD) != 0)
  910. flags |= SEC_DATA;
  911. if ((hdr->sh_flags & SHF_MERGE) != 0)
  912. {
  913. flags |= SEC_MERGE;
  914. newsect->entsize = hdr->sh_entsize;
  915. }
  916. if ((hdr->sh_flags & SHF_STRINGS) != 0)
  917. flags |= SEC_STRINGS;
  918. if (hdr->sh_flags & SHF_GROUP)
  919. if (!setup_group (abfd, hdr, newsect))
  920. return false;
  921. if ((hdr->sh_flags & SHF_TLS) != 0)
  922. flags |= SEC_THREAD_LOCAL;
  923. if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
  924. flags |= SEC_EXCLUDE;
  925. switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
  926. {
  927. /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
  928. but binutils as of 2019-07-23 did not set the EI_OSABI header
  929. byte. */
  930. case ELFOSABI_GNU:
  931. case ELFOSABI_FREEBSD:
  932. if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
  933. elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
  934. /* Fall through */
  935. case ELFOSABI_NONE:
  936. if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
  937. elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
  938. break;
  939. }
  940. if ((flags & SEC_ALLOC) == 0)
  941. {
  942. /* The debugging sections appear to be recognized only by name,
  943. not any sort of flag. Their SEC_ALLOC bits are cleared. */
  944. if (name [0] == '.')
  945. {
  946. if (startswith (name, ".debug")
  947. || startswith (name, ".gnu.debuglto_.debug_")
  948. || startswith (name, ".gnu.linkonce.wi.")
  949. || startswith (name, ".zdebug"))
  950. flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
  951. else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
  952. || startswith (name, ".note.gnu"))
  953. {
  954. flags |= SEC_ELF_OCTETS;
  955. opb = 1;
  956. }
  957. else if (startswith (name, ".line")
  958. || startswith (name, ".stab")
  959. || strcmp (name, ".gdb_index") == 0)
  960. flags |= SEC_DEBUGGING;
  961. }
  962. }
  963. if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
  964. || !bfd_set_section_size (newsect, hdr->sh_size)
  965. || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
  966. & -hdr->sh_addralign)))
  967. return false;
  968. /* As a GNU extension, if the name begins with .gnu.linkonce, we
  969. only link a single copy of the section. This is used to support
  970. g++. g++ will emit each template expansion in its own section.
  971. The symbols will be defined as weak, so that multiple definitions
  972. are permitted. The GNU linker extension is to actually discard
  973. all but one of the sections. */
  974. if (startswith (name, ".gnu.linkonce")
  975. && elf_next_in_group (newsect) == NULL)
  976. flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
  977. if (!bfd_set_section_flags (newsect, flags))
  978. return false;
  979. bed = get_elf_backend_data (abfd);
  980. if (bed->elf_backend_section_flags)
  981. if (!bed->elf_backend_section_flags (hdr))
  982. return false;
  983. /* We do not parse the PT_NOTE segments as we are interested even in the
  984. separate debug info files which may have the segments offsets corrupted.
  985. PT_NOTEs from the core files are currently not parsed using BFD. */
  986. if (hdr->sh_type == SHT_NOTE)
  987. {
  988. bfd_byte *contents;
  989. if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
  990. return false;
  991. elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
  992. hdr->sh_offset, hdr->sh_addralign);
  993. free (contents);
  994. }
  995. if ((newsect->flags & SEC_ALLOC) != 0)
  996. {
  997. Elf_Internal_Phdr *phdr;
  998. unsigned int i, nload;
  999. /* Some ELF linkers produce binaries with all the program header
  1000. p_paddr fields zero. If we have such a binary with more than
  1001. one PT_LOAD header, then leave the section lma equal to vma
  1002. so that we don't create sections with overlapping lma. */
  1003. phdr = elf_tdata (abfd)->phdr;
  1004. for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
  1005. if (phdr->p_paddr != 0)
  1006. break;
  1007. else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
  1008. ++nload;
  1009. if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
  1010. return true;
  1011. phdr = elf_tdata (abfd)->phdr;
  1012. for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
  1013. {
  1014. if (((phdr->p_type == PT_LOAD
  1015. && (hdr->sh_flags & SHF_TLS) == 0)
  1016. || phdr->p_type == PT_TLS)
  1017. && ELF_SECTION_IN_SEGMENT (hdr, phdr))
  1018. {
  1019. if ((newsect->flags & SEC_LOAD) == 0)
  1020. newsect->lma = (phdr->p_paddr
  1021. + hdr->sh_addr - phdr->p_vaddr) / opb;
  1022. else
  1023. /* We used to use the same adjustment for SEC_LOAD
  1024. sections, but that doesn't work if the segment
  1025. is packed with code from multiple VMAs.
  1026. Instead we calculate the section LMA based on
  1027. the segment LMA. It is assumed that the
  1028. segment will contain sections with contiguous
  1029. LMAs, even if the VMAs are not. */
  1030. newsect->lma = (phdr->p_paddr
  1031. + hdr->sh_offset - phdr->p_offset) / opb;
  1032. /* With contiguous segments, we can't tell from file
  1033. offsets whether a section with zero size should
  1034. be placed at the end of one segment or the
  1035. beginning of the next. Decide based on vaddr. */
  1036. if (hdr->sh_addr >= phdr->p_vaddr
  1037. && (hdr->sh_addr + hdr->sh_size
  1038. <= phdr->p_vaddr + phdr->p_memsz))
  1039. break;
  1040. }
  1041. }
  1042. }
  1043. /* Compress/decompress DWARF debug sections with names: .debug_* and
  1044. .zdebug_*, after the section flags is set. */
  1045. if ((newsect->flags & SEC_DEBUGGING)
  1046. && ((name[1] == 'd' && name[6] == '_')
  1047. || (name[1] == 'z' && name[7] == '_')))
  1048. {
  1049. enum { nothing, compress, decompress } action = nothing;
  1050. int compression_header_size;
  1051. bfd_size_type uncompressed_size;
  1052. unsigned int uncompressed_align_power;
  1053. bool compressed
  1054. = bfd_is_section_compressed_with_header (abfd, newsect,
  1055. &compression_header_size,
  1056. &uncompressed_size,
  1057. &uncompressed_align_power);
  1058. if (compressed)
  1059. {
  1060. /* Compressed section. Check if we should decompress. */
  1061. if ((abfd->flags & BFD_DECOMPRESS))
  1062. action = decompress;
  1063. }
  1064. /* Compress the uncompressed section or convert from/to .zdebug*
  1065. section. Check if we should compress. */
  1066. if (action == nothing)
  1067. {
  1068. if (newsect->size != 0
  1069. && (abfd->flags & BFD_COMPRESS)
  1070. && compression_header_size >= 0
  1071. && uncompressed_size > 0
  1072. && (!compressed
  1073. || ((compression_header_size > 0)
  1074. != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
  1075. action = compress;
  1076. else
  1077. return true;
  1078. }
  1079. if (action == compress)
  1080. {
  1081. if (!bfd_init_section_compress_status (abfd, newsect))
  1082. {
  1083. _bfd_error_handler
  1084. /* xgettext:c-format */
  1085. (_("%pB: unable to initialize compress status for section %s"),
  1086. abfd, name);
  1087. return false;
  1088. }
  1089. }
  1090. else
  1091. {
  1092. if (!bfd_init_section_decompress_status (abfd, newsect))
  1093. {
  1094. _bfd_error_handler
  1095. /* xgettext:c-format */
  1096. (_("%pB: unable to initialize decompress status for section %s"),
  1097. abfd, name);
  1098. return false;
  1099. }
  1100. }
  1101. if (abfd->is_linker_input)
  1102. {
  1103. if (name[1] == 'z'
  1104. && (action == decompress
  1105. || (action == compress
  1106. && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
  1107. {
  1108. /* Convert section name from .zdebug_* to .debug_* so
  1109. that linker will consider this section as a debug
  1110. section. */
  1111. char *new_name = convert_zdebug_to_debug (abfd, name);
  1112. if (new_name == NULL)
  1113. return false;
  1114. bfd_rename_section (newsect, new_name);
  1115. }
  1116. }
  1117. else
  1118. /* For objdump, don't rename the section. For objcopy, delay
  1119. section rename to elf_fake_sections. */
  1120. newsect->flags |= SEC_ELF_RENAME;
  1121. }
  1122. /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
  1123. section. */
  1124. if (startswith (name, ".gnu.lto_.lto."))
  1125. {
  1126. struct lto_section lsection;
  1127. if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
  1128. sizeof (struct lto_section)))
  1129. abfd->lto_slim_object = lsection.slim_object;
  1130. }
  1131. return true;
  1132. }
  1133. const char *const bfd_elf_section_type_names[] =
  1134. {
  1135. "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
  1136. "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
  1137. "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
  1138. };
  1139. /* ELF relocs are against symbols. If we are producing relocatable
  1140. output, and the reloc is against an external symbol, and nothing
  1141. has given us any additional addend, the resulting reloc will also
  1142. be against the same symbol. In such a case, we don't want to
  1143. change anything about the way the reloc is handled, since it will
  1144. all be done at final link time. Rather than put special case code
  1145. into bfd_perform_relocation, all the reloc types use this howto
  1146. function, or should call this function for relocatable output. */
  1147. bfd_reloc_status_type
  1148. bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  1149. arelent *reloc_entry,
  1150. asymbol *symbol,
  1151. void *data ATTRIBUTE_UNUSED,
  1152. asection *input_section,
  1153. bfd *output_bfd,
  1154. char **error_message ATTRIBUTE_UNUSED)
  1155. {
  1156. if (output_bfd != NULL
  1157. && (symbol->flags & BSF_SECTION_SYM) == 0
  1158. && (! reloc_entry->howto->partial_inplace
  1159. || reloc_entry->addend == 0))
  1160. {
  1161. reloc_entry->address += input_section->output_offset;
  1162. return bfd_reloc_ok;
  1163. }
  1164. /* In some cases the relocation should be treated as output section
  1165. relative, as when linking ELF DWARF into PE COFF. Many ELF
  1166. targets lack section relative relocations and instead use
  1167. ordinary absolute relocations for references between DWARF
  1168. sections. That is arguably a bug in those targets but it happens
  1169. to work for the usual case of linking to non-loaded ELF debug
  1170. sections with VMAs forced to zero. PE COFF on the other hand
  1171. doesn't allow a section VMA of zero. */
  1172. if (output_bfd == NULL
  1173. && !reloc_entry->howto->pc_relative
  1174. && (symbol->section->flags & SEC_DEBUGGING) != 0
  1175. && (input_section->flags & SEC_DEBUGGING) != 0)
  1176. reloc_entry->addend -= symbol->section->output_section->vma;
  1177. return bfd_reloc_continue;
  1178. }
  1179. /* Returns TRUE if section A matches section B.
  1180. Names, addresses and links may be different, but everything else
  1181. should be the same. */
  1182. static bool
  1183. section_match (const Elf_Internal_Shdr * a,
  1184. const Elf_Internal_Shdr * b)
  1185. {
  1186. if (a->sh_type != b->sh_type
  1187. || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
  1188. || a->sh_addralign != b->sh_addralign
  1189. || a->sh_entsize != b->sh_entsize)
  1190. return false;
  1191. if (a->sh_type == SHT_SYMTAB
  1192. || a->sh_type == SHT_STRTAB)
  1193. return true;
  1194. return a->sh_size == b->sh_size;
  1195. }
  1196. /* Find a section in OBFD that has the same characteristics
  1197. as IHEADER. Return the index of this section or SHN_UNDEF if
  1198. none can be found. Check's section HINT first, as this is likely
  1199. to be the correct section. */
  1200. static unsigned int
  1201. find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
  1202. const unsigned int hint)
  1203. {
  1204. Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
  1205. unsigned int i;
  1206. BFD_ASSERT (iheader != NULL);
  1207. /* See PR 20922 for a reproducer of the NULL test. */
  1208. if (hint < elf_numsections (obfd)
  1209. && oheaders[hint] != NULL
  1210. && section_match (oheaders[hint], iheader))
  1211. return hint;
  1212. for (i = 1; i < elf_numsections (obfd); i++)
  1213. {
  1214. Elf_Internal_Shdr * oheader = oheaders[i];
  1215. if (oheader == NULL)
  1216. continue;
  1217. if (section_match (oheader, iheader))
  1218. /* FIXME: Do we care if there is a potential for
  1219. multiple matches ? */
  1220. return i;
  1221. }
  1222. return SHN_UNDEF;
  1223. }
  1224. /* PR 19938: Attempt to set the ELF section header fields of an OS or
  1225. Processor specific section, based upon a matching input section.
  1226. Returns TRUE upon success, FALSE otherwise. */
  1227. static bool
  1228. copy_special_section_fields (const bfd *ibfd,
  1229. bfd *obfd,
  1230. const Elf_Internal_Shdr *iheader,
  1231. Elf_Internal_Shdr *oheader,
  1232. const unsigned int secnum)
  1233. {
  1234. const struct elf_backend_data *bed = get_elf_backend_data (obfd);
  1235. const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
  1236. bool changed = false;
  1237. unsigned int sh_link;
  1238. if (oheader->sh_type == SHT_NOBITS)
  1239. {
  1240. /* This is a feature for objcopy --only-keep-debug:
  1241. When a section's type is changed to NOBITS, we preserve
  1242. the sh_link and sh_info fields so that they can be
  1243. matched up with the original.
  1244. Note: Strictly speaking these assignments are wrong.
  1245. The sh_link and sh_info fields should point to the
  1246. relevent sections in the output BFD, which may not be in
  1247. the same location as they were in the input BFD. But
  1248. the whole point of this action is to preserve the
  1249. original values of the sh_link and sh_info fields, so
  1250. that they can be matched up with the section headers in
  1251. the original file. So strictly speaking we may be
  1252. creating an invalid ELF file, but it is only for a file
  1253. that just contains debug info and only for sections
  1254. without any contents. */
  1255. if (oheader->sh_link == 0)
  1256. oheader->sh_link = iheader->sh_link;
  1257. if (oheader->sh_info == 0)
  1258. oheader->sh_info = iheader->sh_info;
  1259. return true;
  1260. }
  1261. /* Allow the target a chance to decide how these fields should be set. */
  1262. if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
  1263. iheader, oheader))
  1264. return true;
  1265. /* We have an iheader which might match oheader, and which has non-zero
  1266. sh_info and/or sh_link fields. Attempt to follow those links and find
  1267. the section in the output bfd which corresponds to the linked section
  1268. in the input bfd. */
  1269. if (iheader->sh_link != SHN_UNDEF)
  1270. {
  1271. /* See PR 20931 for a reproducer. */
  1272. if (iheader->sh_link >= elf_numsections (ibfd))
  1273. {
  1274. _bfd_error_handler
  1275. /* xgettext:c-format */
  1276. (_("%pB: invalid sh_link field (%d) in section number %d"),
  1277. ibfd, iheader->sh_link, secnum);
  1278. return false;
  1279. }
  1280. sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
  1281. if (sh_link != SHN_UNDEF)
  1282. {
  1283. oheader->sh_link = sh_link;
  1284. changed = true;
  1285. }
  1286. else
  1287. /* FIXME: Should we install iheader->sh_link
  1288. if we could not find a match ? */
  1289. _bfd_error_handler
  1290. /* xgettext:c-format */
  1291. (_("%pB: failed to find link section for section %d"), obfd, secnum);
  1292. }
  1293. if (iheader->sh_info)
  1294. {
  1295. /* The sh_info field can hold arbitrary information, but if the
  1296. SHF_LINK_INFO flag is set then it should be interpreted as a
  1297. section index. */
  1298. if (iheader->sh_flags & SHF_INFO_LINK)
  1299. {
  1300. sh_link = find_link (obfd, iheaders[iheader->sh_info],
  1301. iheader->sh_info);
  1302. if (sh_link != SHN_UNDEF)
  1303. oheader->sh_flags |= SHF_INFO_LINK;
  1304. }
  1305. else
  1306. /* No idea what it means - just copy it. */
  1307. sh_link = iheader->sh_info;
  1308. if (sh_link != SHN_UNDEF)
  1309. {
  1310. oheader->sh_info = sh_link;
  1311. changed = true;
  1312. }
  1313. else
  1314. _bfd_error_handler
  1315. /* xgettext:c-format */
  1316. (_("%pB: failed to find info section for section %d"), obfd, secnum);
  1317. }
  1318. return changed;
  1319. }
  1320. /* Copy the program header and other data from one object module to
  1321. another. */
  1322. bool
  1323. _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
  1324. {
  1325. const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
  1326. Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
  1327. const struct elf_backend_data *bed;
  1328. unsigned int i;
  1329. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  1330. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  1331. return true;
  1332. if (!elf_flags_init (obfd))
  1333. {
  1334. elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
  1335. elf_flags_init (obfd) = true;
  1336. }
  1337. elf_gp (obfd) = elf_gp (ibfd);
  1338. /* Also copy the EI_OSABI field. */
  1339. elf_elfheader (obfd)->e_ident[EI_OSABI] =
  1340. elf_elfheader (ibfd)->e_ident[EI_OSABI];
  1341. /* If set, copy the EI_ABIVERSION field. */
  1342. if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
  1343. elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
  1344. = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
  1345. /* Copy object attributes. */
  1346. _bfd_elf_copy_obj_attributes (ibfd, obfd);
  1347. if (iheaders == NULL || oheaders == NULL)
  1348. return true;
  1349. bed = get_elf_backend_data (obfd);
  1350. /* Possibly copy other fields in the section header. */
  1351. for (i = 1; i < elf_numsections (obfd); i++)
  1352. {
  1353. unsigned int j;
  1354. Elf_Internal_Shdr * oheader = oheaders[i];
  1355. /* Ignore ordinary sections. SHT_NOBITS sections are considered however
  1356. because of a special case need for generating separate debug info
  1357. files. See below for more details. */
  1358. if (oheader == NULL
  1359. || (oheader->sh_type != SHT_NOBITS
  1360. && oheader->sh_type < SHT_LOOS))
  1361. continue;
  1362. /* Ignore empty sections, and sections whose
  1363. fields have already been initialised. */
  1364. if (oheader->sh_size == 0
  1365. || (oheader->sh_info != 0 && oheader->sh_link != 0))
  1366. continue;
  1367. /* Scan for the matching section in the input bfd.
  1368. First we try for a direct mapping between the input and output sections. */
  1369. for (j = 1; j < elf_numsections (ibfd); j++)
  1370. {
  1371. const Elf_Internal_Shdr * iheader = iheaders[j];
  1372. if (iheader == NULL)
  1373. continue;
  1374. if (oheader->bfd_section != NULL
  1375. && iheader->bfd_section != NULL
  1376. && iheader->bfd_section->output_section != NULL
  1377. && iheader->bfd_section->output_section == oheader->bfd_section)
  1378. {
  1379. /* We have found a connection from the input section to the
  1380. output section. Attempt to copy the header fields. If
  1381. this fails then do not try any further sections - there
  1382. should only be a one-to-one mapping between input and output. */
  1383. if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
  1384. j = elf_numsections (ibfd);
  1385. break;
  1386. }
  1387. }
  1388. if (j < elf_numsections (ibfd))
  1389. continue;
  1390. /* That failed. So try to deduce the corresponding input section.
  1391. Unfortunately we cannot compare names as the output string table
  1392. is empty, so instead we check size, address and type. */
  1393. for (j = 1; j < elf_numsections (ibfd); j++)
  1394. {
  1395. const Elf_Internal_Shdr * iheader = iheaders[j];
  1396. if (iheader == NULL)
  1397. continue;
  1398. /* Try matching fields in the input section's header.
  1399. Since --only-keep-debug turns all non-debug sections into
  1400. SHT_NOBITS sections, the output SHT_NOBITS type matches any
  1401. input type. */
  1402. if ((oheader->sh_type == SHT_NOBITS
  1403. || iheader->sh_type == oheader->sh_type)
  1404. && (iheader->sh_flags & ~ SHF_INFO_LINK)
  1405. == (oheader->sh_flags & ~ SHF_INFO_LINK)
  1406. && iheader->sh_addralign == oheader->sh_addralign
  1407. && iheader->sh_entsize == oheader->sh_entsize
  1408. && iheader->sh_size == oheader->sh_size
  1409. && iheader->sh_addr == oheader->sh_addr
  1410. && (iheader->sh_info != oheader->sh_info
  1411. || iheader->sh_link != oheader->sh_link))
  1412. {
  1413. if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
  1414. break;
  1415. }
  1416. }
  1417. if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
  1418. {
  1419. /* Final attempt. Call the backend copy function
  1420. with a NULL input section. */
  1421. (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
  1422. NULL, oheader);
  1423. }
  1424. }
  1425. return true;
  1426. }
  1427. static const char *
  1428. get_segment_type (unsigned int p_type)
  1429. {
  1430. const char *pt;
  1431. switch (p_type)
  1432. {
  1433. case PT_NULL: pt = "NULL"; break;
  1434. case PT_LOAD: pt = "LOAD"; break;
  1435. case PT_DYNAMIC: pt = "DYNAMIC"; break;
  1436. case PT_INTERP: pt = "INTERP"; break;
  1437. case PT_NOTE: pt = "NOTE"; break;
  1438. case PT_SHLIB: pt = "SHLIB"; break;
  1439. case PT_PHDR: pt = "PHDR"; break;
  1440. case PT_TLS: pt = "TLS"; break;
  1441. case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
  1442. case PT_GNU_STACK: pt = "STACK"; break;
  1443. case PT_GNU_RELRO: pt = "RELRO"; break;
  1444. default: pt = NULL; break;
  1445. }
  1446. return pt;
  1447. }
  1448. /* Print out the program headers. */
  1449. bool
  1450. _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
  1451. {
  1452. FILE *f = (FILE *) farg;
  1453. Elf_Internal_Phdr *p;
  1454. asection *s;
  1455. bfd_byte *dynbuf = NULL;
  1456. p = elf_tdata (abfd)->phdr;
  1457. if (p != NULL)
  1458. {
  1459. unsigned int i, c;
  1460. fprintf (f, _("\nProgram Header:\n"));
  1461. c = elf_elfheader (abfd)->e_phnum;
  1462. for (i = 0; i < c; i++, p++)
  1463. {
  1464. const char *pt = get_segment_type (p->p_type);
  1465. char buf[20];
  1466. if (pt == NULL)
  1467. {
  1468. sprintf (buf, "0x%lx", p->p_type);
  1469. pt = buf;
  1470. }
  1471. fprintf (f, "%8s off 0x", pt);
  1472. bfd_fprintf_vma (abfd, f, p->p_offset);
  1473. fprintf (f, " vaddr 0x");
  1474. bfd_fprintf_vma (abfd, f, p->p_vaddr);
  1475. fprintf (f, " paddr 0x");
  1476. bfd_fprintf_vma (abfd, f, p->p_paddr);
  1477. fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
  1478. fprintf (f, " filesz 0x");
  1479. bfd_fprintf_vma (abfd, f, p->p_filesz);
  1480. fprintf (f, " memsz 0x");
  1481. bfd_fprintf_vma (abfd, f, p->p_memsz);
  1482. fprintf (f, " flags %c%c%c",
  1483. (p->p_flags & PF_R) != 0 ? 'r' : '-',
  1484. (p->p_flags & PF_W) != 0 ? 'w' : '-',
  1485. (p->p_flags & PF_X) != 0 ? 'x' : '-');
  1486. if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
  1487. fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
  1488. fprintf (f, "\n");
  1489. }
  1490. }
  1491. s = bfd_get_section_by_name (abfd, ".dynamic");
  1492. if (s != NULL)
  1493. {
  1494. unsigned int elfsec;
  1495. unsigned long shlink;
  1496. bfd_byte *extdyn, *extdynend;
  1497. size_t extdynsize;
  1498. void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
  1499. fprintf (f, _("\nDynamic Section:\n"));
  1500. if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
  1501. goto error_return;
  1502. elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
  1503. if (elfsec == SHN_BAD)
  1504. goto error_return;
  1505. shlink = elf_elfsections (abfd)[elfsec]->sh_link;
  1506. extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
  1507. swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
  1508. extdyn = dynbuf;
  1509. /* PR 17512: file: 6f427532. */
  1510. if (s->size < extdynsize)
  1511. goto error_return;
  1512. extdynend = extdyn + s->size;
  1513. /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
  1514. Fix range check. */
  1515. for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
  1516. {
  1517. Elf_Internal_Dyn dyn;
  1518. const char *name = "";
  1519. char ab[20];
  1520. bool stringp;
  1521. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1522. (*swap_dyn_in) (abfd, extdyn, &dyn);
  1523. if (dyn.d_tag == DT_NULL)
  1524. break;
  1525. stringp = false;
  1526. switch (dyn.d_tag)
  1527. {
  1528. default:
  1529. if (bed->elf_backend_get_target_dtag)
  1530. name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
  1531. if (!strcmp (name, ""))
  1532. {
  1533. sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
  1534. name = ab;
  1535. }
  1536. break;
  1537. case DT_NEEDED: name = "NEEDED"; stringp = true; break;
  1538. case DT_PLTRELSZ: name = "PLTRELSZ"; break;
  1539. case DT_PLTGOT: name = "PLTGOT"; break;
  1540. case DT_HASH: name = "HASH"; break;
  1541. case DT_STRTAB: name = "STRTAB"; break;
  1542. case DT_SYMTAB: name = "SYMTAB"; break;
  1543. case DT_RELA: name = "RELA"; break;
  1544. case DT_RELASZ: name = "RELASZ"; break;
  1545. case DT_RELAENT: name = "RELAENT"; break;
  1546. case DT_STRSZ: name = "STRSZ"; break;
  1547. case DT_SYMENT: name = "SYMENT"; break;
  1548. case DT_INIT: name = "INIT"; break;
  1549. case DT_FINI: name = "FINI"; break;
  1550. case DT_SONAME: name = "SONAME"; stringp = true; break;
  1551. case DT_RPATH: name = "RPATH"; stringp = true; break;
  1552. case DT_SYMBOLIC: name = "SYMBOLIC"; break;
  1553. case DT_REL: name = "REL"; break;
  1554. case DT_RELSZ: name = "RELSZ"; break;
  1555. case DT_RELENT: name = "RELENT"; break;
  1556. case DT_RELR: name = "RELR"; break;
  1557. case DT_RELRSZ: name = "RELRSZ"; break;
  1558. case DT_RELRENT: name = "RELRENT"; break;
  1559. case DT_PLTREL: name = "PLTREL"; break;
  1560. case DT_DEBUG: name = "DEBUG"; break;
  1561. case DT_TEXTREL: name = "TEXTREL"; break;
  1562. case DT_JMPREL: name = "JMPREL"; break;
  1563. case DT_BIND_NOW: name = "BIND_NOW"; break;
  1564. case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
  1565. case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
  1566. case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
  1567. case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
  1568. case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
  1569. case DT_FLAGS: name = "FLAGS"; break;
  1570. case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
  1571. case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
  1572. case DT_CHECKSUM: name = "CHECKSUM"; break;
  1573. case DT_PLTPADSZ: name = "PLTPADSZ"; break;
  1574. case DT_MOVEENT: name = "MOVEENT"; break;
  1575. case DT_MOVESZ: name = "MOVESZ"; break;
  1576. case DT_FEATURE: name = "FEATURE"; break;
  1577. case DT_POSFLAG_1: name = "POSFLAG_1"; break;
  1578. case DT_SYMINSZ: name = "SYMINSZ"; break;
  1579. case DT_SYMINENT: name = "SYMINENT"; break;
  1580. case DT_CONFIG: name = "CONFIG"; stringp = true; break;
  1581. case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
  1582. case DT_AUDIT: name = "AUDIT"; stringp = true; break;
  1583. case DT_PLTPAD: name = "PLTPAD"; break;
  1584. case DT_MOVETAB: name = "MOVETAB"; break;
  1585. case DT_SYMINFO: name = "SYMINFO"; break;
  1586. case DT_RELACOUNT: name = "RELACOUNT"; break;
  1587. case DT_RELCOUNT: name = "RELCOUNT"; break;
  1588. case DT_FLAGS_1: name = "FLAGS_1"; break;
  1589. case DT_VERSYM: name = "VERSYM"; break;
  1590. case DT_VERDEF: name = "VERDEF"; break;
  1591. case DT_VERDEFNUM: name = "VERDEFNUM"; break;
  1592. case DT_VERNEED: name = "VERNEED"; break;
  1593. case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
  1594. case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
  1595. case DT_USED: name = "USED"; break;
  1596. case DT_FILTER: name = "FILTER"; stringp = true; break;
  1597. case DT_GNU_HASH: name = "GNU_HASH"; break;
  1598. }
  1599. fprintf (f, " %-20s ", name);
  1600. if (! stringp)
  1601. {
  1602. fprintf (f, "0x");
  1603. bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
  1604. }
  1605. else
  1606. {
  1607. const char *string;
  1608. unsigned int tagv = dyn.d_un.d_val;
  1609. string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  1610. if (string == NULL)
  1611. goto error_return;
  1612. fprintf (f, "%s", string);
  1613. }
  1614. fprintf (f, "\n");
  1615. }
  1616. free (dynbuf);
  1617. dynbuf = NULL;
  1618. }
  1619. if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
  1620. || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
  1621. {
  1622. if (! _bfd_elf_slurp_version_tables (abfd, false))
  1623. return false;
  1624. }
  1625. if (elf_dynverdef (abfd) != 0)
  1626. {
  1627. Elf_Internal_Verdef *t;
  1628. fprintf (f, _("\nVersion definitions:\n"));
  1629. for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
  1630. {
  1631. fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
  1632. t->vd_flags, t->vd_hash,
  1633. t->vd_nodename ? t->vd_nodename : "<corrupt>");
  1634. if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
  1635. {
  1636. Elf_Internal_Verdaux *a;
  1637. fprintf (f, "\t");
  1638. for (a = t->vd_auxptr->vda_nextptr;
  1639. a != NULL;
  1640. a = a->vda_nextptr)
  1641. fprintf (f, "%s ",
  1642. a->vda_nodename ? a->vda_nodename : "<corrupt>");
  1643. fprintf (f, "\n");
  1644. }
  1645. }
  1646. }
  1647. if (elf_dynverref (abfd) != 0)
  1648. {
  1649. Elf_Internal_Verneed *t;
  1650. fprintf (f, _("\nVersion References:\n"));
  1651. for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
  1652. {
  1653. Elf_Internal_Vernaux *a;
  1654. fprintf (f, _(" required from %s:\n"),
  1655. t->vn_filename ? t->vn_filename : "<corrupt>");
  1656. for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  1657. fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
  1658. a->vna_flags, a->vna_other,
  1659. a->vna_nodename ? a->vna_nodename : "<corrupt>");
  1660. }
  1661. }
  1662. return true;
  1663. error_return:
  1664. free (dynbuf);
  1665. return false;
  1666. }
  1667. /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
  1668. and return symbol version for symbol version itself. */
  1669. const char *
  1670. _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
  1671. bool base_p,
  1672. bool *hidden)
  1673. {
  1674. const char *version_string = NULL;
  1675. if (elf_dynversym (abfd) != 0
  1676. && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
  1677. {
  1678. unsigned int vernum = ((elf_symbol_type *) symbol)->version;
  1679. *hidden = (vernum & VERSYM_HIDDEN) != 0;
  1680. vernum &= VERSYM_VERSION;
  1681. if (vernum == 0)
  1682. version_string = "";
  1683. else if (vernum == 1
  1684. && (vernum > elf_tdata (abfd)->cverdefs
  1685. || (elf_tdata (abfd)->verdef[0].vd_flags
  1686. == VER_FLG_BASE)))
  1687. version_string = base_p ? "Base" : "";
  1688. else if (vernum <= elf_tdata (abfd)->cverdefs)
  1689. {
  1690. const char *nodename
  1691. = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
  1692. version_string = "";
  1693. if (base_p
  1694. || nodename == NULL
  1695. || symbol->name == NULL
  1696. || strcmp (symbol->name, nodename) != 0)
  1697. version_string = nodename;
  1698. }
  1699. else
  1700. {
  1701. Elf_Internal_Verneed *t;
  1702. version_string = _("<corrupt>");
  1703. for (t = elf_tdata (abfd)->verref;
  1704. t != NULL;
  1705. t = t->vn_nextref)
  1706. {
  1707. Elf_Internal_Vernaux *a;
  1708. for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  1709. {
  1710. if (a->vna_other == vernum)
  1711. {
  1712. *hidden = true;
  1713. version_string = a->vna_nodename;
  1714. break;
  1715. }
  1716. }
  1717. }
  1718. }
  1719. }
  1720. return version_string;
  1721. }
  1722. /* Display ELF-specific fields of a symbol. */
  1723. void
  1724. bfd_elf_print_symbol (bfd *abfd,
  1725. void *filep,
  1726. asymbol *symbol,
  1727. bfd_print_symbol_type how)
  1728. {
  1729. FILE *file = (FILE *) filep;
  1730. switch (how)
  1731. {
  1732. case bfd_print_symbol_name:
  1733. fprintf (file, "%s", symbol->name);
  1734. break;
  1735. case bfd_print_symbol_more:
  1736. fprintf (file, "elf ");
  1737. bfd_fprintf_vma (abfd, file, symbol->value);
  1738. fprintf (file, " %x", symbol->flags);
  1739. break;
  1740. case bfd_print_symbol_all:
  1741. {
  1742. const char *section_name;
  1743. const char *name = NULL;
  1744. const struct elf_backend_data *bed;
  1745. unsigned char st_other;
  1746. bfd_vma val;
  1747. const char *version_string;
  1748. bool hidden;
  1749. section_name = symbol->section ? symbol->section->name : "(*none*)";
  1750. bed = get_elf_backend_data (abfd);
  1751. if (bed->elf_backend_print_symbol_all)
  1752. name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
  1753. if (name == NULL)
  1754. {
  1755. name = symbol->name;
  1756. bfd_print_symbol_vandf (abfd, file, symbol);
  1757. }
  1758. fprintf (file, " %s\t", section_name);
  1759. /* Print the "other" value for a symbol. For common symbols,
  1760. we've already printed the size; now print the alignment.
  1761. For other symbols, we have no specified alignment, and
  1762. we've printed the address; now print the size. */
  1763. if (symbol->section && bfd_is_com_section (symbol->section))
  1764. val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
  1765. else
  1766. val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
  1767. bfd_fprintf_vma (abfd, file, val);
  1768. /* If we have version information, print it. */
  1769. version_string = _bfd_elf_get_symbol_version_string (abfd,
  1770. symbol,
  1771. true,
  1772. &hidden);
  1773. if (version_string)
  1774. {
  1775. if (!hidden)
  1776. fprintf (file, " %-11s", version_string);
  1777. else
  1778. {
  1779. int i;
  1780. fprintf (file, " (%s)", version_string);
  1781. for (i = 10 - strlen (version_string); i > 0; --i)
  1782. putc (' ', file);
  1783. }
  1784. }
  1785. /* If the st_other field is not zero, print it. */
  1786. st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
  1787. switch (st_other)
  1788. {
  1789. case 0: break;
  1790. case STV_INTERNAL: fprintf (file, " .internal"); break;
  1791. case STV_HIDDEN: fprintf (file, " .hidden"); break;
  1792. case STV_PROTECTED: fprintf (file, " .protected"); break;
  1793. default:
  1794. /* Some other non-defined flags are also present, so print
  1795. everything hex. */
  1796. fprintf (file, " 0x%02x", (unsigned int) st_other);
  1797. }
  1798. fprintf (file, " %s", name);
  1799. }
  1800. break;
  1801. }
  1802. }
  1803. /* ELF .o/exec file reading */
  1804. /* Create a new bfd section from an ELF section header. */
  1805. bool
  1806. bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
  1807. {
  1808. Elf_Internal_Shdr *hdr;
  1809. Elf_Internal_Ehdr *ehdr;
  1810. const struct elf_backend_data *bed;
  1811. const char *name;
  1812. bool ret = true;
  1813. if (shindex >= elf_numsections (abfd))
  1814. return false;
  1815. /* PR17512: A corrupt ELF binary might contain a loop of sections via
  1816. sh_link or sh_info. Detect this here, by refusing to load a
  1817. section that we are already in the process of loading. */
  1818. if (elf_tdata (abfd)->being_created[shindex])
  1819. {
  1820. _bfd_error_handler
  1821. (_("%pB: warning: loop in section dependencies detected"), abfd);
  1822. return false;
  1823. }
  1824. elf_tdata (abfd)->being_created[shindex] = true;
  1825. hdr = elf_elfsections (abfd)[shindex];
  1826. ehdr = elf_elfheader (abfd);
  1827. name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
  1828. hdr->sh_name);
  1829. if (name == NULL)
  1830. goto fail;
  1831. bed = get_elf_backend_data (abfd);
  1832. switch (hdr->sh_type)
  1833. {
  1834. case SHT_NULL:
  1835. /* Inactive section. Throw it away. */
  1836. goto success;
  1837. case SHT_PROGBITS: /* Normal section with contents. */
  1838. case SHT_NOBITS: /* .bss section. */
  1839. case SHT_HASH: /* .hash section. */
  1840. case SHT_NOTE: /* .note section. */
  1841. case SHT_INIT_ARRAY: /* .init_array section. */
  1842. case SHT_FINI_ARRAY: /* .fini_array section. */
  1843. case SHT_PREINIT_ARRAY: /* .preinit_array section. */
  1844. case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
  1845. case SHT_GNU_HASH: /* .gnu.hash section. */
  1846. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
  1847. goto success;
  1848. case SHT_DYNAMIC: /* Dynamic linking information. */
  1849. if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  1850. goto fail;
  1851. if (hdr->sh_link > elf_numsections (abfd))
  1852. {
  1853. /* PR 10478: Accept Solaris binaries with a sh_link
  1854. field set to SHN_BEFORE or SHN_AFTER. */
  1855. switch (bfd_get_arch (abfd))
  1856. {
  1857. case bfd_arch_i386:
  1858. case bfd_arch_sparc:
  1859. if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
  1860. || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
  1861. break;
  1862. /* Otherwise fall through. */
  1863. default:
  1864. goto fail;
  1865. }
  1866. }
  1867. else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
  1868. goto fail;
  1869. else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
  1870. {
  1871. Elf_Internal_Shdr *dynsymhdr;
  1872. /* The shared libraries distributed with hpux11 have a bogus
  1873. sh_link field for the ".dynamic" section. Find the
  1874. string table for the ".dynsym" section instead. */
  1875. if (elf_dynsymtab (abfd) != 0)
  1876. {
  1877. dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
  1878. hdr->sh_link = dynsymhdr->sh_link;
  1879. }
  1880. else
  1881. {
  1882. unsigned int i, num_sec;
  1883. num_sec = elf_numsections (abfd);
  1884. for (i = 1; i < num_sec; i++)
  1885. {
  1886. dynsymhdr = elf_elfsections (abfd)[i];
  1887. if (dynsymhdr->sh_type == SHT_DYNSYM)
  1888. {
  1889. hdr->sh_link = dynsymhdr->sh_link;
  1890. break;
  1891. }
  1892. }
  1893. }
  1894. }
  1895. goto success;
  1896. case SHT_SYMTAB: /* A symbol table. */
  1897. if (elf_onesymtab (abfd) == shindex)
  1898. goto success;
  1899. if (hdr->sh_entsize != bed->s->sizeof_sym)
  1900. goto fail;
  1901. if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
  1902. {
  1903. if (hdr->sh_size != 0)
  1904. goto fail;
  1905. /* Some assemblers erroneously set sh_info to one with a
  1906. zero sh_size. ld sees this as a global symbol count
  1907. of (unsigned) -1. Fix it here. */
  1908. hdr->sh_info = 0;
  1909. goto success;
  1910. }
  1911. /* PR 18854: A binary might contain more than one symbol table.
  1912. Unusual, but possible. Warn, but continue. */
  1913. if (elf_onesymtab (abfd) != 0)
  1914. {
  1915. _bfd_error_handler
  1916. /* xgettext:c-format */
  1917. (_("%pB: warning: multiple symbol tables detected"
  1918. " - ignoring the table in section %u"),
  1919. abfd, shindex);
  1920. goto success;
  1921. }
  1922. elf_onesymtab (abfd) = shindex;
  1923. elf_symtab_hdr (abfd) = *hdr;
  1924. elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
  1925. abfd->flags |= HAS_SYMS;
  1926. /* Sometimes a shared object will map in the symbol table. If
  1927. SHF_ALLOC is set, and this is a shared object, then we also
  1928. treat this section as a BFD section. We can not base the
  1929. decision purely on SHF_ALLOC, because that flag is sometimes
  1930. set in a relocatable object file, which would confuse the
  1931. linker. */
  1932. if ((hdr->sh_flags & SHF_ALLOC) != 0
  1933. && (abfd->flags & DYNAMIC) != 0
  1934. && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
  1935. shindex))
  1936. goto fail;
  1937. /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
  1938. can't read symbols without that section loaded as well. It
  1939. is most likely specified by the next section header. */
  1940. {
  1941. elf_section_list * entry;
  1942. unsigned int i, num_sec;
  1943. for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
  1944. if (entry->hdr.sh_link == shindex)
  1945. goto success;
  1946. num_sec = elf_numsections (abfd);
  1947. for (i = shindex + 1; i < num_sec; i++)
  1948. {
  1949. Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
  1950. if (hdr2->sh_type == SHT_SYMTAB_SHNDX
  1951. && hdr2->sh_link == shindex)
  1952. break;
  1953. }
  1954. if (i == num_sec)
  1955. for (i = 1; i < shindex; i++)
  1956. {
  1957. Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
  1958. if (hdr2->sh_type == SHT_SYMTAB_SHNDX
  1959. && hdr2->sh_link == shindex)
  1960. break;
  1961. }
  1962. if (i != shindex)
  1963. ret = bfd_section_from_shdr (abfd, i);
  1964. /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
  1965. goto success;
  1966. }
  1967. case SHT_DYNSYM: /* A dynamic symbol table. */
  1968. if (elf_dynsymtab (abfd) == shindex)
  1969. goto success;
  1970. if (hdr->sh_entsize != bed->s->sizeof_sym)
  1971. goto fail;
  1972. if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
  1973. {
  1974. if (hdr->sh_size != 0)
  1975. goto fail;
  1976. /* Some linkers erroneously set sh_info to one with a
  1977. zero sh_size. ld sees this as a global symbol count
  1978. of (unsigned) -1. Fix it here. */
  1979. hdr->sh_info = 0;
  1980. goto success;
  1981. }
  1982. /* PR 18854: A binary might contain more than one dynamic symbol table.
  1983. Unusual, but possible. Warn, but continue. */
  1984. if (elf_dynsymtab (abfd) != 0)
  1985. {
  1986. _bfd_error_handler
  1987. /* xgettext:c-format */
  1988. (_("%pB: warning: multiple dynamic symbol tables detected"
  1989. " - ignoring the table in section %u"),
  1990. abfd, shindex);
  1991. goto success;
  1992. }
  1993. elf_dynsymtab (abfd) = shindex;
  1994. elf_tdata (abfd)->dynsymtab_hdr = *hdr;
  1995. elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  1996. abfd->flags |= HAS_SYMS;
  1997. /* Besides being a symbol table, we also treat this as a regular
  1998. section, so that objcopy can handle it. */
  1999. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
  2000. goto success;
  2001. case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
  2002. {
  2003. elf_section_list * entry;
  2004. for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
  2005. if (entry->ndx == shindex)
  2006. goto success;
  2007. entry = bfd_alloc (abfd, sizeof (*entry));
  2008. if (entry == NULL)
  2009. goto fail;
  2010. entry->ndx = shindex;
  2011. entry->hdr = * hdr;
  2012. entry->next = elf_symtab_shndx_list (abfd);
  2013. elf_symtab_shndx_list (abfd) = entry;
  2014. elf_elfsections (abfd)[shindex] = & entry->hdr;
  2015. goto success;
  2016. }
  2017. case SHT_STRTAB: /* A string table. */
  2018. if (hdr->bfd_section != NULL)
  2019. goto success;
  2020. if (ehdr->e_shstrndx == shindex)
  2021. {
  2022. elf_tdata (abfd)->shstrtab_hdr = *hdr;
  2023. elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
  2024. goto success;
  2025. }
  2026. if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
  2027. {
  2028. symtab_strtab:
  2029. elf_tdata (abfd)->strtab_hdr = *hdr;
  2030. elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
  2031. goto success;
  2032. }
  2033. if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
  2034. {
  2035. dynsymtab_strtab:
  2036. elf_tdata (abfd)->dynstrtab_hdr = *hdr;
  2037. hdr = &elf_tdata (abfd)->dynstrtab_hdr;
  2038. elf_elfsections (abfd)[shindex] = hdr;
  2039. /* We also treat this as a regular section, so that objcopy
  2040. can handle it. */
  2041. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
  2042. shindex);
  2043. goto success;
  2044. }
  2045. /* If the string table isn't one of the above, then treat it as a
  2046. regular section. We need to scan all the headers to be sure,
  2047. just in case this strtab section appeared before the above. */
  2048. if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
  2049. {
  2050. unsigned int i, num_sec;
  2051. num_sec = elf_numsections (abfd);
  2052. for (i = 1; i < num_sec; i++)
  2053. {
  2054. Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
  2055. if (hdr2->sh_link == shindex)
  2056. {
  2057. /* Prevent endless recursion on broken objects. */
  2058. if (i == shindex)
  2059. goto fail;
  2060. if (! bfd_section_from_shdr (abfd, i))
  2061. goto fail;
  2062. if (elf_onesymtab (abfd) == i)
  2063. goto symtab_strtab;
  2064. if (elf_dynsymtab (abfd) == i)
  2065. goto dynsymtab_strtab;
  2066. }
  2067. }
  2068. }
  2069. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
  2070. goto success;
  2071. case SHT_REL:
  2072. case SHT_RELA:
  2073. case SHT_RELR:
  2074. /* *These* do a lot of work -- but build no sections! */
  2075. {
  2076. asection *target_sect;
  2077. Elf_Internal_Shdr *hdr2, **p_hdr;
  2078. unsigned int num_sec = elf_numsections (abfd);
  2079. struct bfd_elf_section_data *esdt;
  2080. bfd_size_type size;
  2081. if (hdr->sh_type == SHT_REL)
  2082. size = bed->s->sizeof_rel;
  2083. else if (hdr->sh_type == SHT_RELA)
  2084. size = bed->s->sizeof_rela;
  2085. else
  2086. size = bed->s->arch_size / 8;
  2087. if (hdr->sh_entsize != size)
  2088. goto fail;
  2089. /* Check for a bogus link to avoid crashing. */
  2090. if (hdr->sh_link >= num_sec)
  2091. {
  2092. _bfd_error_handler
  2093. /* xgettext:c-format */
  2094. (_("%pB: invalid link %u for reloc section %s (index %u)"),
  2095. abfd, hdr->sh_link, name, shindex);
  2096. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
  2097. shindex);
  2098. goto success;
  2099. }
  2100. /* Get the symbol table. */
  2101. if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
  2102. || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
  2103. && ! bfd_section_from_shdr (abfd, hdr->sh_link))
  2104. goto fail;
  2105. /* If this is an alloc section in an executable or shared
  2106. library, or the reloc section does not use the main symbol
  2107. table we don't treat it as a reloc section. BFD can't
  2108. adequately represent such a section, so at least for now,
  2109. we don't try. We just present it as a normal section. We
  2110. also can't use it as a reloc section if it points to the
  2111. null section, an invalid section, another reloc section, or
  2112. its sh_link points to the null section. */
  2113. if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
  2114. && (hdr->sh_flags & SHF_ALLOC) != 0)
  2115. || hdr->sh_link == SHN_UNDEF
  2116. || hdr->sh_link != elf_onesymtab (abfd)
  2117. || hdr->sh_info == SHN_UNDEF
  2118. || hdr->sh_info >= num_sec
  2119. || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
  2120. || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
  2121. {
  2122. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
  2123. shindex);
  2124. goto success;
  2125. }
  2126. if (! bfd_section_from_shdr (abfd, hdr->sh_info))
  2127. goto fail;
  2128. target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
  2129. if (target_sect == NULL)
  2130. goto fail;
  2131. esdt = elf_section_data (target_sect);
  2132. if (hdr->sh_type == SHT_RELA)
  2133. p_hdr = &esdt->rela.hdr;
  2134. else
  2135. p_hdr = &esdt->rel.hdr;
  2136. /* PR 17512: file: 0b4f81b7.
  2137. Also see PR 24456, for a file which deliberately has two reloc
  2138. sections. */
  2139. if (*p_hdr != NULL)
  2140. {
  2141. if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
  2142. {
  2143. _bfd_error_handler
  2144. /* xgettext:c-format */
  2145. (_("%pB: warning: secondary relocation section '%s' "
  2146. "for section %pA found - ignoring"),
  2147. abfd, name, target_sect);
  2148. }
  2149. else
  2150. esdt->has_secondary_relocs = true;
  2151. goto success;
  2152. }
  2153. hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
  2154. if (hdr2 == NULL)
  2155. goto fail;
  2156. *hdr2 = *hdr;
  2157. *p_hdr = hdr2;
  2158. elf_elfsections (abfd)[shindex] = hdr2;
  2159. target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
  2160. * bed->s->int_rels_per_ext_rel);
  2161. target_sect->flags |= SEC_RELOC;
  2162. target_sect->relocation = NULL;
  2163. target_sect->rel_filepos = hdr->sh_offset;
  2164. /* In the section to which the relocations apply, mark whether
  2165. its relocations are of the REL or RELA variety. */
  2166. if (hdr->sh_size != 0)
  2167. {
  2168. if (hdr->sh_type == SHT_RELA)
  2169. target_sect->use_rela_p = 1;
  2170. }
  2171. abfd->flags |= HAS_RELOC;
  2172. goto success;
  2173. }
  2174. case SHT_GNU_verdef:
  2175. elf_dynverdef (abfd) = shindex;
  2176. elf_tdata (abfd)->dynverdef_hdr = *hdr;
  2177. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
  2178. goto success;
  2179. case SHT_GNU_versym:
  2180. if (hdr->sh_entsize != sizeof (Elf_External_Versym))
  2181. goto fail;
  2182. elf_dynversym (abfd) = shindex;
  2183. elf_tdata (abfd)->dynversym_hdr = *hdr;
  2184. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
  2185. goto success;
  2186. case SHT_GNU_verneed:
  2187. elf_dynverref (abfd) = shindex;
  2188. elf_tdata (abfd)->dynverref_hdr = *hdr;
  2189. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
  2190. goto success;
  2191. case SHT_SHLIB:
  2192. goto success;
  2193. case SHT_GROUP:
  2194. if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
  2195. goto fail;
  2196. if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  2197. goto fail;
  2198. goto success;
  2199. default:
  2200. /* Possibly an attributes section. */
  2201. if (hdr->sh_type == SHT_GNU_ATTRIBUTES
  2202. || hdr->sh_type == bed->obj_attrs_section_type)
  2203. {
  2204. if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  2205. goto fail;
  2206. _bfd_elf_parse_attributes (abfd, hdr);
  2207. goto success;
  2208. }
  2209. /* Check for any processor-specific section types. */
  2210. if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
  2211. goto success;
  2212. if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
  2213. {
  2214. if ((hdr->sh_flags & SHF_ALLOC) != 0)
  2215. /* FIXME: How to properly handle allocated section reserved
  2216. for applications? */
  2217. _bfd_error_handler
  2218. /* xgettext:c-format */
  2219. (_("%pB: unknown type [%#x] section `%s'"),
  2220. abfd, hdr->sh_type, name);
  2221. else
  2222. {
  2223. /* Allow sections reserved for applications. */
  2224. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
  2225. shindex);
  2226. goto success;
  2227. }
  2228. }
  2229. else if (hdr->sh_type >= SHT_LOPROC
  2230. && hdr->sh_type <= SHT_HIPROC)
  2231. /* FIXME: We should handle this section. */
  2232. _bfd_error_handler
  2233. /* xgettext:c-format */
  2234. (_("%pB: unknown type [%#x] section `%s'"),
  2235. abfd, hdr->sh_type, name);
  2236. else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
  2237. {
  2238. /* Unrecognised OS-specific sections. */
  2239. if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
  2240. /* SHF_OS_NONCONFORMING indicates that special knowledge is
  2241. required to correctly process the section and the file should
  2242. be rejected with an error message. */
  2243. _bfd_error_handler
  2244. /* xgettext:c-format */
  2245. (_("%pB: unknown type [%#x] section `%s'"),
  2246. abfd, hdr->sh_type, name);
  2247. else
  2248. {
  2249. /* Otherwise it should be processed. */
  2250. ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
  2251. goto success;
  2252. }
  2253. }
  2254. else
  2255. /* FIXME: We should handle this section. */
  2256. _bfd_error_handler
  2257. /* xgettext:c-format */
  2258. (_("%pB: unknown type [%#x] section `%s'"),
  2259. abfd, hdr->sh_type, name);
  2260. goto fail;
  2261. }
  2262. fail:
  2263. ret = false;
  2264. success:
  2265. elf_tdata (abfd)->being_created[shindex] = false;
  2266. return ret;
  2267. }
  2268. /* Return the local symbol specified by ABFD, R_SYMNDX. */
  2269. Elf_Internal_Sym *
  2270. bfd_sym_from_r_symndx (struct sym_cache *cache,
  2271. bfd *abfd,
  2272. unsigned long r_symndx)
  2273. {
  2274. unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
  2275. if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
  2276. {
  2277. Elf_Internal_Shdr *symtab_hdr;
  2278. unsigned char esym[sizeof (Elf64_External_Sym)];
  2279. Elf_External_Sym_Shndx eshndx;
  2280. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2281. if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
  2282. &cache->sym[ent], esym, &eshndx) == NULL)
  2283. return NULL;
  2284. if (cache->abfd != abfd)
  2285. {
  2286. memset (cache->indx, -1, sizeof (cache->indx));
  2287. cache->abfd = abfd;
  2288. }
  2289. cache->indx[ent] = r_symndx;
  2290. }
  2291. return &cache->sym[ent];
  2292. }
  2293. /* Given an ELF section number, retrieve the corresponding BFD
  2294. section. */
  2295. asection *
  2296. bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
  2297. {
  2298. if (sec_index >= elf_numsections (abfd))
  2299. return NULL;
  2300. return elf_elfsections (abfd)[sec_index]->bfd_section;
  2301. }
  2302. static const struct bfd_elf_special_section special_sections_b[] =
  2303. {
  2304. { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
  2305. { NULL, 0, 0, 0, 0 }
  2306. };
  2307. static const struct bfd_elf_special_section special_sections_c[] =
  2308. {
  2309. { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
  2310. { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
  2311. { NULL, 0, 0, 0, 0 }
  2312. };
  2313. static const struct bfd_elf_special_section special_sections_d[] =
  2314. {
  2315. { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  2316. { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  2317. /* There are more DWARF sections than these, but they needn't be added here
  2318. unless you have to cope with broken compilers that don't emit section
  2319. attributes or you want to help the user writing assembler. */
  2320. { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
  2321. { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
  2322. { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
  2323. { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
  2324. { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
  2325. { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
  2326. { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
  2327. { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
  2328. { NULL, 0, 0, 0, 0 }
  2329. };
  2330. static const struct bfd_elf_special_section special_sections_f[] =
  2331. {
  2332. { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
  2333. { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
  2334. { NULL, 0 , 0, 0, 0 }
  2335. };
  2336. static const struct bfd_elf_special_section special_sections_g[] =
  2337. {
  2338. { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
  2339. { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
  2340. { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  2341. { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
  2342. { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  2343. { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
  2344. { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
  2345. { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
  2346. { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
  2347. { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
  2348. { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
  2349. { NULL, 0, 0, 0, 0 }
  2350. };
  2351. static const struct bfd_elf_special_section special_sections_h[] =
  2352. {
  2353. { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
  2354. { NULL, 0, 0, 0, 0 }
  2355. };
  2356. static const struct bfd_elf_special_section special_sections_i[] =
  2357. {
  2358. { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
  2359. { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
  2360. { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
  2361. { NULL, 0, 0, 0, 0 }
  2362. };
  2363. static const struct bfd_elf_special_section special_sections_l[] =
  2364. {
  2365. { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
  2366. { NULL, 0, 0, 0, 0 }
  2367. };
  2368. static const struct bfd_elf_special_section special_sections_n[] =
  2369. {
  2370. { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
  2371. { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
  2372. { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
  2373. { NULL, 0, 0, 0, 0 }
  2374. };
  2375. static const struct bfd_elf_special_section special_sections_p[] =
  2376. {
  2377. { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
  2378. { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  2379. { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
  2380. { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
  2381. { NULL, 0, 0, 0, 0 }
  2382. };
  2383. static const struct bfd_elf_special_section special_sections_r[] =
  2384. {
  2385. { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
  2386. { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
  2387. { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
  2388. { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
  2389. { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
  2390. { NULL, 0, 0, 0, 0 }
  2391. };
  2392. static const struct bfd_elf_special_section special_sections_s[] =
  2393. {
  2394. { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
  2395. { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
  2396. { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
  2397. /* See struct bfd_elf_special_section declaration for the semantics of
  2398. this special case where .prefix_length != strlen (.prefix). */
  2399. { ".stabstr", 5, 3, SHT_STRTAB, 0 },
  2400. { NULL, 0, 0, 0, 0 }
  2401. };
  2402. static const struct bfd_elf_special_section special_sections_t[] =
  2403. {
  2404. { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
  2405. { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
  2406. { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
  2407. { NULL, 0, 0, 0, 0 }
  2408. };
  2409. static const struct bfd_elf_special_section special_sections_z[] =
  2410. {
  2411. { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
  2412. { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
  2413. { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
  2414. { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
  2415. { NULL, 0, 0, 0, 0 }
  2416. };
  2417. static const struct bfd_elf_special_section * const special_sections[] =
  2418. {
  2419. special_sections_b, /* 'b' */
  2420. special_sections_c, /* 'c' */
  2421. special_sections_d, /* 'd' */
  2422. NULL, /* 'e' */
  2423. special_sections_f, /* 'f' */
  2424. special_sections_g, /* 'g' */
  2425. special_sections_h, /* 'h' */
  2426. special_sections_i, /* 'i' */
  2427. NULL, /* 'j' */
  2428. NULL, /* 'k' */
  2429. special_sections_l, /* 'l' */
  2430. NULL, /* 'm' */
  2431. special_sections_n, /* 'n' */
  2432. NULL, /* 'o' */
  2433. special_sections_p, /* 'p' */
  2434. NULL, /* 'q' */
  2435. special_sections_r, /* 'r' */
  2436. special_sections_s, /* 's' */
  2437. special_sections_t, /* 't' */
  2438. NULL, /* 'u' */
  2439. NULL, /* 'v' */
  2440. NULL, /* 'w' */
  2441. NULL, /* 'x' */
  2442. NULL, /* 'y' */
  2443. special_sections_z /* 'z' */
  2444. };
  2445. const struct bfd_elf_special_section *
  2446. _bfd_elf_get_special_section (const char *name,
  2447. const struct bfd_elf_special_section *spec,
  2448. unsigned int rela)
  2449. {
  2450. int i;
  2451. int len;
  2452. len = strlen (name);
  2453. for (i = 0; spec[i].prefix != NULL; i++)
  2454. {
  2455. int suffix_len;
  2456. int prefix_len = spec[i].prefix_length;
  2457. if (len < prefix_len)
  2458. continue;
  2459. if (memcmp (name, spec[i].prefix, prefix_len) != 0)
  2460. continue;
  2461. suffix_len = spec[i].suffix_length;
  2462. if (suffix_len <= 0)
  2463. {
  2464. if (name[prefix_len] != 0)
  2465. {
  2466. if (suffix_len == 0)
  2467. continue;
  2468. if (name[prefix_len] != '.'
  2469. && (suffix_len == -2
  2470. || (rela && spec[i].type == SHT_REL)))
  2471. continue;
  2472. }
  2473. }
  2474. else
  2475. {
  2476. if (len < prefix_len + suffix_len)
  2477. continue;
  2478. if (memcmp (name + len - suffix_len,
  2479. spec[i].prefix + prefix_len,
  2480. suffix_len) != 0)
  2481. continue;
  2482. }
  2483. return &spec[i];
  2484. }
  2485. return NULL;
  2486. }
  2487. const struct bfd_elf_special_section *
  2488. _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
  2489. {
  2490. int i;
  2491. const struct bfd_elf_special_section *spec;
  2492. const struct elf_backend_data *bed;
  2493. /* See if this is one of the special sections. */
  2494. if (sec->name == NULL)
  2495. return NULL;
  2496. bed = get_elf_backend_data (abfd);
  2497. spec = bed->special_sections;
  2498. if (spec)
  2499. {
  2500. spec = _bfd_elf_get_special_section (sec->name,
  2501. bed->special_sections,
  2502. sec->use_rela_p);
  2503. if (spec != NULL)
  2504. return spec;
  2505. }
  2506. if (sec->name[0] != '.')
  2507. return NULL;
  2508. i = sec->name[1] - 'b';
  2509. if (i < 0 || i > 'z' - 'b')
  2510. return NULL;
  2511. spec = special_sections[i];
  2512. if (spec == NULL)
  2513. return NULL;
  2514. return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
  2515. }
  2516. bool
  2517. _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
  2518. {
  2519. struct bfd_elf_section_data *sdata;
  2520. const struct elf_backend_data *bed;
  2521. const struct bfd_elf_special_section *ssect;
  2522. sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
  2523. if (sdata == NULL)
  2524. {
  2525. sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
  2526. sizeof (*sdata));
  2527. if (sdata == NULL)
  2528. return false;
  2529. sec->used_by_bfd = sdata;
  2530. }
  2531. /* Indicate whether or not this section should use RELA relocations. */
  2532. bed = get_elf_backend_data (abfd);
  2533. sec->use_rela_p = bed->default_use_rela_p;
  2534. /* Set up ELF section type and flags for newly created sections, if
  2535. there is an ABI mandated section. */
  2536. ssect = (*bed->get_sec_type_attr) (abfd, sec);
  2537. if (ssect != NULL)
  2538. {
  2539. elf_section_type (sec) = ssect->type;
  2540. elf_section_flags (sec) = ssect->attr;
  2541. }
  2542. return _bfd_generic_new_section_hook (abfd, sec);
  2543. }
  2544. /* Create a new bfd section from an ELF program header.
  2545. Since program segments have no names, we generate a synthetic name
  2546. of the form segment<NUM>, where NUM is generally the index in the
  2547. program header table. For segments that are split (see below) we
  2548. generate the names segment<NUM>a and segment<NUM>b.
  2549. Note that some program segments may have a file size that is different than
  2550. (less than) the memory size. All this means is that at execution the
  2551. system must allocate the amount of memory specified by the memory size,
  2552. but only initialize it with the first "file size" bytes read from the
  2553. file. This would occur for example, with program segments consisting
  2554. of combined data+bss.
  2555. To handle the above situation, this routine generates TWO bfd sections
  2556. for the single program segment. The first has the length specified by
  2557. the file size of the segment, and the second has the length specified
  2558. by the difference between the two sizes. In effect, the segment is split
  2559. into its initialized and uninitialized parts.
  2560. */
  2561. bool
  2562. _bfd_elf_make_section_from_phdr (bfd *abfd,
  2563. Elf_Internal_Phdr *hdr,
  2564. int hdr_index,
  2565. const char *type_name)
  2566. {
  2567. asection *newsect;
  2568. char *name;
  2569. char namebuf[64];
  2570. size_t len;
  2571. int split;
  2572. unsigned int opb = bfd_octets_per_byte (abfd, NULL);
  2573. split = ((hdr->p_memsz > 0)
  2574. && (hdr->p_filesz > 0)
  2575. && (hdr->p_memsz > hdr->p_filesz));
  2576. if (hdr->p_filesz > 0)
  2577. {
  2578. sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
  2579. len = strlen (namebuf) + 1;
  2580. name = (char *) bfd_alloc (abfd, len);
  2581. if (!name)
  2582. return false;
  2583. memcpy (name, namebuf, len);
  2584. newsect = bfd_make_section (abfd, name);
  2585. if (newsect == NULL)
  2586. return false;
  2587. newsect->vma = hdr->p_vaddr / opb;
  2588. newsect->lma = hdr->p_paddr / opb;
  2589. newsect->size = hdr->p_filesz;
  2590. newsect->filepos = hdr->p_offset;
  2591. newsect->flags |= SEC_HAS_CONTENTS;
  2592. newsect->alignment_power = bfd_log2 (hdr->p_align);
  2593. if (hdr->p_type == PT_LOAD)
  2594. {
  2595. newsect->flags |= SEC_ALLOC;
  2596. newsect->flags |= SEC_LOAD;
  2597. if (hdr->p_flags & PF_X)
  2598. {
  2599. /* FIXME: all we known is that it has execute PERMISSION,
  2600. may be data. */
  2601. newsect->flags |= SEC_CODE;
  2602. }
  2603. }
  2604. if (!(hdr->p_flags & PF_W))
  2605. {
  2606. newsect->flags |= SEC_READONLY;
  2607. }
  2608. }
  2609. if (hdr->p_memsz > hdr->p_filesz)
  2610. {
  2611. bfd_vma align;
  2612. sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
  2613. len = strlen (namebuf) + 1;
  2614. name = (char *) bfd_alloc (abfd, len);
  2615. if (!name)
  2616. return false;
  2617. memcpy (name, namebuf, len);
  2618. newsect = bfd_make_section (abfd, name);
  2619. if (newsect == NULL)
  2620. return false;
  2621. newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
  2622. newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
  2623. newsect->size = hdr->p_memsz - hdr->p_filesz;
  2624. newsect->filepos = hdr->p_offset + hdr->p_filesz;
  2625. align = newsect->vma & -newsect->vma;
  2626. if (align == 0 || align > hdr->p_align)
  2627. align = hdr->p_align;
  2628. newsect->alignment_power = bfd_log2 (align);
  2629. if (hdr->p_type == PT_LOAD)
  2630. {
  2631. newsect->flags |= SEC_ALLOC;
  2632. if (hdr->p_flags & PF_X)
  2633. newsect->flags |= SEC_CODE;
  2634. }
  2635. if (!(hdr->p_flags & PF_W))
  2636. newsect->flags |= SEC_READONLY;
  2637. }
  2638. return true;
  2639. }
  2640. static bool
  2641. _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
  2642. {
  2643. /* The return value is ignored. Build-ids are considered optional. */
  2644. if (templ->xvec->flavour == bfd_target_elf_flavour)
  2645. return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
  2646. (templ, offset);
  2647. return false;
  2648. }
  2649. bool
  2650. bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
  2651. {
  2652. const struct elf_backend_data *bed;
  2653. switch (hdr->p_type)
  2654. {
  2655. case PT_NULL:
  2656. return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
  2657. case PT_LOAD:
  2658. if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
  2659. return false;
  2660. if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
  2661. _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
  2662. return true;
  2663. case PT_DYNAMIC:
  2664. return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
  2665. case PT_INTERP:
  2666. return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
  2667. case PT_NOTE:
  2668. if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
  2669. return false;
  2670. if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
  2671. hdr->p_align))
  2672. return false;
  2673. return true;
  2674. case PT_SHLIB:
  2675. return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
  2676. case PT_PHDR:
  2677. return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
  2678. case PT_GNU_EH_FRAME:
  2679. return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
  2680. "eh_frame_hdr");
  2681. case PT_GNU_STACK:
  2682. return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
  2683. case PT_GNU_RELRO:
  2684. return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
  2685. default:
  2686. /* Check for any processor-specific program segment types. */
  2687. bed = get_elf_backend_data (abfd);
  2688. return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
  2689. }
  2690. }
  2691. /* Return the REL_HDR for SEC, assuming there is only a single one, either
  2692. REL or RELA. */
  2693. Elf_Internal_Shdr *
  2694. _bfd_elf_single_rel_hdr (asection *sec)
  2695. {
  2696. if (elf_section_data (sec)->rel.hdr)
  2697. {
  2698. BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
  2699. return elf_section_data (sec)->rel.hdr;
  2700. }
  2701. else
  2702. return elf_section_data (sec)->rela.hdr;
  2703. }
  2704. static bool
  2705. _bfd_elf_set_reloc_sh_name (bfd *abfd,
  2706. Elf_Internal_Shdr *rel_hdr,
  2707. const char *sec_name,
  2708. bool use_rela_p)
  2709. {
  2710. char *name = (char *) bfd_alloc (abfd,
  2711. sizeof ".rela" + strlen (sec_name));
  2712. if (name == NULL)
  2713. return false;
  2714. sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
  2715. rel_hdr->sh_name =
  2716. (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
  2717. false);
  2718. if (rel_hdr->sh_name == (unsigned int) -1)
  2719. return false;
  2720. return true;
  2721. }
  2722. /* Allocate and initialize a section-header for a new reloc section,
  2723. containing relocations against ASECT. It is stored in RELDATA. If
  2724. USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
  2725. relocations. */
  2726. static bool
  2727. _bfd_elf_init_reloc_shdr (bfd *abfd,
  2728. struct bfd_elf_section_reloc_data *reldata,
  2729. const char *sec_name,
  2730. bool use_rela_p,
  2731. bool delay_st_name_p)
  2732. {
  2733. Elf_Internal_Shdr *rel_hdr;
  2734. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2735. BFD_ASSERT (reldata->hdr == NULL);
  2736. rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
  2737. reldata->hdr = rel_hdr;
  2738. if (delay_st_name_p)
  2739. rel_hdr->sh_name = (unsigned int) -1;
  2740. else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
  2741. use_rela_p))
  2742. return false;
  2743. rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
  2744. rel_hdr->sh_entsize = (use_rela_p
  2745. ? bed->s->sizeof_rela
  2746. : bed->s->sizeof_rel);
  2747. rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
  2748. rel_hdr->sh_flags = 0;
  2749. rel_hdr->sh_addr = 0;
  2750. rel_hdr->sh_size = 0;
  2751. rel_hdr->sh_offset = 0;
  2752. return true;
  2753. }
  2754. /* Return the default section type based on the passed in section flags. */
  2755. int
  2756. bfd_elf_get_default_section_type (flagword flags)
  2757. {
  2758. if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
  2759. && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
  2760. return SHT_NOBITS;
  2761. return SHT_PROGBITS;
  2762. }
  2763. struct fake_section_arg
  2764. {
  2765. struct bfd_link_info *link_info;
  2766. bool failed;
  2767. };
  2768. /* Set up an ELF internal section header for a section. */
  2769. static void
  2770. elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
  2771. {
  2772. struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
  2773. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2774. struct bfd_elf_section_data *esd = elf_section_data (asect);
  2775. Elf_Internal_Shdr *this_hdr;
  2776. unsigned int sh_type;
  2777. const char *name = asect->name;
  2778. bool delay_st_name_p = false;
  2779. bfd_vma mask;
  2780. if (arg->failed)
  2781. {
  2782. /* We already failed; just get out of the bfd_map_over_sections
  2783. loop. */
  2784. return;
  2785. }
  2786. this_hdr = &esd->this_hdr;
  2787. if (arg->link_info)
  2788. {
  2789. /* ld: compress DWARF debug sections with names: .debug_*. */
  2790. if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
  2791. && (asect->flags & SEC_DEBUGGING)
  2792. && name[1] == 'd'
  2793. && name[6] == '_')
  2794. {
  2795. /* Set SEC_ELF_COMPRESS to indicate this section should be
  2796. compressed. */
  2797. asect->flags |= SEC_ELF_COMPRESS;
  2798. /* If this section will be compressed, delay adding section
  2799. name to section name section after it is compressed in
  2800. _bfd_elf_assign_file_positions_for_non_load. */
  2801. delay_st_name_p = true;
  2802. }
  2803. }
  2804. else if ((asect->flags & SEC_ELF_RENAME))
  2805. {
  2806. /* objcopy: rename output DWARF debug section. */
  2807. if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
  2808. {
  2809. /* When we decompress or compress with SHF_COMPRESSED,
  2810. convert section name from .zdebug_* to .debug_* if
  2811. needed. */
  2812. if (name[1] == 'z')
  2813. {
  2814. char *new_name = convert_zdebug_to_debug (abfd, name);
  2815. if (new_name == NULL)
  2816. {
  2817. arg->failed = true;
  2818. return;
  2819. }
  2820. name = new_name;
  2821. }
  2822. }
  2823. else if (asect->compress_status == COMPRESS_SECTION_DONE)
  2824. {
  2825. /* PR binutils/18087: Compression does not always make a
  2826. section smaller. So only rename the section when
  2827. compression has actually taken place. If input section
  2828. name is .zdebug_*, we should never compress it again. */
  2829. char *new_name = convert_debug_to_zdebug (abfd, name);
  2830. if (new_name == NULL)
  2831. {
  2832. arg->failed = true;
  2833. return;
  2834. }
  2835. BFD_ASSERT (name[1] != 'z');
  2836. name = new_name;
  2837. }
  2838. }
  2839. if (delay_st_name_p)
  2840. this_hdr->sh_name = (unsigned int) -1;
  2841. else
  2842. {
  2843. this_hdr->sh_name
  2844. = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
  2845. name, false);
  2846. if (this_hdr->sh_name == (unsigned int) -1)
  2847. {
  2848. arg->failed = true;
  2849. return;
  2850. }
  2851. }
  2852. /* Don't clear sh_flags. Assembler may set additional bits. */
  2853. if ((asect->flags & SEC_ALLOC) != 0
  2854. || asect->user_set_vma)
  2855. this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
  2856. else
  2857. this_hdr->sh_addr = 0;
  2858. this_hdr->sh_offset = 0;
  2859. this_hdr->sh_size = asect->size;
  2860. this_hdr->sh_link = 0;
  2861. /* PR 17512: file: 0eb809fe, 8b0535ee. */
  2862. if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
  2863. {
  2864. _bfd_error_handler
  2865. /* xgettext:c-format */
  2866. (_("%pB: error: alignment power %d of section `%pA' is too big"),
  2867. abfd, asect->alignment_power, asect);
  2868. arg->failed = true;
  2869. return;
  2870. }
  2871. /* Set sh_addralign to the highest power of two given by alignment
  2872. consistent with the section VMA. Linker scripts can force VMA. */
  2873. mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
  2874. this_hdr->sh_addralign = mask & -mask;
  2875. /* The sh_entsize and sh_info fields may have been set already by
  2876. copy_private_section_data. */
  2877. this_hdr->bfd_section = asect;
  2878. this_hdr->contents = NULL;
  2879. /* If the section type is unspecified, we set it based on
  2880. asect->flags. */
  2881. if (asect->type != 0)
  2882. sh_type = asect->type;
  2883. else if ((asect->flags & SEC_GROUP) != 0)
  2884. sh_type = SHT_GROUP;
  2885. else
  2886. sh_type = bfd_elf_get_default_section_type (asect->flags);
  2887. if (this_hdr->sh_type == SHT_NULL)
  2888. this_hdr->sh_type = sh_type;
  2889. else if (this_hdr->sh_type == SHT_NOBITS
  2890. && sh_type == SHT_PROGBITS
  2891. && (asect->flags & SEC_ALLOC) != 0)
  2892. {
  2893. /* Warn if we are changing a NOBITS section to PROGBITS, but
  2894. allow the link to proceed. This can happen when users link
  2895. non-bss input sections to bss output sections, or emit data
  2896. to a bss output section via a linker script. */
  2897. _bfd_error_handler
  2898. (_("warning: section `%pA' type changed to PROGBITS"), asect);
  2899. this_hdr->sh_type = sh_type;
  2900. }
  2901. switch (this_hdr->sh_type)
  2902. {
  2903. default:
  2904. break;
  2905. case SHT_STRTAB:
  2906. case SHT_NOTE:
  2907. case SHT_NOBITS:
  2908. case SHT_PROGBITS:
  2909. break;
  2910. case SHT_INIT_ARRAY:
  2911. case SHT_FINI_ARRAY:
  2912. case SHT_PREINIT_ARRAY:
  2913. this_hdr->sh_entsize = bed->s->arch_size / 8;
  2914. break;
  2915. case SHT_HASH:
  2916. this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
  2917. break;
  2918. case SHT_DYNSYM:
  2919. this_hdr->sh_entsize = bed->s->sizeof_sym;
  2920. break;
  2921. case SHT_DYNAMIC:
  2922. this_hdr->sh_entsize = bed->s->sizeof_dyn;
  2923. break;
  2924. case SHT_RELA:
  2925. if (get_elf_backend_data (abfd)->may_use_rela_p)
  2926. this_hdr->sh_entsize = bed->s->sizeof_rela;
  2927. break;
  2928. case SHT_REL:
  2929. if (get_elf_backend_data (abfd)->may_use_rel_p)
  2930. this_hdr->sh_entsize = bed->s->sizeof_rel;
  2931. break;
  2932. case SHT_GNU_versym:
  2933. this_hdr->sh_entsize = sizeof (Elf_External_Versym);
  2934. break;
  2935. case SHT_GNU_verdef:
  2936. this_hdr->sh_entsize = 0;
  2937. /* objcopy or strip will copy over sh_info, but may not set
  2938. cverdefs. The linker will set cverdefs, but sh_info will be
  2939. zero. */
  2940. if (this_hdr->sh_info == 0)
  2941. this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
  2942. else
  2943. BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
  2944. || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
  2945. break;
  2946. case SHT_GNU_verneed:
  2947. this_hdr->sh_entsize = 0;
  2948. /* objcopy or strip will copy over sh_info, but may not set
  2949. cverrefs. The linker will set cverrefs, but sh_info will be
  2950. zero. */
  2951. if (this_hdr->sh_info == 0)
  2952. this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
  2953. else
  2954. BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
  2955. || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
  2956. break;
  2957. case SHT_GROUP:
  2958. this_hdr->sh_entsize = GRP_ENTRY_SIZE;
  2959. break;
  2960. case SHT_GNU_HASH:
  2961. this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
  2962. break;
  2963. }
  2964. if ((asect->flags & SEC_ALLOC) != 0)
  2965. this_hdr->sh_flags |= SHF_ALLOC;
  2966. if ((asect->flags & SEC_READONLY) == 0)
  2967. this_hdr->sh_flags |= SHF_WRITE;
  2968. if ((asect->flags & SEC_CODE) != 0)
  2969. this_hdr->sh_flags |= SHF_EXECINSTR;
  2970. if ((asect->flags & SEC_MERGE) != 0)
  2971. {
  2972. this_hdr->sh_flags |= SHF_MERGE;
  2973. this_hdr->sh_entsize = asect->entsize;
  2974. }
  2975. if ((asect->flags & SEC_STRINGS) != 0)
  2976. this_hdr->sh_flags |= SHF_STRINGS;
  2977. if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
  2978. this_hdr->sh_flags |= SHF_GROUP;
  2979. if ((asect->flags & SEC_THREAD_LOCAL) != 0)
  2980. {
  2981. this_hdr->sh_flags |= SHF_TLS;
  2982. if (asect->size == 0
  2983. && (asect->flags & SEC_HAS_CONTENTS) == 0)
  2984. {
  2985. struct bfd_link_order *o = asect->map_tail.link_order;
  2986. this_hdr->sh_size = 0;
  2987. if (o != NULL)
  2988. {
  2989. this_hdr->sh_size = o->offset + o->size;
  2990. if (this_hdr->sh_size != 0)
  2991. this_hdr->sh_type = SHT_NOBITS;
  2992. }
  2993. }
  2994. }
  2995. if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
  2996. this_hdr->sh_flags |= SHF_EXCLUDE;
  2997. /* If the section has relocs, set up a section header for the
  2998. SHT_REL[A] section. If two relocation sections are required for
  2999. this section, it is up to the processor-specific back-end to
  3000. create the other. */
  3001. if ((asect->flags & SEC_RELOC) != 0)
  3002. {
  3003. /* When doing a relocatable link, create both REL and RELA sections if
  3004. needed. */
  3005. if (arg->link_info
  3006. /* Do the normal setup if we wouldn't create any sections here. */
  3007. && esd->rel.count + esd->rela.count > 0
  3008. && (bfd_link_relocatable (arg->link_info)
  3009. || arg->link_info->emitrelocations))
  3010. {
  3011. if (esd->rel.count && esd->rel.hdr == NULL
  3012. && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
  3013. false, delay_st_name_p))
  3014. {
  3015. arg->failed = true;
  3016. return;
  3017. }
  3018. if (esd->rela.count && esd->rela.hdr == NULL
  3019. && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
  3020. true, delay_st_name_p))
  3021. {
  3022. arg->failed = true;
  3023. return;
  3024. }
  3025. }
  3026. else if (!_bfd_elf_init_reloc_shdr (abfd,
  3027. (asect->use_rela_p
  3028. ? &esd->rela : &esd->rel),
  3029. name,
  3030. asect->use_rela_p,
  3031. delay_st_name_p))
  3032. {
  3033. arg->failed = true;
  3034. return;
  3035. }
  3036. }
  3037. /* Check for processor-specific section types. */
  3038. sh_type = this_hdr->sh_type;
  3039. if (bed->elf_backend_fake_sections
  3040. && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
  3041. {
  3042. arg->failed = true;
  3043. return;
  3044. }
  3045. if (sh_type == SHT_NOBITS && asect->size != 0)
  3046. {
  3047. /* Don't change the header type from NOBITS if we are being
  3048. called for objcopy --only-keep-debug. */
  3049. this_hdr->sh_type = sh_type;
  3050. }
  3051. }
  3052. /* Fill in the contents of a SHT_GROUP section. Called from
  3053. _bfd_elf_compute_section_file_positions for gas, objcopy, and
  3054. when ELF targets use the generic linker, ld. Called for ld -r
  3055. from bfd_elf_final_link. */
  3056. void
  3057. bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
  3058. {
  3059. bool *failedptr = (bool *) failedptrarg;
  3060. asection *elt, *first;
  3061. unsigned char *loc;
  3062. bool gas;
  3063. /* Ignore linker created group section. See elfNN_ia64_object_p in
  3064. elfxx-ia64.c. */
  3065. if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
  3066. || sec->size == 0
  3067. || *failedptr)
  3068. return;
  3069. if (elf_section_data (sec)->this_hdr.sh_info == 0)
  3070. {
  3071. unsigned long symindx = 0;
  3072. /* elf_group_id will have been set up by objcopy and the
  3073. generic linker. */
  3074. if (elf_group_id (sec) != NULL)
  3075. symindx = elf_group_id (sec)->udata.i;
  3076. if (symindx == 0)
  3077. {
  3078. /* If called from the assembler, swap_out_syms will have set up
  3079. elf_section_syms.
  3080. PR 25699: A corrupt input file could contain bogus group info. */
  3081. if (sec->index >= elf_num_section_syms (abfd)
  3082. || elf_section_syms (abfd)[sec->index] == NULL)
  3083. {
  3084. *failedptr = true;
  3085. return;
  3086. }
  3087. symindx = elf_section_syms (abfd)[sec->index]->udata.i;
  3088. }
  3089. elf_section_data (sec)->this_hdr.sh_info = symindx;
  3090. }
  3091. else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
  3092. {
  3093. /* The ELF backend linker sets sh_info to -2 when the group
  3094. signature symbol is global, and thus the index can't be
  3095. set until all local symbols are output. */
  3096. asection *igroup;
  3097. struct bfd_elf_section_data *sec_data;
  3098. unsigned long symndx;
  3099. unsigned long extsymoff;
  3100. struct elf_link_hash_entry *h;
  3101. /* The point of this little dance to the first SHF_GROUP section
  3102. then back to the SHT_GROUP section is that this gets us to
  3103. the SHT_GROUP in the input object. */
  3104. igroup = elf_sec_group (elf_next_in_group (sec));
  3105. sec_data = elf_section_data (igroup);
  3106. symndx = sec_data->this_hdr.sh_info;
  3107. extsymoff = 0;
  3108. if (!elf_bad_symtab (igroup->owner))
  3109. {
  3110. Elf_Internal_Shdr *symtab_hdr;
  3111. symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
  3112. extsymoff = symtab_hdr->sh_info;
  3113. }
  3114. h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
  3115. while (h->root.type == bfd_link_hash_indirect
  3116. || h->root.type == bfd_link_hash_warning)
  3117. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  3118. elf_section_data (sec)->this_hdr.sh_info = h->indx;
  3119. }
  3120. /* The contents won't be allocated for "ld -r" or objcopy. */
  3121. gas = true;
  3122. if (sec->contents == NULL)
  3123. {
  3124. gas = false;
  3125. sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
  3126. /* Arrange for the section to be written out. */
  3127. elf_section_data (sec)->this_hdr.contents = sec->contents;
  3128. if (sec->contents == NULL)
  3129. {
  3130. *failedptr = true;
  3131. return;
  3132. }
  3133. }
  3134. loc = sec->contents + sec->size;
  3135. /* Get the pointer to the first section in the group that gas
  3136. squirreled away here. objcopy arranges for this to be set to the
  3137. start of the input section group. */
  3138. first = elt = elf_next_in_group (sec);
  3139. /* First element is a flag word. Rest of section is elf section
  3140. indices for all the sections of the group. Write them backwards
  3141. just to keep the group in the same order as given in .section
  3142. directives, not that it matters. */
  3143. while (elt != NULL)
  3144. {
  3145. asection *s;
  3146. s = elt;
  3147. if (!gas)
  3148. s = s->output_section;
  3149. if (s != NULL
  3150. && !bfd_is_abs_section (s))
  3151. {
  3152. struct bfd_elf_section_data *elf_sec = elf_section_data (s);
  3153. struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
  3154. if (elf_sec->rel.hdr != NULL
  3155. && (gas
  3156. || (input_elf_sec->rel.hdr != NULL
  3157. && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
  3158. {
  3159. elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
  3160. loc -= 4;
  3161. H_PUT_32 (abfd, elf_sec->rel.idx, loc);
  3162. }
  3163. if (elf_sec->rela.hdr != NULL
  3164. && (gas
  3165. || (input_elf_sec->rela.hdr != NULL
  3166. && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
  3167. {
  3168. elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
  3169. loc -= 4;
  3170. H_PUT_32 (abfd, elf_sec->rela.idx, loc);
  3171. }
  3172. loc -= 4;
  3173. H_PUT_32 (abfd, elf_sec->this_idx, loc);
  3174. }
  3175. elt = elf_next_in_group (elt);
  3176. if (elt == first)
  3177. break;
  3178. }
  3179. loc -= 4;
  3180. BFD_ASSERT (loc == sec->contents);
  3181. H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
  3182. }
  3183. /* Given NAME, the name of a relocation section stripped of its
  3184. .rel/.rela prefix, return the section in ABFD to which the
  3185. relocations apply. */
  3186. asection *
  3187. _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
  3188. {
  3189. /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
  3190. section likely apply to .got.plt or .got section. */
  3191. if (get_elf_backend_data (abfd)->want_got_plt
  3192. && strcmp (name, ".plt") == 0)
  3193. {
  3194. asection *sec;
  3195. name = ".got.plt";
  3196. sec = bfd_get_section_by_name (abfd, name);
  3197. if (sec != NULL)
  3198. return sec;
  3199. name = ".got";
  3200. }
  3201. return bfd_get_section_by_name (abfd, name);
  3202. }
  3203. /* Return the section to which RELOC_SEC applies. */
  3204. static asection *
  3205. elf_get_reloc_section (asection *reloc_sec)
  3206. {
  3207. const char *name;
  3208. unsigned int type;
  3209. bfd *abfd;
  3210. const struct elf_backend_data *bed;
  3211. type = elf_section_data (reloc_sec)->this_hdr.sh_type;
  3212. if (type != SHT_REL && type != SHT_RELA)
  3213. return NULL;
  3214. /* We look up the section the relocs apply to by name. */
  3215. name = reloc_sec->name;
  3216. if (!startswith (name, ".rel"))
  3217. return NULL;
  3218. name += 4;
  3219. if (type == SHT_RELA && *name++ != 'a')
  3220. return NULL;
  3221. abfd = reloc_sec->owner;
  3222. bed = get_elf_backend_data (abfd);
  3223. return bed->get_reloc_section (abfd, name);
  3224. }
  3225. /* Assign all ELF section numbers. The dummy first section is handled here
  3226. too. The link/info pointers for the standard section types are filled
  3227. in here too, while we're at it. LINK_INFO will be 0 when arriving
  3228. here for objcopy, and when using the generic ELF linker. */
  3229. static bool
  3230. assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
  3231. {
  3232. struct elf_obj_tdata *t = elf_tdata (abfd);
  3233. asection *sec;
  3234. unsigned int section_number;
  3235. Elf_Internal_Shdr **i_shdrp;
  3236. struct bfd_elf_section_data *d;
  3237. bool need_symtab;
  3238. size_t amt;
  3239. section_number = 1;
  3240. _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
  3241. /* SHT_GROUP sections are in relocatable files only. */
  3242. if (link_info == NULL || !link_info->resolve_section_groups)
  3243. {
  3244. size_t reloc_count = 0;
  3245. /* Put SHT_GROUP sections first. */
  3246. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  3247. {
  3248. d = elf_section_data (sec);
  3249. if (d->this_hdr.sh_type == SHT_GROUP)
  3250. {
  3251. if (sec->flags & SEC_LINKER_CREATED)
  3252. {
  3253. /* Remove the linker created SHT_GROUP sections. */
  3254. bfd_section_list_remove (abfd, sec);
  3255. abfd->section_count--;
  3256. }
  3257. else
  3258. d->this_idx = section_number++;
  3259. }
  3260. /* Count relocations. */
  3261. reloc_count += sec->reloc_count;
  3262. }
  3263. /* Clear HAS_RELOC if there are no relocations. */
  3264. if (reloc_count == 0)
  3265. abfd->flags &= ~HAS_RELOC;
  3266. }
  3267. for (sec = abfd->sections; sec; sec = sec->next)
  3268. {
  3269. d = elf_section_data (sec);
  3270. if (d->this_hdr.sh_type != SHT_GROUP)
  3271. d->this_idx = section_number++;
  3272. if (d->this_hdr.sh_name != (unsigned int) -1)
  3273. _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
  3274. if (d->rel.hdr)
  3275. {
  3276. d->rel.idx = section_number++;
  3277. if (d->rel.hdr->sh_name != (unsigned int) -1)
  3278. _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
  3279. }
  3280. else
  3281. d->rel.idx = 0;
  3282. if (d->rela.hdr)
  3283. {
  3284. d->rela.idx = section_number++;
  3285. if (d->rela.hdr->sh_name != (unsigned int) -1)
  3286. _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
  3287. }
  3288. else
  3289. d->rela.idx = 0;
  3290. }
  3291. need_symtab = (bfd_get_symcount (abfd) > 0
  3292. || (link_info == NULL
  3293. && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
  3294. == HAS_RELOC)));
  3295. if (need_symtab)
  3296. {
  3297. elf_onesymtab (abfd) = section_number++;
  3298. _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
  3299. if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
  3300. {
  3301. elf_section_list *entry;
  3302. BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
  3303. entry = bfd_zalloc (abfd, sizeof (*entry));
  3304. entry->ndx = section_number++;
  3305. elf_symtab_shndx_list (abfd) = entry;
  3306. entry->hdr.sh_name
  3307. = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
  3308. ".symtab_shndx", false);
  3309. if (entry->hdr.sh_name == (unsigned int) -1)
  3310. return false;
  3311. }
  3312. elf_strtab_sec (abfd) = section_number++;
  3313. _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
  3314. }
  3315. elf_shstrtab_sec (abfd) = section_number++;
  3316. _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
  3317. elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
  3318. if (section_number >= SHN_LORESERVE)
  3319. {
  3320. /* xgettext:c-format */
  3321. _bfd_error_handler (_("%pB: too many sections: %u"),
  3322. abfd, section_number);
  3323. return false;
  3324. }
  3325. elf_numsections (abfd) = section_number;
  3326. elf_elfheader (abfd)->e_shnum = section_number;
  3327. /* Set up the list of section header pointers, in agreement with the
  3328. indices. */
  3329. amt = section_number * sizeof (Elf_Internal_Shdr *);
  3330. i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
  3331. if (i_shdrp == NULL)
  3332. return false;
  3333. i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
  3334. sizeof (Elf_Internal_Shdr));
  3335. if (i_shdrp[0] == NULL)
  3336. {
  3337. bfd_release (abfd, i_shdrp);
  3338. return false;
  3339. }
  3340. elf_elfsections (abfd) = i_shdrp;
  3341. i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
  3342. if (need_symtab)
  3343. {
  3344. i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
  3345. if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
  3346. {
  3347. elf_section_list * entry = elf_symtab_shndx_list (abfd);
  3348. BFD_ASSERT (entry != NULL);
  3349. i_shdrp[entry->ndx] = & entry->hdr;
  3350. entry->hdr.sh_link = elf_onesymtab (abfd);
  3351. }
  3352. i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
  3353. t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
  3354. }
  3355. for (sec = abfd->sections; sec; sec = sec->next)
  3356. {
  3357. asection *s;
  3358. d = elf_section_data (sec);
  3359. i_shdrp[d->this_idx] = &d->this_hdr;
  3360. if (d->rel.idx != 0)
  3361. i_shdrp[d->rel.idx] = d->rel.hdr;
  3362. if (d->rela.idx != 0)
  3363. i_shdrp[d->rela.idx] = d->rela.hdr;
  3364. /* Fill in the sh_link and sh_info fields while we're at it. */
  3365. /* sh_link of a reloc section is the section index of the symbol
  3366. table. sh_info is the section index of the section to which
  3367. the relocation entries apply. */
  3368. if (d->rel.idx != 0)
  3369. {
  3370. d->rel.hdr->sh_link = elf_onesymtab (abfd);
  3371. d->rel.hdr->sh_info = d->this_idx;
  3372. d->rel.hdr->sh_flags |= SHF_INFO_LINK;
  3373. }
  3374. if (d->rela.idx != 0)
  3375. {
  3376. d->rela.hdr->sh_link = elf_onesymtab (abfd);
  3377. d->rela.hdr->sh_info = d->this_idx;
  3378. d->rela.hdr->sh_flags |= SHF_INFO_LINK;
  3379. }
  3380. /* We need to set up sh_link for SHF_LINK_ORDER. */
  3381. if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
  3382. {
  3383. s = elf_linked_to_section (sec);
  3384. /* We can now have a NULL linked section pointer.
  3385. This happens when the sh_link field is 0, which is done
  3386. when a linked to section is discarded but the linking
  3387. section has been retained for some reason. */
  3388. if (s)
  3389. {
  3390. /* Check discarded linkonce section. */
  3391. if (discarded_section (s))
  3392. {
  3393. asection *kept;
  3394. _bfd_error_handler
  3395. /* xgettext:c-format */
  3396. (_("%pB: sh_link of section `%pA' points to"
  3397. " discarded section `%pA' of `%pB'"),
  3398. abfd, d->this_hdr.bfd_section, s, s->owner);
  3399. /* Point to the kept section if it has the same
  3400. size as the discarded one. */
  3401. kept = _bfd_elf_check_kept_section (s, link_info);
  3402. if (kept == NULL)
  3403. {
  3404. bfd_set_error (bfd_error_bad_value);
  3405. return false;
  3406. }
  3407. s = kept;
  3408. }
  3409. /* Handle objcopy. */
  3410. else if (s->output_section == NULL)
  3411. {
  3412. _bfd_error_handler
  3413. /* xgettext:c-format */
  3414. (_("%pB: sh_link of section `%pA' points to"
  3415. " removed section `%pA' of `%pB'"),
  3416. abfd, d->this_hdr.bfd_section, s, s->owner);
  3417. bfd_set_error (bfd_error_bad_value);
  3418. return false;
  3419. }
  3420. s = s->output_section;
  3421. d->this_hdr.sh_link = elf_section_data (s)->this_idx;
  3422. }
  3423. }
  3424. switch (d->this_hdr.sh_type)
  3425. {
  3426. case SHT_REL:
  3427. case SHT_RELA:
  3428. /* A reloc section which we are treating as a normal BFD
  3429. section. sh_link is the section index of the symbol
  3430. table. sh_info is the section index of the section to
  3431. which the relocation entries apply. We assume that an
  3432. allocated reloc section uses the dynamic symbol table
  3433. if there is one. Otherwise we guess the normal symbol
  3434. table. FIXME: How can we be sure? */
  3435. if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
  3436. {
  3437. s = bfd_get_section_by_name (abfd, ".dynsym");
  3438. if (s != NULL)
  3439. d->this_hdr.sh_link = elf_section_data (s)->this_idx;
  3440. }
  3441. if (d->this_hdr.sh_link == 0)
  3442. d->this_hdr.sh_link = elf_onesymtab (abfd);
  3443. s = elf_get_reloc_section (sec);
  3444. if (s != NULL)
  3445. {
  3446. d->this_hdr.sh_info = elf_section_data (s)->this_idx;
  3447. d->this_hdr.sh_flags |= SHF_INFO_LINK;
  3448. }
  3449. break;
  3450. case SHT_STRTAB:
  3451. /* We assume that a section named .stab*str is a stabs
  3452. string section. We look for a section with the same name
  3453. but without the trailing ``str'', and set its sh_link
  3454. field to point to this section. */
  3455. if (startswith (sec->name, ".stab")
  3456. && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
  3457. {
  3458. size_t len;
  3459. char *alc;
  3460. len = strlen (sec->name);
  3461. alc = (char *) bfd_malloc (len - 2);
  3462. if (alc == NULL)
  3463. return false;
  3464. memcpy (alc, sec->name, len - 3);
  3465. alc[len - 3] = '\0';
  3466. s = bfd_get_section_by_name (abfd, alc);
  3467. free (alc);
  3468. if (s != NULL)
  3469. {
  3470. elf_section_data (s)->this_hdr.sh_link = d->this_idx;
  3471. /* This is a .stab section. */
  3472. elf_section_data (s)->this_hdr.sh_entsize = 12;
  3473. }
  3474. }
  3475. break;
  3476. case SHT_DYNAMIC:
  3477. case SHT_DYNSYM:
  3478. case SHT_GNU_verneed:
  3479. case SHT_GNU_verdef:
  3480. /* sh_link is the section header index of the string table
  3481. used for the dynamic entries, or the symbol table, or the
  3482. version strings. */
  3483. s = bfd_get_section_by_name (abfd, ".dynstr");
  3484. if (s != NULL)
  3485. d->this_hdr.sh_link = elf_section_data (s)->this_idx;
  3486. break;
  3487. case SHT_GNU_LIBLIST:
  3488. /* sh_link is the section header index of the prelink library
  3489. list used for the dynamic entries, or the symbol table, or
  3490. the version strings. */
  3491. s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
  3492. ? ".dynstr" : ".gnu.libstr"));
  3493. if (s != NULL)
  3494. d->this_hdr.sh_link = elf_section_data (s)->this_idx;
  3495. break;
  3496. case SHT_HASH:
  3497. case SHT_GNU_HASH:
  3498. case SHT_GNU_versym:
  3499. /* sh_link is the section header index of the symbol table
  3500. this hash table or version table is for. */
  3501. s = bfd_get_section_by_name (abfd, ".dynsym");
  3502. if (s != NULL)
  3503. d->this_hdr.sh_link = elf_section_data (s)->this_idx;
  3504. break;
  3505. case SHT_GROUP:
  3506. d->this_hdr.sh_link = elf_onesymtab (abfd);
  3507. }
  3508. }
  3509. /* Delay setting sh_name to _bfd_elf_write_object_contents so that
  3510. _bfd_elf_assign_file_positions_for_non_load can convert DWARF
  3511. debug section name from .debug_* to .zdebug_* if needed. */
  3512. return true;
  3513. }
  3514. static bool
  3515. sym_is_global (bfd *abfd, asymbol *sym)
  3516. {
  3517. /* If the backend has a special mapping, use it. */
  3518. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  3519. if (bed->elf_backend_sym_is_global)
  3520. return (*bed->elf_backend_sym_is_global) (abfd, sym);
  3521. return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
  3522. || bfd_is_und_section (bfd_asymbol_section (sym))
  3523. || bfd_is_com_section (bfd_asymbol_section (sym)));
  3524. }
  3525. /* Filter global symbols of ABFD to include in the import library. All
  3526. SYMCOUNT symbols of ABFD can be examined from their pointers in
  3527. SYMS. Pointers of symbols to keep should be stored contiguously at
  3528. the beginning of that array.
  3529. Returns the number of symbols to keep. */
  3530. unsigned int
  3531. _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
  3532. asymbol **syms, long symcount)
  3533. {
  3534. long src_count, dst_count = 0;
  3535. for (src_count = 0; src_count < symcount; src_count++)
  3536. {
  3537. asymbol *sym = syms[src_count];
  3538. char *name = (char *) bfd_asymbol_name (sym);
  3539. struct bfd_link_hash_entry *h;
  3540. if (!sym_is_global (abfd, sym))
  3541. continue;
  3542. h = bfd_link_hash_lookup (info->hash, name, false, false, false);
  3543. if (h == NULL)
  3544. continue;
  3545. if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
  3546. continue;
  3547. if (h->linker_def || h->ldscript_def)
  3548. continue;
  3549. syms[dst_count++] = sym;
  3550. }
  3551. syms[dst_count] = NULL;
  3552. return dst_count;
  3553. }
  3554. /* Don't output section symbols for sections that are not going to be
  3555. output, that are duplicates or there is no BFD section. */
  3556. static bool
  3557. ignore_section_sym (bfd *abfd, asymbol *sym)
  3558. {
  3559. elf_symbol_type *type_ptr;
  3560. if (sym == NULL)
  3561. return false;
  3562. if ((sym->flags & BSF_SECTION_SYM) == 0)
  3563. return false;
  3564. /* Ignore the section symbol if it isn't used. */
  3565. if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
  3566. return true;
  3567. if (sym->section == NULL)
  3568. return true;
  3569. type_ptr = elf_symbol_from (sym);
  3570. return ((type_ptr != NULL
  3571. && type_ptr->internal_elf_sym.st_shndx != 0
  3572. && bfd_is_abs_section (sym->section))
  3573. || !(sym->section->owner == abfd
  3574. || (sym->section->output_section != NULL
  3575. && sym->section->output_section->owner == abfd
  3576. && sym->section->output_offset == 0)
  3577. || bfd_is_abs_section (sym->section)));
  3578. }
  3579. /* Map symbol from it's internal number to the external number, moving
  3580. all local symbols to be at the head of the list. */
  3581. static bool
  3582. elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
  3583. {
  3584. unsigned int symcount = bfd_get_symcount (abfd);
  3585. asymbol **syms = bfd_get_outsymbols (abfd);
  3586. asymbol **sect_syms;
  3587. unsigned int num_locals = 0;
  3588. unsigned int num_globals = 0;
  3589. unsigned int num_locals2 = 0;
  3590. unsigned int num_globals2 = 0;
  3591. unsigned int max_index = 0;
  3592. unsigned int idx;
  3593. asection *asect;
  3594. asymbol **new_syms;
  3595. size_t amt;
  3596. #ifdef DEBUG
  3597. fprintf (stderr, "elf_map_symbols\n");
  3598. fflush (stderr);
  3599. #endif
  3600. for (asect = abfd->sections; asect; asect = asect->next)
  3601. {
  3602. if (max_index < asect->index)
  3603. max_index = asect->index;
  3604. }
  3605. max_index++;
  3606. amt = max_index * sizeof (asymbol *);
  3607. sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
  3608. if (sect_syms == NULL)
  3609. return false;
  3610. elf_section_syms (abfd) = sect_syms;
  3611. elf_num_section_syms (abfd) = max_index;
  3612. /* Init sect_syms entries for any section symbols we have already
  3613. decided to output. */
  3614. for (idx = 0; idx < symcount; idx++)
  3615. {
  3616. asymbol *sym = syms[idx];
  3617. if ((sym->flags & BSF_SECTION_SYM) != 0
  3618. && sym->value == 0
  3619. && !ignore_section_sym (abfd, sym)
  3620. && !bfd_is_abs_section (sym->section))
  3621. {
  3622. asection *sec = sym->section;
  3623. if (sec->owner != abfd)
  3624. sec = sec->output_section;
  3625. sect_syms[sec->index] = syms[idx];
  3626. }
  3627. }
  3628. /* Classify all of the symbols. */
  3629. for (idx = 0; idx < symcount; idx++)
  3630. {
  3631. if (sym_is_global (abfd, syms[idx]))
  3632. num_globals++;
  3633. else if (!ignore_section_sym (abfd, syms[idx]))
  3634. num_locals++;
  3635. }
  3636. /* We will be adding a section symbol for each normal BFD section. Most
  3637. sections will already have a section symbol in outsymbols, but
  3638. eg. SHT_GROUP sections will not, and we need the section symbol mapped
  3639. at least in that case. */
  3640. for (asect = abfd->sections; asect; asect = asect->next)
  3641. {
  3642. asymbol *sym = asect->symbol;
  3643. /* Don't include ignored section symbols. */
  3644. if (!ignore_section_sym (abfd, sym)
  3645. && sect_syms[asect->index] == NULL)
  3646. {
  3647. if (!sym_is_global (abfd, asect->symbol))
  3648. num_locals++;
  3649. else
  3650. num_globals++;
  3651. }
  3652. }
  3653. /* Now sort the symbols so the local symbols are first. */
  3654. amt = (num_locals + num_globals) * sizeof (asymbol *);
  3655. new_syms = (asymbol **) bfd_alloc (abfd, amt);
  3656. if (new_syms == NULL)
  3657. return false;
  3658. for (idx = 0; idx < symcount; idx++)
  3659. {
  3660. asymbol *sym = syms[idx];
  3661. unsigned int i;
  3662. if (sym_is_global (abfd, sym))
  3663. i = num_locals + num_globals2++;
  3664. /* Don't include ignored section symbols. */
  3665. else if (!ignore_section_sym (abfd, sym))
  3666. i = num_locals2++;
  3667. else
  3668. continue;
  3669. new_syms[i] = sym;
  3670. sym->udata.i = i + 1;
  3671. }
  3672. for (asect = abfd->sections; asect; asect = asect->next)
  3673. {
  3674. asymbol *sym = asect->symbol;
  3675. if (!ignore_section_sym (abfd, sym)
  3676. && sect_syms[asect->index] == NULL)
  3677. {
  3678. unsigned int i;
  3679. sect_syms[asect->index] = sym;
  3680. if (!sym_is_global (abfd, sym))
  3681. i = num_locals2++;
  3682. else
  3683. i = num_locals + num_globals2++;
  3684. new_syms[i] = sym;
  3685. sym->udata.i = i + 1;
  3686. }
  3687. }
  3688. bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
  3689. *pnum_locals = num_locals;
  3690. return true;
  3691. }
  3692. /* Align to the maximum file alignment that could be required for any
  3693. ELF data structure. */
  3694. static inline file_ptr
  3695. align_file_position (file_ptr off, int align)
  3696. {
  3697. return (off + align - 1) & ~(align - 1);
  3698. }
  3699. /* Assign a file position to a section, optionally aligning to the
  3700. required section alignment. */
  3701. file_ptr
  3702. _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
  3703. file_ptr offset,
  3704. bool align)
  3705. {
  3706. if (align && i_shdrp->sh_addralign > 1)
  3707. offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
  3708. i_shdrp->sh_offset = offset;
  3709. if (i_shdrp->bfd_section != NULL)
  3710. i_shdrp->bfd_section->filepos = offset;
  3711. if (i_shdrp->sh_type != SHT_NOBITS)
  3712. offset += i_shdrp->sh_size;
  3713. return offset;
  3714. }
  3715. /* Compute the file positions we are going to put the sections at, and
  3716. otherwise prepare to begin writing out the ELF file. If LINK_INFO
  3717. is not NULL, this is being called by the ELF backend linker. */
  3718. bool
  3719. _bfd_elf_compute_section_file_positions (bfd *abfd,
  3720. struct bfd_link_info *link_info)
  3721. {
  3722. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  3723. struct fake_section_arg fsargs;
  3724. bool failed;
  3725. struct elf_strtab_hash *strtab = NULL;
  3726. Elf_Internal_Shdr *shstrtab_hdr;
  3727. bool need_symtab;
  3728. if (abfd->output_has_begun)
  3729. return true;
  3730. /* Do any elf backend specific processing first. */
  3731. if (bed->elf_backend_begin_write_processing)
  3732. (*bed->elf_backend_begin_write_processing) (abfd, link_info);
  3733. if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
  3734. return false;
  3735. fsargs.failed = false;
  3736. fsargs.link_info = link_info;
  3737. bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
  3738. if (fsargs.failed)
  3739. return false;
  3740. if (!assign_section_numbers (abfd, link_info))
  3741. return false;
  3742. /* The backend linker builds symbol table information itself. */
  3743. need_symtab = (link_info == NULL
  3744. && (bfd_get_symcount (abfd) > 0
  3745. || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
  3746. == HAS_RELOC)));
  3747. if (need_symtab)
  3748. {
  3749. /* Non-zero if doing a relocatable link. */
  3750. int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
  3751. if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
  3752. return false;
  3753. }
  3754. failed = false;
  3755. if (link_info == NULL)
  3756. {
  3757. bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
  3758. if (failed)
  3759. return false;
  3760. }
  3761. shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
  3762. /* sh_name was set in init_file_header. */
  3763. shstrtab_hdr->sh_type = SHT_STRTAB;
  3764. shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
  3765. shstrtab_hdr->sh_addr = 0;
  3766. /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
  3767. shstrtab_hdr->sh_entsize = 0;
  3768. shstrtab_hdr->sh_link = 0;
  3769. shstrtab_hdr->sh_info = 0;
  3770. /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
  3771. shstrtab_hdr->sh_addralign = 1;
  3772. if (!assign_file_positions_except_relocs (abfd, link_info))
  3773. return false;
  3774. if (need_symtab)
  3775. {
  3776. file_ptr off;
  3777. Elf_Internal_Shdr *hdr;
  3778. off = elf_next_file_pos (abfd);
  3779. hdr = & elf_symtab_hdr (abfd);
  3780. off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
  3781. if (elf_symtab_shndx_list (abfd) != NULL)
  3782. {
  3783. hdr = & elf_symtab_shndx_list (abfd)->hdr;
  3784. if (hdr->sh_size != 0)
  3785. off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
  3786. /* FIXME: What about other symtab_shndx sections in the list ? */
  3787. }
  3788. hdr = &elf_tdata (abfd)->strtab_hdr;
  3789. off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
  3790. elf_next_file_pos (abfd) = off;
  3791. /* Now that we know where the .strtab section goes, write it
  3792. out. */
  3793. if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
  3794. || ! _bfd_elf_strtab_emit (abfd, strtab))
  3795. return false;
  3796. _bfd_elf_strtab_free (strtab);
  3797. }
  3798. abfd->output_has_begun = true;
  3799. return true;
  3800. }
  3801. /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
  3802. function effectively returns whether --eh-frame-hdr is given on the
  3803. command line. After size_dynamic_sections the result reflects
  3804. whether .eh_frame_hdr will actually be output (sizing isn't done
  3805. until ldemul_after_allocation). */
  3806. static asection *
  3807. elf_eh_frame_hdr (const struct bfd_link_info *info)
  3808. {
  3809. if (info != NULL && is_elf_hash_table (info->hash))
  3810. return elf_hash_table (info)->eh_info.hdr_sec;
  3811. return NULL;
  3812. }
  3813. /* Make an initial estimate of the size of the program header. If we
  3814. get the number wrong here, we'll redo section placement. */
  3815. static bfd_size_type
  3816. get_program_header_size (bfd *abfd, struct bfd_link_info *info)
  3817. {
  3818. size_t segs;
  3819. asection *s;
  3820. const struct elf_backend_data *bed;
  3821. /* Assume we will need exactly two PT_LOAD segments: one for text
  3822. and one for data. */
  3823. segs = 2;
  3824. s = bfd_get_section_by_name (abfd, ".interp");
  3825. if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
  3826. {
  3827. /* If we have a loadable interpreter section, we need a
  3828. PT_INTERP segment. In this case, assume we also need a
  3829. PT_PHDR segment, although that may not be true for all
  3830. targets. */
  3831. segs += 2;
  3832. }
  3833. if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
  3834. {
  3835. /* We need a PT_DYNAMIC segment. */
  3836. ++segs;
  3837. }
  3838. if (info != NULL && info->relro)
  3839. {
  3840. /* We need a PT_GNU_RELRO segment. */
  3841. ++segs;
  3842. }
  3843. if (elf_eh_frame_hdr (info))
  3844. {
  3845. /* We need a PT_GNU_EH_FRAME segment. */
  3846. ++segs;
  3847. }
  3848. if (elf_stack_flags (abfd))
  3849. {
  3850. /* We need a PT_GNU_STACK segment. */
  3851. ++segs;
  3852. }
  3853. s = bfd_get_section_by_name (abfd,
  3854. NOTE_GNU_PROPERTY_SECTION_NAME);
  3855. if (s != NULL && s->size != 0)
  3856. {
  3857. /* We need a PT_GNU_PROPERTY segment. */
  3858. ++segs;
  3859. }
  3860. for (s = abfd->sections; s != NULL; s = s->next)
  3861. {
  3862. if ((s->flags & SEC_LOAD) != 0
  3863. && elf_section_type (s) == SHT_NOTE)
  3864. {
  3865. unsigned int alignment_power;
  3866. /* We need a PT_NOTE segment. */
  3867. ++segs;
  3868. /* Try to create just one PT_NOTE segment for all adjacent
  3869. loadable SHT_NOTE sections. gABI requires that within a
  3870. PT_NOTE segment (and also inside of each SHT_NOTE section)
  3871. each note should have the same alignment. So we check
  3872. whether the sections are correctly aligned. */
  3873. alignment_power = s->alignment_power;
  3874. while (s->next != NULL
  3875. && s->next->alignment_power == alignment_power
  3876. && (s->next->flags & SEC_LOAD) != 0
  3877. && elf_section_type (s->next) == SHT_NOTE)
  3878. s = s->next;
  3879. }
  3880. }
  3881. for (s = abfd->sections; s != NULL; s = s->next)
  3882. {
  3883. if (s->flags & SEC_THREAD_LOCAL)
  3884. {
  3885. /* We need a PT_TLS segment. */
  3886. ++segs;
  3887. break;
  3888. }
  3889. }
  3890. bed = get_elf_backend_data (abfd);
  3891. if ((abfd->flags & D_PAGED) != 0
  3892. && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
  3893. {
  3894. /* Add a PT_GNU_MBIND segment for each mbind section. */
  3895. bfd_vma commonpagesize;
  3896. unsigned int page_align_power;
  3897. if (info != NULL)
  3898. commonpagesize = info->commonpagesize;
  3899. else
  3900. commonpagesize = bed->commonpagesize;
  3901. page_align_power = bfd_log2 (commonpagesize);
  3902. for (s = abfd->sections; s != NULL; s = s->next)
  3903. if (elf_section_flags (s) & SHF_GNU_MBIND)
  3904. {
  3905. if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
  3906. {
  3907. _bfd_error_handler
  3908. /* xgettext:c-format */
  3909. (_("%pB: GNU_MBIND section `%pA' has invalid "
  3910. "sh_info field: %d"),
  3911. abfd, s, elf_section_data (s)->this_hdr.sh_info);
  3912. continue;
  3913. }
  3914. /* Align mbind section to page size. */
  3915. if (s->alignment_power < page_align_power)
  3916. s->alignment_power = page_align_power;
  3917. segs ++;
  3918. }
  3919. }
  3920. /* Let the backend count up any program headers it might need. */
  3921. if (bed->elf_backend_additional_program_headers)
  3922. {
  3923. int a;
  3924. a = (*bed->elf_backend_additional_program_headers) (abfd, info);
  3925. if (a == -1)
  3926. abort ();
  3927. segs += a;
  3928. }
  3929. return segs * bed->s->sizeof_phdr;
  3930. }
  3931. /* Find the segment that contains the output_section of section. */
  3932. Elf_Internal_Phdr *
  3933. _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
  3934. {
  3935. struct elf_segment_map *m;
  3936. Elf_Internal_Phdr *p;
  3937. for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
  3938. m != NULL;
  3939. m = m->next, p++)
  3940. {
  3941. int i;
  3942. for (i = m->count - 1; i >= 0; i--)
  3943. if (m->sections[i] == section)
  3944. return p;
  3945. }
  3946. return NULL;
  3947. }
  3948. /* Create a mapping from a set of sections to a program segment. */
  3949. static struct elf_segment_map *
  3950. make_mapping (bfd *abfd,
  3951. asection **sections,
  3952. unsigned int from,
  3953. unsigned int to,
  3954. bool phdr)
  3955. {
  3956. struct elf_segment_map *m;
  3957. unsigned int i;
  3958. asection **hdrpp;
  3959. size_t amt;
  3960. amt = sizeof (struct elf_segment_map) - sizeof (asection *);
  3961. amt += (to - from) * sizeof (asection *);
  3962. m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  3963. if (m == NULL)
  3964. return NULL;
  3965. m->next = NULL;
  3966. m->p_type = PT_LOAD;
  3967. for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
  3968. m->sections[i - from] = *hdrpp;
  3969. m->count = to - from;
  3970. if (from == 0 && phdr)
  3971. {
  3972. /* Include the headers in the first PT_LOAD segment. */
  3973. m->includes_filehdr = 1;
  3974. m->includes_phdrs = 1;
  3975. }
  3976. return m;
  3977. }
  3978. /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
  3979. on failure. */
  3980. struct elf_segment_map *
  3981. _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
  3982. {
  3983. struct elf_segment_map *m;
  3984. m = (struct elf_segment_map *) bfd_zalloc (abfd,
  3985. sizeof (struct elf_segment_map));
  3986. if (m == NULL)
  3987. return NULL;
  3988. m->next = NULL;
  3989. m->p_type = PT_DYNAMIC;
  3990. m->count = 1;
  3991. m->sections[0] = dynsec;
  3992. return m;
  3993. }
  3994. /* Possibly add or remove segments from the segment map. */
  3995. static bool
  3996. elf_modify_segment_map (bfd *abfd,
  3997. struct bfd_link_info *info,
  3998. bool remove_empty_load)
  3999. {
  4000. struct elf_segment_map **m;
  4001. const struct elf_backend_data *bed;
  4002. /* The placement algorithm assumes that non allocated sections are
  4003. not in PT_LOAD segments. We ensure this here by removing such
  4004. sections from the segment map. We also remove excluded
  4005. sections. Finally, any PT_LOAD segment without sections is
  4006. removed. */
  4007. m = &elf_seg_map (abfd);
  4008. while (*m)
  4009. {
  4010. unsigned int i, new_count;
  4011. for (new_count = 0, i = 0; i < (*m)->count; i++)
  4012. {
  4013. if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
  4014. && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
  4015. || (*m)->p_type != PT_LOAD))
  4016. {
  4017. (*m)->sections[new_count] = (*m)->sections[i];
  4018. new_count++;
  4019. }
  4020. }
  4021. (*m)->count = new_count;
  4022. if (remove_empty_load
  4023. && (*m)->p_type == PT_LOAD
  4024. && (*m)->count == 0
  4025. && !(*m)->includes_phdrs)
  4026. *m = (*m)->next;
  4027. else
  4028. m = &(*m)->next;
  4029. }
  4030. bed = get_elf_backend_data (abfd);
  4031. if (bed->elf_backend_modify_segment_map != NULL)
  4032. {
  4033. if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
  4034. return false;
  4035. }
  4036. return true;
  4037. }
  4038. #define IS_TBSS(s) \
  4039. ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
  4040. /* Set up a mapping from BFD sections to program segments. Update
  4041. NEED_LAYOUT if the section layout is changed. */
  4042. bool
  4043. _bfd_elf_map_sections_to_segments (bfd *abfd,
  4044. struct bfd_link_info *info,
  4045. bool *need_layout)
  4046. {
  4047. unsigned int count;
  4048. struct elf_segment_map *m;
  4049. asection **sections = NULL;
  4050. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  4051. bool no_user_phdrs;
  4052. no_user_phdrs = elf_seg_map (abfd) == NULL;
  4053. if (info != NULL)
  4054. {
  4055. info->user_phdrs = !no_user_phdrs;
  4056. /* Size the relative relocations if DT_RELR is enabled. */
  4057. if (info->enable_dt_relr
  4058. && need_layout != NULL
  4059. && bed->size_relative_relocs
  4060. && !bed->size_relative_relocs (info, need_layout))
  4061. info->callbacks->einfo
  4062. (_("%F%P: failed to size relative relocations\n"));
  4063. }
  4064. if (no_user_phdrs && bfd_count_sections (abfd) != 0)
  4065. {
  4066. asection *s;
  4067. unsigned int i;
  4068. struct elf_segment_map *mfirst;
  4069. struct elf_segment_map **pm;
  4070. asection *last_hdr;
  4071. bfd_vma last_size;
  4072. unsigned int hdr_index;
  4073. bfd_vma maxpagesize;
  4074. asection **hdrpp;
  4075. bool phdr_in_segment;
  4076. bool writable;
  4077. bool executable;
  4078. unsigned int tls_count = 0;
  4079. asection *first_tls = NULL;
  4080. asection *first_mbind = NULL;
  4081. asection *dynsec, *eh_frame_hdr;
  4082. size_t amt;
  4083. bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
  4084. bfd_size_type phdr_size; /* Octets/bytes. */
  4085. unsigned int opb = bfd_octets_per_byte (abfd, NULL);
  4086. /* Select the allocated sections, and sort them. */
  4087. amt = bfd_count_sections (abfd) * sizeof (asection *);
  4088. sections = (asection **) bfd_malloc (amt);
  4089. if (sections == NULL)
  4090. goto error_return;
  4091. /* Calculate top address, avoiding undefined behaviour of shift
  4092. left operator when shift count is equal to size of type
  4093. being shifted. */
  4094. addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
  4095. addr_mask = (addr_mask << 1) + 1;
  4096. i = 0;
  4097. for (s = abfd->sections; s != NULL; s = s->next)
  4098. {
  4099. if ((s->flags & SEC_ALLOC) != 0)
  4100. {
  4101. /* target_index is unused until bfd_elf_final_link
  4102. starts output of section symbols. Use it to make
  4103. qsort stable. */
  4104. s->target_index = i;
  4105. sections[i] = s;
  4106. ++i;
  4107. /* A wrapping section potentially clashes with header. */
  4108. if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
  4109. wrap_to = (s->lma + s->size / opb) & addr_mask;
  4110. }
  4111. }
  4112. BFD_ASSERT (i <= bfd_count_sections (abfd));
  4113. count = i;
  4114. qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
  4115. phdr_size = elf_program_header_size (abfd);
  4116. if (phdr_size == (bfd_size_type) -1)
  4117. phdr_size = get_program_header_size (abfd, info);
  4118. phdr_size += bed->s->sizeof_ehdr;
  4119. /* phdr_size is compared to LMA values which are in bytes. */
  4120. phdr_size /= opb;
  4121. if (info != NULL)
  4122. maxpagesize = info->maxpagesize;
  4123. else
  4124. maxpagesize = bed->maxpagesize;
  4125. if (maxpagesize == 0)
  4126. maxpagesize = 1;
  4127. phdr_in_segment = info != NULL && info->load_phdrs;
  4128. if (count != 0
  4129. && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
  4130. >= (phdr_size & (maxpagesize - 1))))
  4131. /* For compatibility with old scripts that may not be using
  4132. SIZEOF_HEADERS, add headers when it looks like space has
  4133. been left for them. */
  4134. phdr_in_segment = true;
  4135. /* Build the mapping. */
  4136. mfirst = NULL;
  4137. pm = &mfirst;
  4138. /* If we have a .interp section, then create a PT_PHDR segment for
  4139. the program headers and a PT_INTERP segment for the .interp
  4140. section. */
  4141. s = bfd_get_section_by_name (abfd, ".interp");
  4142. if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
  4143. {
  4144. amt = sizeof (struct elf_segment_map);
  4145. m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  4146. if (m == NULL)
  4147. goto error_return;
  4148. m->next = NULL;
  4149. m->p_type = PT_PHDR;
  4150. m->p_flags = PF_R;
  4151. m->p_flags_valid = 1;
  4152. m->includes_phdrs = 1;
  4153. phdr_in_segment = true;
  4154. *pm = m;
  4155. pm = &m->next;
  4156. amt = sizeof (struct elf_segment_map);
  4157. m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  4158. if (m == NULL)
  4159. goto error_return;
  4160. m->next = NULL;
  4161. m->p_type = PT_INTERP;
  4162. m->count = 1;
  4163. m->sections[0] = s;
  4164. *pm = m;
  4165. pm = &m->next;
  4166. }
  4167. /* Look through the sections. We put sections in the same program
  4168. segment when the start of the second section can be placed within
  4169. a few bytes of the end of the first section. */
  4170. last_hdr = NULL;
  4171. last_size = 0;
  4172. hdr_index = 0;
  4173. writable = false;
  4174. executable = false;
  4175. dynsec = bfd_get_section_by_name (abfd, ".dynamic");
  4176. if (dynsec != NULL
  4177. && (dynsec->flags & SEC_LOAD) == 0)
  4178. dynsec = NULL;
  4179. if ((abfd->flags & D_PAGED) == 0)
  4180. phdr_in_segment = false;
  4181. /* Deal with -Ttext or something similar such that the first section
  4182. is not adjacent to the program headers. This is an
  4183. approximation, since at this point we don't know exactly how many
  4184. program headers we will need. */
  4185. if (phdr_in_segment && count > 0)
  4186. {
  4187. bfd_vma phdr_lma; /* Bytes. */
  4188. bool separate_phdr = false;
  4189. phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
  4190. if (info != NULL
  4191. && info->separate_code
  4192. && (sections[0]->flags & SEC_CODE) != 0)
  4193. {
  4194. /* If data sections should be separate from code and
  4195. thus not executable, and the first section is
  4196. executable then put the file and program headers in
  4197. their own PT_LOAD. */
  4198. separate_phdr = true;
  4199. if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
  4200. == (sections[0]->lma & addr_mask & -maxpagesize)))
  4201. {
  4202. /* The file and program headers are currently on the
  4203. same page as the first section. Put them on the
  4204. previous page if we can. */
  4205. if (phdr_lma >= maxpagesize)
  4206. phdr_lma -= maxpagesize;
  4207. else
  4208. separate_phdr = false;
  4209. }
  4210. }
  4211. if ((sections[0]->lma & addr_mask) < phdr_lma
  4212. || (sections[0]->lma & addr_mask) < phdr_size)
  4213. /* If file and program headers would be placed at the end
  4214. of memory then it's probably better to omit them. */
  4215. phdr_in_segment = false;
  4216. else if (phdr_lma < wrap_to)
  4217. /* If a section wraps around to where we'll be placing
  4218. file and program headers, then the headers will be
  4219. overwritten. */
  4220. phdr_in_segment = false;
  4221. else if (separate_phdr)
  4222. {
  4223. m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
  4224. if (m == NULL)
  4225. goto error_return;
  4226. m->p_paddr = phdr_lma * opb;
  4227. m->p_vaddr_offset
  4228. = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
  4229. m->p_paddr_valid = 1;
  4230. *pm = m;
  4231. pm = &m->next;
  4232. phdr_in_segment = false;
  4233. }
  4234. }
  4235. for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
  4236. {
  4237. asection *hdr;
  4238. bool new_segment;
  4239. hdr = *hdrpp;
  4240. /* See if this section and the last one will fit in the same
  4241. segment. */
  4242. if (last_hdr == NULL)
  4243. {
  4244. /* If we don't have a segment yet, then we don't need a new
  4245. one (we build the last one after this loop). */
  4246. new_segment = false;
  4247. }
  4248. else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
  4249. {
  4250. /* If this section has a different relation between the
  4251. virtual address and the load address, then we need a new
  4252. segment. */
  4253. new_segment = true;
  4254. }
  4255. else if (hdr->lma < last_hdr->lma + last_size
  4256. || last_hdr->lma + last_size < last_hdr->lma)
  4257. {
  4258. /* If this section has a load address that makes it overlap
  4259. the previous section, then we need a new segment. */
  4260. new_segment = true;
  4261. }
  4262. else if ((abfd->flags & D_PAGED) != 0
  4263. && (((last_hdr->lma + last_size - 1) & -maxpagesize)
  4264. == (hdr->lma & -maxpagesize)))
  4265. {
  4266. /* If we are demand paged then we can't map two disk
  4267. pages onto the same memory page. */
  4268. new_segment = false;
  4269. }
  4270. /* In the next test we have to be careful when last_hdr->lma is close
  4271. to the end of the address space. If the aligned address wraps
  4272. around to the start of the address space, then there are no more
  4273. pages left in memory and it is OK to assume that the current
  4274. section can be included in the current segment. */
  4275. else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
  4276. + maxpagesize > last_hdr->lma)
  4277. && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
  4278. + maxpagesize <= hdr->lma))
  4279. {
  4280. /* If putting this section in this segment would force us to
  4281. skip a page in the segment, then we need a new segment. */
  4282. new_segment = true;
  4283. }
  4284. else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
  4285. && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
  4286. {
  4287. /* We don't want to put a loaded section after a
  4288. nonloaded (ie. bss style) section in the same segment
  4289. as that will force the non-loaded section to be loaded.
  4290. Consider .tbss sections as loaded for this purpose. */
  4291. new_segment = true;
  4292. }
  4293. else if ((abfd->flags & D_PAGED) == 0)
  4294. {
  4295. /* If the file is not demand paged, which means that we
  4296. don't require the sections to be correctly aligned in the
  4297. file, then there is no other reason for a new segment. */
  4298. new_segment = false;
  4299. }
  4300. else if (info != NULL
  4301. && info->separate_code
  4302. && executable != ((hdr->flags & SEC_CODE) != 0))
  4303. {
  4304. new_segment = true;
  4305. }
  4306. else if (! writable
  4307. && (hdr->flags & SEC_READONLY) == 0)
  4308. {
  4309. /* We don't want to put a writable section in a read only
  4310. segment. */
  4311. new_segment = true;
  4312. }
  4313. else
  4314. {
  4315. /* Otherwise, we can use the same segment. */
  4316. new_segment = false;
  4317. }
  4318. /* Allow interested parties a chance to override our decision. */
  4319. if (last_hdr != NULL
  4320. && info != NULL
  4321. && info->callbacks->override_segment_assignment != NULL)
  4322. new_segment
  4323. = info->callbacks->override_segment_assignment (info, abfd, hdr,
  4324. last_hdr,
  4325. new_segment);
  4326. if (! new_segment)
  4327. {
  4328. if ((hdr->flags & SEC_READONLY) == 0)
  4329. writable = true;
  4330. if ((hdr->flags & SEC_CODE) != 0)
  4331. executable = true;
  4332. last_hdr = hdr;
  4333. /* .tbss sections effectively have zero size. */
  4334. last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
  4335. continue;
  4336. }
  4337. /* We need a new program segment. We must create a new program
  4338. header holding all the sections from hdr_index until hdr. */
  4339. m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
  4340. if (m == NULL)
  4341. goto error_return;
  4342. *pm = m;
  4343. pm = &m->next;
  4344. if ((hdr->flags & SEC_READONLY) == 0)
  4345. writable = true;
  4346. else
  4347. writable = false;
  4348. if ((hdr->flags & SEC_CODE) == 0)
  4349. executable = false;
  4350. else
  4351. executable = true;
  4352. last_hdr = hdr;
  4353. /* .tbss sections effectively have zero size. */
  4354. last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
  4355. hdr_index = i;
  4356. phdr_in_segment = false;
  4357. }
  4358. /* Create a final PT_LOAD program segment, but not if it's just
  4359. for .tbss. */
  4360. if (last_hdr != NULL
  4361. && (i - hdr_index != 1
  4362. || !IS_TBSS (last_hdr)))
  4363. {
  4364. m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
  4365. if (m == NULL)
  4366. goto error_return;
  4367. *pm = m;
  4368. pm = &m->next;
  4369. }
  4370. /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
  4371. if (dynsec != NULL)
  4372. {
  4373. m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
  4374. if (m == NULL)
  4375. goto error_return;
  4376. *pm = m;
  4377. pm = &m->next;
  4378. }
  4379. /* For each batch of consecutive loadable SHT_NOTE sections,
  4380. add a PT_NOTE segment. We don't use bfd_get_section_by_name,
  4381. because if we link together nonloadable .note sections and
  4382. loadable .note sections, we will generate two .note sections
  4383. in the output file. */
  4384. for (s = abfd->sections; s != NULL; s = s->next)
  4385. {
  4386. if ((s->flags & SEC_LOAD) != 0
  4387. && elf_section_type (s) == SHT_NOTE)
  4388. {
  4389. asection *s2;
  4390. unsigned int alignment_power = s->alignment_power;
  4391. count = 1;
  4392. for (s2 = s; s2->next != NULL; s2 = s2->next)
  4393. {
  4394. if (s2->next->alignment_power == alignment_power
  4395. && (s2->next->flags & SEC_LOAD) != 0
  4396. && elf_section_type (s2->next) == SHT_NOTE
  4397. && align_power (s2->lma + s2->size / opb,
  4398. alignment_power)
  4399. == s2->next->lma)
  4400. count++;
  4401. else
  4402. break;
  4403. }
  4404. amt = sizeof (struct elf_segment_map) - sizeof (asection *);
  4405. amt += count * sizeof (asection *);
  4406. m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  4407. if (m == NULL)
  4408. goto error_return;
  4409. m->next = NULL;
  4410. m->p_type = PT_NOTE;
  4411. m->count = count;
  4412. while (count > 1)
  4413. {
  4414. m->sections[m->count - count--] = s;
  4415. BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
  4416. s = s->next;
  4417. }
  4418. m->sections[m->count - 1] = s;
  4419. BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
  4420. *pm = m;
  4421. pm = &m->next;
  4422. }
  4423. if (s->flags & SEC_THREAD_LOCAL)
  4424. {
  4425. if (! tls_count)
  4426. first_tls = s;
  4427. tls_count++;
  4428. }
  4429. if (first_mbind == NULL
  4430. && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
  4431. first_mbind = s;
  4432. }
  4433. /* If there are any SHF_TLS output sections, add PT_TLS segment. */
  4434. if (tls_count > 0)
  4435. {
  4436. amt = sizeof (struct elf_segment_map) - sizeof (asection *);
  4437. amt += tls_count * sizeof (asection *);
  4438. m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  4439. if (m == NULL)
  4440. goto error_return;
  4441. m->next = NULL;
  4442. m->p_type = PT_TLS;
  4443. m->count = tls_count;
  4444. /* Mandated PF_R. */
  4445. m->p_flags = PF_R;
  4446. m->p_flags_valid = 1;
  4447. s = first_tls;
  4448. for (i = 0; i < tls_count; ++i)
  4449. {
  4450. if ((s->flags & SEC_THREAD_LOCAL) == 0)
  4451. {
  4452. _bfd_error_handler
  4453. (_("%pB: TLS sections are not adjacent:"), abfd);
  4454. s = first_tls;
  4455. i = 0;
  4456. while (i < tls_count)
  4457. {
  4458. if ((s->flags & SEC_THREAD_LOCAL) != 0)
  4459. {
  4460. _bfd_error_handler (_(" TLS: %pA"), s);
  4461. i++;
  4462. }
  4463. else
  4464. _bfd_error_handler (_(" non-TLS: %pA"), s);
  4465. s = s->next;
  4466. }
  4467. bfd_set_error (bfd_error_bad_value);
  4468. goto error_return;
  4469. }
  4470. m->sections[i] = s;
  4471. s = s->next;
  4472. }
  4473. *pm = m;
  4474. pm = &m->next;
  4475. }
  4476. if (first_mbind
  4477. && (abfd->flags & D_PAGED) != 0
  4478. && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
  4479. for (s = first_mbind; s != NULL; s = s->next)
  4480. if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
  4481. && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
  4482. {
  4483. /* Mandated PF_R. */
  4484. unsigned long p_flags = PF_R;
  4485. if ((s->flags & SEC_READONLY) == 0)
  4486. p_flags |= PF_W;
  4487. if ((s->flags & SEC_CODE) != 0)
  4488. p_flags |= PF_X;
  4489. amt = sizeof (struct elf_segment_map) + sizeof (asection *);
  4490. m = bfd_zalloc (abfd, amt);
  4491. if (m == NULL)
  4492. goto error_return;
  4493. m->next = NULL;
  4494. m->p_type = (PT_GNU_MBIND_LO
  4495. + elf_section_data (s)->this_hdr.sh_info);
  4496. m->count = 1;
  4497. m->p_flags_valid = 1;
  4498. m->sections[0] = s;
  4499. m->p_flags = p_flags;
  4500. *pm = m;
  4501. pm = &m->next;
  4502. }
  4503. s = bfd_get_section_by_name (abfd,
  4504. NOTE_GNU_PROPERTY_SECTION_NAME);
  4505. if (s != NULL && s->size != 0)
  4506. {
  4507. amt = sizeof (struct elf_segment_map) + sizeof (asection *);
  4508. m = bfd_zalloc (abfd, amt);
  4509. if (m == NULL)
  4510. goto error_return;
  4511. m->next = NULL;
  4512. m->p_type = PT_GNU_PROPERTY;
  4513. m->count = 1;
  4514. m->p_flags_valid = 1;
  4515. m->sections[0] = s;
  4516. m->p_flags = PF_R;
  4517. *pm = m;
  4518. pm = &m->next;
  4519. }
  4520. /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
  4521. segment. */
  4522. eh_frame_hdr = elf_eh_frame_hdr (info);
  4523. if (eh_frame_hdr != NULL
  4524. && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
  4525. {
  4526. amt = sizeof (struct elf_segment_map);
  4527. m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  4528. if (m == NULL)
  4529. goto error_return;
  4530. m->next = NULL;
  4531. m->p_type = PT_GNU_EH_FRAME;
  4532. m->count = 1;
  4533. m->sections[0] = eh_frame_hdr->output_section;
  4534. *pm = m;
  4535. pm = &m->next;
  4536. }
  4537. if (elf_stack_flags (abfd))
  4538. {
  4539. amt = sizeof (struct elf_segment_map);
  4540. m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  4541. if (m == NULL)
  4542. goto error_return;
  4543. m->next = NULL;
  4544. m->p_type = PT_GNU_STACK;
  4545. m->p_flags = elf_stack_flags (abfd);
  4546. m->p_align = bed->stack_align;
  4547. m->p_flags_valid = 1;
  4548. m->p_align_valid = m->p_align != 0;
  4549. if (info->stacksize > 0)
  4550. {
  4551. m->p_size = info->stacksize;
  4552. m->p_size_valid = 1;
  4553. }
  4554. *pm = m;
  4555. pm = &m->next;
  4556. }
  4557. if (info != NULL && info->relro)
  4558. {
  4559. for (m = mfirst; m != NULL; m = m->next)
  4560. {
  4561. if (m->p_type == PT_LOAD
  4562. && m->count != 0
  4563. && m->sections[0]->vma >= info->relro_start
  4564. && m->sections[0]->vma < info->relro_end)
  4565. {
  4566. i = m->count;
  4567. while (--i != (unsigned) -1)
  4568. {
  4569. if (m->sections[i]->size > 0
  4570. && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
  4571. == (SEC_LOAD | SEC_HAS_CONTENTS))
  4572. break;
  4573. }
  4574. if (i != (unsigned) -1)
  4575. break;
  4576. }
  4577. }
  4578. /* Make a PT_GNU_RELRO segment only when it isn't empty. */
  4579. if (m != NULL)
  4580. {
  4581. amt = sizeof (struct elf_segment_map);
  4582. m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  4583. if (m == NULL)
  4584. goto error_return;
  4585. m->next = NULL;
  4586. m->p_type = PT_GNU_RELRO;
  4587. *pm = m;
  4588. pm = &m->next;
  4589. }
  4590. }
  4591. free (sections);
  4592. elf_seg_map (abfd) = mfirst;
  4593. }
  4594. if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
  4595. return false;
  4596. for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
  4597. ++count;
  4598. elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
  4599. return true;
  4600. error_return:
  4601. free (sections);
  4602. return false;
  4603. }
  4604. /* Sort sections by address. */
  4605. static int
  4606. elf_sort_sections (const void *arg1, const void *arg2)
  4607. {
  4608. const asection *sec1 = *(const asection **) arg1;
  4609. const asection *sec2 = *(const asection **) arg2;
  4610. bfd_size_type size1, size2;
  4611. /* Sort by LMA first, since this is the address used to
  4612. place the section into a segment. */
  4613. if (sec1->lma < sec2->lma)
  4614. return -1;
  4615. else if (sec1->lma > sec2->lma)
  4616. return 1;
  4617. /* Then sort by VMA. Normally the LMA and the VMA will be
  4618. the same, and this will do nothing. */
  4619. if (sec1->vma < sec2->vma)
  4620. return -1;
  4621. else if (sec1->vma > sec2->vma)
  4622. return 1;
  4623. /* Put !SEC_LOAD sections after SEC_LOAD ones. */
  4624. #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
  4625. && (x)->size != 0)
  4626. if (TOEND (sec1))
  4627. {
  4628. if (!TOEND (sec2))
  4629. return 1;
  4630. }
  4631. else if (TOEND (sec2))
  4632. return -1;
  4633. #undef TOEND
  4634. /* Sort by size, to put zero sized sections
  4635. before others at the same address. */
  4636. size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
  4637. size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
  4638. if (size1 < size2)
  4639. return -1;
  4640. if (size1 > size2)
  4641. return 1;
  4642. return sec1->target_index - sec2->target_index;
  4643. }
  4644. /* This qsort comparison functions sorts PT_LOAD segments first and
  4645. by p_paddr, for assign_file_positions_for_load_sections. */
  4646. static int
  4647. elf_sort_segments (const void *arg1, const void *arg2)
  4648. {
  4649. const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
  4650. const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
  4651. if (m1->p_type != m2->p_type)
  4652. {
  4653. if (m1->p_type == PT_NULL)
  4654. return 1;
  4655. if (m2->p_type == PT_NULL)
  4656. return -1;
  4657. return m1->p_type < m2->p_type ? -1 : 1;
  4658. }
  4659. if (m1->includes_filehdr != m2->includes_filehdr)
  4660. return m1->includes_filehdr ? -1 : 1;
  4661. if (m1->no_sort_lma != m2->no_sort_lma)
  4662. return m1->no_sort_lma ? -1 : 1;
  4663. if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
  4664. {
  4665. bfd_vma lma1, lma2; /* Octets. */
  4666. lma1 = 0;
  4667. if (m1->p_paddr_valid)
  4668. lma1 = m1->p_paddr;
  4669. else if (m1->count != 0)
  4670. {
  4671. unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
  4672. m1->sections[0]);
  4673. lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
  4674. }
  4675. lma2 = 0;
  4676. if (m2->p_paddr_valid)
  4677. lma2 = m2->p_paddr;
  4678. else if (m2->count != 0)
  4679. {
  4680. unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
  4681. m2->sections[0]);
  4682. lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
  4683. }
  4684. if (lma1 != lma2)
  4685. return lma1 < lma2 ? -1 : 1;
  4686. }
  4687. if (m1->idx != m2->idx)
  4688. return m1->idx < m2->idx ? -1 : 1;
  4689. return 0;
  4690. }
  4691. /* Ian Lance Taylor writes:
  4692. We shouldn't be using % with a negative signed number. That's just
  4693. not good. We have to make sure either that the number is not
  4694. negative, or that the number has an unsigned type. When the types
  4695. are all the same size they wind up as unsigned. When file_ptr is a
  4696. larger signed type, the arithmetic winds up as signed long long,
  4697. which is wrong.
  4698. What we're trying to say here is something like ``increase OFF by
  4699. the least amount that will cause it to be equal to the VMA modulo
  4700. the page size.'' */
  4701. /* In other words, something like:
  4702. vma_offset = m->sections[0]->vma % bed->maxpagesize;
  4703. off_offset = off % bed->maxpagesize;
  4704. if (vma_offset < off_offset)
  4705. adjustment = vma_offset + bed->maxpagesize - off_offset;
  4706. else
  4707. adjustment = vma_offset - off_offset;
  4708. which can be collapsed into the expression below. */
  4709. static file_ptr
  4710. vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
  4711. {
  4712. /* PR binutils/16199: Handle an alignment of zero. */
  4713. if (maxpagesize == 0)
  4714. maxpagesize = 1;
  4715. return ((vma - off) % maxpagesize);
  4716. }
  4717. static void
  4718. print_segment_map (const struct elf_segment_map *m)
  4719. {
  4720. unsigned int j;
  4721. const char *pt = get_segment_type (m->p_type);
  4722. char buf[32];
  4723. if (pt == NULL)
  4724. {
  4725. if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
  4726. sprintf (buf, "LOPROC+%7.7x",
  4727. (unsigned int) (m->p_type - PT_LOPROC));
  4728. else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
  4729. sprintf (buf, "LOOS+%7.7x",
  4730. (unsigned int) (m->p_type - PT_LOOS));
  4731. else
  4732. snprintf (buf, sizeof (buf), "%8.8x",
  4733. (unsigned int) m->p_type);
  4734. pt = buf;
  4735. }
  4736. fflush (stdout);
  4737. fprintf (stderr, "%s:", pt);
  4738. for (j = 0; j < m->count; j++)
  4739. fprintf (stderr, " %s", m->sections [j]->name);
  4740. putc ('\n',stderr);
  4741. fflush (stderr);
  4742. }
  4743. static bool
  4744. write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
  4745. {
  4746. void *buf;
  4747. bool ret;
  4748. if (bfd_seek (abfd, pos, SEEK_SET) != 0)
  4749. return false;
  4750. buf = bfd_zmalloc (len);
  4751. if (buf == NULL)
  4752. return false;
  4753. ret = bfd_bwrite (buf, len, abfd) == len;
  4754. free (buf);
  4755. return ret;
  4756. }
  4757. /* Assign file positions to the sections based on the mapping from
  4758. sections to segments. This function also sets up some fields in
  4759. the file header. */
  4760. static bool
  4761. assign_file_positions_for_load_sections (bfd *abfd,
  4762. struct bfd_link_info *link_info)
  4763. {
  4764. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  4765. struct elf_segment_map *m;
  4766. struct elf_segment_map *phdr_load_seg;
  4767. Elf_Internal_Phdr *phdrs;
  4768. Elf_Internal_Phdr *p;
  4769. file_ptr off; /* Octets. */
  4770. bfd_size_type maxpagesize;
  4771. bfd_size_type p_align;
  4772. bool p_align_p = false;
  4773. unsigned int alloc, actual;
  4774. unsigned int i, j;
  4775. struct elf_segment_map **sorted_seg_map;
  4776. unsigned int opb = bfd_octets_per_byte (abfd, NULL);
  4777. if (link_info == NULL
  4778. && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
  4779. return false;
  4780. alloc = 0;
  4781. for (m = elf_seg_map (abfd); m != NULL; m = m->next)
  4782. m->idx = alloc++;
  4783. if (alloc)
  4784. {
  4785. elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
  4786. elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
  4787. }
  4788. else
  4789. {
  4790. /* PR binutils/12467. */
  4791. elf_elfheader (abfd)->e_phoff = 0;
  4792. elf_elfheader (abfd)->e_phentsize = 0;
  4793. }
  4794. elf_elfheader (abfd)->e_phnum = alloc;
  4795. if (elf_program_header_size (abfd) == (bfd_size_type) -1)
  4796. {
  4797. actual = alloc;
  4798. elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
  4799. }
  4800. else
  4801. {
  4802. actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
  4803. BFD_ASSERT (elf_program_header_size (abfd)
  4804. == actual * bed->s->sizeof_phdr);
  4805. BFD_ASSERT (actual >= alloc);
  4806. }
  4807. if (alloc == 0)
  4808. {
  4809. elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
  4810. return true;
  4811. }
  4812. /* We're writing the size in elf_program_header_size (abfd),
  4813. see assign_file_positions_except_relocs, so make sure we have
  4814. that amount allocated, with trailing space cleared.
  4815. The variable alloc contains the computed need, while
  4816. elf_program_header_size (abfd) contains the size used for the
  4817. layout.
  4818. See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
  4819. where the layout is forced to according to a larger size in the
  4820. last iterations for the testcase ld-elf/header. */
  4821. phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
  4822. + alloc * sizeof (*sorted_seg_map)));
  4823. sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
  4824. elf_tdata (abfd)->phdr = phdrs;
  4825. if (phdrs == NULL)
  4826. return false;
  4827. for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
  4828. {
  4829. sorted_seg_map[j] = m;
  4830. /* If elf_segment_map is not from map_sections_to_segments, the
  4831. sections may not be correctly ordered. NOTE: sorting should
  4832. not be done to the PT_NOTE section of a corefile, which may
  4833. contain several pseudo-sections artificially created by bfd.
  4834. Sorting these pseudo-sections breaks things badly. */
  4835. if (m->count > 1
  4836. && !(elf_elfheader (abfd)->e_type == ET_CORE
  4837. && m->p_type == PT_NOTE))
  4838. {
  4839. for (i = 0; i < m->count; i++)
  4840. m->sections[i]->target_index = i;
  4841. qsort (m->sections, (size_t) m->count, sizeof (asection *),
  4842. elf_sort_sections);
  4843. }
  4844. }
  4845. if (alloc > 1)
  4846. qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
  4847. elf_sort_segments);
  4848. p_align = bed->p_align;
  4849. maxpagesize = 1;
  4850. if ((abfd->flags & D_PAGED) != 0)
  4851. {
  4852. if (link_info != NULL)
  4853. maxpagesize = link_info->maxpagesize;
  4854. else
  4855. maxpagesize = bed->maxpagesize;
  4856. }
  4857. /* Sections must map to file offsets past the ELF file header. */
  4858. off = bed->s->sizeof_ehdr;
  4859. /* And if one of the PT_LOAD headers doesn't include the program
  4860. headers then we'll be mapping program headers in the usual
  4861. position after the ELF file header. */
  4862. phdr_load_seg = NULL;
  4863. for (j = 0; j < alloc; j++)
  4864. {
  4865. m = sorted_seg_map[j];
  4866. if (m->p_type != PT_LOAD)
  4867. break;
  4868. if (m->includes_phdrs)
  4869. {
  4870. phdr_load_seg = m;
  4871. break;
  4872. }
  4873. }
  4874. if (phdr_load_seg == NULL)
  4875. off += actual * bed->s->sizeof_phdr;
  4876. for (j = 0; j < alloc; j++)
  4877. {
  4878. asection **secpp;
  4879. bfd_vma off_adjust; /* Octets. */
  4880. bool no_contents;
  4881. /* An ELF segment (described by Elf_Internal_Phdr) may contain a
  4882. number of sections with contents contributing to both p_filesz
  4883. and p_memsz, followed by a number of sections with no contents
  4884. that just contribute to p_memsz. In this loop, OFF tracks next
  4885. available file offset for PT_LOAD and PT_NOTE segments. */
  4886. m = sorted_seg_map[j];
  4887. p = phdrs + m->idx;
  4888. p->p_type = m->p_type;
  4889. p->p_flags = m->p_flags;
  4890. if (m->count == 0)
  4891. p->p_vaddr = m->p_vaddr_offset * opb;
  4892. else
  4893. p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
  4894. if (m->p_paddr_valid)
  4895. p->p_paddr = m->p_paddr;
  4896. else if (m->count == 0)
  4897. p->p_paddr = 0;
  4898. else
  4899. p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
  4900. if (p->p_type == PT_LOAD
  4901. && (abfd->flags & D_PAGED) != 0)
  4902. {
  4903. /* p_align in demand paged PT_LOAD segments effectively stores
  4904. the maximum page size. When copying an executable with
  4905. objcopy, we set m->p_align from the input file. Use this
  4906. value for maxpagesize rather than bed->maxpagesize, which
  4907. may be different. Note that we use maxpagesize for PT_TLS
  4908. segment alignment later in this function, so we are relying
  4909. on at least one PT_LOAD segment appearing before a PT_TLS
  4910. segment. */
  4911. if (m->p_align_valid)
  4912. maxpagesize = m->p_align;
  4913. else if (p_align != 0
  4914. && (link_info == NULL
  4915. || !link_info->maxpagesize_is_set))
  4916. /* Set p_align to the default p_align value while laying
  4917. out segments aligning to the maximum page size or the
  4918. largest section alignment. The run-time loader can
  4919. align segments to the default p_align value or the
  4920. maximum page size, depending on system page size. */
  4921. p_align_p = true;
  4922. p->p_align = maxpagesize;
  4923. }
  4924. else if (m->p_align_valid)
  4925. p->p_align = m->p_align;
  4926. else if (m->count == 0)
  4927. p->p_align = 1 << bed->s->log_file_align;
  4928. if (m == phdr_load_seg)
  4929. {
  4930. if (!m->includes_filehdr)
  4931. p->p_offset = off;
  4932. off += actual * bed->s->sizeof_phdr;
  4933. }
  4934. no_contents = false;
  4935. off_adjust = 0;
  4936. if (p->p_type == PT_LOAD
  4937. && m->count > 0)
  4938. {
  4939. bfd_size_type align; /* Bytes. */
  4940. unsigned int align_power = 0;
  4941. if (m->p_align_valid)
  4942. align = p->p_align;
  4943. else
  4944. {
  4945. for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
  4946. {
  4947. unsigned int secalign;
  4948. secalign = bfd_section_alignment (*secpp);
  4949. if (secalign > align_power)
  4950. align_power = secalign;
  4951. }
  4952. align = (bfd_size_type) 1 << align_power;
  4953. if (align < maxpagesize)
  4954. {
  4955. /* If a section requires alignment higher than the
  4956. default p_align value, don't set p_align to the
  4957. default p_align value. */
  4958. if (align > p_align)
  4959. p_align_p = false;
  4960. align = maxpagesize;
  4961. }
  4962. else
  4963. {
  4964. /* If a section requires alignment higher than the
  4965. maximum page size, set p_align to the section
  4966. alignment. */
  4967. p_align_p = true;
  4968. p_align = align;
  4969. }
  4970. }
  4971. for (i = 0; i < m->count; i++)
  4972. if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
  4973. /* If we aren't making room for this section, then
  4974. it must be SHT_NOBITS regardless of what we've
  4975. set via struct bfd_elf_special_section. */
  4976. elf_section_type (m->sections[i]) = SHT_NOBITS;
  4977. /* Find out whether this segment contains any loadable
  4978. sections. */
  4979. no_contents = true;
  4980. for (i = 0; i < m->count; i++)
  4981. if (elf_section_type (m->sections[i]) != SHT_NOBITS)
  4982. {
  4983. no_contents = false;
  4984. break;
  4985. }
  4986. off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
  4987. /* Broken hardware and/or kernel require that files do not
  4988. map the same page with different permissions on some hppa
  4989. processors. */
  4990. if (j != 0
  4991. && (abfd->flags & D_PAGED) != 0
  4992. && bed->no_page_alias
  4993. && (off & (maxpagesize - 1)) != 0
  4994. && ((off & -maxpagesize)
  4995. == ((off + off_adjust) & -maxpagesize)))
  4996. off_adjust += maxpagesize;
  4997. off += off_adjust;
  4998. if (no_contents)
  4999. {
  5000. /* We shouldn't need to align the segment on disk since
  5001. the segment doesn't need file space, but the gABI
  5002. arguably requires the alignment and glibc ld.so
  5003. checks it. So to comply with the alignment
  5004. requirement but not waste file space, we adjust
  5005. p_offset for just this segment. (OFF_ADJUST is
  5006. subtracted from OFF later.) This may put p_offset
  5007. past the end of file, but that shouldn't matter. */
  5008. }
  5009. else
  5010. off_adjust = 0;
  5011. }
  5012. /* Make sure the .dynamic section is the first section in the
  5013. PT_DYNAMIC segment. */
  5014. else if (p->p_type == PT_DYNAMIC
  5015. && m->count > 1
  5016. && strcmp (m->sections[0]->name, ".dynamic") != 0)
  5017. {
  5018. _bfd_error_handler
  5019. (_("%pB: The first section in the PT_DYNAMIC segment"
  5020. " is not the .dynamic section"),
  5021. abfd);
  5022. bfd_set_error (bfd_error_bad_value);
  5023. return false;
  5024. }
  5025. /* Set the note section type to SHT_NOTE. */
  5026. else if (p->p_type == PT_NOTE)
  5027. for (i = 0; i < m->count; i++)
  5028. elf_section_type (m->sections[i]) = SHT_NOTE;
  5029. if (m->includes_filehdr)
  5030. {
  5031. if (!m->p_flags_valid)
  5032. p->p_flags |= PF_R;
  5033. p->p_filesz = bed->s->sizeof_ehdr;
  5034. p->p_memsz = bed->s->sizeof_ehdr;
  5035. if (p->p_type == PT_LOAD)
  5036. {
  5037. if (m->count > 0)
  5038. {
  5039. if (p->p_vaddr < (bfd_vma) off
  5040. || (!m->p_paddr_valid
  5041. && p->p_paddr < (bfd_vma) off))
  5042. {
  5043. _bfd_error_handler
  5044. (_("%pB: not enough room for program headers,"
  5045. " try linking with -N"),
  5046. abfd);
  5047. bfd_set_error (bfd_error_bad_value);
  5048. return false;
  5049. }
  5050. p->p_vaddr -= off;
  5051. if (!m->p_paddr_valid)
  5052. p->p_paddr -= off;
  5053. }
  5054. }
  5055. else if (sorted_seg_map[0]->includes_filehdr)
  5056. {
  5057. Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
  5058. p->p_vaddr = filehdr->p_vaddr;
  5059. if (!m->p_paddr_valid)
  5060. p->p_paddr = filehdr->p_paddr;
  5061. }
  5062. }
  5063. if (m->includes_phdrs)
  5064. {
  5065. if (!m->p_flags_valid)
  5066. p->p_flags |= PF_R;
  5067. p->p_filesz += actual * bed->s->sizeof_phdr;
  5068. p->p_memsz += actual * bed->s->sizeof_phdr;
  5069. if (!m->includes_filehdr)
  5070. {
  5071. if (p->p_type == PT_LOAD)
  5072. {
  5073. elf_elfheader (abfd)->e_phoff = p->p_offset;
  5074. if (m->count > 0)
  5075. {
  5076. p->p_vaddr -= off - p->p_offset;
  5077. if (!m->p_paddr_valid)
  5078. p->p_paddr -= off - p->p_offset;
  5079. }
  5080. }
  5081. else if (phdr_load_seg != NULL)
  5082. {
  5083. Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
  5084. bfd_vma phdr_off = 0; /* Octets. */
  5085. if (phdr_load_seg->includes_filehdr)
  5086. phdr_off = bed->s->sizeof_ehdr;
  5087. p->p_vaddr = phdr->p_vaddr + phdr_off;
  5088. if (!m->p_paddr_valid)
  5089. p->p_paddr = phdr->p_paddr + phdr_off;
  5090. p->p_offset = phdr->p_offset + phdr_off;
  5091. }
  5092. else
  5093. p->p_offset = bed->s->sizeof_ehdr;
  5094. }
  5095. }
  5096. if (p->p_type == PT_LOAD
  5097. || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
  5098. {
  5099. if (!m->includes_filehdr && !m->includes_phdrs)
  5100. {
  5101. p->p_offset = off;
  5102. if (no_contents)
  5103. {
  5104. /* Put meaningless p_offset for PT_LOAD segments
  5105. without file contents somewhere within the first
  5106. page, in an attempt to not point past EOF. */
  5107. bfd_size_type align = maxpagesize;
  5108. if (align < p->p_align)
  5109. align = p->p_align;
  5110. if (align < 1)
  5111. align = 1;
  5112. p->p_offset = off % align;
  5113. }
  5114. }
  5115. else
  5116. {
  5117. file_ptr adjust; /* Octets. */
  5118. adjust = off - (p->p_offset + p->p_filesz);
  5119. if (!no_contents)
  5120. p->p_filesz += adjust;
  5121. p->p_memsz += adjust;
  5122. }
  5123. }
  5124. /* Set up p_filesz, p_memsz, p_align and p_flags from the section
  5125. maps. Set filepos for sections in PT_LOAD segments, and in
  5126. core files, for sections in PT_NOTE segments.
  5127. assign_file_positions_for_non_load_sections will set filepos
  5128. for other sections and update p_filesz for other segments. */
  5129. for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
  5130. {
  5131. asection *sec;
  5132. bfd_size_type align;
  5133. Elf_Internal_Shdr *this_hdr;
  5134. sec = *secpp;
  5135. this_hdr = &elf_section_data (sec)->this_hdr;
  5136. align = (bfd_size_type) 1 << bfd_section_alignment (sec);
  5137. if ((p->p_type == PT_LOAD
  5138. || p->p_type == PT_TLS)
  5139. && (this_hdr->sh_type != SHT_NOBITS
  5140. || ((this_hdr->sh_flags & SHF_ALLOC) != 0
  5141. && ((this_hdr->sh_flags & SHF_TLS) == 0
  5142. || p->p_type == PT_TLS))))
  5143. {
  5144. bfd_vma p_start = p->p_paddr; /* Octets. */
  5145. bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
  5146. bfd_vma s_start = sec->lma * opb; /* Octets. */
  5147. bfd_vma adjust = s_start - p_end; /* Octets. */
  5148. if (adjust != 0
  5149. && (s_start < p_end
  5150. || p_end < p_start))
  5151. {
  5152. _bfd_error_handler
  5153. /* xgettext:c-format */
  5154. (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
  5155. abfd, sec, (uint64_t) s_start / opb,
  5156. (uint64_t) p_end / opb);
  5157. adjust = 0;
  5158. sec->lma = p_end / opb;
  5159. }
  5160. p->p_memsz += adjust;
  5161. if (p->p_type == PT_LOAD)
  5162. {
  5163. if (this_hdr->sh_type != SHT_NOBITS)
  5164. {
  5165. off_adjust = 0;
  5166. if (p->p_filesz + adjust < p->p_memsz)
  5167. {
  5168. /* We have a PROGBITS section following NOBITS ones.
  5169. Allocate file space for the NOBITS section(s) and
  5170. zero it. */
  5171. adjust = p->p_memsz - p->p_filesz;
  5172. if (!write_zeros (abfd, off, adjust))
  5173. return false;
  5174. }
  5175. }
  5176. /* We only adjust sh_offset in SHT_NOBITS sections
  5177. as would seem proper for their address when the
  5178. section is first in the segment. sh_offset
  5179. doesn't really have any significance for
  5180. SHT_NOBITS anyway, apart from a notional position
  5181. relative to other sections. Historically we
  5182. didn't bother with adjusting sh_offset and some
  5183. programs depend on it not being adjusted. See
  5184. pr12921 and pr25662. */
  5185. if (this_hdr->sh_type != SHT_NOBITS || i == 0)
  5186. {
  5187. off += adjust;
  5188. if (this_hdr->sh_type == SHT_NOBITS)
  5189. off_adjust += adjust;
  5190. }
  5191. }
  5192. if (this_hdr->sh_type != SHT_NOBITS)
  5193. p->p_filesz += adjust;
  5194. }
  5195. if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
  5196. {
  5197. /* The section at i == 0 is the one that actually contains
  5198. everything. */
  5199. if (i == 0)
  5200. {
  5201. this_hdr->sh_offset = sec->filepos = off;
  5202. off += this_hdr->sh_size;
  5203. p->p_filesz = this_hdr->sh_size;
  5204. p->p_memsz = 0;
  5205. p->p_align = 1;
  5206. }
  5207. else
  5208. {
  5209. /* The rest are fake sections that shouldn't be written. */
  5210. sec->filepos = 0;
  5211. sec->size = 0;
  5212. sec->flags = 0;
  5213. continue;
  5214. }
  5215. }
  5216. else
  5217. {
  5218. if (p->p_type == PT_LOAD)
  5219. {
  5220. this_hdr->sh_offset = sec->filepos = off;
  5221. if (this_hdr->sh_type != SHT_NOBITS)
  5222. off += this_hdr->sh_size;
  5223. }
  5224. else if (this_hdr->sh_type == SHT_NOBITS
  5225. && (this_hdr->sh_flags & SHF_TLS) != 0
  5226. && this_hdr->sh_offset == 0)
  5227. {
  5228. /* This is a .tbss section that didn't get a PT_LOAD.
  5229. (See _bfd_elf_map_sections_to_segments "Create a
  5230. final PT_LOAD".) Set sh_offset to the value it
  5231. would have if we had created a zero p_filesz and
  5232. p_memsz PT_LOAD header for the section. This
  5233. also makes the PT_TLS header have the same
  5234. p_offset value. */
  5235. bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
  5236. off, align);
  5237. this_hdr->sh_offset = sec->filepos = off + adjust;
  5238. }
  5239. if (this_hdr->sh_type != SHT_NOBITS)
  5240. {
  5241. p->p_filesz += this_hdr->sh_size;
  5242. /* A load section without SHF_ALLOC is something like
  5243. a note section in a PT_NOTE segment. These take
  5244. file space but are not loaded into memory. */
  5245. if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
  5246. p->p_memsz += this_hdr->sh_size;
  5247. }
  5248. else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
  5249. {
  5250. if (p->p_type == PT_TLS)
  5251. p->p_memsz += this_hdr->sh_size;
  5252. /* .tbss is special. It doesn't contribute to p_memsz of
  5253. normal segments. */
  5254. else if ((this_hdr->sh_flags & SHF_TLS) == 0)
  5255. p->p_memsz += this_hdr->sh_size;
  5256. }
  5257. if (align > p->p_align
  5258. && !m->p_align_valid
  5259. && (p->p_type != PT_LOAD
  5260. || (abfd->flags & D_PAGED) == 0))
  5261. p->p_align = align;
  5262. }
  5263. if (!m->p_flags_valid)
  5264. {
  5265. p->p_flags |= PF_R;
  5266. if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
  5267. p->p_flags |= PF_X;
  5268. if ((this_hdr->sh_flags & SHF_WRITE) != 0)
  5269. p->p_flags |= PF_W;
  5270. }
  5271. }
  5272. off -= off_adjust;
  5273. /* PR ld/20815 - Check that the program header segment, if
  5274. present, will be loaded into memory. */
  5275. if (p->p_type == PT_PHDR
  5276. && phdr_load_seg == NULL
  5277. && !(bed->elf_backend_allow_non_load_phdr != NULL
  5278. && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
  5279. {
  5280. /* The fix for this error is usually to edit the linker script being
  5281. used and set up the program headers manually. Either that or
  5282. leave room for the headers at the start of the SECTIONS. */
  5283. _bfd_error_handler (_("%pB: error: PHDR segment not covered"
  5284. " by LOAD segment"),
  5285. abfd);
  5286. if (link_info == NULL)
  5287. return false;
  5288. /* Arrange for the linker to exit with an error, deleting
  5289. the output file unless --noinhibit-exec is given. */
  5290. link_info->callbacks->info ("%X");
  5291. }
  5292. /* Check that all sections are in a PT_LOAD segment.
  5293. Don't check funky gdb generated core files. */
  5294. if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
  5295. {
  5296. bool check_vma = true;
  5297. for (i = 1; i < m->count; i++)
  5298. if (m->sections[i]->vma == m->sections[i - 1]->vma
  5299. && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
  5300. ->this_hdr), p) != 0
  5301. && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
  5302. ->this_hdr), p) != 0)
  5303. {
  5304. /* Looks like we have overlays packed into the segment. */
  5305. check_vma = false;
  5306. break;
  5307. }
  5308. for (i = 0; i < m->count; i++)
  5309. {
  5310. Elf_Internal_Shdr *this_hdr;
  5311. asection *sec;
  5312. sec = m->sections[i];
  5313. this_hdr = &(elf_section_data(sec)->this_hdr);
  5314. if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
  5315. && !ELF_TBSS_SPECIAL (this_hdr, p))
  5316. {
  5317. _bfd_error_handler
  5318. /* xgettext:c-format */
  5319. (_("%pB: section `%pA' can't be allocated in segment %d"),
  5320. abfd, sec, j);
  5321. print_segment_map (m);
  5322. }
  5323. }
  5324. if (p_align_p)
  5325. p->p_align = p_align;
  5326. }
  5327. }
  5328. elf_next_file_pos (abfd) = off;
  5329. if (link_info != NULL
  5330. && phdr_load_seg != NULL
  5331. && phdr_load_seg->includes_filehdr)
  5332. {
  5333. /* There is a segment that contains both the file headers and the
  5334. program headers, so provide a symbol __ehdr_start pointing there.
  5335. A program can use this to examine itself robustly. */
  5336. struct elf_link_hash_entry *hash
  5337. = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
  5338. false, false, true);
  5339. /* If the symbol was referenced and not defined, define it. */
  5340. if (hash != NULL
  5341. && (hash->root.type == bfd_link_hash_new
  5342. || hash->root.type == bfd_link_hash_undefined
  5343. || hash->root.type == bfd_link_hash_undefweak
  5344. || hash->root.type == bfd_link_hash_common))
  5345. {
  5346. asection *s = NULL;
  5347. bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
  5348. if (phdr_load_seg->count != 0)
  5349. /* The segment contains sections, so use the first one. */
  5350. s = phdr_load_seg->sections[0];
  5351. else
  5352. /* Use the first (i.e. lowest-addressed) section in any segment. */
  5353. for (m = elf_seg_map (abfd); m != NULL; m = m->next)
  5354. if (m->p_type == PT_LOAD && m->count != 0)
  5355. {
  5356. s = m->sections[0];
  5357. break;
  5358. }
  5359. if (s != NULL)
  5360. {
  5361. hash->root.u.def.value = filehdr_vaddr - s->vma;
  5362. hash->root.u.def.section = s;
  5363. }
  5364. else
  5365. {
  5366. hash->root.u.def.value = filehdr_vaddr;
  5367. hash->root.u.def.section = bfd_abs_section_ptr;
  5368. }
  5369. hash->root.type = bfd_link_hash_defined;
  5370. hash->def_regular = 1;
  5371. hash->non_elf = 0;
  5372. }
  5373. }
  5374. return true;
  5375. }
  5376. /* Determine if a bfd is a debuginfo file. Unfortunately there
  5377. is no defined method for detecting such files, so we have to
  5378. use heuristics instead. */
  5379. bool
  5380. is_debuginfo_file (bfd *abfd)
  5381. {
  5382. if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  5383. return false;
  5384. Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
  5385. Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
  5386. Elf_Internal_Shdr **headerp;
  5387. for (headerp = start_headers; headerp < end_headers; headerp ++)
  5388. {
  5389. Elf_Internal_Shdr *header = * headerp;
  5390. /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
  5391. The only allocated sections are SHT_NOBITS or SHT_NOTES. */
  5392. if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
  5393. && header->sh_type != SHT_NOBITS
  5394. && header->sh_type != SHT_NOTE)
  5395. return false;
  5396. }
  5397. return true;
  5398. }
  5399. /* Assign file positions for the other sections, except for compressed debugging
  5400. and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
  5401. static bool
  5402. assign_file_positions_for_non_load_sections (bfd *abfd,
  5403. struct bfd_link_info *link_info)
  5404. {
  5405. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  5406. Elf_Internal_Shdr **i_shdrpp;
  5407. Elf_Internal_Shdr **hdrpp, **end_hdrpp;
  5408. Elf_Internal_Phdr *phdrs;
  5409. Elf_Internal_Phdr *p;
  5410. struct elf_segment_map *m;
  5411. file_ptr off;
  5412. unsigned int opb = bfd_octets_per_byte (abfd, NULL);
  5413. bfd_vma maxpagesize;
  5414. if (link_info != NULL)
  5415. maxpagesize = link_info->maxpagesize;
  5416. else
  5417. maxpagesize = bed->maxpagesize;
  5418. i_shdrpp = elf_elfsections (abfd);
  5419. end_hdrpp = i_shdrpp + elf_numsections (abfd);
  5420. off = elf_next_file_pos (abfd);
  5421. for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
  5422. {
  5423. Elf_Internal_Shdr *hdr;
  5424. bfd_vma align;
  5425. hdr = *hdrpp;
  5426. if (hdr->bfd_section != NULL
  5427. && (hdr->bfd_section->filepos != 0
  5428. || (hdr->sh_type == SHT_NOBITS
  5429. && hdr->contents == NULL)))
  5430. BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
  5431. else if ((hdr->sh_flags & SHF_ALLOC) != 0)
  5432. {
  5433. if (hdr->sh_size != 0
  5434. /* PR 24717 - debuginfo files are known to be not strictly
  5435. compliant with the ELF standard. In particular they often
  5436. have .note.gnu.property sections that are outside of any
  5437. loadable segment. This is not a problem for such files,
  5438. so do not warn about them. */
  5439. && ! is_debuginfo_file (abfd))
  5440. _bfd_error_handler
  5441. /* xgettext:c-format */
  5442. (_("%pB: warning: allocated section `%s' not in segment"),
  5443. abfd,
  5444. (hdr->bfd_section == NULL
  5445. ? "*unknown*"
  5446. : hdr->bfd_section->name));
  5447. /* We don't need to page align empty sections. */
  5448. if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
  5449. align = maxpagesize;
  5450. else
  5451. align = hdr->sh_addralign & -hdr->sh_addralign;
  5452. off += vma_page_aligned_bias (hdr->sh_addr, off, align);
  5453. off = _bfd_elf_assign_file_position_for_section (hdr, off,
  5454. false);
  5455. }
  5456. else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
  5457. && hdr->bfd_section == NULL)
  5458. /* We don't know the offset of these sections yet: their size has
  5459. not been decided. */
  5460. || (hdr->bfd_section != NULL
  5461. && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
  5462. || (bfd_section_is_ctf (hdr->bfd_section)
  5463. && abfd->is_linker_output)))
  5464. || hdr == i_shdrpp[elf_onesymtab (abfd)]
  5465. || (elf_symtab_shndx_list (abfd) != NULL
  5466. && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
  5467. || hdr == i_shdrpp[elf_strtab_sec (abfd)]
  5468. || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
  5469. hdr->sh_offset = -1;
  5470. else
  5471. off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
  5472. }
  5473. elf_next_file_pos (abfd) = off;
  5474. /* Now that we have set the section file positions, we can set up
  5475. the file positions for the non PT_LOAD segments. */
  5476. phdrs = elf_tdata (abfd)->phdr;
  5477. for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
  5478. {
  5479. if (p->p_type == PT_GNU_RELRO)
  5480. {
  5481. bfd_vma start, end; /* Bytes. */
  5482. bool ok;
  5483. if (link_info != NULL)
  5484. {
  5485. /* During linking the range of the RELRO segment is passed
  5486. in link_info. Note that there may be padding between
  5487. relro_start and the first RELRO section. */
  5488. start = link_info->relro_start;
  5489. end = link_info->relro_end;
  5490. }
  5491. else if (m->count != 0)
  5492. {
  5493. if (!m->p_size_valid)
  5494. abort ();
  5495. start = m->sections[0]->vma;
  5496. end = start + m->p_size / opb;
  5497. }
  5498. else
  5499. {
  5500. start = 0;
  5501. end = 0;
  5502. }
  5503. ok = false;
  5504. if (start < end)
  5505. {
  5506. struct elf_segment_map *lm;
  5507. const Elf_Internal_Phdr *lp;
  5508. unsigned int i;
  5509. /* Find a LOAD segment containing a section in the RELRO
  5510. segment. */
  5511. for (lm = elf_seg_map (abfd), lp = phdrs;
  5512. lm != NULL;
  5513. lm = lm->next, lp++)
  5514. {
  5515. if (lp->p_type == PT_LOAD
  5516. && lm->count != 0
  5517. && (lm->sections[lm->count - 1]->vma
  5518. + (!IS_TBSS (lm->sections[lm->count - 1])
  5519. ? lm->sections[lm->count - 1]->size / opb
  5520. : 0)) > start
  5521. && lm->sections[0]->vma < end)
  5522. break;
  5523. }
  5524. if (lm != NULL)
  5525. {
  5526. /* Find the section starting the RELRO segment. */
  5527. for (i = 0; i < lm->count; i++)
  5528. {
  5529. asection *s = lm->sections[i];
  5530. if (s->vma >= start
  5531. && s->vma < end
  5532. && s->size != 0)
  5533. break;
  5534. }
  5535. if (i < lm->count)
  5536. {
  5537. p->p_vaddr = lm->sections[i]->vma * opb;
  5538. p->p_paddr = lm->sections[i]->lma * opb;
  5539. p->p_offset = lm->sections[i]->filepos;
  5540. p->p_memsz = end * opb - p->p_vaddr;
  5541. p->p_filesz = p->p_memsz;
  5542. /* The RELRO segment typically ends a few bytes
  5543. into .got.plt but other layouts are possible.
  5544. In cases where the end does not match any
  5545. loaded section (for instance is in file
  5546. padding), trim p_filesz back to correspond to
  5547. the end of loaded section contents. */
  5548. if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
  5549. p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
  5550. /* Preserve the alignment and flags if they are
  5551. valid. The gold linker generates RW/4 for
  5552. the PT_GNU_RELRO section. It is better for
  5553. objcopy/strip to honor these attributes
  5554. otherwise gdb will choke when using separate
  5555. debug files. */
  5556. if (!m->p_align_valid)
  5557. p->p_align = 1;
  5558. if (!m->p_flags_valid)
  5559. p->p_flags = PF_R;
  5560. ok = true;
  5561. }
  5562. }
  5563. }
  5564. if (!ok)
  5565. {
  5566. if (link_info != NULL)
  5567. _bfd_error_handler
  5568. (_("%pB: warning: unable to allocate any sections to PT_GNU_RELRO segment"),
  5569. abfd);
  5570. memset (p, 0, sizeof *p);
  5571. }
  5572. }
  5573. else if (p->p_type == PT_GNU_STACK)
  5574. {
  5575. if (m->p_size_valid)
  5576. p->p_memsz = m->p_size;
  5577. }
  5578. else if (m->count != 0)
  5579. {
  5580. unsigned int i;
  5581. if (p->p_type != PT_LOAD
  5582. && (p->p_type != PT_NOTE
  5583. || bfd_get_format (abfd) != bfd_core))
  5584. {
  5585. /* A user specified segment layout may include a PHDR
  5586. segment that overlaps with a LOAD segment... */
  5587. if (p->p_type == PT_PHDR)
  5588. {
  5589. m->count = 0;
  5590. continue;
  5591. }
  5592. if (m->includes_filehdr || m->includes_phdrs)
  5593. {
  5594. /* PR 17512: file: 2195325e. */
  5595. _bfd_error_handler
  5596. (_("%pB: error: non-load segment %d includes file header "
  5597. "and/or program header"),
  5598. abfd, (int) (p - phdrs));
  5599. return false;
  5600. }
  5601. p->p_filesz = 0;
  5602. p->p_offset = m->sections[0]->filepos;
  5603. for (i = m->count; i-- != 0;)
  5604. {
  5605. asection *sect = m->sections[i];
  5606. Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
  5607. if (hdr->sh_type != SHT_NOBITS)
  5608. {
  5609. p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
  5610. /* NB: p_memsz of the loadable PT_NOTE segment
  5611. should be the same as p_filesz. */
  5612. if (p->p_type == PT_NOTE
  5613. && (hdr->sh_flags & SHF_ALLOC) != 0)
  5614. p->p_memsz = p->p_filesz;
  5615. break;
  5616. }
  5617. }
  5618. }
  5619. }
  5620. }
  5621. return true;
  5622. }
  5623. static elf_section_list *
  5624. find_section_in_list (unsigned int i, elf_section_list * list)
  5625. {
  5626. for (;list != NULL; list = list->next)
  5627. if (list->ndx == i)
  5628. break;
  5629. return list;
  5630. }
  5631. /* Work out the file positions of all the sections. This is called by
  5632. _bfd_elf_compute_section_file_positions. All the section sizes and
  5633. VMAs must be known before this is called.
  5634. Reloc sections come in two flavours: Those processed specially as
  5635. "side-channel" data attached to a section to which they apply, and those that
  5636. bfd doesn't process as relocations. The latter sort are stored in a normal
  5637. bfd section by bfd_section_from_shdr. We don't consider the former sort
  5638. here, unless they form part of the loadable image. Reloc sections not
  5639. assigned here (and compressed debugging sections and CTF sections which
  5640. nothing else in the file can rely upon) will be handled later by
  5641. assign_file_positions_for_relocs.
  5642. We also don't set the positions of the .symtab and .strtab here. */
  5643. static bool
  5644. assign_file_positions_except_relocs (bfd *abfd,
  5645. struct bfd_link_info *link_info)
  5646. {
  5647. struct elf_obj_tdata *tdata = elf_tdata (abfd);
  5648. Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
  5649. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  5650. unsigned int alloc;
  5651. if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
  5652. && bfd_get_format (abfd) != bfd_core)
  5653. {
  5654. Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
  5655. unsigned int num_sec = elf_numsections (abfd);
  5656. Elf_Internal_Shdr **hdrpp;
  5657. unsigned int i;
  5658. file_ptr off;
  5659. /* Start after the ELF header. */
  5660. off = i_ehdrp->e_ehsize;
  5661. /* We are not creating an executable, which means that we are
  5662. not creating a program header, and that the actual order of
  5663. the sections in the file is unimportant. */
  5664. for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
  5665. {
  5666. Elf_Internal_Shdr *hdr;
  5667. hdr = *hdrpp;
  5668. if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
  5669. && hdr->bfd_section == NULL)
  5670. /* Do not assign offsets for these sections yet: we don't know
  5671. their sizes. */
  5672. || (hdr->bfd_section != NULL
  5673. && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
  5674. || (bfd_section_is_ctf (hdr->bfd_section)
  5675. && abfd->is_linker_output)))
  5676. || i == elf_onesymtab (abfd)
  5677. || (elf_symtab_shndx_list (abfd) != NULL
  5678. && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
  5679. || i == elf_strtab_sec (abfd)
  5680. || i == elf_shstrtab_sec (abfd))
  5681. {
  5682. hdr->sh_offset = -1;
  5683. }
  5684. else
  5685. off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
  5686. }
  5687. elf_next_file_pos (abfd) = off;
  5688. elf_program_header_size (abfd) = 0;
  5689. }
  5690. else
  5691. {
  5692. /* Assign file positions for the loaded sections based on the
  5693. assignment of sections to segments. */
  5694. if (!assign_file_positions_for_load_sections (abfd, link_info))
  5695. return false;
  5696. /* And for non-load sections. */
  5697. if (!assign_file_positions_for_non_load_sections (abfd, link_info))
  5698. return false;
  5699. }
  5700. if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
  5701. return false;
  5702. /* Write out the program headers. */
  5703. alloc = i_ehdrp->e_phnum;
  5704. if (alloc != 0)
  5705. {
  5706. if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
  5707. || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
  5708. return false;
  5709. }
  5710. return true;
  5711. }
  5712. bool
  5713. _bfd_elf_init_file_header (bfd *abfd,
  5714. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  5715. {
  5716. Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
  5717. struct elf_strtab_hash *shstrtab;
  5718. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  5719. i_ehdrp = elf_elfheader (abfd);
  5720. shstrtab = _bfd_elf_strtab_init ();
  5721. if (shstrtab == NULL)
  5722. return false;
  5723. elf_shstrtab (abfd) = shstrtab;
  5724. i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
  5725. i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
  5726. i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
  5727. i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
  5728. i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
  5729. i_ehdrp->e_ident[EI_DATA] =
  5730. bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
  5731. i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
  5732. if ((abfd->flags & DYNAMIC) != 0)
  5733. i_ehdrp->e_type = ET_DYN;
  5734. else if ((abfd->flags & EXEC_P) != 0)
  5735. i_ehdrp->e_type = ET_EXEC;
  5736. else if (bfd_get_format (abfd) == bfd_core)
  5737. i_ehdrp->e_type = ET_CORE;
  5738. else
  5739. i_ehdrp->e_type = ET_REL;
  5740. switch (bfd_get_arch (abfd))
  5741. {
  5742. case bfd_arch_unknown:
  5743. i_ehdrp->e_machine = EM_NONE;
  5744. break;
  5745. /* There used to be a long list of cases here, each one setting
  5746. e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
  5747. in the corresponding bfd definition. To avoid duplication,
  5748. the switch was removed. Machines that need special handling
  5749. can generally do it in elf_backend_final_write_processing(),
  5750. unless they need the information earlier than the final write.
  5751. Such need can generally be supplied by replacing the tests for
  5752. e_machine with the conditions used to determine it. */
  5753. default:
  5754. i_ehdrp->e_machine = bed->elf_machine_code;
  5755. }
  5756. i_ehdrp->e_version = bed->s->ev_current;
  5757. i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
  5758. /* No program header, for now. */
  5759. i_ehdrp->e_phoff = 0;
  5760. i_ehdrp->e_phentsize = 0;
  5761. i_ehdrp->e_phnum = 0;
  5762. /* Each bfd section is section header entry. */
  5763. i_ehdrp->e_entry = bfd_get_start_address (abfd);
  5764. i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
  5765. elf_tdata (abfd)->symtab_hdr.sh_name =
  5766. (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
  5767. elf_tdata (abfd)->strtab_hdr.sh_name =
  5768. (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
  5769. elf_tdata (abfd)->shstrtab_hdr.sh_name =
  5770. (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
  5771. if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
  5772. || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
  5773. || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
  5774. return false;
  5775. return true;
  5776. }
  5777. /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
  5778. FIXME: We used to have code here to sort the PT_LOAD segments into
  5779. ascending order, as per the ELF spec. But this breaks some programs,
  5780. including the Linux kernel. But really either the spec should be
  5781. changed or the programs updated. */
  5782. bool
  5783. _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
  5784. {
  5785. if (link_info != NULL && bfd_link_pie (link_info))
  5786. {
  5787. Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
  5788. unsigned int num_segments = i_ehdrp->e_phnum;
  5789. struct elf_obj_tdata *tdata = elf_tdata (obfd);
  5790. Elf_Internal_Phdr *segment = tdata->phdr;
  5791. Elf_Internal_Phdr *end_segment = &segment[num_segments];
  5792. /* Find the lowest p_vaddr in PT_LOAD segments. */
  5793. bfd_vma p_vaddr = (bfd_vma) -1;
  5794. for (; segment < end_segment; segment++)
  5795. if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
  5796. p_vaddr = segment->p_vaddr;
  5797. /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
  5798. segments is non-zero. */
  5799. if (p_vaddr)
  5800. i_ehdrp->e_type = ET_EXEC;
  5801. }
  5802. return true;
  5803. }
  5804. /* Assign file positions for all the reloc sections which are not part
  5805. of the loadable file image, and the file position of section headers. */
  5806. static bool
  5807. _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
  5808. {
  5809. file_ptr off;
  5810. Elf_Internal_Shdr **shdrpp, **end_shdrpp;
  5811. Elf_Internal_Shdr *shdrp;
  5812. Elf_Internal_Ehdr *i_ehdrp;
  5813. const struct elf_backend_data *bed;
  5814. off = elf_next_file_pos (abfd);
  5815. shdrpp = elf_elfsections (abfd);
  5816. end_shdrpp = shdrpp + elf_numsections (abfd);
  5817. for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
  5818. {
  5819. shdrp = *shdrpp;
  5820. if (shdrp->sh_offset == -1)
  5821. {
  5822. asection *sec = shdrp->bfd_section;
  5823. bool is_rel = (shdrp->sh_type == SHT_REL
  5824. || shdrp->sh_type == SHT_RELA);
  5825. bool is_ctf = sec && bfd_section_is_ctf (sec);
  5826. if (is_rel
  5827. || is_ctf
  5828. || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
  5829. {
  5830. if (!is_rel && !is_ctf)
  5831. {
  5832. const char *name = sec->name;
  5833. struct bfd_elf_section_data *d;
  5834. /* Compress DWARF debug sections. */
  5835. if (!bfd_compress_section (abfd, sec,
  5836. shdrp->contents))
  5837. return false;
  5838. if (sec->compress_status == COMPRESS_SECTION_DONE
  5839. && (abfd->flags & BFD_COMPRESS_GABI) == 0)
  5840. {
  5841. /* If section is compressed with zlib-gnu, convert
  5842. section name from .debug_* to .zdebug_*. */
  5843. char *new_name
  5844. = convert_debug_to_zdebug (abfd, name);
  5845. if (new_name == NULL)
  5846. return false;
  5847. name = new_name;
  5848. }
  5849. /* Add section name to section name section. */
  5850. if (shdrp->sh_name != (unsigned int) -1)
  5851. abort ();
  5852. shdrp->sh_name
  5853. = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
  5854. name, false);
  5855. d = elf_section_data (sec);
  5856. /* Add reloc section name to section name section. */
  5857. if (d->rel.hdr
  5858. && !_bfd_elf_set_reloc_sh_name (abfd,
  5859. d->rel.hdr,
  5860. name, false))
  5861. return false;
  5862. if (d->rela.hdr
  5863. && !_bfd_elf_set_reloc_sh_name (abfd,
  5864. d->rela.hdr,
  5865. name, true))
  5866. return false;
  5867. /* Update section size and contents. */
  5868. shdrp->sh_size = sec->size;
  5869. shdrp->contents = sec->contents;
  5870. shdrp->bfd_section->contents = NULL;
  5871. }
  5872. else if (is_ctf)
  5873. {
  5874. /* Update section size and contents. */
  5875. shdrp->sh_size = sec->size;
  5876. shdrp->contents = sec->contents;
  5877. }
  5878. off = _bfd_elf_assign_file_position_for_section (shdrp,
  5879. off,
  5880. true);
  5881. }
  5882. }
  5883. }
  5884. /* Place section name section after DWARF debug sections have been
  5885. compressed. */
  5886. _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
  5887. shdrp = &elf_tdata (abfd)->shstrtab_hdr;
  5888. shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
  5889. off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
  5890. /* Place the section headers. */
  5891. i_ehdrp = elf_elfheader (abfd);
  5892. bed = get_elf_backend_data (abfd);
  5893. off = align_file_position (off, 1 << bed->s->log_file_align);
  5894. i_ehdrp->e_shoff = off;
  5895. off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
  5896. elf_next_file_pos (abfd) = off;
  5897. return true;
  5898. }
  5899. bool
  5900. _bfd_elf_write_object_contents (bfd *abfd)
  5901. {
  5902. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  5903. Elf_Internal_Shdr **i_shdrp;
  5904. bool failed;
  5905. unsigned int count, num_sec;
  5906. struct elf_obj_tdata *t;
  5907. if (! abfd->output_has_begun
  5908. && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
  5909. return false;
  5910. /* Do not rewrite ELF data when the BFD has been opened for update.
  5911. abfd->output_has_begun was set to TRUE on opening, so creation of new
  5912. sections, and modification of existing section sizes was restricted.
  5913. This means the ELF header, program headers and section headers can't have
  5914. changed.
  5915. If the contents of any sections has been modified, then those changes have
  5916. already been written to the BFD. */
  5917. else if (abfd->direction == both_direction)
  5918. {
  5919. BFD_ASSERT (abfd->output_has_begun);
  5920. return true;
  5921. }
  5922. i_shdrp = elf_elfsections (abfd);
  5923. failed = false;
  5924. bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
  5925. if (failed)
  5926. return false;
  5927. if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
  5928. return false;
  5929. /* After writing the headers, we need to write the sections too... */
  5930. num_sec = elf_numsections (abfd);
  5931. for (count = 1; count < num_sec; count++)
  5932. {
  5933. i_shdrp[count]->sh_name
  5934. = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
  5935. i_shdrp[count]->sh_name);
  5936. if (bed->elf_backend_section_processing)
  5937. if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
  5938. return false;
  5939. if (i_shdrp[count]->contents)
  5940. {
  5941. bfd_size_type amt = i_shdrp[count]->sh_size;
  5942. if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
  5943. || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
  5944. return false;
  5945. }
  5946. }
  5947. /* Write out the section header names. */
  5948. t = elf_tdata (abfd);
  5949. if (elf_shstrtab (abfd) != NULL
  5950. && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
  5951. || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
  5952. return false;
  5953. if (!(*bed->elf_backend_final_write_processing) (abfd))
  5954. return false;
  5955. if (!bed->s->write_shdrs_and_ehdr (abfd))
  5956. return false;
  5957. /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
  5958. if (t->o->build_id.after_write_object_contents != NULL)
  5959. return (*t->o->build_id.after_write_object_contents) (abfd);
  5960. return true;
  5961. }
  5962. bool
  5963. _bfd_elf_write_corefile_contents (bfd *abfd)
  5964. {
  5965. /* Hopefully this can be done just like an object file. */
  5966. return _bfd_elf_write_object_contents (abfd);
  5967. }
  5968. /* Given a section, search the header to find them. */
  5969. unsigned int
  5970. _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
  5971. {
  5972. const struct elf_backend_data *bed;
  5973. unsigned int sec_index;
  5974. if (elf_section_data (asect) != NULL
  5975. && elf_section_data (asect)->this_idx != 0)
  5976. return elf_section_data (asect)->this_idx;
  5977. if (bfd_is_abs_section (asect))
  5978. sec_index = SHN_ABS;
  5979. else if (bfd_is_com_section (asect))
  5980. sec_index = SHN_COMMON;
  5981. else if (bfd_is_und_section (asect))
  5982. sec_index = SHN_UNDEF;
  5983. else
  5984. sec_index = SHN_BAD;
  5985. bed = get_elf_backend_data (abfd);
  5986. if (bed->elf_backend_section_from_bfd_section)
  5987. {
  5988. int retval = sec_index;
  5989. if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
  5990. return retval;
  5991. }
  5992. if (sec_index == SHN_BAD)
  5993. bfd_set_error (bfd_error_nonrepresentable_section);
  5994. return sec_index;
  5995. }
  5996. /* Given a BFD symbol, return the index in the ELF symbol table, or -1
  5997. on error. */
  5998. int
  5999. _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
  6000. {
  6001. asymbol *asym_ptr = *asym_ptr_ptr;
  6002. int idx;
  6003. flagword flags = asym_ptr->flags;
  6004. /* When gas creates relocations against local labels, it creates its
  6005. own symbol for the section, but does put the symbol into the
  6006. symbol chain, so udata is 0. When the linker is generating
  6007. relocatable output, this section symbol may be for one of the
  6008. input sections rather than the output section. */
  6009. if (asym_ptr->udata.i == 0
  6010. && (flags & BSF_SECTION_SYM)
  6011. && asym_ptr->section)
  6012. {
  6013. asection *sec;
  6014. sec = asym_ptr->section;
  6015. if (sec->owner != abfd && sec->output_section != NULL)
  6016. sec = sec->output_section;
  6017. if (sec->owner == abfd
  6018. && sec->index < elf_num_section_syms (abfd)
  6019. && elf_section_syms (abfd)[sec->index] != NULL)
  6020. asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
  6021. }
  6022. idx = asym_ptr->udata.i;
  6023. if (idx == 0)
  6024. {
  6025. /* This case can occur when using --strip-symbol on a symbol
  6026. which is used in a relocation entry. */
  6027. _bfd_error_handler
  6028. /* xgettext:c-format */
  6029. (_("%pB: symbol `%s' required but not present"),
  6030. abfd, bfd_asymbol_name (asym_ptr));
  6031. bfd_set_error (bfd_error_no_symbols);
  6032. return -1;
  6033. }
  6034. #if DEBUG & 4
  6035. {
  6036. fprintf (stderr,
  6037. "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
  6038. (long) asym_ptr, asym_ptr->name, idx, flags);
  6039. fflush (stderr);
  6040. }
  6041. #endif
  6042. return idx;
  6043. }
  6044. /* Rewrite program header information. */
  6045. static bool
  6046. rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
  6047. {
  6048. Elf_Internal_Ehdr *iehdr;
  6049. struct elf_segment_map *map;
  6050. struct elf_segment_map *map_first;
  6051. struct elf_segment_map **pointer_to_map;
  6052. Elf_Internal_Phdr *segment;
  6053. asection *section;
  6054. unsigned int i;
  6055. unsigned int num_segments;
  6056. bool phdr_included = false;
  6057. bool p_paddr_valid;
  6058. struct elf_segment_map *phdr_adjust_seg = NULL;
  6059. unsigned int phdr_adjust_num = 0;
  6060. const struct elf_backend_data *bed;
  6061. unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
  6062. bed = get_elf_backend_data (ibfd);
  6063. iehdr = elf_elfheader (ibfd);
  6064. map_first = NULL;
  6065. pointer_to_map = &map_first;
  6066. num_segments = elf_elfheader (ibfd)->e_phnum;
  6067. /* Returns the end address of the segment + 1. */
  6068. #define SEGMENT_END(segment, start) \
  6069. (start + (segment->p_memsz > segment->p_filesz \
  6070. ? segment->p_memsz : segment->p_filesz))
  6071. #define SECTION_SIZE(section, segment) \
  6072. (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
  6073. != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
  6074. ? section->size : 0)
  6075. /* Returns TRUE if the given section is contained within
  6076. the given segment. VMA addresses are compared. */
  6077. #define IS_CONTAINED_BY_VMA(section, segment, opb) \
  6078. (section->vma * (opb) >= segment->p_vaddr \
  6079. && (section->vma * (opb) + SECTION_SIZE (section, segment) \
  6080. <= (SEGMENT_END (segment, segment->p_vaddr))))
  6081. /* Returns TRUE if the given section is contained within
  6082. the given segment. LMA addresses are compared. */
  6083. #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
  6084. (section->lma * (opb) >= base \
  6085. && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
  6086. && (section->lma * (opb) + SECTION_SIZE (section, segment) \
  6087. <= SEGMENT_END (segment, base)))
  6088. /* Handle PT_NOTE segment. */
  6089. #define IS_NOTE(p, s) \
  6090. (p->p_type == PT_NOTE \
  6091. && elf_section_type (s) == SHT_NOTE \
  6092. && (bfd_vma) s->filepos >= p->p_offset \
  6093. && ((bfd_vma) s->filepos + s->size \
  6094. <= p->p_offset + p->p_filesz))
  6095. /* Special case: corefile "NOTE" section containing regs, prpsinfo
  6096. etc. */
  6097. #define IS_COREFILE_NOTE(p, s) \
  6098. (IS_NOTE (p, s) \
  6099. && bfd_get_format (ibfd) == bfd_core \
  6100. && s->vma == 0 \
  6101. && s->lma == 0)
  6102. /* The complicated case when p_vaddr is 0 is to handle the Solaris
  6103. linker, which generates a PT_INTERP section with p_vaddr and
  6104. p_memsz set to 0. */
  6105. #define IS_SOLARIS_PT_INTERP(p, s) \
  6106. (p->p_vaddr == 0 \
  6107. && p->p_paddr == 0 \
  6108. && p->p_memsz == 0 \
  6109. && p->p_filesz > 0 \
  6110. && (s->flags & SEC_HAS_CONTENTS) != 0 \
  6111. && s->size > 0 \
  6112. && (bfd_vma) s->filepos >= p->p_offset \
  6113. && ((bfd_vma) s->filepos + s->size \
  6114. <= p->p_offset + p->p_filesz))
  6115. /* Decide if the given section should be included in the given segment.
  6116. A section will be included if:
  6117. 1. It is within the address space of the segment -- we use the LMA
  6118. if that is set for the segment and the VMA otherwise,
  6119. 2. It is an allocated section or a NOTE section in a PT_NOTE
  6120. segment.
  6121. 3. There is an output section associated with it,
  6122. 4. The section has not already been allocated to a previous segment.
  6123. 5. PT_GNU_STACK segments do not include any sections.
  6124. 6. PT_TLS segment includes only SHF_TLS sections.
  6125. 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
  6126. 8. PT_DYNAMIC should not contain empty sections at the beginning
  6127. (with the possible exception of .dynamic). */
  6128. #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
  6129. ((((segment->p_paddr \
  6130. ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
  6131. : IS_CONTAINED_BY_VMA (section, segment, opb)) \
  6132. && (section->flags & SEC_ALLOC) != 0) \
  6133. || IS_NOTE (segment, section)) \
  6134. && segment->p_type != PT_GNU_STACK \
  6135. && (segment->p_type != PT_TLS \
  6136. || (section->flags & SEC_THREAD_LOCAL)) \
  6137. && (segment->p_type == PT_LOAD \
  6138. || segment->p_type == PT_TLS \
  6139. || (section->flags & SEC_THREAD_LOCAL) == 0) \
  6140. && (segment->p_type != PT_DYNAMIC \
  6141. || SECTION_SIZE (section, segment) > 0 \
  6142. || (segment->p_paddr \
  6143. ? segment->p_paddr != section->lma * (opb) \
  6144. : segment->p_vaddr != section->vma * (opb)) \
  6145. || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
  6146. && (segment->p_type != PT_LOAD || !section->segment_mark))
  6147. /* If the output section of a section in the input segment is NULL,
  6148. it is removed from the corresponding output segment. */
  6149. #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
  6150. (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
  6151. && section->output_section != NULL)
  6152. /* Returns TRUE iff seg1 starts after the end of seg2. */
  6153. #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
  6154. (seg1->field >= SEGMENT_END (seg2, seg2->field))
  6155. /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
  6156. their VMA address ranges and their LMA address ranges overlap.
  6157. It is possible to have overlapping VMA ranges without overlapping LMA
  6158. ranges. RedBoot images for example can have both .data and .bss mapped
  6159. to the same VMA range, but with the .data section mapped to a different
  6160. LMA. */
  6161. #define SEGMENT_OVERLAPS(seg1, seg2) \
  6162. ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
  6163. || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
  6164. && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
  6165. || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
  6166. /* Initialise the segment mark field, and discard stupid alignment. */
  6167. for (section = ibfd->sections; section != NULL; section = section->next)
  6168. {
  6169. asection *o = section->output_section;
  6170. if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
  6171. o->alignment_power = 0;
  6172. section->segment_mark = false;
  6173. }
  6174. /* The Solaris linker creates program headers in which all the
  6175. p_paddr fields are zero. When we try to objcopy or strip such a
  6176. file, we get confused. Check for this case, and if we find it
  6177. don't set the p_paddr_valid fields. */
  6178. p_paddr_valid = false;
  6179. for (i = 0, segment = elf_tdata (ibfd)->phdr;
  6180. i < num_segments;
  6181. i++, segment++)
  6182. if (segment->p_paddr != 0)
  6183. {
  6184. p_paddr_valid = true;
  6185. break;
  6186. }
  6187. /* Scan through the segments specified in the program header
  6188. of the input BFD. For this first scan we look for overlaps
  6189. in the loadable segments. These can be created by weird
  6190. parameters to objcopy. Also, fix some solaris weirdness. */
  6191. for (i = 0, segment = elf_tdata (ibfd)->phdr;
  6192. i < num_segments;
  6193. i++, segment++)
  6194. {
  6195. unsigned int j;
  6196. Elf_Internal_Phdr *segment2;
  6197. if (segment->p_type == PT_INTERP)
  6198. for (section = ibfd->sections; section; section = section->next)
  6199. if (IS_SOLARIS_PT_INTERP (segment, section))
  6200. {
  6201. /* Mininal change so that the normal section to segment
  6202. assignment code will work. */
  6203. segment->p_vaddr = section->vma * opb;
  6204. break;
  6205. }
  6206. if (segment->p_type != PT_LOAD)
  6207. {
  6208. /* Remove PT_GNU_RELRO segment. */
  6209. if (segment->p_type == PT_GNU_RELRO)
  6210. segment->p_type = PT_NULL;
  6211. continue;
  6212. }
  6213. /* Determine if this segment overlaps any previous segments. */
  6214. for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
  6215. {
  6216. bfd_signed_vma extra_length;
  6217. if (segment2->p_type != PT_LOAD
  6218. || !SEGMENT_OVERLAPS (segment, segment2))
  6219. continue;
  6220. /* Merge the two segments together. */
  6221. if (segment2->p_vaddr < segment->p_vaddr)
  6222. {
  6223. /* Extend SEGMENT2 to include SEGMENT and then delete
  6224. SEGMENT. */
  6225. extra_length = (SEGMENT_END (segment, segment->p_vaddr)
  6226. - SEGMENT_END (segment2, segment2->p_vaddr));
  6227. if (extra_length > 0)
  6228. {
  6229. segment2->p_memsz += extra_length;
  6230. segment2->p_filesz += extra_length;
  6231. }
  6232. segment->p_type = PT_NULL;
  6233. /* Since we have deleted P we must restart the outer loop. */
  6234. i = 0;
  6235. segment = elf_tdata (ibfd)->phdr;
  6236. break;
  6237. }
  6238. else
  6239. {
  6240. /* Extend SEGMENT to include SEGMENT2 and then delete
  6241. SEGMENT2. */
  6242. extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
  6243. - SEGMENT_END (segment, segment->p_vaddr));
  6244. if (extra_length > 0)
  6245. {
  6246. segment->p_memsz += extra_length;
  6247. segment->p_filesz += extra_length;
  6248. }
  6249. segment2->p_type = PT_NULL;
  6250. }
  6251. }
  6252. }
  6253. /* The second scan attempts to assign sections to segments. */
  6254. for (i = 0, segment = elf_tdata (ibfd)->phdr;
  6255. i < num_segments;
  6256. i++, segment++)
  6257. {
  6258. unsigned int section_count;
  6259. asection **sections;
  6260. asection *output_section;
  6261. unsigned int isec;
  6262. asection *matching_lma;
  6263. asection *suggested_lma;
  6264. unsigned int j;
  6265. size_t amt;
  6266. asection *first_section;
  6267. if (segment->p_type == PT_NULL)
  6268. continue;
  6269. first_section = NULL;
  6270. /* Compute how many sections might be placed into this segment. */
  6271. for (section = ibfd->sections, section_count = 0;
  6272. section != NULL;
  6273. section = section->next)
  6274. {
  6275. /* Find the first section in the input segment, which may be
  6276. removed from the corresponding output segment. */
  6277. if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
  6278. {
  6279. if (first_section == NULL)
  6280. first_section = section;
  6281. if (section->output_section != NULL)
  6282. ++section_count;
  6283. }
  6284. }
  6285. /* Allocate a segment map big enough to contain
  6286. all of the sections we have selected. */
  6287. amt = sizeof (struct elf_segment_map) - sizeof (asection *);
  6288. amt += section_count * sizeof (asection *);
  6289. map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
  6290. if (map == NULL)
  6291. return false;
  6292. /* Initialise the fields of the segment map. Default to
  6293. using the physical address of the segment in the input BFD. */
  6294. map->next = NULL;
  6295. map->p_type = segment->p_type;
  6296. map->p_flags = segment->p_flags;
  6297. map->p_flags_valid = 1;
  6298. if (map->p_type == PT_LOAD
  6299. && (ibfd->flags & D_PAGED) != 0
  6300. && maxpagesize > 1
  6301. && segment->p_align > 1)
  6302. {
  6303. map->p_align = segment->p_align;
  6304. if (segment->p_align > maxpagesize)
  6305. map->p_align = maxpagesize;
  6306. map->p_align_valid = 1;
  6307. }
  6308. /* If the first section in the input segment is removed, there is
  6309. no need to preserve segment physical address in the corresponding
  6310. output segment. */
  6311. if (!first_section || first_section->output_section != NULL)
  6312. {
  6313. map->p_paddr = segment->p_paddr;
  6314. map->p_paddr_valid = p_paddr_valid;
  6315. }
  6316. /* Determine if this segment contains the ELF file header
  6317. and if it contains the program headers themselves. */
  6318. map->includes_filehdr = (segment->p_offset == 0
  6319. && segment->p_filesz >= iehdr->e_ehsize);
  6320. map->includes_phdrs = 0;
  6321. if (!phdr_included || segment->p_type != PT_LOAD)
  6322. {
  6323. map->includes_phdrs =
  6324. (segment->p_offset <= (bfd_vma) iehdr->e_phoff
  6325. && (segment->p_offset + segment->p_filesz
  6326. >= ((bfd_vma) iehdr->e_phoff
  6327. + iehdr->e_phnum * iehdr->e_phentsize)));
  6328. if (segment->p_type == PT_LOAD && map->includes_phdrs)
  6329. phdr_included = true;
  6330. }
  6331. if (section_count == 0)
  6332. {
  6333. /* Special segments, such as the PT_PHDR segment, may contain
  6334. no sections, but ordinary, loadable segments should contain
  6335. something. They are allowed by the ELF spec however, so only
  6336. a warning is produced.
  6337. There is however the valid use case of embedded systems which
  6338. have segments with p_filesz of 0 and a p_memsz > 0 to initialize
  6339. flash memory with zeros. No warning is shown for that case. */
  6340. if (segment->p_type == PT_LOAD
  6341. && (segment->p_filesz > 0 || segment->p_memsz == 0))
  6342. /* xgettext:c-format */
  6343. _bfd_error_handler
  6344. (_("%pB: warning: empty loadable segment detected"
  6345. " at vaddr=%#" PRIx64 ", is this intentional?"),
  6346. ibfd, (uint64_t) segment->p_vaddr);
  6347. map->p_vaddr_offset = segment->p_vaddr / opb;
  6348. map->count = 0;
  6349. *pointer_to_map = map;
  6350. pointer_to_map = &map->next;
  6351. continue;
  6352. }
  6353. /* Now scan the sections in the input BFD again and attempt
  6354. to add their corresponding output sections to the segment map.
  6355. The problem here is how to handle an output section which has
  6356. been moved (ie had its LMA changed). There are four possibilities:
  6357. 1. None of the sections have been moved.
  6358. In this case we can continue to use the segment LMA from the
  6359. input BFD.
  6360. 2. All of the sections have been moved by the same amount.
  6361. In this case we can change the segment's LMA to match the LMA
  6362. of the first section.
  6363. 3. Some of the sections have been moved, others have not.
  6364. In this case those sections which have not been moved can be
  6365. placed in the current segment which will have to have its size,
  6366. and possibly its LMA changed, and a new segment or segments will
  6367. have to be created to contain the other sections.
  6368. 4. The sections have been moved, but not by the same amount.
  6369. In this case we can change the segment's LMA to match the LMA
  6370. of the first section and we will have to create a new segment
  6371. or segments to contain the other sections.
  6372. In order to save time, we allocate an array to hold the section
  6373. pointers that we are interested in. As these sections get assigned
  6374. to a segment, they are removed from this array. */
  6375. amt = section_count * sizeof (asection *);
  6376. sections = (asection **) bfd_malloc (amt);
  6377. if (sections == NULL)
  6378. return false;
  6379. /* Step One: Scan for segment vs section LMA conflicts.
  6380. Also add the sections to the section array allocated above.
  6381. Also add the sections to the current segment. In the common
  6382. case, where the sections have not been moved, this means that
  6383. we have completely filled the segment, and there is nothing
  6384. more to do. */
  6385. isec = 0;
  6386. matching_lma = NULL;
  6387. suggested_lma = NULL;
  6388. for (section = first_section, j = 0;
  6389. section != NULL;
  6390. section = section->next)
  6391. {
  6392. if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
  6393. {
  6394. output_section = section->output_section;
  6395. sections[j++] = section;
  6396. /* The Solaris native linker always sets p_paddr to 0.
  6397. We try to catch that case here, and set it to the
  6398. correct value. Note - some backends require that
  6399. p_paddr be left as zero. */
  6400. if (!p_paddr_valid
  6401. && segment->p_vaddr != 0
  6402. && !bed->want_p_paddr_set_to_zero
  6403. && isec == 0
  6404. && output_section->lma != 0
  6405. && (align_power (segment->p_vaddr
  6406. + (map->includes_filehdr
  6407. ? iehdr->e_ehsize : 0)
  6408. + (map->includes_phdrs
  6409. ? iehdr->e_phnum * iehdr->e_phentsize
  6410. : 0),
  6411. output_section->alignment_power * opb)
  6412. == (output_section->vma * opb)))
  6413. map->p_paddr = segment->p_vaddr;
  6414. /* Match up the physical address of the segment with the
  6415. LMA address of the output section. */
  6416. if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
  6417. opb)
  6418. || IS_COREFILE_NOTE (segment, section)
  6419. || (bed->want_p_paddr_set_to_zero
  6420. && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
  6421. {
  6422. if (matching_lma == NULL
  6423. || output_section->lma < matching_lma->lma)
  6424. matching_lma = output_section;
  6425. /* We assume that if the section fits within the segment
  6426. then it does not overlap any other section within that
  6427. segment. */
  6428. map->sections[isec++] = output_section;
  6429. }
  6430. else if (suggested_lma == NULL)
  6431. suggested_lma = output_section;
  6432. if (j == section_count)
  6433. break;
  6434. }
  6435. }
  6436. BFD_ASSERT (j == section_count);
  6437. /* Step Two: Adjust the physical address of the current segment,
  6438. if necessary. */
  6439. if (isec == section_count)
  6440. {
  6441. /* All of the sections fitted within the segment as currently
  6442. specified. This is the default case. Add the segment to
  6443. the list of built segments and carry on to process the next
  6444. program header in the input BFD. */
  6445. map->count = section_count;
  6446. *pointer_to_map = map;
  6447. pointer_to_map = &map->next;
  6448. if (p_paddr_valid
  6449. && !bed->want_p_paddr_set_to_zero)
  6450. {
  6451. bfd_vma hdr_size = 0;
  6452. if (map->includes_filehdr)
  6453. hdr_size = iehdr->e_ehsize;
  6454. if (map->includes_phdrs)
  6455. hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
  6456. /* Account for padding before the first section in the
  6457. segment. */
  6458. map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
  6459. - matching_lma->lma);
  6460. }
  6461. free (sections);
  6462. continue;
  6463. }
  6464. else
  6465. {
  6466. /* Change the current segment's physical address to match
  6467. the LMA of the first section that fitted, or if no
  6468. section fitted, the first section. */
  6469. if (matching_lma == NULL)
  6470. matching_lma = suggested_lma;
  6471. map->p_paddr = matching_lma->lma * opb;
  6472. /* Offset the segment physical address from the lma
  6473. to allow for space taken up by elf headers. */
  6474. if (map->includes_phdrs)
  6475. {
  6476. map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
  6477. /* iehdr->e_phnum is just an estimate of the number
  6478. of program headers that we will need. Make a note
  6479. here of the number we used and the segment we chose
  6480. to hold these headers, so that we can adjust the
  6481. offset when we know the correct value. */
  6482. phdr_adjust_num = iehdr->e_phnum;
  6483. phdr_adjust_seg = map;
  6484. }
  6485. if (map->includes_filehdr)
  6486. {
  6487. bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
  6488. map->p_paddr -= iehdr->e_ehsize;
  6489. /* We've subtracted off the size of headers from the
  6490. first section lma, but there may have been some
  6491. alignment padding before that section too. Try to
  6492. account for that by adjusting the segment lma down to
  6493. the same alignment. */
  6494. if (segment->p_align != 0 && segment->p_align < align)
  6495. align = segment->p_align;
  6496. map->p_paddr &= -(align * opb);
  6497. }
  6498. }
  6499. /* Step Three: Loop over the sections again, this time assigning
  6500. those that fit to the current segment and removing them from the
  6501. sections array; but making sure not to leave large gaps. Once all
  6502. possible sections have been assigned to the current segment it is
  6503. added to the list of built segments and if sections still remain
  6504. to be assigned, a new segment is constructed before repeating
  6505. the loop. */
  6506. isec = 0;
  6507. do
  6508. {
  6509. map->count = 0;
  6510. suggested_lma = NULL;
  6511. /* Fill the current segment with sections that fit. */
  6512. for (j = 0; j < section_count; j++)
  6513. {
  6514. section = sections[j];
  6515. if (section == NULL)
  6516. continue;
  6517. output_section = section->output_section;
  6518. BFD_ASSERT (output_section != NULL);
  6519. if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
  6520. opb)
  6521. || IS_COREFILE_NOTE (segment, section))
  6522. {
  6523. if (map->count == 0)
  6524. {
  6525. /* If the first section in a segment does not start at
  6526. the beginning of the segment, then something is
  6527. wrong. */
  6528. if (align_power (map->p_paddr
  6529. + (map->includes_filehdr
  6530. ? iehdr->e_ehsize : 0)
  6531. + (map->includes_phdrs
  6532. ? iehdr->e_phnum * iehdr->e_phentsize
  6533. : 0),
  6534. output_section->alignment_power * opb)
  6535. != output_section->lma * opb)
  6536. goto sorry;
  6537. }
  6538. else
  6539. {
  6540. asection *prev_sec;
  6541. prev_sec = map->sections[map->count - 1];
  6542. /* If the gap between the end of the previous section
  6543. and the start of this section is more than
  6544. maxpagesize then we need to start a new segment. */
  6545. if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
  6546. maxpagesize)
  6547. < BFD_ALIGN (output_section->lma, maxpagesize))
  6548. || (prev_sec->lma + prev_sec->size
  6549. > output_section->lma))
  6550. {
  6551. if (suggested_lma == NULL)
  6552. suggested_lma = output_section;
  6553. continue;
  6554. }
  6555. }
  6556. map->sections[map->count++] = output_section;
  6557. ++isec;
  6558. sections[j] = NULL;
  6559. if (segment->p_type == PT_LOAD)
  6560. section->segment_mark = true;
  6561. }
  6562. else if (suggested_lma == NULL)
  6563. suggested_lma = output_section;
  6564. }
  6565. /* PR 23932. A corrupt input file may contain sections that cannot
  6566. be assigned to any segment - because for example they have a
  6567. negative size - or segments that do not contain any sections.
  6568. But there are also valid reasons why a segment can be empty.
  6569. So allow a count of zero. */
  6570. /* Add the current segment to the list of built segments. */
  6571. *pointer_to_map = map;
  6572. pointer_to_map = &map->next;
  6573. if (isec < section_count)
  6574. {
  6575. /* We still have not allocated all of the sections to
  6576. segments. Create a new segment here, initialise it
  6577. and carry on looping. */
  6578. amt = sizeof (struct elf_segment_map) - sizeof (asection *);
  6579. amt += section_count * sizeof (asection *);
  6580. map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
  6581. if (map == NULL)
  6582. {
  6583. free (sections);
  6584. return false;
  6585. }
  6586. /* Initialise the fields of the segment map. Set the physical
  6587. physical address to the LMA of the first section that has
  6588. not yet been assigned. */
  6589. map->next = NULL;
  6590. map->p_type = segment->p_type;
  6591. map->p_flags = segment->p_flags;
  6592. map->p_flags_valid = 1;
  6593. map->p_paddr = suggested_lma->lma * opb;
  6594. map->p_paddr_valid = p_paddr_valid;
  6595. map->includes_filehdr = 0;
  6596. map->includes_phdrs = 0;
  6597. }
  6598. continue;
  6599. sorry:
  6600. bfd_set_error (bfd_error_sorry);
  6601. free (sections);
  6602. return false;
  6603. }
  6604. while (isec < section_count);
  6605. free (sections);
  6606. }
  6607. elf_seg_map (obfd) = map_first;
  6608. /* If we had to estimate the number of program headers that were
  6609. going to be needed, then check our estimate now and adjust
  6610. the offset if necessary. */
  6611. if (phdr_adjust_seg != NULL)
  6612. {
  6613. unsigned int count;
  6614. for (count = 0, map = map_first; map != NULL; map = map->next)
  6615. count++;
  6616. if (count > phdr_adjust_num)
  6617. phdr_adjust_seg->p_paddr
  6618. -= (count - phdr_adjust_num) * iehdr->e_phentsize;
  6619. for (map = map_first; map != NULL; map = map->next)
  6620. if (map->p_type == PT_PHDR)
  6621. {
  6622. bfd_vma adjust
  6623. = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
  6624. map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
  6625. break;
  6626. }
  6627. }
  6628. #undef SEGMENT_END
  6629. #undef SECTION_SIZE
  6630. #undef IS_CONTAINED_BY_VMA
  6631. #undef IS_CONTAINED_BY_LMA
  6632. #undef IS_NOTE
  6633. #undef IS_COREFILE_NOTE
  6634. #undef IS_SOLARIS_PT_INTERP
  6635. #undef IS_SECTION_IN_INPUT_SEGMENT
  6636. #undef INCLUDE_SECTION_IN_SEGMENT
  6637. #undef SEGMENT_AFTER_SEGMENT
  6638. #undef SEGMENT_OVERLAPS
  6639. return true;
  6640. }
  6641. /* Return true if p_align in the ELF program header in ABFD is valid. */
  6642. static bool
  6643. elf_is_p_align_valid (bfd *abfd)
  6644. {
  6645. unsigned int i;
  6646. Elf_Internal_Phdr *segment;
  6647. unsigned int num_segments;
  6648. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  6649. bfd_size_type maxpagesize = bed->maxpagesize;
  6650. bfd_size_type p_align = bed->p_align;
  6651. /* Return true if the default p_align value isn't set or the maximum
  6652. page size is the same as the minimum page size. */
  6653. if (p_align == 0 || maxpagesize == bed->minpagesize)
  6654. return true;
  6655. /* When the default p_align value is set, p_align may be set to the
  6656. default p_align value while segments are aligned to the maximum
  6657. page size. In this case, the input p_align will be ignored and
  6658. the maximum page size will be used to align the output segments. */
  6659. segment = elf_tdata (abfd)->phdr;
  6660. num_segments = elf_elfheader (abfd)->e_phnum;
  6661. for (i = 0; i < num_segments; i++, segment++)
  6662. if (segment->p_type == PT_LOAD
  6663. && (segment->p_align != p_align
  6664. || vma_page_aligned_bias (segment->p_vaddr,
  6665. segment->p_offset,
  6666. maxpagesize) != 0))
  6667. return true;
  6668. return false;
  6669. }
  6670. /* Copy ELF program header information. */
  6671. static bool
  6672. copy_elf_program_header (bfd *ibfd, bfd *obfd)
  6673. {
  6674. Elf_Internal_Ehdr *iehdr;
  6675. struct elf_segment_map *map;
  6676. struct elf_segment_map *map_first;
  6677. struct elf_segment_map **pointer_to_map;
  6678. Elf_Internal_Phdr *segment;
  6679. unsigned int i;
  6680. unsigned int num_segments;
  6681. bool phdr_included = false;
  6682. bool p_paddr_valid;
  6683. bool p_palign_valid;
  6684. unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
  6685. iehdr = elf_elfheader (ibfd);
  6686. map_first = NULL;
  6687. pointer_to_map = &map_first;
  6688. /* If all the segment p_paddr fields are zero, don't set
  6689. map->p_paddr_valid. */
  6690. p_paddr_valid = false;
  6691. num_segments = elf_elfheader (ibfd)->e_phnum;
  6692. for (i = 0, segment = elf_tdata (ibfd)->phdr;
  6693. i < num_segments;
  6694. i++, segment++)
  6695. if (segment->p_paddr != 0)
  6696. {
  6697. p_paddr_valid = true;
  6698. break;
  6699. }
  6700. p_palign_valid = elf_is_p_align_valid (ibfd);
  6701. for (i = 0, segment = elf_tdata (ibfd)->phdr;
  6702. i < num_segments;
  6703. i++, segment++)
  6704. {
  6705. asection *section;
  6706. unsigned int section_count;
  6707. size_t amt;
  6708. Elf_Internal_Shdr *this_hdr;
  6709. asection *first_section = NULL;
  6710. asection *lowest_section;
  6711. /* Compute how many sections are in this segment. */
  6712. for (section = ibfd->sections, section_count = 0;
  6713. section != NULL;
  6714. section = section->next)
  6715. {
  6716. this_hdr = &(elf_section_data(section)->this_hdr);
  6717. if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
  6718. {
  6719. if (first_section == NULL)
  6720. first_section = section;
  6721. section_count++;
  6722. }
  6723. }
  6724. /* Allocate a segment map big enough to contain
  6725. all of the sections we have selected. */
  6726. amt = sizeof (struct elf_segment_map) - sizeof (asection *);
  6727. amt += section_count * sizeof (asection *);
  6728. map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
  6729. if (map == NULL)
  6730. return false;
  6731. /* Initialize the fields of the output segment map with the
  6732. input segment. */
  6733. map->next = NULL;
  6734. map->p_type = segment->p_type;
  6735. map->p_flags = segment->p_flags;
  6736. map->p_flags_valid = 1;
  6737. map->p_paddr = segment->p_paddr;
  6738. map->p_paddr_valid = p_paddr_valid;
  6739. map->p_align = segment->p_align;
  6740. map->p_align_valid = p_palign_valid;
  6741. map->p_vaddr_offset = 0;
  6742. if (map->p_type == PT_GNU_RELRO
  6743. || map->p_type == PT_GNU_STACK)
  6744. {
  6745. /* The PT_GNU_RELRO segment may contain the first a few
  6746. bytes in the .got.plt section even if the whole .got.plt
  6747. section isn't in the PT_GNU_RELRO segment. We won't
  6748. change the size of the PT_GNU_RELRO segment.
  6749. Similarly, PT_GNU_STACK size is significant on uclinux
  6750. systems. */
  6751. map->p_size = segment->p_memsz;
  6752. map->p_size_valid = 1;
  6753. }
  6754. /* Determine if this segment contains the ELF file header
  6755. and if it contains the program headers themselves. */
  6756. map->includes_filehdr = (segment->p_offset == 0
  6757. && segment->p_filesz >= iehdr->e_ehsize);
  6758. map->includes_phdrs = 0;
  6759. if (! phdr_included || segment->p_type != PT_LOAD)
  6760. {
  6761. map->includes_phdrs =
  6762. (segment->p_offset <= (bfd_vma) iehdr->e_phoff
  6763. && (segment->p_offset + segment->p_filesz
  6764. >= ((bfd_vma) iehdr->e_phoff
  6765. + iehdr->e_phnum * iehdr->e_phentsize)));
  6766. if (segment->p_type == PT_LOAD && map->includes_phdrs)
  6767. phdr_included = true;
  6768. }
  6769. lowest_section = NULL;
  6770. if (section_count != 0)
  6771. {
  6772. unsigned int isec = 0;
  6773. for (section = first_section;
  6774. section != NULL;
  6775. section = section->next)
  6776. {
  6777. this_hdr = &(elf_section_data(section)->this_hdr);
  6778. if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
  6779. {
  6780. map->sections[isec++] = section->output_section;
  6781. if ((section->flags & SEC_ALLOC) != 0)
  6782. {
  6783. bfd_vma seg_off;
  6784. if (lowest_section == NULL
  6785. || section->lma < lowest_section->lma)
  6786. lowest_section = section;
  6787. /* Section lmas are set up from PT_LOAD header
  6788. p_paddr in _bfd_elf_make_section_from_shdr.
  6789. If this header has a p_paddr that disagrees
  6790. with the section lma, flag the p_paddr as
  6791. invalid. */
  6792. if ((section->flags & SEC_LOAD) != 0)
  6793. seg_off = this_hdr->sh_offset - segment->p_offset;
  6794. else
  6795. seg_off = this_hdr->sh_addr - segment->p_vaddr;
  6796. if (section->lma * opb - segment->p_paddr != seg_off)
  6797. map->p_paddr_valid = false;
  6798. }
  6799. if (isec == section_count)
  6800. break;
  6801. }
  6802. }
  6803. }
  6804. if (section_count == 0)
  6805. map->p_vaddr_offset = segment->p_vaddr / opb;
  6806. else if (map->p_paddr_valid)
  6807. {
  6808. /* Account for padding before the first section in the segment. */
  6809. bfd_vma hdr_size = 0;
  6810. if (map->includes_filehdr)
  6811. hdr_size = iehdr->e_ehsize;
  6812. if (map->includes_phdrs)
  6813. hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
  6814. map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
  6815. - (lowest_section ? lowest_section->lma : 0));
  6816. }
  6817. map->count = section_count;
  6818. *pointer_to_map = map;
  6819. pointer_to_map = &map->next;
  6820. }
  6821. elf_seg_map (obfd) = map_first;
  6822. return true;
  6823. }
  6824. /* Copy private BFD data. This copies or rewrites ELF program header
  6825. information. */
  6826. static bool
  6827. copy_private_bfd_data (bfd *ibfd, bfd *obfd)
  6828. {
  6829. bfd_vma maxpagesize;
  6830. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  6831. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  6832. return true;
  6833. if (elf_tdata (ibfd)->phdr == NULL)
  6834. return true;
  6835. if (ibfd->xvec == obfd->xvec)
  6836. {
  6837. /* Check to see if any sections in the input BFD
  6838. covered by ELF program header have changed. */
  6839. Elf_Internal_Phdr *segment;
  6840. asection *section, *osec;
  6841. unsigned int i, num_segments;
  6842. Elf_Internal_Shdr *this_hdr;
  6843. const struct elf_backend_data *bed;
  6844. bed = get_elf_backend_data (ibfd);
  6845. /* Regenerate the segment map if p_paddr is set to 0. */
  6846. if (bed->want_p_paddr_set_to_zero)
  6847. goto rewrite;
  6848. /* Initialize the segment mark field. */
  6849. for (section = obfd->sections; section != NULL;
  6850. section = section->next)
  6851. section->segment_mark = false;
  6852. num_segments = elf_elfheader (ibfd)->e_phnum;
  6853. for (i = 0, segment = elf_tdata (ibfd)->phdr;
  6854. i < num_segments;
  6855. i++, segment++)
  6856. {
  6857. /* PR binutils/3535. The Solaris linker always sets the p_paddr
  6858. and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
  6859. which severly confuses things, so always regenerate the segment
  6860. map in this case. */
  6861. if (segment->p_paddr == 0
  6862. && segment->p_memsz == 0
  6863. && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
  6864. goto rewrite;
  6865. for (section = ibfd->sections;
  6866. section != NULL; section = section->next)
  6867. {
  6868. /* We mark the output section so that we know it comes
  6869. from the input BFD. */
  6870. osec = section->output_section;
  6871. if (osec)
  6872. osec->segment_mark = true;
  6873. /* Check if this section is covered by the segment. */
  6874. this_hdr = &(elf_section_data(section)->this_hdr);
  6875. if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
  6876. {
  6877. /* FIXME: Check if its output section is changed or
  6878. removed. What else do we need to check? */
  6879. if (osec == NULL
  6880. || section->flags != osec->flags
  6881. || section->lma != osec->lma
  6882. || section->vma != osec->vma
  6883. || section->size != osec->size
  6884. || section->rawsize != osec->rawsize
  6885. || section->alignment_power != osec->alignment_power)
  6886. goto rewrite;
  6887. }
  6888. }
  6889. }
  6890. /* Check to see if any output section do not come from the
  6891. input BFD. */
  6892. for (section = obfd->sections; section != NULL;
  6893. section = section->next)
  6894. {
  6895. if (!section->segment_mark)
  6896. goto rewrite;
  6897. else
  6898. section->segment_mark = false;
  6899. }
  6900. return copy_elf_program_header (ibfd, obfd);
  6901. }
  6902. rewrite:
  6903. maxpagesize = 0;
  6904. if (ibfd->xvec == obfd->xvec)
  6905. {
  6906. /* When rewriting program header, set the output maxpagesize to
  6907. the maximum alignment of input PT_LOAD segments. */
  6908. Elf_Internal_Phdr *segment;
  6909. unsigned int i;
  6910. unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
  6911. for (i = 0, segment = elf_tdata (ibfd)->phdr;
  6912. i < num_segments;
  6913. i++, segment++)
  6914. if (segment->p_type == PT_LOAD
  6915. && maxpagesize < segment->p_align)
  6916. {
  6917. /* PR 17512: file: f17299af. */
  6918. if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
  6919. /* xgettext:c-format */
  6920. _bfd_error_handler (_("%pB: warning: segment alignment of %#"
  6921. PRIx64 " is too large"),
  6922. ibfd, (uint64_t) segment->p_align);
  6923. else
  6924. maxpagesize = segment->p_align;
  6925. }
  6926. }
  6927. if (maxpagesize == 0)
  6928. maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
  6929. return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
  6930. }
  6931. /* Initialize private output section information from input section. */
  6932. bool
  6933. _bfd_elf_init_private_section_data (bfd *ibfd,
  6934. asection *isec,
  6935. bfd *obfd,
  6936. asection *osec,
  6937. struct bfd_link_info *link_info)
  6938. {
  6939. Elf_Internal_Shdr *ihdr, *ohdr;
  6940. bool final_link = (link_info != NULL
  6941. && !bfd_link_relocatable (link_info));
  6942. if (ibfd->xvec->flavour != bfd_target_elf_flavour
  6943. || obfd->xvec->flavour != bfd_target_elf_flavour)
  6944. return true;
  6945. BFD_ASSERT (elf_section_data (osec) != NULL);
  6946. /* If this is a known ABI section, ELF section type and flags may
  6947. have been set up when OSEC was created. For normal sections we
  6948. allow the user to override the type and flags other than
  6949. SHF_MASKOS and SHF_MASKPROC. */
  6950. if (elf_section_type (osec) == SHT_PROGBITS
  6951. || elf_section_type (osec) == SHT_NOTE
  6952. || elf_section_type (osec) == SHT_NOBITS)
  6953. elf_section_type (osec) = SHT_NULL;
  6954. /* For objcopy and relocatable link, copy the ELF section type from
  6955. the input file if the BFD section flags are the same. (If they
  6956. are different the user may be doing something like
  6957. "objcopy --set-section-flags .text=alloc,data".) For a final
  6958. link allow some flags that the linker clears to differ. */
  6959. if (elf_section_type (osec) == SHT_NULL
  6960. && (osec->flags == isec->flags
  6961. || (final_link
  6962. && ((osec->flags ^ isec->flags)
  6963. & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
  6964. elf_section_type (osec) = elf_section_type (isec);
  6965. /* FIXME: Is this correct for all OS/PROC specific flags? */
  6966. elf_section_flags (osec) = (elf_section_flags (isec)
  6967. & (SHF_MASKOS | SHF_MASKPROC));
  6968. /* Copy sh_info from input for mbind section. */
  6969. if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
  6970. && elf_section_flags (isec) & SHF_GNU_MBIND)
  6971. elf_section_data (osec)->this_hdr.sh_info
  6972. = elf_section_data (isec)->this_hdr.sh_info;
  6973. /* Set things up for objcopy and relocatable link. The output
  6974. SHT_GROUP section will have its elf_next_in_group pointing back
  6975. to the input group members. Ignore linker created group section.
  6976. See elfNN_ia64_object_p in elfxx-ia64.c. */
  6977. if ((link_info == NULL
  6978. || !link_info->resolve_section_groups)
  6979. && (elf_sec_group (isec) == NULL
  6980. || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
  6981. {
  6982. if (elf_section_flags (isec) & SHF_GROUP)
  6983. elf_section_flags (osec) |= SHF_GROUP;
  6984. elf_next_in_group (osec) = elf_next_in_group (isec);
  6985. elf_section_data (osec)->group = elf_section_data (isec)->group;
  6986. }
  6987. /* If not decompress, preserve SHF_COMPRESSED. */
  6988. if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
  6989. elf_section_flags (osec) |= (elf_section_flags (isec)
  6990. & SHF_COMPRESSED);
  6991. ihdr = &elf_section_data (isec)->this_hdr;
  6992. /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
  6993. don't use the output section of the linked-to section since it
  6994. may be NULL at this point. */
  6995. if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
  6996. {
  6997. ohdr = &elf_section_data (osec)->this_hdr;
  6998. ohdr->sh_flags |= SHF_LINK_ORDER;
  6999. elf_linked_to_section (osec) = elf_linked_to_section (isec);
  7000. }
  7001. osec->use_rela_p = isec->use_rela_p;
  7002. return true;
  7003. }
  7004. /* Copy private section information. This copies over the entsize
  7005. field, and sometimes the info field. */
  7006. bool
  7007. _bfd_elf_copy_private_section_data (bfd *ibfd,
  7008. asection *isec,
  7009. bfd *obfd,
  7010. asection *osec)
  7011. {
  7012. Elf_Internal_Shdr *ihdr, *ohdr;
  7013. if (ibfd->xvec->flavour != bfd_target_elf_flavour
  7014. || obfd->xvec->flavour != bfd_target_elf_flavour)
  7015. return true;
  7016. ihdr = &elf_section_data (isec)->this_hdr;
  7017. ohdr = &elf_section_data (osec)->this_hdr;
  7018. ohdr->sh_entsize = ihdr->sh_entsize;
  7019. if (ihdr->sh_type == SHT_SYMTAB
  7020. || ihdr->sh_type == SHT_DYNSYM
  7021. || ihdr->sh_type == SHT_GNU_verneed
  7022. || ihdr->sh_type == SHT_GNU_verdef)
  7023. ohdr->sh_info = ihdr->sh_info;
  7024. return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
  7025. NULL);
  7026. }
  7027. /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
  7028. necessary if we are removing either the SHT_GROUP section or any of
  7029. the group member sections. DISCARDED is the value that a section's
  7030. output_section has if the section will be discarded, NULL when this
  7031. function is called from objcopy, bfd_abs_section_ptr when called
  7032. from the linker. */
  7033. bool
  7034. _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
  7035. {
  7036. asection *isec;
  7037. for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  7038. if (elf_section_type (isec) == SHT_GROUP)
  7039. {
  7040. asection *first = elf_next_in_group (isec);
  7041. asection *s = first;
  7042. bfd_size_type removed = 0;
  7043. while (s != NULL)
  7044. {
  7045. /* If this member section is being output but the
  7046. SHT_GROUP section is not, then clear the group info
  7047. set up by _bfd_elf_copy_private_section_data. */
  7048. if (s->output_section != discarded
  7049. && isec->output_section == discarded)
  7050. {
  7051. elf_section_flags (s->output_section) &= ~SHF_GROUP;
  7052. elf_group_name (s->output_section) = NULL;
  7053. }
  7054. else
  7055. {
  7056. struct bfd_elf_section_data *elf_sec = elf_section_data (s);
  7057. if (s->output_section == discarded
  7058. && isec->output_section != discarded)
  7059. {
  7060. /* Conversely, if the member section is not being
  7061. output but the SHT_GROUP section is, then adjust
  7062. its size. */
  7063. removed += 4;
  7064. if (elf_sec->rel.hdr != NULL
  7065. && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
  7066. removed += 4;
  7067. if (elf_sec->rela.hdr != NULL
  7068. && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
  7069. removed += 4;
  7070. }
  7071. else
  7072. {
  7073. /* Also adjust for zero-sized relocation member
  7074. section. */
  7075. if (elf_sec->rel.hdr != NULL
  7076. && elf_sec->rel.hdr->sh_size == 0)
  7077. removed += 4;
  7078. if (elf_sec->rela.hdr != NULL
  7079. && elf_sec->rela.hdr->sh_size == 0)
  7080. removed += 4;
  7081. }
  7082. }
  7083. s = elf_next_in_group (s);
  7084. if (s == first)
  7085. break;
  7086. }
  7087. if (removed != 0)
  7088. {
  7089. if (discarded != NULL)
  7090. {
  7091. /* If we've been called for ld -r, then we need to
  7092. adjust the input section size. */
  7093. if (isec->rawsize == 0)
  7094. isec->rawsize = isec->size;
  7095. isec->size = isec->rawsize - removed;
  7096. if (isec->size <= 4)
  7097. {
  7098. isec->size = 0;
  7099. isec->flags |= SEC_EXCLUDE;
  7100. }
  7101. }
  7102. else if (isec->output_section != NULL)
  7103. {
  7104. /* Adjust the output section size when called from
  7105. objcopy. */
  7106. isec->output_section->size -= removed;
  7107. if (isec->output_section->size <= 4)
  7108. {
  7109. isec->output_section->size = 0;
  7110. isec->output_section->flags |= SEC_EXCLUDE;
  7111. }
  7112. }
  7113. }
  7114. }
  7115. return true;
  7116. }
  7117. /* Copy private header information. */
  7118. bool
  7119. _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
  7120. {
  7121. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  7122. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  7123. return true;
  7124. /* Copy over private BFD data if it has not already been copied.
  7125. This must be done here, rather than in the copy_private_bfd_data
  7126. entry point, because the latter is called after the section
  7127. contents have been set, which means that the program headers have
  7128. already been worked out. */
  7129. if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
  7130. {
  7131. if (! copy_private_bfd_data (ibfd, obfd))
  7132. return false;
  7133. }
  7134. return _bfd_elf_fixup_group_sections (ibfd, NULL);
  7135. }
  7136. /* Copy private symbol information. If this symbol is in a section
  7137. which we did not map into a BFD section, try to map the section
  7138. index correctly. We use special macro definitions for the mapped
  7139. section indices; these definitions are interpreted by the
  7140. swap_out_syms function. */
  7141. #define MAP_ONESYMTAB (SHN_HIOS + 1)
  7142. #define MAP_DYNSYMTAB (SHN_HIOS + 2)
  7143. #define MAP_STRTAB (SHN_HIOS + 3)
  7144. #define MAP_SHSTRTAB (SHN_HIOS + 4)
  7145. #define MAP_SYM_SHNDX (SHN_HIOS + 5)
  7146. bool
  7147. _bfd_elf_copy_private_symbol_data (bfd *ibfd,
  7148. asymbol *isymarg,
  7149. bfd *obfd,
  7150. asymbol *osymarg)
  7151. {
  7152. elf_symbol_type *isym, *osym;
  7153. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  7154. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  7155. return true;
  7156. isym = elf_symbol_from (isymarg);
  7157. osym = elf_symbol_from (osymarg);
  7158. if (isym != NULL
  7159. && isym->internal_elf_sym.st_shndx != 0
  7160. && osym != NULL
  7161. && bfd_is_abs_section (isym->symbol.section))
  7162. {
  7163. unsigned int shndx;
  7164. shndx = isym->internal_elf_sym.st_shndx;
  7165. if (shndx == elf_onesymtab (ibfd))
  7166. shndx = MAP_ONESYMTAB;
  7167. else if (shndx == elf_dynsymtab (ibfd))
  7168. shndx = MAP_DYNSYMTAB;
  7169. else if (shndx == elf_strtab_sec (ibfd))
  7170. shndx = MAP_STRTAB;
  7171. else if (shndx == elf_shstrtab_sec (ibfd))
  7172. shndx = MAP_SHSTRTAB;
  7173. else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
  7174. shndx = MAP_SYM_SHNDX;
  7175. osym->internal_elf_sym.st_shndx = shndx;
  7176. }
  7177. return true;
  7178. }
  7179. /* Swap out the symbols. */
  7180. static bool
  7181. swap_out_syms (bfd *abfd,
  7182. struct elf_strtab_hash **sttp,
  7183. int relocatable_p,
  7184. struct bfd_link_info *info)
  7185. {
  7186. const struct elf_backend_data *bed;
  7187. unsigned int symcount;
  7188. asymbol **syms;
  7189. struct elf_strtab_hash *stt;
  7190. Elf_Internal_Shdr *symtab_hdr;
  7191. Elf_Internal_Shdr *symtab_shndx_hdr;
  7192. Elf_Internal_Shdr *symstrtab_hdr;
  7193. struct elf_sym_strtab *symstrtab;
  7194. bfd_byte *outbound_syms;
  7195. bfd_byte *outbound_shndx;
  7196. unsigned long outbound_syms_index;
  7197. unsigned int idx;
  7198. unsigned int num_locals;
  7199. size_t amt;
  7200. bool name_local_sections;
  7201. if (!elf_map_symbols (abfd, &num_locals))
  7202. return false;
  7203. /* Dump out the symtabs. */
  7204. stt = _bfd_elf_strtab_init ();
  7205. if (stt == NULL)
  7206. return false;
  7207. bed = get_elf_backend_data (abfd);
  7208. symcount = bfd_get_symcount (abfd);
  7209. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  7210. symtab_hdr->sh_type = SHT_SYMTAB;
  7211. symtab_hdr->sh_entsize = bed->s->sizeof_sym;
  7212. symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
  7213. symtab_hdr->sh_info = num_locals + 1;
  7214. symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
  7215. symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
  7216. symstrtab_hdr->sh_type = SHT_STRTAB;
  7217. /* Allocate buffer to swap out the .strtab section. */
  7218. if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
  7219. || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
  7220. {
  7221. bfd_set_error (bfd_error_no_memory);
  7222. _bfd_elf_strtab_free (stt);
  7223. return false;
  7224. }
  7225. if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
  7226. || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
  7227. {
  7228. error_no_mem:
  7229. bfd_set_error (bfd_error_no_memory);
  7230. error_return:
  7231. free (symstrtab);
  7232. _bfd_elf_strtab_free (stt);
  7233. return false;
  7234. }
  7235. symtab_hdr->contents = outbound_syms;
  7236. outbound_syms_index = 0;
  7237. outbound_shndx = NULL;
  7238. if (elf_symtab_shndx_list (abfd))
  7239. {
  7240. symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
  7241. if (symtab_shndx_hdr->sh_name != 0)
  7242. {
  7243. if (_bfd_mul_overflow (symcount + 1,
  7244. sizeof (Elf_External_Sym_Shndx), &amt))
  7245. goto error_no_mem;
  7246. outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
  7247. if (outbound_shndx == NULL)
  7248. goto error_return;
  7249. symtab_shndx_hdr->contents = outbound_shndx;
  7250. symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
  7251. symtab_shndx_hdr->sh_size = amt;
  7252. symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
  7253. symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
  7254. }
  7255. /* FIXME: What about any other headers in the list ? */
  7256. }
  7257. /* Now generate the data (for "contents"). */
  7258. {
  7259. /* Fill in zeroth symbol and swap it out. */
  7260. Elf_Internal_Sym sym;
  7261. sym.st_name = 0;
  7262. sym.st_value = 0;
  7263. sym.st_size = 0;
  7264. sym.st_info = 0;
  7265. sym.st_other = 0;
  7266. sym.st_shndx = SHN_UNDEF;
  7267. sym.st_target_internal = 0;
  7268. symstrtab[0].sym = sym;
  7269. symstrtab[0].dest_index = outbound_syms_index;
  7270. outbound_syms_index++;
  7271. }
  7272. name_local_sections
  7273. = (bed->elf_backend_name_local_section_symbols
  7274. && bed->elf_backend_name_local_section_symbols (abfd));
  7275. syms = bfd_get_outsymbols (abfd);
  7276. for (idx = 0; idx < symcount;)
  7277. {
  7278. Elf_Internal_Sym sym;
  7279. bfd_vma value = syms[idx]->value;
  7280. elf_symbol_type *type_ptr;
  7281. flagword flags = syms[idx]->flags;
  7282. int type;
  7283. if (!name_local_sections
  7284. && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
  7285. {
  7286. /* Local section symbols have no name. */
  7287. sym.st_name = (unsigned long) -1;
  7288. }
  7289. else
  7290. {
  7291. /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
  7292. to get the final offset for st_name. */
  7293. sym.st_name
  7294. = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
  7295. false);
  7296. if (sym.st_name == (unsigned long) -1)
  7297. goto error_return;
  7298. }
  7299. type_ptr = elf_symbol_from (syms[idx]);
  7300. if ((flags & BSF_SECTION_SYM) == 0
  7301. && bfd_is_com_section (syms[idx]->section))
  7302. {
  7303. /* ELF common symbols put the alignment into the `value' field,
  7304. and the size into the `size' field. This is backwards from
  7305. how BFD handles it, so reverse it here. */
  7306. sym.st_size = value;
  7307. if (type_ptr == NULL
  7308. || type_ptr->internal_elf_sym.st_value == 0)
  7309. sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
  7310. else
  7311. sym.st_value = type_ptr->internal_elf_sym.st_value;
  7312. sym.st_shndx = _bfd_elf_section_from_bfd_section
  7313. (abfd, syms[idx]->section);
  7314. }
  7315. else
  7316. {
  7317. asection *sec = syms[idx]->section;
  7318. unsigned int shndx;
  7319. if (sec->output_section)
  7320. {
  7321. value += sec->output_offset;
  7322. sec = sec->output_section;
  7323. }
  7324. /* Don't add in the section vma for relocatable output. */
  7325. if (! relocatable_p)
  7326. value += sec->vma;
  7327. sym.st_value = value;
  7328. sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
  7329. if (bfd_is_abs_section (sec)
  7330. && type_ptr != NULL
  7331. && type_ptr->internal_elf_sym.st_shndx != 0)
  7332. {
  7333. /* This symbol is in a real ELF section which we did
  7334. not create as a BFD section. Undo the mapping done
  7335. by copy_private_symbol_data. */
  7336. shndx = type_ptr->internal_elf_sym.st_shndx;
  7337. switch (shndx)
  7338. {
  7339. case MAP_ONESYMTAB:
  7340. shndx = elf_onesymtab (abfd);
  7341. break;
  7342. case MAP_DYNSYMTAB:
  7343. shndx = elf_dynsymtab (abfd);
  7344. break;
  7345. case MAP_STRTAB:
  7346. shndx = elf_strtab_sec (abfd);
  7347. break;
  7348. case MAP_SHSTRTAB:
  7349. shndx = elf_shstrtab_sec (abfd);
  7350. break;
  7351. case MAP_SYM_SHNDX:
  7352. if (elf_symtab_shndx_list (abfd))
  7353. shndx = elf_symtab_shndx_list (abfd)->ndx;
  7354. break;
  7355. case SHN_COMMON:
  7356. case SHN_ABS:
  7357. shndx = SHN_ABS;
  7358. break;
  7359. default:
  7360. if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
  7361. {
  7362. if (bed->symbol_section_index)
  7363. shndx = bed->symbol_section_index (abfd, type_ptr);
  7364. /* Otherwise just leave the index alone. */
  7365. }
  7366. else
  7367. {
  7368. if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
  7369. _bfd_error_handler (_("%pB: \
  7370. Unable to handle section index %x in ELF symbol. Using ABS instead."),
  7371. abfd, shndx);
  7372. shndx = SHN_ABS;
  7373. }
  7374. break;
  7375. }
  7376. }
  7377. else
  7378. {
  7379. shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  7380. if (shndx == SHN_BAD)
  7381. {
  7382. asection *sec2;
  7383. /* Writing this would be a hell of a lot easier if
  7384. we had some decent documentation on bfd, and
  7385. knew what to expect of the library, and what to
  7386. demand of applications. For example, it
  7387. appears that `objcopy' might not set the
  7388. section of a symbol to be a section that is
  7389. actually in the output file. */
  7390. sec2 = bfd_get_section_by_name (abfd, sec->name);
  7391. if (sec2 != NULL)
  7392. shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
  7393. if (shndx == SHN_BAD)
  7394. {
  7395. /* xgettext:c-format */
  7396. _bfd_error_handler
  7397. (_("unable to find equivalent output section"
  7398. " for symbol '%s' from section '%s'"),
  7399. syms[idx]->name ? syms[idx]->name : "<Local sym>",
  7400. sec->name);
  7401. bfd_set_error (bfd_error_invalid_operation);
  7402. goto error_return;
  7403. }
  7404. }
  7405. }
  7406. sym.st_shndx = shndx;
  7407. }
  7408. if ((flags & BSF_THREAD_LOCAL) != 0)
  7409. type = STT_TLS;
  7410. else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
  7411. type = STT_GNU_IFUNC;
  7412. else if ((flags & BSF_FUNCTION) != 0)
  7413. type = STT_FUNC;
  7414. else if ((flags & BSF_OBJECT) != 0)
  7415. type = STT_OBJECT;
  7416. else if ((flags & BSF_RELC) != 0)
  7417. type = STT_RELC;
  7418. else if ((flags & BSF_SRELC) != 0)
  7419. type = STT_SRELC;
  7420. else
  7421. type = STT_NOTYPE;
  7422. if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
  7423. type = STT_TLS;
  7424. /* Processor-specific types. */
  7425. if (type_ptr != NULL
  7426. && bed->elf_backend_get_symbol_type)
  7427. type = ((*bed->elf_backend_get_symbol_type)
  7428. (&type_ptr->internal_elf_sym, type));
  7429. if (flags & BSF_SECTION_SYM)
  7430. {
  7431. if (flags & BSF_GLOBAL)
  7432. sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  7433. else
  7434. sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  7435. }
  7436. else if (bfd_is_com_section (syms[idx]->section))
  7437. {
  7438. if (type != STT_TLS)
  7439. {
  7440. if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
  7441. type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
  7442. ? STT_COMMON : STT_OBJECT);
  7443. else
  7444. type = ((flags & BSF_ELF_COMMON) != 0
  7445. ? STT_COMMON : STT_OBJECT);
  7446. }
  7447. sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
  7448. }
  7449. else if (bfd_is_und_section (syms[idx]->section))
  7450. sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
  7451. ? STB_WEAK
  7452. : STB_GLOBAL),
  7453. type);
  7454. else if (flags & BSF_FILE)
  7455. sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
  7456. else
  7457. {
  7458. int bind = STB_LOCAL;
  7459. if (flags & BSF_LOCAL)
  7460. bind = STB_LOCAL;
  7461. else if (flags & BSF_GNU_UNIQUE)
  7462. bind = STB_GNU_UNIQUE;
  7463. else if (flags & BSF_WEAK)
  7464. bind = STB_WEAK;
  7465. else if (flags & BSF_GLOBAL)
  7466. bind = STB_GLOBAL;
  7467. sym.st_info = ELF_ST_INFO (bind, type);
  7468. }
  7469. if (type_ptr != NULL)
  7470. {
  7471. sym.st_other = type_ptr->internal_elf_sym.st_other;
  7472. sym.st_target_internal
  7473. = type_ptr->internal_elf_sym.st_target_internal;
  7474. }
  7475. else
  7476. {
  7477. sym.st_other = 0;
  7478. sym.st_target_internal = 0;
  7479. }
  7480. idx++;
  7481. symstrtab[idx].sym = sym;
  7482. symstrtab[idx].dest_index = outbound_syms_index;
  7483. outbound_syms_index++;
  7484. }
  7485. /* Finalize the .strtab section. */
  7486. _bfd_elf_strtab_finalize (stt);
  7487. /* Swap out the .strtab section. */
  7488. for (idx = 0; idx <= symcount; idx++)
  7489. {
  7490. struct elf_sym_strtab *elfsym = &symstrtab[idx];
  7491. if (elfsym->sym.st_name == (unsigned long) -1)
  7492. elfsym->sym.st_name = 0;
  7493. else
  7494. elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
  7495. elfsym->sym.st_name);
  7496. if (info && info->callbacks->ctf_new_symbol)
  7497. info->callbacks->ctf_new_symbol (elfsym->dest_index,
  7498. &elfsym->sym);
  7499. /* Inform the linker of the addition of this symbol. */
  7500. bed->s->swap_symbol_out (abfd, &elfsym->sym,
  7501. (outbound_syms
  7502. + (elfsym->dest_index
  7503. * bed->s->sizeof_sym)),
  7504. NPTR_ADD (outbound_shndx,
  7505. (elfsym->dest_index
  7506. * sizeof (Elf_External_Sym_Shndx))));
  7507. }
  7508. free (symstrtab);
  7509. *sttp = stt;
  7510. symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
  7511. symstrtab_hdr->sh_type = SHT_STRTAB;
  7512. symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
  7513. symstrtab_hdr->sh_addr = 0;
  7514. symstrtab_hdr->sh_entsize = 0;
  7515. symstrtab_hdr->sh_link = 0;
  7516. symstrtab_hdr->sh_info = 0;
  7517. symstrtab_hdr->sh_addralign = 1;
  7518. return true;
  7519. }
  7520. /* Return the number of bytes required to hold the symtab vector.
  7521. Note that we base it on the count plus 1, since we will null terminate
  7522. the vector allocated based on this size. However, the ELF symbol table
  7523. always has a dummy entry as symbol #0, so it ends up even. */
  7524. long
  7525. _bfd_elf_get_symtab_upper_bound (bfd *abfd)
  7526. {
  7527. bfd_size_type symcount;
  7528. long symtab_size;
  7529. Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
  7530. symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
  7531. if (symcount > LONG_MAX / sizeof (asymbol *))
  7532. {
  7533. bfd_set_error (bfd_error_file_too_big);
  7534. return -1;
  7535. }
  7536. symtab_size = symcount * (sizeof (asymbol *));
  7537. if (symcount == 0)
  7538. symtab_size = sizeof (asymbol *);
  7539. else if (!bfd_write_p (abfd))
  7540. {
  7541. ufile_ptr filesize = bfd_get_file_size (abfd);
  7542. if (filesize != 0 && (unsigned long) symtab_size > filesize)
  7543. {
  7544. bfd_set_error (bfd_error_file_truncated);
  7545. return -1;
  7546. }
  7547. }
  7548. return symtab_size;
  7549. }
  7550. long
  7551. _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
  7552. {
  7553. bfd_size_type symcount;
  7554. long symtab_size;
  7555. Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  7556. if (elf_dynsymtab (abfd) == 0)
  7557. {
  7558. bfd_set_error (bfd_error_invalid_operation);
  7559. return -1;
  7560. }
  7561. symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
  7562. if (symcount > LONG_MAX / sizeof (asymbol *))
  7563. {
  7564. bfd_set_error (bfd_error_file_too_big);
  7565. return -1;
  7566. }
  7567. symtab_size = symcount * (sizeof (asymbol *));
  7568. if (symcount == 0)
  7569. symtab_size = sizeof (asymbol *);
  7570. else if (!bfd_write_p (abfd))
  7571. {
  7572. ufile_ptr filesize = bfd_get_file_size (abfd);
  7573. if (filesize != 0 && (unsigned long) symtab_size > filesize)
  7574. {
  7575. bfd_set_error (bfd_error_file_truncated);
  7576. return -1;
  7577. }
  7578. }
  7579. return symtab_size;
  7580. }
  7581. long
  7582. _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
  7583. {
  7584. if (asect->reloc_count != 0 && !bfd_write_p (abfd))
  7585. {
  7586. /* Sanity check reloc section size. */
  7587. struct bfd_elf_section_data *d = elf_section_data (asect);
  7588. Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
  7589. bfd_size_type ext_rel_size = rel_hdr->sh_size;
  7590. ufile_ptr filesize = bfd_get_file_size (abfd);
  7591. if (filesize != 0 && ext_rel_size > filesize)
  7592. {
  7593. bfd_set_error (bfd_error_file_truncated);
  7594. return -1;
  7595. }
  7596. }
  7597. #if SIZEOF_LONG == SIZEOF_INT
  7598. if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
  7599. {
  7600. bfd_set_error (bfd_error_file_too_big);
  7601. return -1;
  7602. }
  7603. #endif
  7604. return (asect->reloc_count + 1L) * sizeof (arelent *);
  7605. }
  7606. /* Canonicalize the relocs. */
  7607. long
  7608. _bfd_elf_canonicalize_reloc (bfd *abfd,
  7609. sec_ptr section,
  7610. arelent **relptr,
  7611. asymbol **symbols)
  7612. {
  7613. arelent *tblptr;
  7614. unsigned int i;
  7615. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  7616. if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
  7617. return -1;
  7618. tblptr = section->relocation;
  7619. for (i = 0; i < section->reloc_count; i++)
  7620. *relptr++ = tblptr++;
  7621. *relptr = NULL;
  7622. return section->reloc_count;
  7623. }
  7624. long
  7625. _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
  7626. {
  7627. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  7628. long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
  7629. if (symcount >= 0)
  7630. abfd->symcount = symcount;
  7631. return symcount;
  7632. }
  7633. long
  7634. _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
  7635. asymbol **allocation)
  7636. {
  7637. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  7638. long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
  7639. if (symcount >= 0)
  7640. abfd->dynsymcount = symcount;
  7641. return symcount;
  7642. }
  7643. /* Return the size required for the dynamic reloc entries. Any loadable
  7644. section that was actually installed in the BFD, and has type SHT_REL
  7645. or SHT_RELA, and uses the dynamic symbol table, is considered to be a
  7646. dynamic reloc section. */
  7647. long
  7648. _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
  7649. {
  7650. bfd_size_type count, ext_rel_size;
  7651. asection *s;
  7652. if (elf_dynsymtab (abfd) == 0)
  7653. {
  7654. bfd_set_error (bfd_error_invalid_operation);
  7655. return -1;
  7656. }
  7657. count = 1;
  7658. ext_rel_size = 0;
  7659. for (s = abfd->sections; s != NULL; s = s->next)
  7660. if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
  7661. && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
  7662. || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
  7663. {
  7664. ext_rel_size += s->size;
  7665. if (ext_rel_size < s->size)
  7666. {
  7667. bfd_set_error (bfd_error_file_truncated);
  7668. return -1;
  7669. }
  7670. count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
  7671. if (count > LONG_MAX / sizeof (arelent *))
  7672. {
  7673. bfd_set_error (bfd_error_file_too_big);
  7674. return -1;
  7675. }
  7676. }
  7677. if (count > 1 && !bfd_write_p (abfd))
  7678. {
  7679. /* Sanity check reloc section sizes. */
  7680. ufile_ptr filesize = bfd_get_file_size (abfd);
  7681. if (filesize != 0 && ext_rel_size > filesize)
  7682. {
  7683. bfd_set_error (bfd_error_file_truncated);
  7684. return -1;
  7685. }
  7686. }
  7687. return count * sizeof (arelent *);
  7688. }
  7689. /* Canonicalize the dynamic relocation entries. Note that we return the
  7690. dynamic relocations as a single block, although they are actually
  7691. associated with particular sections; the interface, which was
  7692. designed for SunOS style shared libraries, expects that there is only
  7693. one set of dynamic relocs. Any loadable section that was actually
  7694. installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
  7695. dynamic symbol table, is considered to be a dynamic reloc section. */
  7696. long
  7697. _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
  7698. arelent **storage,
  7699. asymbol **syms)
  7700. {
  7701. bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
  7702. asection *s;
  7703. long ret;
  7704. if (elf_dynsymtab (abfd) == 0)
  7705. {
  7706. bfd_set_error (bfd_error_invalid_operation);
  7707. return -1;
  7708. }
  7709. slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
  7710. ret = 0;
  7711. for (s = abfd->sections; s != NULL; s = s->next)
  7712. {
  7713. if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
  7714. && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
  7715. || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
  7716. {
  7717. arelent *p;
  7718. long count, i;
  7719. if (! (*slurp_relocs) (abfd, s, syms, true))
  7720. return -1;
  7721. count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
  7722. p = s->relocation;
  7723. for (i = 0; i < count; i++)
  7724. *storage++ = p++;
  7725. ret += count;
  7726. }
  7727. }
  7728. *storage = NULL;
  7729. return ret;
  7730. }
  7731. /* Read in the version information. */
  7732. bool
  7733. _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
  7734. {
  7735. bfd_byte *contents = NULL;
  7736. unsigned int freeidx = 0;
  7737. size_t amt;
  7738. if (elf_dynverref (abfd) != 0)
  7739. {
  7740. Elf_Internal_Shdr *hdr;
  7741. Elf_External_Verneed *everneed;
  7742. Elf_Internal_Verneed *iverneed;
  7743. unsigned int i;
  7744. bfd_byte *contents_end;
  7745. hdr = &elf_tdata (abfd)->dynverref_hdr;
  7746. if (hdr->sh_info == 0
  7747. || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
  7748. {
  7749. error_return_bad_verref:
  7750. _bfd_error_handler
  7751. (_("%pB: .gnu.version_r invalid entry"), abfd);
  7752. bfd_set_error (bfd_error_bad_value);
  7753. error_return_verref:
  7754. elf_tdata (abfd)->verref = NULL;
  7755. elf_tdata (abfd)->cverrefs = 0;
  7756. goto error_return;
  7757. }
  7758. if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
  7759. goto error_return_verref;
  7760. contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
  7761. if (contents == NULL)
  7762. goto error_return_verref;
  7763. if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
  7764. {
  7765. bfd_set_error (bfd_error_file_too_big);
  7766. goto error_return_verref;
  7767. }
  7768. elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
  7769. if (elf_tdata (abfd)->verref == NULL)
  7770. goto error_return_verref;
  7771. BFD_ASSERT (sizeof (Elf_External_Verneed)
  7772. == sizeof (Elf_External_Vernaux));
  7773. contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
  7774. everneed = (Elf_External_Verneed *) contents;
  7775. iverneed = elf_tdata (abfd)->verref;
  7776. for (i = 0; i < hdr->sh_info; i++, iverneed++)
  7777. {
  7778. Elf_External_Vernaux *evernaux;
  7779. Elf_Internal_Vernaux *ivernaux;
  7780. unsigned int j;
  7781. _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
  7782. iverneed->vn_bfd = abfd;
  7783. iverneed->vn_filename =
  7784. bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
  7785. iverneed->vn_file);
  7786. if (iverneed->vn_filename == NULL)
  7787. goto error_return_bad_verref;
  7788. if (iverneed->vn_cnt == 0)
  7789. iverneed->vn_auxptr = NULL;
  7790. else
  7791. {
  7792. if (_bfd_mul_overflow (iverneed->vn_cnt,
  7793. sizeof (Elf_Internal_Vernaux), &amt))
  7794. {
  7795. bfd_set_error (bfd_error_file_too_big);
  7796. goto error_return_verref;
  7797. }
  7798. iverneed->vn_auxptr = (struct elf_internal_vernaux *)
  7799. bfd_alloc (abfd, amt);
  7800. if (iverneed->vn_auxptr == NULL)
  7801. goto error_return_verref;
  7802. }
  7803. if (iverneed->vn_aux
  7804. > (size_t) (contents_end - (bfd_byte *) everneed))
  7805. goto error_return_bad_verref;
  7806. evernaux = ((Elf_External_Vernaux *)
  7807. ((bfd_byte *) everneed + iverneed->vn_aux));
  7808. ivernaux = iverneed->vn_auxptr;
  7809. for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
  7810. {
  7811. _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
  7812. ivernaux->vna_nodename =
  7813. bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
  7814. ivernaux->vna_name);
  7815. if (ivernaux->vna_nodename == NULL)
  7816. goto error_return_bad_verref;
  7817. if (ivernaux->vna_other > freeidx)
  7818. freeidx = ivernaux->vna_other;
  7819. ivernaux->vna_nextptr = NULL;
  7820. if (ivernaux->vna_next == 0)
  7821. {
  7822. iverneed->vn_cnt = j + 1;
  7823. break;
  7824. }
  7825. if (j + 1 < iverneed->vn_cnt)
  7826. ivernaux->vna_nextptr = ivernaux + 1;
  7827. if (ivernaux->vna_next
  7828. > (size_t) (contents_end - (bfd_byte *) evernaux))
  7829. goto error_return_bad_verref;
  7830. evernaux = ((Elf_External_Vernaux *)
  7831. ((bfd_byte *) evernaux + ivernaux->vna_next));
  7832. }
  7833. iverneed->vn_nextref = NULL;
  7834. if (iverneed->vn_next == 0)
  7835. break;
  7836. if (i + 1 < hdr->sh_info)
  7837. iverneed->vn_nextref = iverneed + 1;
  7838. if (iverneed->vn_next
  7839. > (size_t) (contents_end - (bfd_byte *) everneed))
  7840. goto error_return_bad_verref;
  7841. everneed = ((Elf_External_Verneed *)
  7842. ((bfd_byte *) everneed + iverneed->vn_next));
  7843. }
  7844. elf_tdata (abfd)->cverrefs = i;
  7845. free (contents);
  7846. contents = NULL;
  7847. }
  7848. if (elf_dynverdef (abfd) != 0)
  7849. {
  7850. Elf_Internal_Shdr *hdr;
  7851. Elf_External_Verdef *everdef;
  7852. Elf_Internal_Verdef *iverdef;
  7853. Elf_Internal_Verdef *iverdefarr;
  7854. Elf_Internal_Verdef iverdefmem;
  7855. unsigned int i;
  7856. unsigned int maxidx;
  7857. bfd_byte *contents_end_def, *contents_end_aux;
  7858. hdr = &elf_tdata (abfd)->dynverdef_hdr;
  7859. if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
  7860. {
  7861. error_return_bad_verdef:
  7862. _bfd_error_handler
  7863. (_("%pB: .gnu.version_d invalid entry"), abfd);
  7864. bfd_set_error (bfd_error_bad_value);
  7865. error_return_verdef:
  7866. elf_tdata (abfd)->verdef = NULL;
  7867. elf_tdata (abfd)->cverdefs = 0;
  7868. goto error_return;
  7869. }
  7870. if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
  7871. goto error_return_verdef;
  7872. contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
  7873. if (contents == NULL)
  7874. goto error_return_verdef;
  7875. BFD_ASSERT (sizeof (Elf_External_Verdef)
  7876. >= sizeof (Elf_External_Verdaux));
  7877. contents_end_def = contents + hdr->sh_size
  7878. - sizeof (Elf_External_Verdef);
  7879. contents_end_aux = contents + hdr->sh_size
  7880. - sizeof (Elf_External_Verdaux);
  7881. /* We know the number of entries in the section but not the maximum
  7882. index. Therefore we have to run through all entries and find
  7883. the maximum. */
  7884. everdef = (Elf_External_Verdef *) contents;
  7885. maxidx = 0;
  7886. for (i = 0; i < hdr->sh_info; ++i)
  7887. {
  7888. _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
  7889. if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
  7890. goto error_return_bad_verdef;
  7891. if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
  7892. maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
  7893. if (iverdefmem.vd_next == 0)
  7894. break;
  7895. if (iverdefmem.vd_next
  7896. > (size_t) (contents_end_def - (bfd_byte *) everdef))
  7897. goto error_return_bad_verdef;
  7898. everdef = ((Elf_External_Verdef *)
  7899. ((bfd_byte *) everdef + iverdefmem.vd_next));
  7900. }
  7901. if (default_imported_symver)
  7902. {
  7903. if (freeidx > maxidx)
  7904. maxidx = ++freeidx;
  7905. else
  7906. freeidx = ++maxidx;
  7907. }
  7908. if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
  7909. {
  7910. bfd_set_error (bfd_error_file_too_big);
  7911. goto error_return_verdef;
  7912. }
  7913. elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
  7914. if (elf_tdata (abfd)->verdef == NULL)
  7915. goto error_return_verdef;
  7916. elf_tdata (abfd)->cverdefs = maxidx;
  7917. everdef = (Elf_External_Verdef *) contents;
  7918. iverdefarr = elf_tdata (abfd)->verdef;
  7919. for (i = 0; i < hdr->sh_info; i++)
  7920. {
  7921. Elf_External_Verdaux *everdaux;
  7922. Elf_Internal_Verdaux *iverdaux;
  7923. unsigned int j;
  7924. _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
  7925. if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
  7926. goto error_return_bad_verdef;
  7927. iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
  7928. memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
  7929. iverdef->vd_bfd = abfd;
  7930. if (iverdef->vd_cnt == 0)
  7931. iverdef->vd_auxptr = NULL;
  7932. else
  7933. {
  7934. if (_bfd_mul_overflow (iverdef->vd_cnt,
  7935. sizeof (Elf_Internal_Verdaux), &amt))
  7936. {
  7937. bfd_set_error (bfd_error_file_too_big);
  7938. goto error_return_verdef;
  7939. }
  7940. iverdef->vd_auxptr = (struct elf_internal_verdaux *)
  7941. bfd_alloc (abfd, amt);
  7942. if (iverdef->vd_auxptr == NULL)
  7943. goto error_return_verdef;
  7944. }
  7945. if (iverdef->vd_aux
  7946. > (size_t) (contents_end_aux - (bfd_byte *) everdef))
  7947. goto error_return_bad_verdef;
  7948. everdaux = ((Elf_External_Verdaux *)
  7949. ((bfd_byte *) everdef + iverdef->vd_aux));
  7950. iverdaux = iverdef->vd_auxptr;
  7951. for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
  7952. {
  7953. _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
  7954. iverdaux->vda_nodename =
  7955. bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
  7956. iverdaux->vda_name);
  7957. if (iverdaux->vda_nodename == NULL)
  7958. goto error_return_bad_verdef;
  7959. iverdaux->vda_nextptr = NULL;
  7960. if (iverdaux->vda_next == 0)
  7961. {
  7962. iverdef->vd_cnt = j + 1;
  7963. break;
  7964. }
  7965. if (j + 1 < iverdef->vd_cnt)
  7966. iverdaux->vda_nextptr = iverdaux + 1;
  7967. if (iverdaux->vda_next
  7968. > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
  7969. goto error_return_bad_verdef;
  7970. everdaux = ((Elf_External_Verdaux *)
  7971. ((bfd_byte *) everdaux + iverdaux->vda_next));
  7972. }
  7973. iverdef->vd_nodename = NULL;
  7974. if (iverdef->vd_cnt)
  7975. iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
  7976. iverdef->vd_nextdef = NULL;
  7977. if (iverdef->vd_next == 0)
  7978. break;
  7979. if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
  7980. iverdef->vd_nextdef = iverdef + 1;
  7981. everdef = ((Elf_External_Verdef *)
  7982. ((bfd_byte *) everdef + iverdef->vd_next));
  7983. }
  7984. free (contents);
  7985. contents = NULL;
  7986. }
  7987. else if (default_imported_symver)
  7988. {
  7989. if (freeidx < 3)
  7990. freeidx = 3;
  7991. else
  7992. freeidx++;
  7993. if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
  7994. {
  7995. bfd_set_error (bfd_error_file_too_big);
  7996. goto error_return;
  7997. }
  7998. elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
  7999. if (elf_tdata (abfd)->verdef == NULL)
  8000. goto error_return;
  8001. elf_tdata (abfd)->cverdefs = freeidx;
  8002. }
  8003. /* Create a default version based on the soname. */
  8004. if (default_imported_symver)
  8005. {
  8006. Elf_Internal_Verdef *iverdef;
  8007. Elf_Internal_Verdaux *iverdaux;
  8008. iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
  8009. iverdef->vd_version = VER_DEF_CURRENT;
  8010. iverdef->vd_flags = 0;
  8011. iverdef->vd_ndx = freeidx;
  8012. iverdef->vd_cnt = 1;
  8013. iverdef->vd_bfd = abfd;
  8014. iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
  8015. if (iverdef->vd_nodename == NULL)
  8016. goto error_return_verdef;
  8017. iverdef->vd_nextdef = NULL;
  8018. iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
  8019. bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
  8020. if (iverdef->vd_auxptr == NULL)
  8021. goto error_return_verdef;
  8022. iverdaux = iverdef->vd_auxptr;
  8023. iverdaux->vda_nodename = iverdef->vd_nodename;
  8024. }
  8025. return true;
  8026. error_return:
  8027. free (contents);
  8028. return false;
  8029. }
  8030. asymbol *
  8031. _bfd_elf_make_empty_symbol (bfd *abfd)
  8032. {
  8033. elf_symbol_type *newsym;
  8034. newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
  8035. if (!newsym)
  8036. return NULL;
  8037. newsym->symbol.the_bfd = abfd;
  8038. return &newsym->symbol;
  8039. }
  8040. void
  8041. _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
  8042. asymbol *symbol,
  8043. symbol_info *ret)
  8044. {
  8045. bfd_symbol_info (symbol, ret);
  8046. }
  8047. /* Return whether a symbol name implies a local symbol. Most targets
  8048. use this function for the is_local_label_name entry point, but some
  8049. override it. */
  8050. bool
  8051. _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
  8052. const char *name)
  8053. {
  8054. /* Normal local symbols start with ``.L''. */
  8055. if (name[0] == '.' && name[1] == 'L')
  8056. return true;
  8057. /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
  8058. DWARF debugging symbols starting with ``..''. */
  8059. if (name[0] == '.' && name[1] == '.')
  8060. return true;
  8061. /* gcc will sometimes generate symbols beginning with ``_.L_'' when
  8062. emitting DWARF debugging output. I suspect this is actually a
  8063. small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
  8064. ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
  8065. underscore to be emitted on some ELF targets). For ease of use,
  8066. we treat such symbols as local. */
  8067. if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
  8068. return true;
  8069. /* Treat assembler generated fake symbols, dollar local labels and
  8070. forward-backward labels (aka local labels) as locals.
  8071. These labels have the form:
  8072. L0^A.* (fake symbols)
  8073. [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
  8074. Versions which start with .L will have already been matched above,
  8075. so we only need to match the rest. */
  8076. if (name[0] == 'L' && ISDIGIT (name[1]))
  8077. {
  8078. bool ret = false;
  8079. const char * p;
  8080. char c;
  8081. for (p = name + 2; (c = *p); p++)
  8082. {
  8083. if (c == 1 || c == 2)
  8084. {
  8085. if (c == 1 && p == name + 2)
  8086. /* A fake symbol. */
  8087. return true;
  8088. /* FIXME: We are being paranoid here and treating symbols like
  8089. L0^Bfoo as if there were non-local, on the grounds that the
  8090. assembler will never generate them. But can any symbol
  8091. containing an ASCII value in the range 1-31 ever be anything
  8092. other than some kind of local ? */
  8093. ret = true;
  8094. }
  8095. if (! ISDIGIT (c))
  8096. {
  8097. ret = false;
  8098. break;
  8099. }
  8100. }
  8101. return ret;
  8102. }
  8103. return false;
  8104. }
  8105. alent *
  8106. _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
  8107. asymbol *symbol ATTRIBUTE_UNUSED)
  8108. {
  8109. abort ();
  8110. return NULL;
  8111. }
  8112. bool
  8113. _bfd_elf_set_arch_mach (bfd *abfd,
  8114. enum bfd_architecture arch,
  8115. unsigned long machine)
  8116. {
  8117. /* If this isn't the right architecture for this backend, and this
  8118. isn't the generic backend, fail. */
  8119. if (arch != get_elf_backend_data (abfd)->arch
  8120. && arch != bfd_arch_unknown
  8121. && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
  8122. return false;
  8123. return bfd_default_set_arch_mach (abfd, arch, machine);
  8124. }
  8125. /* Find the nearest line to a particular section and offset,
  8126. for error reporting. */
  8127. bool
  8128. _bfd_elf_find_nearest_line (bfd *abfd,
  8129. asymbol **symbols,
  8130. asection *section,
  8131. bfd_vma offset,
  8132. const char **filename_ptr,
  8133. const char **functionname_ptr,
  8134. unsigned int *line_ptr,
  8135. unsigned int *discriminator_ptr)
  8136. {
  8137. bool found;
  8138. if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
  8139. filename_ptr, functionname_ptr,
  8140. line_ptr, discriminator_ptr,
  8141. dwarf_debug_sections,
  8142. &elf_tdata (abfd)->dwarf2_find_line_info))
  8143. return true;
  8144. if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
  8145. filename_ptr, functionname_ptr, line_ptr))
  8146. {
  8147. if (!*functionname_ptr)
  8148. _bfd_elf_find_function (abfd, symbols, section, offset,
  8149. *filename_ptr ? NULL : filename_ptr,
  8150. functionname_ptr);
  8151. return true;
  8152. }
  8153. if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
  8154. &found, filename_ptr,
  8155. functionname_ptr, line_ptr,
  8156. &elf_tdata (abfd)->line_info))
  8157. return false;
  8158. if (found && (*functionname_ptr || *line_ptr))
  8159. return true;
  8160. if (symbols == NULL)
  8161. return false;
  8162. if (! _bfd_elf_find_function (abfd, symbols, section, offset,
  8163. filename_ptr, functionname_ptr))
  8164. return false;
  8165. *line_ptr = 0;
  8166. return true;
  8167. }
  8168. /* Find the line for a symbol. */
  8169. bool
  8170. _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
  8171. const char **filename_ptr, unsigned int *line_ptr)
  8172. {
  8173. return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
  8174. filename_ptr, NULL, line_ptr, NULL,
  8175. dwarf_debug_sections,
  8176. &elf_tdata (abfd)->dwarf2_find_line_info);
  8177. }
  8178. /* After a call to bfd_find_nearest_line, successive calls to
  8179. bfd_find_inliner_info can be used to get source information about
  8180. each level of function inlining that terminated at the address
  8181. passed to bfd_find_nearest_line. Currently this is only supported
  8182. for DWARF2 with appropriate DWARF3 extensions. */
  8183. bool
  8184. _bfd_elf_find_inliner_info (bfd *abfd,
  8185. const char **filename_ptr,
  8186. const char **functionname_ptr,
  8187. unsigned int *line_ptr)
  8188. {
  8189. bool found;
  8190. found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
  8191. functionname_ptr, line_ptr,
  8192. & elf_tdata (abfd)->dwarf2_find_line_info);
  8193. return found;
  8194. }
  8195. int
  8196. _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
  8197. {
  8198. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  8199. int ret = bed->s->sizeof_ehdr;
  8200. if (!bfd_link_relocatable (info))
  8201. {
  8202. bfd_size_type phdr_size = elf_program_header_size (abfd);
  8203. if (phdr_size == (bfd_size_type) -1)
  8204. {
  8205. struct elf_segment_map *m;
  8206. phdr_size = 0;
  8207. for (m = elf_seg_map (abfd); m != NULL; m = m->next)
  8208. phdr_size += bed->s->sizeof_phdr;
  8209. if (phdr_size == 0)
  8210. phdr_size = get_program_header_size (abfd, info);
  8211. }
  8212. elf_program_header_size (abfd) = phdr_size;
  8213. ret += phdr_size;
  8214. }
  8215. return ret;
  8216. }
  8217. bool
  8218. _bfd_elf_set_section_contents (bfd *abfd,
  8219. sec_ptr section,
  8220. const void *location,
  8221. file_ptr offset,
  8222. bfd_size_type count)
  8223. {
  8224. Elf_Internal_Shdr *hdr;
  8225. file_ptr pos;
  8226. if (! abfd->output_has_begun
  8227. && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
  8228. return false;
  8229. if (!count)
  8230. return true;
  8231. hdr = &elf_section_data (section)->this_hdr;
  8232. if (hdr->sh_offset == (file_ptr) -1)
  8233. {
  8234. unsigned char *contents;
  8235. if (bfd_section_is_ctf (section))
  8236. /* Nothing to do with this section: the contents are generated
  8237. later. */
  8238. return true;
  8239. if ((section->flags & SEC_ELF_COMPRESS) == 0)
  8240. {
  8241. _bfd_error_handler
  8242. (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
  8243. abfd, section);
  8244. bfd_set_error (bfd_error_invalid_operation);
  8245. return false;
  8246. }
  8247. if ((offset + count) > hdr->sh_size)
  8248. {
  8249. _bfd_error_handler
  8250. (_("%pB:%pA: error: attempting to write over the end of the section"),
  8251. abfd, section);
  8252. bfd_set_error (bfd_error_invalid_operation);
  8253. return false;
  8254. }
  8255. contents = hdr->contents;
  8256. if (contents == NULL)
  8257. {
  8258. _bfd_error_handler
  8259. (_("%pB:%pA: error: attempting to write section into an empty buffer"),
  8260. abfd, section);
  8261. bfd_set_error (bfd_error_invalid_operation);
  8262. return false;
  8263. }
  8264. memcpy (contents + offset, location, count);
  8265. return true;
  8266. }
  8267. pos = hdr->sh_offset + offset;
  8268. if (bfd_seek (abfd, pos, SEEK_SET) != 0
  8269. || bfd_bwrite (location, count, abfd) != count)
  8270. return false;
  8271. return true;
  8272. }
  8273. bool
  8274. _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  8275. arelent *cache_ptr ATTRIBUTE_UNUSED,
  8276. Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
  8277. {
  8278. abort ();
  8279. return false;
  8280. }
  8281. /* Try to convert a non-ELF reloc into an ELF one. */
  8282. bool
  8283. _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
  8284. {
  8285. /* Check whether we really have an ELF howto. */
  8286. if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
  8287. {
  8288. bfd_reloc_code_real_type code;
  8289. reloc_howto_type *howto;
  8290. /* Alien reloc: Try to determine its type to replace it with an
  8291. equivalent ELF reloc. */
  8292. if (areloc->howto->pc_relative)
  8293. {
  8294. switch (areloc->howto->bitsize)
  8295. {
  8296. case 8:
  8297. code = BFD_RELOC_8_PCREL;
  8298. break;
  8299. case 12:
  8300. code = BFD_RELOC_12_PCREL;
  8301. break;
  8302. case 16:
  8303. code = BFD_RELOC_16_PCREL;
  8304. break;
  8305. case 24:
  8306. code = BFD_RELOC_24_PCREL;
  8307. break;
  8308. case 32:
  8309. code = BFD_RELOC_32_PCREL;
  8310. break;
  8311. case 64:
  8312. code = BFD_RELOC_64_PCREL;
  8313. break;
  8314. default:
  8315. goto fail;
  8316. }
  8317. howto = bfd_reloc_type_lookup (abfd, code);
  8318. if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
  8319. {
  8320. if (howto->pcrel_offset)
  8321. areloc->addend += areloc->address;
  8322. else
  8323. areloc->addend -= areloc->address; /* addend is unsigned!! */
  8324. }
  8325. }
  8326. else
  8327. {
  8328. switch (areloc->howto->bitsize)
  8329. {
  8330. case 8:
  8331. code = BFD_RELOC_8;
  8332. break;
  8333. case 14:
  8334. code = BFD_RELOC_14;
  8335. break;
  8336. case 16:
  8337. code = BFD_RELOC_16;
  8338. break;
  8339. case 26:
  8340. code = BFD_RELOC_26;
  8341. break;
  8342. case 32:
  8343. code = BFD_RELOC_32;
  8344. break;
  8345. case 64:
  8346. code = BFD_RELOC_64;
  8347. break;
  8348. default:
  8349. goto fail;
  8350. }
  8351. howto = bfd_reloc_type_lookup (abfd, code);
  8352. }
  8353. if (howto)
  8354. areloc->howto = howto;
  8355. else
  8356. goto fail;
  8357. }
  8358. return true;
  8359. fail:
  8360. /* xgettext:c-format */
  8361. _bfd_error_handler (_("%pB: %s unsupported"),
  8362. abfd, areloc->howto->name);
  8363. bfd_set_error (bfd_error_sorry);
  8364. return false;
  8365. }
  8366. bool
  8367. _bfd_elf_close_and_cleanup (bfd *abfd)
  8368. {
  8369. struct elf_obj_tdata *tdata = elf_tdata (abfd);
  8370. if (tdata != NULL
  8371. && (bfd_get_format (abfd) == bfd_object
  8372. || bfd_get_format (abfd) == bfd_core))
  8373. {
  8374. if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
  8375. _bfd_elf_strtab_free (elf_shstrtab (abfd));
  8376. _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
  8377. }
  8378. return _bfd_generic_close_and_cleanup (abfd);
  8379. }
  8380. /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
  8381. in the relocation's offset. Thus we cannot allow any sort of sanity
  8382. range-checking to interfere. There is nothing else to do in processing
  8383. this reloc. */
  8384. bfd_reloc_status_type
  8385. _bfd_elf_rel_vtable_reloc_fn
  8386. (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
  8387. struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
  8388. void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
  8389. bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
  8390. {
  8391. return bfd_reloc_ok;
  8392. }
  8393. /* Elf core file support. Much of this only works on native
  8394. toolchains, since we rely on knowing the
  8395. machine-dependent procfs structure in order to pick
  8396. out details about the corefile. */
  8397. #ifdef HAVE_SYS_PROCFS_H
  8398. # include <sys/procfs.h>
  8399. #endif
  8400. /* Return a PID that identifies a "thread" for threaded cores, or the
  8401. PID of the main process for non-threaded cores. */
  8402. static int
  8403. elfcore_make_pid (bfd *abfd)
  8404. {
  8405. int pid;
  8406. pid = elf_tdata (abfd)->core->lwpid;
  8407. if (pid == 0)
  8408. pid = elf_tdata (abfd)->core->pid;
  8409. return pid;
  8410. }
  8411. /* If there isn't a section called NAME, make one, using
  8412. data from SECT. Note, this function will generate a
  8413. reference to NAME, so you shouldn't deallocate or
  8414. overwrite it. */
  8415. static bool
  8416. elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
  8417. {
  8418. asection *sect2;
  8419. if (bfd_get_section_by_name (abfd, name) != NULL)
  8420. return true;
  8421. sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
  8422. if (sect2 == NULL)
  8423. return false;
  8424. sect2->size = sect->size;
  8425. sect2->filepos = sect->filepos;
  8426. sect2->alignment_power = sect->alignment_power;
  8427. return true;
  8428. }
  8429. /* Create a pseudosection containing SIZE bytes at FILEPOS. This
  8430. actually creates up to two pseudosections:
  8431. - For the single-threaded case, a section named NAME, unless
  8432. such a section already exists.
  8433. - For the multi-threaded case, a section named "NAME/PID", where
  8434. PID is elfcore_make_pid (abfd).
  8435. Both pseudosections have identical contents. */
  8436. bool
  8437. _bfd_elfcore_make_pseudosection (bfd *abfd,
  8438. char *name,
  8439. size_t size,
  8440. ufile_ptr filepos)
  8441. {
  8442. char buf[100];
  8443. char *threaded_name;
  8444. size_t len;
  8445. asection *sect;
  8446. /* Build the section name. */
  8447. sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
  8448. len = strlen (buf) + 1;
  8449. threaded_name = (char *) bfd_alloc (abfd, len);
  8450. if (threaded_name == NULL)
  8451. return false;
  8452. memcpy (threaded_name, buf, len);
  8453. sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
  8454. SEC_HAS_CONTENTS);
  8455. if (sect == NULL)
  8456. return false;
  8457. sect->size = size;
  8458. sect->filepos = filepos;
  8459. sect->alignment_power = 2;
  8460. return elfcore_maybe_make_sect (abfd, name, sect);
  8461. }
  8462. static bool
  8463. elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
  8464. size_t offs)
  8465. {
  8466. asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
  8467. SEC_HAS_CONTENTS);
  8468. if (sect == NULL)
  8469. return false;
  8470. sect->size = note->descsz - offs;
  8471. sect->filepos = note->descpos + offs;
  8472. sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
  8473. return true;
  8474. }
  8475. /* prstatus_t exists on:
  8476. solaris 2.5+
  8477. linux 2.[01] + glibc
  8478. unixware 4.2
  8479. */
  8480. #if defined (HAVE_PRSTATUS_T)
  8481. static bool
  8482. elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  8483. {
  8484. size_t size;
  8485. int offset;
  8486. if (note->descsz == sizeof (prstatus_t))
  8487. {
  8488. prstatus_t prstat;
  8489. size = sizeof (prstat.pr_reg);
  8490. offset = offsetof (prstatus_t, pr_reg);
  8491. memcpy (&prstat, note->descdata, sizeof (prstat));
  8492. /* Do not overwrite the core signal if it
  8493. has already been set by another thread. */
  8494. if (elf_tdata (abfd)->core->signal == 0)
  8495. elf_tdata (abfd)->core->signal = prstat.pr_cursig;
  8496. if (elf_tdata (abfd)->core->pid == 0)
  8497. elf_tdata (abfd)->core->pid = prstat.pr_pid;
  8498. /* pr_who exists on:
  8499. solaris 2.5+
  8500. unixware 4.2
  8501. pr_who doesn't exist on:
  8502. linux 2.[01]
  8503. */
  8504. #if defined (HAVE_PRSTATUS_T_PR_WHO)
  8505. elf_tdata (abfd)->core->lwpid = prstat.pr_who;
  8506. #else
  8507. elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
  8508. #endif
  8509. }
  8510. #if defined (HAVE_PRSTATUS32_T)
  8511. else if (note->descsz == sizeof (prstatus32_t))
  8512. {
  8513. /* 64-bit host, 32-bit corefile */
  8514. prstatus32_t prstat;
  8515. size = sizeof (prstat.pr_reg);
  8516. offset = offsetof (prstatus32_t, pr_reg);
  8517. memcpy (&prstat, note->descdata, sizeof (prstat));
  8518. /* Do not overwrite the core signal if it
  8519. has already been set by another thread. */
  8520. if (elf_tdata (abfd)->core->signal == 0)
  8521. elf_tdata (abfd)->core->signal = prstat.pr_cursig;
  8522. if (elf_tdata (abfd)->core->pid == 0)
  8523. elf_tdata (abfd)->core->pid = prstat.pr_pid;
  8524. /* pr_who exists on:
  8525. solaris 2.5+
  8526. unixware 4.2
  8527. pr_who doesn't exist on:
  8528. linux 2.[01]
  8529. */
  8530. #if defined (HAVE_PRSTATUS32_T_PR_WHO)
  8531. elf_tdata (abfd)->core->lwpid = prstat.pr_who;
  8532. #else
  8533. elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
  8534. #endif
  8535. }
  8536. #endif /* HAVE_PRSTATUS32_T */
  8537. else
  8538. {
  8539. /* Fail - we don't know how to handle any other
  8540. note size (ie. data object type). */
  8541. return true;
  8542. }
  8543. /* Make a ".reg/999" section and a ".reg" section. */
  8544. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  8545. size, note->descpos + offset);
  8546. }
  8547. #endif /* defined (HAVE_PRSTATUS_T) */
  8548. /* Create a pseudosection containing the exact contents of NOTE. */
  8549. static bool
  8550. elfcore_make_note_pseudosection (bfd *abfd,
  8551. char *name,
  8552. Elf_Internal_Note *note)
  8553. {
  8554. return _bfd_elfcore_make_pseudosection (abfd, name,
  8555. note->descsz, note->descpos);
  8556. }
  8557. /* There isn't a consistent prfpregset_t across platforms,
  8558. but it doesn't matter, because we don't have to pick this
  8559. data structure apart. */
  8560. static bool
  8561. elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
  8562. {
  8563. return elfcore_make_note_pseudosection (abfd, ".reg2", note);
  8564. }
  8565. /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
  8566. type of NT_PRXFPREG. Just include the whole note's contents
  8567. literally. */
  8568. static bool
  8569. elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
  8570. {
  8571. return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
  8572. }
  8573. /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
  8574. with a note type of NT_X86_XSTATE. Just include the whole note's
  8575. contents literally. */
  8576. static bool
  8577. elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
  8578. {
  8579. return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
  8580. }
  8581. static bool
  8582. elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
  8583. {
  8584. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
  8585. }
  8586. static bool
  8587. elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
  8588. {
  8589. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
  8590. }
  8591. static bool
  8592. elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
  8593. {
  8594. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
  8595. }
  8596. static bool
  8597. elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
  8598. {
  8599. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
  8600. }
  8601. static bool
  8602. elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
  8603. {
  8604. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
  8605. }
  8606. static bool
  8607. elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
  8608. {
  8609. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
  8610. }
  8611. static bool
  8612. elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
  8613. {
  8614. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
  8615. }
  8616. static bool
  8617. elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
  8618. {
  8619. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
  8620. }
  8621. static bool
  8622. elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
  8623. {
  8624. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
  8625. }
  8626. static bool
  8627. elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
  8628. {
  8629. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
  8630. }
  8631. static bool
  8632. elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
  8633. {
  8634. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
  8635. }
  8636. static bool
  8637. elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
  8638. {
  8639. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
  8640. }
  8641. static bool
  8642. elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
  8643. {
  8644. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
  8645. }
  8646. static bool
  8647. elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
  8648. {
  8649. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
  8650. }
  8651. static bool
  8652. elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
  8653. {
  8654. return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
  8655. }
  8656. static bool
  8657. elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
  8658. {
  8659. return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
  8660. }
  8661. static bool
  8662. elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
  8663. {
  8664. return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
  8665. }
  8666. static bool
  8667. elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
  8668. {
  8669. return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
  8670. }
  8671. static bool
  8672. elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
  8673. {
  8674. return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
  8675. }
  8676. static bool
  8677. elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
  8678. {
  8679. return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
  8680. }
  8681. static bool
  8682. elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
  8683. {
  8684. return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
  8685. }
  8686. static bool
  8687. elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
  8688. {
  8689. return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
  8690. }
  8691. static bool
  8692. elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
  8693. {
  8694. return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
  8695. }
  8696. static bool
  8697. elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
  8698. {
  8699. return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
  8700. }
  8701. static bool
  8702. elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
  8703. {
  8704. return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
  8705. }
  8706. static bool
  8707. elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
  8708. {
  8709. return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
  8710. }
  8711. static bool
  8712. elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
  8713. {
  8714. return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
  8715. }
  8716. static bool
  8717. elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
  8718. {
  8719. return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
  8720. }
  8721. static bool
  8722. elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
  8723. {
  8724. return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
  8725. }
  8726. static bool
  8727. elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
  8728. {
  8729. return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
  8730. }
  8731. static bool
  8732. elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
  8733. {
  8734. return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
  8735. }
  8736. static bool
  8737. elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
  8738. {
  8739. return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
  8740. }
  8741. static bool
  8742. elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
  8743. {
  8744. return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
  8745. }
  8746. static bool
  8747. elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
  8748. {
  8749. return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
  8750. }
  8751. static bool
  8752. elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
  8753. {
  8754. return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
  8755. note);
  8756. }
  8757. static bool
  8758. elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
  8759. {
  8760. return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
  8761. }
  8762. /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
  8763. successful otherwise, return FALSE. */
  8764. static bool
  8765. elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
  8766. {
  8767. return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
  8768. }
  8769. /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
  8770. successful otherwise, return FALSE. */
  8771. static bool
  8772. elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
  8773. {
  8774. return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
  8775. }
  8776. static bool
  8777. elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
  8778. {
  8779. return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
  8780. }
  8781. static bool
  8782. elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
  8783. {
  8784. return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
  8785. }
  8786. static bool
  8787. elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
  8788. {
  8789. return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
  8790. }
  8791. static bool
  8792. elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
  8793. {
  8794. return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
  8795. }
  8796. #if defined (HAVE_PRPSINFO_T)
  8797. typedef prpsinfo_t elfcore_psinfo_t;
  8798. #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
  8799. typedef prpsinfo32_t elfcore_psinfo32_t;
  8800. #endif
  8801. #endif
  8802. #if defined (HAVE_PSINFO_T)
  8803. typedef psinfo_t elfcore_psinfo_t;
  8804. #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
  8805. typedef psinfo32_t elfcore_psinfo32_t;
  8806. #endif
  8807. #endif
  8808. /* return a malloc'ed copy of a string at START which is at
  8809. most MAX bytes long, possibly without a terminating '\0'.
  8810. the copy will always have a terminating '\0'. */
  8811. char *
  8812. _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
  8813. {
  8814. char *dups;
  8815. char *end = (char *) memchr (start, '\0', max);
  8816. size_t len;
  8817. if (end == NULL)
  8818. len = max;
  8819. else
  8820. len = end - start;
  8821. dups = (char *) bfd_alloc (abfd, len + 1);
  8822. if (dups == NULL)
  8823. return NULL;
  8824. memcpy (dups, start, len);
  8825. dups[len] = '\0';
  8826. return dups;
  8827. }
  8828. #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
  8829. static bool
  8830. elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  8831. {
  8832. if (note->descsz == sizeof (elfcore_psinfo_t))
  8833. {
  8834. elfcore_psinfo_t psinfo;
  8835. memcpy (&psinfo, note->descdata, sizeof (psinfo));
  8836. #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
  8837. elf_tdata (abfd)->core->pid = psinfo.pr_pid;
  8838. #endif
  8839. elf_tdata (abfd)->core->program
  8840. = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
  8841. sizeof (psinfo.pr_fname));
  8842. elf_tdata (abfd)->core->command
  8843. = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
  8844. sizeof (psinfo.pr_psargs));
  8845. }
  8846. #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
  8847. else if (note->descsz == sizeof (elfcore_psinfo32_t))
  8848. {
  8849. /* 64-bit host, 32-bit corefile */
  8850. elfcore_psinfo32_t psinfo;
  8851. memcpy (&psinfo, note->descdata, sizeof (psinfo));
  8852. #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
  8853. elf_tdata (abfd)->core->pid = psinfo.pr_pid;
  8854. #endif
  8855. elf_tdata (abfd)->core->program
  8856. = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
  8857. sizeof (psinfo.pr_fname));
  8858. elf_tdata (abfd)->core->command
  8859. = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
  8860. sizeof (psinfo.pr_psargs));
  8861. }
  8862. #endif
  8863. else
  8864. {
  8865. /* Fail - we don't know how to handle any other
  8866. note size (ie. data object type). */
  8867. return true;
  8868. }
  8869. /* Note that for some reason, a spurious space is tacked
  8870. onto the end of the args in some (at least one anyway)
  8871. implementations, so strip it off if it exists. */
  8872. {
  8873. char *command = elf_tdata (abfd)->core->command;
  8874. int n = strlen (command);
  8875. if (0 < n && command[n - 1] == ' ')
  8876. command[n - 1] = '\0';
  8877. }
  8878. return true;
  8879. }
  8880. #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
  8881. #if defined (HAVE_PSTATUS_T)
  8882. static bool
  8883. elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
  8884. {
  8885. if (note->descsz == sizeof (pstatus_t)
  8886. #if defined (HAVE_PXSTATUS_T)
  8887. || note->descsz == sizeof (pxstatus_t)
  8888. #endif
  8889. )
  8890. {
  8891. pstatus_t pstat;
  8892. memcpy (&pstat, note->descdata, sizeof (pstat));
  8893. elf_tdata (abfd)->core->pid = pstat.pr_pid;
  8894. }
  8895. #if defined (HAVE_PSTATUS32_T)
  8896. else if (note->descsz == sizeof (pstatus32_t))
  8897. {
  8898. /* 64-bit host, 32-bit corefile */
  8899. pstatus32_t pstat;
  8900. memcpy (&pstat, note->descdata, sizeof (pstat));
  8901. elf_tdata (abfd)->core->pid = pstat.pr_pid;
  8902. }
  8903. #endif
  8904. /* Could grab some more details from the "representative"
  8905. lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
  8906. NT_LWPSTATUS note, presumably. */
  8907. return true;
  8908. }
  8909. #endif /* defined (HAVE_PSTATUS_T) */
  8910. #if defined (HAVE_LWPSTATUS_T)
  8911. static bool
  8912. elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
  8913. {
  8914. lwpstatus_t lwpstat;
  8915. char buf[100];
  8916. char *name;
  8917. size_t len;
  8918. asection *sect;
  8919. if (note->descsz != sizeof (lwpstat)
  8920. #if defined (HAVE_LWPXSTATUS_T)
  8921. && note->descsz != sizeof (lwpxstatus_t)
  8922. #endif
  8923. )
  8924. return true;
  8925. memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
  8926. elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
  8927. /* Do not overwrite the core signal if it has already been set by
  8928. another thread. */
  8929. if (elf_tdata (abfd)->core->signal == 0)
  8930. elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
  8931. /* Make a ".reg/999" section. */
  8932. sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
  8933. len = strlen (buf) + 1;
  8934. name = bfd_alloc (abfd, len);
  8935. if (name == NULL)
  8936. return false;
  8937. memcpy (name, buf, len);
  8938. sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
  8939. if (sect == NULL)
  8940. return false;
  8941. #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
  8942. sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
  8943. sect->filepos = note->descpos
  8944. + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
  8945. #endif
  8946. #if defined (HAVE_LWPSTATUS_T_PR_REG)
  8947. sect->size = sizeof (lwpstat.pr_reg);
  8948. sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
  8949. #endif
  8950. sect->alignment_power = 2;
  8951. if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
  8952. return false;
  8953. /* Make a ".reg2/999" section */
  8954. sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
  8955. len = strlen (buf) + 1;
  8956. name = bfd_alloc (abfd, len);
  8957. if (name == NULL)
  8958. return false;
  8959. memcpy (name, buf, len);
  8960. sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
  8961. if (sect == NULL)
  8962. return false;
  8963. #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
  8964. sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
  8965. sect->filepos = note->descpos
  8966. + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
  8967. #endif
  8968. #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
  8969. sect->size = sizeof (lwpstat.pr_fpreg);
  8970. sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
  8971. #endif
  8972. sect->alignment_power = 2;
  8973. return elfcore_maybe_make_sect (abfd, ".reg2", sect);
  8974. }
  8975. #endif /* defined (HAVE_LWPSTATUS_T) */
  8976. /* These constants, and the structure offsets used below, are defined by
  8977. Cygwin's core_dump.h */
  8978. #define NOTE_INFO_PROCESS 1
  8979. #define NOTE_INFO_THREAD 2
  8980. #define NOTE_INFO_MODULE 3
  8981. #define NOTE_INFO_MODULE64 4
  8982. static bool
  8983. elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
  8984. {
  8985. char buf[30];
  8986. char *name;
  8987. size_t len;
  8988. unsigned int name_size;
  8989. asection *sect;
  8990. unsigned int type;
  8991. int is_active_thread;
  8992. bfd_vma base_addr;
  8993. if (note->descsz < 4)
  8994. return true;
  8995. if (! startswith (note->namedata, "win32"))
  8996. return true;
  8997. type = bfd_get_32 (abfd, note->descdata);
  8998. struct
  8999. {
  9000. const char *type_name;
  9001. unsigned long min_size;
  9002. } size_check[] =
  9003. {
  9004. { "NOTE_INFO_PROCESS", 12 },
  9005. { "NOTE_INFO_THREAD", 12 },
  9006. { "NOTE_INFO_MODULE", 12 },
  9007. { "NOTE_INFO_MODULE64", 16 },
  9008. };
  9009. if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
  9010. return true;
  9011. if (note->descsz < size_check[type - 1].min_size)
  9012. {
  9013. _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
  9014. abfd, size_check[type - 1].type_name, note->descsz);
  9015. return true;
  9016. }
  9017. switch (type)
  9018. {
  9019. case NOTE_INFO_PROCESS:
  9020. /* FIXME: need to add ->core->command. */
  9021. elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
  9022. elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
  9023. break;
  9024. case NOTE_INFO_THREAD:
  9025. /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
  9026. structure. */
  9027. /* thread_info.tid */
  9028. sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
  9029. len = strlen (buf) + 1;
  9030. name = (char *) bfd_alloc (abfd, len);
  9031. if (name == NULL)
  9032. return false;
  9033. memcpy (name, buf, len);
  9034. sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
  9035. if (sect == NULL)
  9036. return false;
  9037. /* sizeof (thread_info.thread_context) */
  9038. sect->size = note->descsz - 12;
  9039. /* offsetof (thread_info.thread_context) */
  9040. sect->filepos = note->descpos + 12;
  9041. sect->alignment_power = 2;
  9042. /* thread_info.is_active_thread */
  9043. is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
  9044. if (is_active_thread)
  9045. if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
  9046. return false;
  9047. break;
  9048. case NOTE_INFO_MODULE:
  9049. case NOTE_INFO_MODULE64:
  9050. /* Make a ".module/xxxxxxxx" section. */
  9051. if (type == NOTE_INFO_MODULE)
  9052. {
  9053. /* module_info.base_address */
  9054. base_addr = bfd_get_32 (abfd, note->descdata + 4);
  9055. sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
  9056. /* module_info.module_name_size */
  9057. name_size = bfd_get_32 (abfd, note->descdata + 8);
  9058. }
  9059. else /* NOTE_INFO_MODULE64 */
  9060. {
  9061. /* module_info.base_address */
  9062. base_addr = bfd_get_64 (abfd, note->descdata + 4);
  9063. sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
  9064. /* module_info.module_name_size */
  9065. name_size = bfd_get_32 (abfd, note->descdata + 12);
  9066. }
  9067. len = strlen (buf) + 1;
  9068. name = (char *) bfd_alloc (abfd, len);
  9069. if (name == NULL)
  9070. return false;
  9071. memcpy (name, buf, len);
  9072. sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
  9073. if (sect == NULL)
  9074. return false;
  9075. if (note->descsz < 12 + name_size)
  9076. {
  9077. _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
  9078. abfd, note->descsz, name_size);
  9079. return true;
  9080. }
  9081. sect->size = note->descsz;
  9082. sect->filepos = note->descpos;
  9083. sect->alignment_power = 2;
  9084. break;
  9085. default:
  9086. return true;
  9087. }
  9088. return true;
  9089. }
  9090. static bool
  9091. elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
  9092. {
  9093. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  9094. switch (note->type)
  9095. {
  9096. default:
  9097. return true;
  9098. case NT_PRSTATUS:
  9099. if (bed->elf_backend_grok_prstatus)
  9100. if ((*bed->elf_backend_grok_prstatus) (abfd, note))
  9101. return true;
  9102. #if defined (HAVE_PRSTATUS_T)
  9103. return elfcore_grok_prstatus (abfd, note);
  9104. #else
  9105. return true;
  9106. #endif
  9107. #if defined (HAVE_PSTATUS_T)
  9108. case NT_PSTATUS:
  9109. return elfcore_grok_pstatus (abfd, note);
  9110. #endif
  9111. #if defined (HAVE_LWPSTATUS_T)
  9112. case NT_LWPSTATUS:
  9113. return elfcore_grok_lwpstatus (abfd, note);
  9114. #endif
  9115. case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
  9116. return elfcore_grok_prfpreg (abfd, note);
  9117. case NT_WIN32PSTATUS:
  9118. return elfcore_grok_win32pstatus (abfd, note);
  9119. case NT_PRXFPREG: /* Linux SSE extension */
  9120. if (note->namesz == 6
  9121. && strcmp (note->namedata, "LINUX") == 0)
  9122. return elfcore_grok_prxfpreg (abfd, note);
  9123. else
  9124. return true;
  9125. case NT_X86_XSTATE: /* Linux XSAVE extension */
  9126. if (note->namesz == 6
  9127. && strcmp (note->namedata, "LINUX") == 0)
  9128. return elfcore_grok_xstatereg (abfd, note);
  9129. else
  9130. return true;
  9131. case NT_PPC_VMX:
  9132. if (note->namesz == 6
  9133. && strcmp (note->namedata, "LINUX") == 0)
  9134. return elfcore_grok_ppc_vmx (abfd, note);
  9135. else
  9136. return true;
  9137. case NT_PPC_VSX:
  9138. if (note->namesz == 6
  9139. && strcmp (note->namedata, "LINUX") == 0)
  9140. return elfcore_grok_ppc_vsx (abfd, note);
  9141. else
  9142. return true;
  9143. case NT_PPC_TAR:
  9144. if (note->namesz == 6
  9145. && strcmp (note->namedata, "LINUX") == 0)
  9146. return elfcore_grok_ppc_tar (abfd, note);
  9147. else
  9148. return true;
  9149. case NT_PPC_PPR:
  9150. if (note->namesz == 6
  9151. && strcmp (note->namedata, "LINUX") == 0)
  9152. return elfcore_grok_ppc_ppr (abfd, note);
  9153. else
  9154. return true;
  9155. case NT_PPC_DSCR:
  9156. if (note->namesz == 6
  9157. && strcmp (note->namedata, "LINUX") == 0)
  9158. return elfcore_grok_ppc_dscr (abfd, note);
  9159. else
  9160. return true;
  9161. case NT_PPC_EBB:
  9162. if (note->namesz == 6
  9163. && strcmp (note->namedata, "LINUX") == 0)
  9164. return elfcore_grok_ppc_ebb (abfd, note);
  9165. else
  9166. return true;
  9167. case NT_PPC_PMU:
  9168. if (note->namesz == 6
  9169. && strcmp (note->namedata, "LINUX") == 0)
  9170. return elfcore_grok_ppc_pmu (abfd, note);
  9171. else
  9172. return true;
  9173. case NT_PPC_TM_CGPR:
  9174. if (note->namesz == 6
  9175. && strcmp (note->namedata, "LINUX") == 0)
  9176. return elfcore_grok_ppc_tm_cgpr (abfd, note);
  9177. else
  9178. return true;
  9179. case NT_PPC_TM_CFPR:
  9180. if (note->namesz == 6
  9181. && strcmp (note->namedata, "LINUX") == 0)
  9182. return elfcore_grok_ppc_tm_cfpr (abfd, note);
  9183. else
  9184. return true;
  9185. case NT_PPC_TM_CVMX:
  9186. if (note->namesz == 6
  9187. && strcmp (note->namedata, "LINUX") == 0)
  9188. return elfcore_grok_ppc_tm_cvmx (abfd, note);
  9189. else
  9190. return true;
  9191. case NT_PPC_TM_CVSX:
  9192. if (note->namesz == 6
  9193. && strcmp (note->namedata, "LINUX") == 0)
  9194. return elfcore_grok_ppc_tm_cvsx (abfd, note);
  9195. else
  9196. return true;
  9197. case NT_PPC_TM_SPR:
  9198. if (note->namesz == 6
  9199. && strcmp (note->namedata, "LINUX") == 0)
  9200. return elfcore_grok_ppc_tm_spr (abfd, note);
  9201. else
  9202. return true;
  9203. case NT_PPC_TM_CTAR:
  9204. if (note->namesz == 6
  9205. && strcmp (note->namedata, "LINUX") == 0)
  9206. return elfcore_grok_ppc_tm_ctar (abfd, note);
  9207. else
  9208. return true;
  9209. case NT_PPC_TM_CPPR:
  9210. if (note->namesz == 6
  9211. && strcmp (note->namedata, "LINUX") == 0)
  9212. return elfcore_grok_ppc_tm_cppr (abfd, note);
  9213. else
  9214. return true;
  9215. case NT_PPC_TM_CDSCR:
  9216. if (note->namesz == 6
  9217. && strcmp (note->namedata, "LINUX") == 0)
  9218. return elfcore_grok_ppc_tm_cdscr (abfd, note);
  9219. else
  9220. return true;
  9221. case NT_S390_HIGH_GPRS:
  9222. if (note->namesz == 6
  9223. && strcmp (note->namedata, "LINUX") == 0)
  9224. return elfcore_grok_s390_high_gprs (abfd, note);
  9225. else
  9226. return true;
  9227. case NT_S390_TIMER:
  9228. if (note->namesz == 6
  9229. && strcmp (note->namedata, "LINUX") == 0)
  9230. return elfcore_grok_s390_timer (abfd, note);
  9231. else
  9232. return true;
  9233. case NT_S390_TODCMP:
  9234. if (note->namesz == 6
  9235. && strcmp (note->namedata, "LINUX") == 0)
  9236. return elfcore_grok_s390_todcmp (abfd, note);
  9237. else
  9238. return true;
  9239. case NT_S390_TODPREG:
  9240. if (note->namesz == 6
  9241. && strcmp (note->namedata, "LINUX") == 0)
  9242. return elfcore_grok_s390_todpreg (abfd, note);
  9243. else
  9244. return true;
  9245. case NT_S390_CTRS:
  9246. if (note->namesz == 6
  9247. && strcmp (note->namedata, "LINUX") == 0)
  9248. return elfcore_grok_s390_ctrs (abfd, note);
  9249. else
  9250. return true;
  9251. case NT_S390_PREFIX:
  9252. if (note->namesz == 6
  9253. && strcmp (note->namedata, "LINUX") == 0)
  9254. return elfcore_grok_s390_prefix (abfd, note);
  9255. else
  9256. return true;
  9257. case NT_S390_LAST_BREAK:
  9258. if (note->namesz == 6
  9259. && strcmp (note->namedata, "LINUX") == 0)
  9260. return elfcore_grok_s390_last_break (abfd, note);
  9261. else
  9262. return true;
  9263. case NT_S390_SYSTEM_CALL:
  9264. if (note->namesz == 6
  9265. && strcmp (note->namedata, "LINUX") == 0)
  9266. return elfcore_grok_s390_system_call (abfd, note);
  9267. else
  9268. return true;
  9269. case NT_S390_TDB:
  9270. if (note->namesz == 6
  9271. && strcmp (note->namedata, "LINUX") == 0)
  9272. return elfcore_grok_s390_tdb (abfd, note);
  9273. else
  9274. return true;
  9275. case NT_S390_VXRS_LOW:
  9276. if (note->namesz == 6
  9277. && strcmp (note->namedata, "LINUX") == 0)
  9278. return elfcore_grok_s390_vxrs_low (abfd, note);
  9279. else
  9280. return true;
  9281. case NT_S390_VXRS_HIGH:
  9282. if (note->namesz == 6
  9283. && strcmp (note->namedata, "LINUX") == 0)
  9284. return elfcore_grok_s390_vxrs_high (abfd, note);
  9285. else
  9286. return true;
  9287. case NT_S390_GS_CB:
  9288. if (note->namesz == 6
  9289. && strcmp (note->namedata, "LINUX") == 0)
  9290. return elfcore_grok_s390_gs_cb (abfd, note);
  9291. else
  9292. return true;
  9293. case NT_S390_GS_BC:
  9294. if (note->namesz == 6
  9295. && strcmp (note->namedata, "LINUX") == 0)
  9296. return elfcore_grok_s390_gs_bc (abfd, note);
  9297. else
  9298. return true;
  9299. case NT_ARC_V2:
  9300. if (note->namesz == 6
  9301. && strcmp (note->namedata, "LINUX") == 0)
  9302. return elfcore_grok_arc_v2 (abfd, note);
  9303. else
  9304. return true;
  9305. case NT_ARM_VFP:
  9306. if (note->namesz == 6
  9307. && strcmp (note->namedata, "LINUX") == 0)
  9308. return elfcore_grok_arm_vfp (abfd, note);
  9309. else
  9310. return true;
  9311. case NT_ARM_TLS:
  9312. if (note->namesz == 6
  9313. && strcmp (note->namedata, "LINUX") == 0)
  9314. return elfcore_grok_aarch_tls (abfd, note);
  9315. else
  9316. return true;
  9317. case NT_ARM_HW_BREAK:
  9318. if (note->namesz == 6
  9319. && strcmp (note->namedata, "LINUX") == 0)
  9320. return elfcore_grok_aarch_hw_break (abfd, note);
  9321. else
  9322. return true;
  9323. case NT_ARM_HW_WATCH:
  9324. if (note->namesz == 6
  9325. && strcmp (note->namedata, "LINUX") == 0)
  9326. return elfcore_grok_aarch_hw_watch (abfd, note);
  9327. else
  9328. return true;
  9329. case NT_ARM_SVE:
  9330. if (note->namesz == 6
  9331. && strcmp (note->namedata, "LINUX") == 0)
  9332. return elfcore_grok_aarch_sve (abfd, note);
  9333. else
  9334. return true;
  9335. case NT_ARM_PAC_MASK:
  9336. if (note->namesz == 6
  9337. && strcmp (note->namedata, "LINUX") == 0)
  9338. return elfcore_grok_aarch_pauth (abfd, note);
  9339. else
  9340. return true;
  9341. case NT_ARM_TAGGED_ADDR_CTRL:
  9342. if (note->namesz == 6
  9343. && strcmp (note->namedata, "LINUX") == 0)
  9344. return elfcore_grok_aarch_mte (abfd, note);
  9345. else
  9346. return true;
  9347. case NT_GDB_TDESC:
  9348. if (note->namesz == 4
  9349. && strcmp (note->namedata, "GDB") == 0)
  9350. return elfcore_grok_gdb_tdesc (abfd, note);
  9351. else
  9352. return true;
  9353. case NT_RISCV_CSR:
  9354. if (note->namesz == 4
  9355. && strcmp (note->namedata, "GDB") == 0)
  9356. return elfcore_grok_riscv_csr (abfd, note);
  9357. else
  9358. return true;
  9359. case NT_LARCH_CPUCFG:
  9360. if (note->namesz == 6
  9361. && strcmp (note->namedata, "LINUX") == 0)
  9362. return elfcore_grok_loongarch_cpucfg (abfd, note);
  9363. else
  9364. return true;
  9365. case NT_LARCH_LBT:
  9366. if (note->namesz == 6
  9367. && strcmp (note->namedata, "LINUX") == 0)
  9368. return elfcore_grok_loongarch_lbt (abfd, note);
  9369. else
  9370. return true;
  9371. case NT_LARCH_LSX:
  9372. if (note->namesz == 6
  9373. && strcmp (note->namedata, "LINUX") == 0)
  9374. return elfcore_grok_loongarch_lsx (abfd, note);
  9375. else
  9376. return true;
  9377. case NT_LARCH_LASX:
  9378. if (note->namesz == 6
  9379. && strcmp (note->namedata, "LINUX") == 0)
  9380. return elfcore_grok_loongarch_lasx (abfd, note);
  9381. else
  9382. return true;
  9383. case NT_PRPSINFO:
  9384. case NT_PSINFO:
  9385. if (bed->elf_backend_grok_psinfo)
  9386. if ((*bed->elf_backend_grok_psinfo) (abfd, note))
  9387. return true;
  9388. #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
  9389. return elfcore_grok_psinfo (abfd, note);
  9390. #else
  9391. return true;
  9392. #endif
  9393. case NT_AUXV:
  9394. return elfcore_make_auxv_note_section (abfd, note, 0);
  9395. case NT_FILE:
  9396. return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
  9397. note);
  9398. case NT_SIGINFO:
  9399. return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
  9400. note);
  9401. }
  9402. }
  9403. static bool
  9404. elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
  9405. {
  9406. struct bfd_build_id* build_id;
  9407. if (note->descsz == 0)
  9408. return false;
  9409. build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
  9410. if (build_id == NULL)
  9411. return false;
  9412. build_id->size = note->descsz;
  9413. memcpy (build_id->data, note->descdata, note->descsz);
  9414. abfd->build_id = build_id;
  9415. return true;
  9416. }
  9417. static bool
  9418. elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
  9419. {
  9420. switch (note->type)
  9421. {
  9422. default:
  9423. return true;
  9424. case NT_GNU_PROPERTY_TYPE_0:
  9425. return _bfd_elf_parse_gnu_properties (abfd, note);
  9426. case NT_GNU_BUILD_ID:
  9427. return elfobj_grok_gnu_build_id (abfd, note);
  9428. }
  9429. }
  9430. static bool
  9431. elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
  9432. {
  9433. struct sdt_note *cur =
  9434. (struct sdt_note *) bfd_alloc (abfd,
  9435. sizeof (struct sdt_note) + note->descsz);
  9436. cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
  9437. cur->size = (bfd_size_type) note->descsz;
  9438. memcpy (cur->data, note->descdata, note->descsz);
  9439. elf_tdata (abfd)->sdt_note_head = cur;
  9440. return true;
  9441. }
  9442. static bool
  9443. elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
  9444. {
  9445. switch (note->type)
  9446. {
  9447. case NT_STAPSDT:
  9448. return elfobj_grok_stapsdt_note_1 (abfd, note);
  9449. default:
  9450. return true;
  9451. }
  9452. }
  9453. static bool
  9454. elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
  9455. {
  9456. size_t offset;
  9457. switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
  9458. {
  9459. case ELFCLASS32:
  9460. if (note->descsz < 108)
  9461. return false;
  9462. break;
  9463. case ELFCLASS64:
  9464. if (note->descsz < 120)
  9465. return false;
  9466. break;
  9467. default:
  9468. return false;
  9469. }
  9470. /* Check for version 1 in pr_version. */
  9471. if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
  9472. return false;
  9473. offset = 4;
  9474. /* Skip over pr_psinfosz. */
  9475. if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
  9476. offset += 4;
  9477. else
  9478. {
  9479. offset += 4; /* Padding before pr_psinfosz. */
  9480. offset += 8;
  9481. }
  9482. /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
  9483. elf_tdata (abfd)->core->program
  9484. = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
  9485. offset += 17;
  9486. /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
  9487. elf_tdata (abfd)->core->command
  9488. = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
  9489. offset += 81;
  9490. /* Padding before pr_pid. */
  9491. offset += 2;
  9492. /* The pr_pid field was added in version "1a". */
  9493. if (note->descsz < offset + 4)
  9494. return true;
  9495. elf_tdata (abfd)->core->pid
  9496. = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
  9497. return true;
  9498. }
  9499. static bool
  9500. elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
  9501. {
  9502. size_t offset;
  9503. size_t size;
  9504. size_t min_size;
  9505. /* Compute offset of pr_getregsz, skipping over pr_statussz.
  9506. Also compute minimum size of this note. */
  9507. switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
  9508. {
  9509. case ELFCLASS32:
  9510. offset = 4 + 4;
  9511. min_size = offset + (4 * 2) + 4 + 4 + 4;
  9512. break;
  9513. case ELFCLASS64:
  9514. offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
  9515. min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
  9516. break;
  9517. default:
  9518. return false;
  9519. }
  9520. if (note->descsz < min_size)
  9521. return false;
  9522. /* Check for version 1 in pr_version. */
  9523. if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
  9524. return false;
  9525. /* Extract size of pr_reg from pr_gregsetsz. */
  9526. /* Skip over pr_gregsetsz and pr_fpregsetsz. */
  9527. if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
  9528. {
  9529. size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
  9530. offset += 4 * 2;
  9531. }
  9532. else
  9533. {
  9534. size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
  9535. offset += 8 * 2;
  9536. }
  9537. /* Skip over pr_osreldate. */
  9538. offset += 4;
  9539. /* Read signal from pr_cursig. */
  9540. if (elf_tdata (abfd)->core->signal == 0)
  9541. elf_tdata (abfd)->core->signal
  9542. = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
  9543. offset += 4;
  9544. /* Read TID from pr_pid. */
  9545. elf_tdata (abfd)->core->lwpid
  9546. = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
  9547. offset += 4;
  9548. /* Padding before pr_reg. */
  9549. if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
  9550. offset += 4;
  9551. /* Make sure that there is enough data remaining in the note. */
  9552. if ((note->descsz - offset) < size)
  9553. return false;
  9554. /* Make a ".reg/999" section and a ".reg" section. */
  9555. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  9556. size, note->descpos + offset);
  9557. }
  9558. static bool
  9559. elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
  9560. {
  9561. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  9562. switch (note->type)
  9563. {
  9564. case NT_PRSTATUS:
  9565. if (bed->elf_backend_grok_freebsd_prstatus)
  9566. if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
  9567. return true;
  9568. return elfcore_grok_freebsd_prstatus (abfd, note);
  9569. case NT_FPREGSET:
  9570. return elfcore_grok_prfpreg (abfd, note);
  9571. case NT_PRPSINFO:
  9572. return elfcore_grok_freebsd_psinfo (abfd, note);
  9573. case NT_FREEBSD_THRMISC:
  9574. return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
  9575. case NT_FREEBSD_PROCSTAT_PROC:
  9576. return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
  9577. note);
  9578. case NT_FREEBSD_PROCSTAT_FILES:
  9579. return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
  9580. note);
  9581. case NT_FREEBSD_PROCSTAT_VMMAP:
  9582. return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
  9583. note);
  9584. case NT_FREEBSD_PROCSTAT_AUXV:
  9585. return elfcore_make_auxv_note_section (abfd, note, 4);
  9586. case NT_FREEBSD_X86_SEGBASES:
  9587. return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
  9588. case NT_X86_XSTATE:
  9589. return elfcore_grok_xstatereg (abfd, note);
  9590. case NT_FREEBSD_PTLWPINFO:
  9591. return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
  9592. note);
  9593. case NT_ARM_VFP:
  9594. return elfcore_grok_arm_vfp (abfd, note);
  9595. default:
  9596. return true;
  9597. }
  9598. }
  9599. static bool
  9600. elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
  9601. {
  9602. char *cp;
  9603. cp = strchr (note->namedata, '@');
  9604. if (cp != NULL)
  9605. {
  9606. *lwpidp = atoi(cp + 1);
  9607. return true;
  9608. }
  9609. return false;
  9610. }
  9611. static bool
  9612. elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
  9613. {
  9614. if (note->descsz <= 0x7c + 31)
  9615. return false;
  9616. /* Signal number at offset 0x08. */
  9617. elf_tdata (abfd)->core->signal
  9618. = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
  9619. /* Process ID at offset 0x50. */
  9620. elf_tdata (abfd)->core->pid
  9621. = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
  9622. /* Command name at 0x7c (max 32 bytes, including nul). */
  9623. elf_tdata (abfd)->core->command
  9624. = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
  9625. return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
  9626. note);
  9627. }
  9628. static bool
  9629. elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
  9630. {
  9631. int lwp;
  9632. if (elfcore_netbsd_get_lwpid (note, &lwp))
  9633. elf_tdata (abfd)->core->lwpid = lwp;
  9634. switch (note->type)
  9635. {
  9636. case NT_NETBSDCORE_PROCINFO:
  9637. /* NetBSD-specific core "procinfo". Note that we expect to
  9638. find this note before any of the others, which is fine,
  9639. since the kernel writes this note out first when it
  9640. creates a core file. */
  9641. return elfcore_grok_netbsd_procinfo (abfd, note);
  9642. case NT_NETBSDCORE_AUXV:
  9643. /* NetBSD-specific Elf Auxiliary Vector data. */
  9644. return elfcore_make_auxv_note_section (abfd, note, 4);
  9645. case NT_NETBSDCORE_LWPSTATUS:
  9646. return elfcore_make_note_pseudosection (abfd,
  9647. ".note.netbsdcore.lwpstatus",
  9648. note);
  9649. default:
  9650. break;
  9651. }
  9652. /* As of March 2020 there are no other machine-independent notes
  9653. defined for NetBSD core files. If the note type is less
  9654. than the start of the machine-dependent note types, we don't
  9655. understand it. */
  9656. if (note->type < NT_NETBSDCORE_FIRSTMACH)
  9657. return true;
  9658. switch (bfd_get_arch (abfd))
  9659. {
  9660. /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
  9661. PT_GETFPREGS == mach+2. */
  9662. case bfd_arch_aarch64:
  9663. case bfd_arch_alpha:
  9664. case bfd_arch_sparc:
  9665. switch (note->type)
  9666. {
  9667. case NT_NETBSDCORE_FIRSTMACH+0:
  9668. return elfcore_make_note_pseudosection (abfd, ".reg", note);
  9669. case NT_NETBSDCORE_FIRSTMACH+2:
  9670. return elfcore_make_note_pseudosection (abfd, ".reg2", note);
  9671. default:
  9672. return true;
  9673. }
  9674. /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
  9675. There's also old PT___GETREGS40 == mach + 1 for old reg
  9676. structure which lacks GBR. */
  9677. case bfd_arch_sh:
  9678. switch (note->type)
  9679. {
  9680. case NT_NETBSDCORE_FIRSTMACH+3:
  9681. return elfcore_make_note_pseudosection (abfd, ".reg", note);
  9682. case NT_NETBSDCORE_FIRSTMACH+5:
  9683. return elfcore_make_note_pseudosection (abfd, ".reg2", note);
  9684. default:
  9685. return true;
  9686. }
  9687. /* On all other arch's, PT_GETREGS == mach+1 and
  9688. PT_GETFPREGS == mach+3. */
  9689. default:
  9690. switch (note->type)
  9691. {
  9692. case NT_NETBSDCORE_FIRSTMACH+1:
  9693. return elfcore_make_note_pseudosection (abfd, ".reg", note);
  9694. case NT_NETBSDCORE_FIRSTMACH+3:
  9695. return elfcore_make_note_pseudosection (abfd, ".reg2", note);
  9696. default:
  9697. return true;
  9698. }
  9699. }
  9700. /* NOTREACHED */
  9701. }
  9702. static bool
  9703. elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
  9704. {
  9705. if (note->descsz <= 0x48 + 31)
  9706. return false;
  9707. /* Signal number at offset 0x08. */
  9708. elf_tdata (abfd)->core->signal
  9709. = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
  9710. /* Process ID at offset 0x20. */
  9711. elf_tdata (abfd)->core->pid
  9712. = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
  9713. /* Command name at 0x48 (max 32 bytes, including nul). */
  9714. elf_tdata (abfd)->core->command
  9715. = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
  9716. return true;
  9717. }
  9718. /* Processes Solaris's process status note.
  9719. sig_off ~ offsetof(prstatus_t, pr_cursig)
  9720. pid_off ~ offsetof(prstatus_t, pr_pid)
  9721. lwpid_off ~ offsetof(prstatus_t, pr_who)
  9722. gregset_size ~ sizeof(gregset_t)
  9723. gregset_offset ~ offsetof(prstatus_t, pr_reg) */
  9724. static bool
  9725. elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
  9726. int pid_off, int lwpid_off, size_t gregset_size,
  9727. size_t gregset_offset)
  9728. {
  9729. asection *sect = NULL;
  9730. elf_tdata (abfd)->core->signal
  9731. = bfd_get_16 (abfd, note->descdata + sig_off);
  9732. elf_tdata (abfd)->core->pid
  9733. = bfd_get_32 (abfd, note->descdata + pid_off);
  9734. elf_tdata (abfd)->core->lwpid
  9735. = bfd_get_32 (abfd, note->descdata + lwpid_off);
  9736. sect = bfd_get_section_by_name (abfd, ".reg");
  9737. if (sect != NULL)
  9738. sect->size = gregset_size;
  9739. return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
  9740. note->descpos + gregset_offset);
  9741. }
  9742. /* Gets program and arguments from a core.
  9743. prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
  9744. comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
  9745. static bool
  9746. elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
  9747. int prog_off, int comm_off)
  9748. {
  9749. elf_tdata (abfd)->core->program
  9750. = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
  9751. elf_tdata (abfd)->core->command
  9752. = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
  9753. return true;
  9754. }
  9755. /* Processes Solaris's LWP status note.
  9756. gregset_size ~ sizeof(gregset_t)
  9757. gregset_off ~ offsetof(lwpstatus_t, pr_reg)
  9758. fpregset_size ~ sizeof(fpregset_t)
  9759. fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
  9760. static bool
  9761. elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
  9762. size_t gregset_size, int gregset_off,
  9763. size_t fpregset_size, int fpregset_off)
  9764. {
  9765. asection *sect = NULL;
  9766. char reg2_section_name[16] = { 0 };
  9767. (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
  9768. elf_tdata (abfd)->core->lwpid);
  9769. /* offsetof(lwpstatus_t, pr_lwpid) */
  9770. elf_tdata (abfd)->core->lwpid
  9771. = bfd_get_32 (abfd, note->descdata + 4);
  9772. /* offsetof(lwpstatus_t, pr_cursig) */
  9773. elf_tdata (abfd)->core->signal
  9774. = bfd_get_16 (abfd, note->descdata + 12);
  9775. sect = bfd_get_section_by_name (abfd, ".reg");
  9776. if (sect != NULL)
  9777. sect->size = gregset_size;
  9778. else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
  9779. note->descpos + gregset_off))
  9780. return false;
  9781. sect = bfd_get_section_by_name (abfd, reg2_section_name);
  9782. if (sect != NULL)
  9783. {
  9784. sect->size = fpregset_size;
  9785. sect->filepos = note->descpos + fpregset_off;
  9786. sect->alignment_power = 2;
  9787. }
  9788. else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
  9789. note->descpos + fpregset_off))
  9790. return false;
  9791. return true;
  9792. }
  9793. static bool
  9794. elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
  9795. {
  9796. if (note == NULL)
  9797. return false;
  9798. /* core files are identified as 32- or 64-bit, SPARC or x86,
  9799. by the size of the descsz which matches the sizeof()
  9800. the type appropriate for that note type (e.g., prstatus_t for
  9801. SOLARIS_NT_PRSTATUS) for the corresponding architecture
  9802. on Solaris. The core file bitness may differ from the bitness of
  9803. gdb itself, so fixed values are used instead of sizeof().
  9804. Appropriate fixed offsets are also used to obtain data from
  9805. the note. */
  9806. switch ((int) note->type)
  9807. {
  9808. case SOLARIS_NT_PRSTATUS:
  9809. switch (note->descsz)
  9810. {
  9811. case 508: /* sizeof(prstatus_t) SPARC 32-bit */
  9812. return elfcore_grok_solaris_prstatus(abfd, note,
  9813. 136, 216, 308, 152, 356);
  9814. case 904: /* sizeof(prstatus_t) SPARC 64-bit */
  9815. return elfcore_grok_solaris_prstatus(abfd, note,
  9816. 264, 360, 520, 304, 600);
  9817. case 432: /* sizeof(prstatus_t) Intel 32-bit */
  9818. return elfcore_grok_solaris_prstatus(abfd, note,
  9819. 136, 216, 308, 76, 356);
  9820. case 824: /* sizeof(prstatus_t) Intel 64-bit */
  9821. return elfcore_grok_solaris_prstatus(abfd, note,
  9822. 264, 360, 520, 224, 600);
  9823. default:
  9824. return true;
  9825. }
  9826. case SOLARIS_NT_PSINFO:
  9827. case SOLARIS_NT_PRPSINFO:
  9828. switch (note->descsz)
  9829. {
  9830. case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
  9831. return elfcore_grok_solaris_info(abfd, note, 84, 100);
  9832. case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
  9833. return elfcore_grok_solaris_info(abfd, note, 120, 136);
  9834. case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
  9835. return elfcore_grok_solaris_info(abfd, note, 88, 104);
  9836. case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
  9837. return elfcore_grok_solaris_info(abfd, note, 136, 152);
  9838. default:
  9839. return true;
  9840. }
  9841. case SOLARIS_NT_LWPSTATUS:
  9842. switch (note->descsz)
  9843. {
  9844. case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
  9845. return elfcore_grok_solaris_lwpstatus(abfd, note,
  9846. 152, 344, 400, 496);
  9847. case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
  9848. return elfcore_grok_solaris_lwpstatus(abfd, note,
  9849. 304, 544, 544, 848);
  9850. case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
  9851. return elfcore_grok_solaris_lwpstatus(abfd, note,
  9852. 76, 344, 380, 420);
  9853. case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
  9854. return elfcore_grok_solaris_lwpstatus(abfd, note,
  9855. 224, 544, 528, 768);
  9856. default:
  9857. return true;
  9858. }
  9859. case SOLARIS_NT_LWPSINFO:
  9860. /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
  9861. if (note->descsz == 128 || note->descsz == 152)
  9862. elf_tdata (abfd)->core->lwpid =
  9863. bfd_get_32 (abfd, note->descdata + 4);
  9864. break;
  9865. default:
  9866. break;
  9867. }
  9868. return true;
  9869. }
  9870. /* For name starting with "CORE" this may be either a Solaris
  9871. core file or a gdb-generated core file. Do Solaris-specific
  9872. processing on selected note types first with
  9873. elfcore_grok_solaris_note(), then process the note
  9874. in elfcore_grok_note(). */
  9875. static bool
  9876. elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
  9877. {
  9878. if (!elfcore_grok_solaris_note_impl (abfd, note))
  9879. return false;
  9880. return elfcore_grok_note (abfd, note);
  9881. }
  9882. static bool
  9883. elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
  9884. {
  9885. if (note->type == NT_OPENBSD_PROCINFO)
  9886. return elfcore_grok_openbsd_procinfo (abfd, note);
  9887. if (note->type == NT_OPENBSD_REGS)
  9888. return elfcore_make_note_pseudosection (abfd, ".reg", note);
  9889. if (note->type == NT_OPENBSD_FPREGS)
  9890. return elfcore_make_note_pseudosection (abfd, ".reg2", note);
  9891. if (note->type == NT_OPENBSD_XFPREGS)
  9892. return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
  9893. if (note->type == NT_OPENBSD_AUXV)
  9894. return elfcore_make_auxv_note_section (abfd, note, 0);
  9895. if (note->type == NT_OPENBSD_WCOOKIE)
  9896. {
  9897. asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
  9898. SEC_HAS_CONTENTS);
  9899. if (sect == NULL)
  9900. return false;
  9901. sect->size = note->descsz;
  9902. sect->filepos = note->descpos;
  9903. sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
  9904. return true;
  9905. }
  9906. return true;
  9907. }
  9908. static bool
  9909. elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
  9910. {
  9911. void *ddata = note->descdata;
  9912. char buf[100];
  9913. char *name;
  9914. asection *sect;
  9915. short sig;
  9916. unsigned flags;
  9917. if (note->descsz < 16)
  9918. return false;
  9919. /* nto_procfs_status 'pid' field is at offset 0. */
  9920. elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
  9921. /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
  9922. *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
  9923. /* nto_procfs_status 'flags' field is at offset 8. */
  9924. flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
  9925. /* nto_procfs_status 'what' field is at offset 14. */
  9926. if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
  9927. {
  9928. elf_tdata (abfd)->core->signal = sig;
  9929. elf_tdata (abfd)->core->lwpid = *tid;
  9930. }
  9931. /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
  9932. do not come from signals so we make sure we set the current
  9933. thread just in case. */
  9934. if (flags & 0x00000080)
  9935. elf_tdata (abfd)->core->lwpid = *tid;
  9936. /* Make a ".qnx_core_status/%d" section. */
  9937. sprintf (buf, ".qnx_core_status/%ld", *tid);
  9938. name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
  9939. if (name == NULL)
  9940. return false;
  9941. strcpy (name, buf);
  9942. sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
  9943. if (sect == NULL)
  9944. return false;
  9945. sect->size = note->descsz;
  9946. sect->filepos = note->descpos;
  9947. sect->alignment_power = 2;
  9948. return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
  9949. }
  9950. static bool
  9951. elfcore_grok_nto_regs (bfd *abfd,
  9952. Elf_Internal_Note *note,
  9953. long tid,
  9954. char *base)
  9955. {
  9956. char buf[100];
  9957. char *name;
  9958. asection *sect;
  9959. /* Make a "(base)/%d" section. */
  9960. sprintf (buf, "%s/%ld", base, tid);
  9961. name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
  9962. if (name == NULL)
  9963. return false;
  9964. strcpy (name, buf);
  9965. sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
  9966. if (sect == NULL)
  9967. return false;
  9968. sect->size = note->descsz;
  9969. sect->filepos = note->descpos;
  9970. sect->alignment_power = 2;
  9971. /* This is the current thread. */
  9972. if (elf_tdata (abfd)->core->lwpid == tid)
  9973. return elfcore_maybe_make_sect (abfd, base, sect);
  9974. return true;
  9975. }
  9976. #define BFD_QNT_CORE_INFO 7
  9977. #define BFD_QNT_CORE_STATUS 8
  9978. #define BFD_QNT_CORE_GREG 9
  9979. #define BFD_QNT_CORE_FPREG 10
  9980. static bool
  9981. elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
  9982. {
  9983. /* Every GREG section has a STATUS section before it. Store the
  9984. tid from the previous call to pass down to the next gregs
  9985. function. */
  9986. static long tid = 1;
  9987. switch (note->type)
  9988. {
  9989. case BFD_QNT_CORE_INFO:
  9990. return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
  9991. case BFD_QNT_CORE_STATUS:
  9992. return elfcore_grok_nto_status (abfd, note, &tid);
  9993. case BFD_QNT_CORE_GREG:
  9994. return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
  9995. case BFD_QNT_CORE_FPREG:
  9996. return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
  9997. default:
  9998. return true;
  9999. }
  10000. }
  10001. static bool
  10002. elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
  10003. {
  10004. char *name;
  10005. asection *sect;
  10006. size_t len;
  10007. /* Use note name as section name. */
  10008. len = note->namesz;
  10009. name = (char *) bfd_alloc (abfd, len);
  10010. if (name == NULL)
  10011. return false;
  10012. memcpy (name, note->namedata, len);
  10013. name[len - 1] = '\0';
  10014. sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
  10015. if (sect == NULL)
  10016. return false;
  10017. sect->size = note->descsz;
  10018. sect->filepos = note->descpos;
  10019. sect->alignment_power = 1;
  10020. return true;
  10021. }
  10022. /* Function: elfcore_write_note
  10023. Inputs:
  10024. buffer to hold note, and current size of buffer
  10025. name of note
  10026. type of note
  10027. data for note
  10028. size of data for note
  10029. Writes note to end of buffer. ELF64 notes are written exactly as
  10030. for ELF32, despite the current (as of 2006) ELF gabi specifying
  10031. that they ought to have 8-byte namesz and descsz field, and have
  10032. 8-byte alignment. Other writers, eg. Linux kernel, do the same.
  10033. Return:
  10034. Pointer to realloc'd buffer, *BUFSIZ updated. */
  10035. char *
  10036. elfcore_write_note (bfd *abfd,
  10037. char *buf,
  10038. int *bufsiz,
  10039. const char *name,
  10040. int type,
  10041. const void *input,
  10042. int size)
  10043. {
  10044. Elf_External_Note *xnp;
  10045. size_t namesz;
  10046. size_t newspace;
  10047. char *dest;
  10048. namesz = 0;
  10049. if (name != NULL)
  10050. namesz = strlen (name) + 1;
  10051. newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
  10052. buf = (char *) realloc (buf, *bufsiz + newspace);
  10053. if (buf == NULL)
  10054. return buf;
  10055. dest = buf + *bufsiz;
  10056. *bufsiz += newspace;
  10057. xnp = (Elf_External_Note *) dest;
  10058. H_PUT_32 (abfd, namesz, xnp->namesz);
  10059. H_PUT_32 (abfd, size, xnp->descsz);
  10060. H_PUT_32 (abfd, type, xnp->type);
  10061. dest = xnp->name;
  10062. if (name != NULL)
  10063. {
  10064. memcpy (dest, name, namesz);
  10065. dest += namesz;
  10066. while (namesz & 3)
  10067. {
  10068. *dest++ = '\0';
  10069. ++namesz;
  10070. }
  10071. }
  10072. memcpy (dest, input, size);
  10073. dest += size;
  10074. while (size & 3)
  10075. {
  10076. *dest++ = '\0';
  10077. ++size;
  10078. }
  10079. return buf;
  10080. }
  10081. /* gcc-8 warns (*) on all the strncpy calls in this function about
  10082. possible string truncation. The "truncation" is not a bug. We
  10083. have an external representation of structs with fields that are not
  10084. necessarily NULL terminated and corresponding internal
  10085. representation fields that are one larger so that they can always
  10086. be NULL terminated.
  10087. gcc versions between 4.2 and 4.6 do not allow pragma control of
  10088. diagnostics inside functions, giving a hard error if you try to use
  10089. the finer control available with later versions.
  10090. gcc prior to 4.2 warns about diagnostic push and pop.
  10091. gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
  10092. unless you also add #pragma GCC diagnostic ignored "-Wpragma".
  10093. (*) Depending on your system header files! */
  10094. #if GCC_VERSION >= 8000
  10095. # pragma GCC diagnostic push
  10096. # pragma GCC diagnostic ignored "-Wstringop-truncation"
  10097. #endif
  10098. char *
  10099. elfcore_write_prpsinfo (bfd *abfd,
  10100. char *buf,
  10101. int *bufsiz,
  10102. const char *fname,
  10103. const char *psargs)
  10104. {
  10105. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  10106. if (bed->elf_backend_write_core_note != NULL)
  10107. {
  10108. char *ret;
  10109. ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
  10110. NT_PRPSINFO, fname, psargs);
  10111. if (ret != NULL)
  10112. return ret;
  10113. }
  10114. #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
  10115. # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
  10116. if (bed->s->elfclass == ELFCLASS32)
  10117. {
  10118. # if defined (HAVE_PSINFO32_T)
  10119. psinfo32_t data;
  10120. int note_type = NT_PSINFO;
  10121. # else
  10122. prpsinfo32_t data;
  10123. int note_type = NT_PRPSINFO;
  10124. # endif
  10125. memset (&data, 0, sizeof (data));
  10126. strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
  10127. strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
  10128. return elfcore_write_note (abfd, buf, bufsiz,
  10129. "CORE", note_type, &data, sizeof (data));
  10130. }
  10131. else
  10132. # endif
  10133. {
  10134. # if defined (HAVE_PSINFO_T)
  10135. psinfo_t data;
  10136. int note_type = NT_PSINFO;
  10137. # else
  10138. prpsinfo_t data;
  10139. int note_type = NT_PRPSINFO;
  10140. # endif
  10141. memset (&data, 0, sizeof (data));
  10142. strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
  10143. strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
  10144. return elfcore_write_note (abfd, buf, bufsiz,
  10145. "CORE", note_type, &data, sizeof (data));
  10146. }
  10147. #endif /* PSINFO_T or PRPSINFO_T */
  10148. free (buf);
  10149. return NULL;
  10150. }
  10151. #if GCC_VERSION >= 8000
  10152. # pragma GCC diagnostic pop
  10153. #endif
  10154. char *
  10155. elfcore_write_linux_prpsinfo32
  10156. (bfd *abfd, char *buf, int *bufsiz,
  10157. const struct elf_internal_linux_prpsinfo *prpsinfo)
  10158. {
  10159. if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
  10160. {
  10161. struct elf_external_linux_prpsinfo32_ugid16 data;
  10162. swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
  10163. return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
  10164. &data, sizeof (data));
  10165. }
  10166. else
  10167. {
  10168. struct elf_external_linux_prpsinfo32_ugid32 data;
  10169. swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
  10170. return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
  10171. &data, sizeof (data));
  10172. }
  10173. }
  10174. char *
  10175. elfcore_write_linux_prpsinfo64
  10176. (bfd *abfd, char *buf, int *bufsiz,
  10177. const struct elf_internal_linux_prpsinfo *prpsinfo)
  10178. {
  10179. if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
  10180. {
  10181. struct elf_external_linux_prpsinfo64_ugid16 data;
  10182. swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
  10183. return elfcore_write_note (abfd, buf, bufsiz,
  10184. "CORE", NT_PRPSINFO, &data, sizeof (data));
  10185. }
  10186. else
  10187. {
  10188. struct elf_external_linux_prpsinfo64_ugid32 data;
  10189. swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
  10190. return elfcore_write_note (abfd, buf, bufsiz,
  10191. "CORE", NT_PRPSINFO, &data, sizeof (data));
  10192. }
  10193. }
  10194. char *
  10195. elfcore_write_prstatus (bfd *abfd,
  10196. char *buf,
  10197. int *bufsiz,
  10198. long pid,
  10199. int cursig,
  10200. const void *gregs)
  10201. {
  10202. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  10203. if (bed->elf_backend_write_core_note != NULL)
  10204. {
  10205. char *ret;
  10206. ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
  10207. NT_PRSTATUS,
  10208. pid, cursig, gregs);
  10209. if (ret != NULL)
  10210. return ret;
  10211. }
  10212. #if defined (HAVE_PRSTATUS_T)
  10213. #if defined (HAVE_PRSTATUS32_T)
  10214. if (bed->s->elfclass == ELFCLASS32)
  10215. {
  10216. prstatus32_t prstat;
  10217. memset (&prstat, 0, sizeof (prstat));
  10218. prstat.pr_pid = pid;
  10219. prstat.pr_cursig = cursig;
  10220. memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
  10221. return elfcore_write_note (abfd, buf, bufsiz, "CORE",
  10222. NT_PRSTATUS, &prstat, sizeof (prstat));
  10223. }
  10224. else
  10225. #endif
  10226. {
  10227. prstatus_t prstat;
  10228. memset (&prstat, 0, sizeof (prstat));
  10229. prstat.pr_pid = pid;
  10230. prstat.pr_cursig = cursig;
  10231. memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
  10232. return elfcore_write_note (abfd, buf, bufsiz, "CORE",
  10233. NT_PRSTATUS, &prstat, sizeof (prstat));
  10234. }
  10235. #endif /* HAVE_PRSTATUS_T */
  10236. free (buf);
  10237. return NULL;
  10238. }
  10239. #if defined (HAVE_LWPSTATUS_T)
  10240. char *
  10241. elfcore_write_lwpstatus (bfd *abfd,
  10242. char *buf,
  10243. int *bufsiz,
  10244. long pid,
  10245. int cursig,
  10246. const void *gregs)
  10247. {
  10248. lwpstatus_t lwpstat;
  10249. const char *note_name = "CORE";
  10250. memset (&lwpstat, 0, sizeof (lwpstat));
  10251. lwpstat.pr_lwpid = pid >> 16;
  10252. lwpstat.pr_cursig = cursig;
  10253. #if defined (HAVE_LWPSTATUS_T_PR_REG)
  10254. memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
  10255. #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
  10256. #if !defined(gregs)
  10257. memcpy (lwpstat.pr_context.uc_mcontext.gregs,
  10258. gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
  10259. #else
  10260. memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
  10261. gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
  10262. #endif
  10263. #endif
  10264. return elfcore_write_note (abfd, buf, bufsiz, note_name,
  10265. NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
  10266. }
  10267. #endif /* HAVE_LWPSTATUS_T */
  10268. #if defined (HAVE_PSTATUS_T)
  10269. char *
  10270. elfcore_write_pstatus (bfd *abfd,
  10271. char *buf,
  10272. int *bufsiz,
  10273. long pid,
  10274. int cursig ATTRIBUTE_UNUSED,
  10275. const void *gregs ATTRIBUTE_UNUSED)
  10276. {
  10277. const char *note_name = "CORE";
  10278. #if defined (HAVE_PSTATUS32_T)
  10279. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  10280. if (bed->s->elfclass == ELFCLASS32)
  10281. {
  10282. pstatus32_t pstat;
  10283. memset (&pstat, 0, sizeof (pstat));
  10284. pstat.pr_pid = pid & 0xffff;
  10285. buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
  10286. NT_PSTATUS, &pstat, sizeof (pstat));
  10287. return buf;
  10288. }
  10289. else
  10290. #endif
  10291. {
  10292. pstatus_t pstat;
  10293. memset (&pstat, 0, sizeof (pstat));
  10294. pstat.pr_pid = pid & 0xffff;
  10295. buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
  10296. NT_PSTATUS, &pstat, sizeof (pstat));
  10297. return buf;
  10298. }
  10299. }
  10300. #endif /* HAVE_PSTATUS_T */
  10301. char *
  10302. elfcore_write_prfpreg (bfd *abfd,
  10303. char *buf,
  10304. int *bufsiz,
  10305. const void *fpregs,
  10306. int size)
  10307. {
  10308. const char *note_name = "CORE";
  10309. return elfcore_write_note (abfd, buf, bufsiz,
  10310. note_name, NT_FPREGSET, fpregs, size);
  10311. }
  10312. char *
  10313. elfcore_write_prxfpreg (bfd *abfd,
  10314. char *buf,
  10315. int *bufsiz,
  10316. const void *xfpregs,
  10317. int size)
  10318. {
  10319. char *note_name = "LINUX";
  10320. return elfcore_write_note (abfd, buf, bufsiz,
  10321. note_name, NT_PRXFPREG, xfpregs, size);
  10322. }
  10323. char *
  10324. elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
  10325. const void *xfpregs, int size)
  10326. {
  10327. char *note_name;
  10328. if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
  10329. note_name = "FreeBSD";
  10330. else
  10331. note_name = "LINUX";
  10332. return elfcore_write_note (abfd, buf, bufsiz,
  10333. note_name, NT_X86_XSTATE, xfpregs, size);
  10334. }
  10335. char *
  10336. elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
  10337. const void *regs, int size)
  10338. {
  10339. char *note_name = "FreeBSD";
  10340. return elfcore_write_note (abfd, buf, bufsiz,
  10341. note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
  10342. }
  10343. char *
  10344. elfcore_write_ppc_vmx (bfd *abfd,
  10345. char *buf,
  10346. int *bufsiz,
  10347. const void *ppc_vmx,
  10348. int size)
  10349. {
  10350. char *note_name = "LINUX";
  10351. return elfcore_write_note (abfd, buf, bufsiz,
  10352. note_name, NT_PPC_VMX, ppc_vmx, size);
  10353. }
  10354. char *
  10355. elfcore_write_ppc_vsx (bfd *abfd,
  10356. char *buf,
  10357. int *bufsiz,
  10358. const void *ppc_vsx,
  10359. int size)
  10360. {
  10361. char *note_name = "LINUX";
  10362. return elfcore_write_note (abfd, buf, bufsiz,
  10363. note_name, NT_PPC_VSX, ppc_vsx, size);
  10364. }
  10365. char *
  10366. elfcore_write_ppc_tar (bfd *abfd,
  10367. char *buf,
  10368. int *bufsiz,
  10369. const void *ppc_tar,
  10370. int size)
  10371. {
  10372. char *note_name = "LINUX";
  10373. return elfcore_write_note (abfd, buf, bufsiz,
  10374. note_name, NT_PPC_TAR, ppc_tar, size);
  10375. }
  10376. char *
  10377. elfcore_write_ppc_ppr (bfd *abfd,
  10378. char *buf,
  10379. int *bufsiz,
  10380. const void *ppc_ppr,
  10381. int size)
  10382. {
  10383. char *note_name = "LINUX";
  10384. return elfcore_write_note (abfd, buf, bufsiz,
  10385. note_name, NT_PPC_PPR, ppc_ppr, size);
  10386. }
  10387. char *
  10388. elfcore_write_ppc_dscr (bfd *abfd,
  10389. char *buf,
  10390. int *bufsiz,
  10391. const void *ppc_dscr,
  10392. int size)
  10393. {
  10394. char *note_name = "LINUX";
  10395. return elfcore_write_note (abfd, buf, bufsiz,
  10396. note_name, NT_PPC_DSCR, ppc_dscr, size);
  10397. }
  10398. char *
  10399. elfcore_write_ppc_ebb (bfd *abfd,
  10400. char *buf,
  10401. int *bufsiz,
  10402. const void *ppc_ebb,
  10403. int size)
  10404. {
  10405. char *note_name = "LINUX";
  10406. return elfcore_write_note (abfd, buf, bufsiz,
  10407. note_name, NT_PPC_EBB, ppc_ebb, size);
  10408. }
  10409. char *
  10410. elfcore_write_ppc_pmu (bfd *abfd,
  10411. char *buf,
  10412. int *bufsiz,
  10413. const void *ppc_pmu,
  10414. int size)
  10415. {
  10416. char *note_name = "LINUX";
  10417. return elfcore_write_note (abfd, buf, bufsiz,
  10418. note_name, NT_PPC_PMU, ppc_pmu, size);
  10419. }
  10420. char *
  10421. elfcore_write_ppc_tm_cgpr (bfd *abfd,
  10422. char *buf,
  10423. int *bufsiz,
  10424. const void *ppc_tm_cgpr,
  10425. int size)
  10426. {
  10427. char *note_name = "LINUX";
  10428. return elfcore_write_note (abfd, buf, bufsiz,
  10429. note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
  10430. }
  10431. char *
  10432. elfcore_write_ppc_tm_cfpr (bfd *abfd,
  10433. char *buf,
  10434. int *bufsiz,
  10435. const void *ppc_tm_cfpr,
  10436. int size)
  10437. {
  10438. char *note_name = "LINUX";
  10439. return elfcore_write_note (abfd, buf, bufsiz,
  10440. note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
  10441. }
  10442. char *
  10443. elfcore_write_ppc_tm_cvmx (bfd *abfd,
  10444. char *buf,
  10445. int *bufsiz,
  10446. const void *ppc_tm_cvmx,
  10447. int size)
  10448. {
  10449. char *note_name = "LINUX";
  10450. return elfcore_write_note (abfd, buf, bufsiz,
  10451. note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
  10452. }
  10453. char *
  10454. elfcore_write_ppc_tm_cvsx (bfd *abfd,
  10455. char *buf,
  10456. int *bufsiz,
  10457. const void *ppc_tm_cvsx,
  10458. int size)
  10459. {
  10460. char *note_name = "LINUX";
  10461. return elfcore_write_note (abfd, buf, bufsiz,
  10462. note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
  10463. }
  10464. char *
  10465. elfcore_write_ppc_tm_spr (bfd *abfd,
  10466. char *buf,
  10467. int *bufsiz,
  10468. const void *ppc_tm_spr,
  10469. int size)
  10470. {
  10471. char *note_name = "LINUX";
  10472. return elfcore_write_note (abfd, buf, bufsiz,
  10473. note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
  10474. }
  10475. char *
  10476. elfcore_write_ppc_tm_ctar (bfd *abfd,
  10477. char *buf,
  10478. int *bufsiz,
  10479. const void *ppc_tm_ctar,
  10480. int size)
  10481. {
  10482. char *note_name = "LINUX";
  10483. return elfcore_write_note (abfd, buf, bufsiz,
  10484. note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
  10485. }
  10486. char *
  10487. elfcore_write_ppc_tm_cppr (bfd *abfd,
  10488. char *buf,
  10489. int *bufsiz,
  10490. const void *ppc_tm_cppr,
  10491. int size)
  10492. {
  10493. char *note_name = "LINUX";
  10494. return elfcore_write_note (abfd, buf, bufsiz,
  10495. note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
  10496. }
  10497. char *
  10498. elfcore_write_ppc_tm_cdscr (bfd *abfd,
  10499. char *buf,
  10500. int *bufsiz,
  10501. const void *ppc_tm_cdscr,
  10502. int size)
  10503. {
  10504. char *note_name = "LINUX";
  10505. return elfcore_write_note (abfd, buf, bufsiz,
  10506. note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
  10507. }
  10508. static char *
  10509. elfcore_write_s390_high_gprs (bfd *abfd,
  10510. char *buf,
  10511. int *bufsiz,
  10512. const void *s390_high_gprs,
  10513. int size)
  10514. {
  10515. char *note_name = "LINUX";
  10516. return elfcore_write_note (abfd, buf, bufsiz,
  10517. note_name, NT_S390_HIGH_GPRS,
  10518. s390_high_gprs, size);
  10519. }
  10520. char *
  10521. elfcore_write_s390_timer (bfd *abfd,
  10522. char *buf,
  10523. int *bufsiz,
  10524. const void *s390_timer,
  10525. int size)
  10526. {
  10527. char *note_name = "LINUX";
  10528. return elfcore_write_note (abfd, buf, bufsiz,
  10529. note_name, NT_S390_TIMER, s390_timer, size);
  10530. }
  10531. char *
  10532. elfcore_write_s390_todcmp (bfd *abfd,
  10533. char *buf,
  10534. int *bufsiz,
  10535. const void *s390_todcmp,
  10536. int size)
  10537. {
  10538. char *note_name = "LINUX";
  10539. return elfcore_write_note (abfd, buf, bufsiz,
  10540. note_name, NT_S390_TODCMP, s390_todcmp, size);
  10541. }
  10542. char *
  10543. elfcore_write_s390_todpreg (bfd *abfd,
  10544. char *buf,
  10545. int *bufsiz,
  10546. const void *s390_todpreg,
  10547. int size)
  10548. {
  10549. char *note_name = "LINUX";
  10550. return elfcore_write_note (abfd, buf, bufsiz,
  10551. note_name, NT_S390_TODPREG, s390_todpreg, size);
  10552. }
  10553. char *
  10554. elfcore_write_s390_ctrs (bfd *abfd,
  10555. char *buf,
  10556. int *bufsiz,
  10557. const void *s390_ctrs,
  10558. int size)
  10559. {
  10560. char *note_name = "LINUX";
  10561. return elfcore_write_note (abfd, buf, bufsiz,
  10562. note_name, NT_S390_CTRS, s390_ctrs, size);
  10563. }
  10564. char *
  10565. elfcore_write_s390_prefix (bfd *abfd,
  10566. char *buf,
  10567. int *bufsiz,
  10568. const void *s390_prefix,
  10569. int size)
  10570. {
  10571. char *note_name = "LINUX";
  10572. return elfcore_write_note (abfd, buf, bufsiz,
  10573. note_name, NT_S390_PREFIX, s390_prefix, size);
  10574. }
  10575. char *
  10576. elfcore_write_s390_last_break (bfd *abfd,
  10577. char *buf,
  10578. int *bufsiz,
  10579. const void *s390_last_break,
  10580. int size)
  10581. {
  10582. char *note_name = "LINUX";
  10583. return elfcore_write_note (abfd, buf, bufsiz,
  10584. note_name, NT_S390_LAST_BREAK,
  10585. s390_last_break, size);
  10586. }
  10587. char *
  10588. elfcore_write_s390_system_call (bfd *abfd,
  10589. char *buf,
  10590. int *bufsiz,
  10591. const void *s390_system_call,
  10592. int size)
  10593. {
  10594. char *note_name = "LINUX";
  10595. return elfcore_write_note (abfd, buf, bufsiz,
  10596. note_name, NT_S390_SYSTEM_CALL,
  10597. s390_system_call, size);
  10598. }
  10599. char *
  10600. elfcore_write_s390_tdb (bfd *abfd,
  10601. char *buf,
  10602. int *bufsiz,
  10603. const void *s390_tdb,
  10604. int size)
  10605. {
  10606. char *note_name = "LINUX";
  10607. return elfcore_write_note (abfd, buf, bufsiz,
  10608. note_name, NT_S390_TDB, s390_tdb, size);
  10609. }
  10610. char *
  10611. elfcore_write_s390_vxrs_low (bfd *abfd,
  10612. char *buf,
  10613. int *bufsiz,
  10614. const void *s390_vxrs_low,
  10615. int size)
  10616. {
  10617. char *note_name = "LINUX";
  10618. return elfcore_write_note (abfd, buf, bufsiz,
  10619. note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
  10620. }
  10621. char *
  10622. elfcore_write_s390_vxrs_high (bfd *abfd,
  10623. char *buf,
  10624. int *bufsiz,
  10625. const void *s390_vxrs_high,
  10626. int size)
  10627. {
  10628. char *note_name = "LINUX";
  10629. return elfcore_write_note (abfd, buf, bufsiz,
  10630. note_name, NT_S390_VXRS_HIGH,
  10631. s390_vxrs_high, size);
  10632. }
  10633. char *
  10634. elfcore_write_s390_gs_cb (bfd *abfd,
  10635. char *buf,
  10636. int *bufsiz,
  10637. const void *s390_gs_cb,
  10638. int size)
  10639. {
  10640. char *note_name = "LINUX";
  10641. return elfcore_write_note (abfd, buf, bufsiz,
  10642. note_name, NT_S390_GS_CB,
  10643. s390_gs_cb, size);
  10644. }
  10645. char *
  10646. elfcore_write_s390_gs_bc (bfd *abfd,
  10647. char *buf,
  10648. int *bufsiz,
  10649. const void *s390_gs_bc,
  10650. int size)
  10651. {
  10652. char *note_name = "LINUX";
  10653. return elfcore_write_note (abfd, buf, bufsiz,
  10654. note_name, NT_S390_GS_BC,
  10655. s390_gs_bc, size);
  10656. }
  10657. char *
  10658. elfcore_write_arm_vfp (bfd *abfd,
  10659. char *buf,
  10660. int *bufsiz,
  10661. const void *arm_vfp,
  10662. int size)
  10663. {
  10664. char *note_name = "LINUX";
  10665. return elfcore_write_note (abfd, buf, bufsiz,
  10666. note_name, NT_ARM_VFP, arm_vfp, size);
  10667. }
  10668. char *
  10669. elfcore_write_aarch_tls (bfd *abfd,
  10670. char *buf,
  10671. int *bufsiz,
  10672. const void *aarch_tls,
  10673. int size)
  10674. {
  10675. char *note_name = "LINUX";
  10676. return elfcore_write_note (abfd, buf, bufsiz,
  10677. note_name, NT_ARM_TLS, aarch_tls, size);
  10678. }
  10679. char *
  10680. elfcore_write_aarch_hw_break (bfd *abfd,
  10681. char *buf,
  10682. int *bufsiz,
  10683. const void *aarch_hw_break,
  10684. int size)
  10685. {
  10686. char *note_name = "LINUX";
  10687. return elfcore_write_note (abfd, buf, bufsiz,
  10688. note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
  10689. }
  10690. char *
  10691. elfcore_write_aarch_hw_watch (bfd *abfd,
  10692. char *buf,
  10693. int *bufsiz,
  10694. const void *aarch_hw_watch,
  10695. int size)
  10696. {
  10697. char *note_name = "LINUX";
  10698. return elfcore_write_note (abfd, buf, bufsiz,
  10699. note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
  10700. }
  10701. char *
  10702. elfcore_write_aarch_sve (bfd *abfd,
  10703. char *buf,
  10704. int *bufsiz,
  10705. const void *aarch_sve,
  10706. int size)
  10707. {
  10708. char *note_name = "LINUX";
  10709. return elfcore_write_note (abfd, buf, bufsiz,
  10710. note_name, NT_ARM_SVE, aarch_sve, size);
  10711. }
  10712. char *
  10713. elfcore_write_aarch_pauth (bfd *abfd,
  10714. char *buf,
  10715. int *bufsiz,
  10716. const void *aarch_pauth,
  10717. int size)
  10718. {
  10719. char *note_name = "LINUX";
  10720. return elfcore_write_note (abfd, buf, bufsiz,
  10721. note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
  10722. }
  10723. char *
  10724. elfcore_write_aarch_mte (bfd *abfd,
  10725. char *buf,
  10726. int *bufsiz,
  10727. const void *aarch_mte,
  10728. int size)
  10729. {
  10730. char *note_name = "LINUX";
  10731. return elfcore_write_note (abfd, buf, bufsiz,
  10732. note_name, NT_ARM_TAGGED_ADDR_CTRL,
  10733. aarch_mte,
  10734. size);
  10735. }
  10736. char *
  10737. elfcore_write_arc_v2 (bfd *abfd,
  10738. char *buf,
  10739. int *bufsiz,
  10740. const void *arc_v2,
  10741. int size)
  10742. {
  10743. char *note_name = "LINUX";
  10744. return elfcore_write_note (abfd, buf, bufsiz,
  10745. note_name, NT_ARC_V2, arc_v2, size);
  10746. }
  10747. char *
  10748. elfcore_write_loongarch_cpucfg (bfd *abfd,
  10749. char *buf,
  10750. int *bufsiz,
  10751. const void *loongarch_cpucfg,
  10752. int size)
  10753. {
  10754. char *note_name = "LINUX";
  10755. return elfcore_write_note (abfd, buf, bufsiz,
  10756. note_name, NT_LARCH_CPUCFG,
  10757. loongarch_cpucfg, size);
  10758. }
  10759. char *
  10760. elfcore_write_loongarch_lbt (bfd *abfd,
  10761. char *buf,
  10762. int *bufsiz,
  10763. const void *loongarch_lbt,
  10764. int size)
  10765. {
  10766. char *note_name = "LINUX";
  10767. return elfcore_write_note (abfd, buf, bufsiz,
  10768. note_name, NT_LARCH_LBT, loongarch_lbt, size);
  10769. }
  10770. char *
  10771. elfcore_write_loongarch_lsx (bfd *abfd,
  10772. char *buf,
  10773. int *bufsiz,
  10774. const void *loongarch_lsx,
  10775. int size)
  10776. {
  10777. char *note_name = "LINUX";
  10778. return elfcore_write_note (abfd, buf, bufsiz,
  10779. note_name, NT_LARCH_LSX, loongarch_lsx, size);
  10780. }
  10781. char *
  10782. elfcore_write_loongarch_lasx (bfd *abfd,
  10783. char *buf,
  10784. int *bufsiz,
  10785. const void *loongarch_lasx,
  10786. int size)
  10787. {
  10788. char *note_name = "LINUX";
  10789. return elfcore_write_note (abfd, buf, bufsiz,
  10790. note_name, NT_LARCH_LASX, loongarch_lasx, size);
  10791. }
  10792. /* Write the buffer of csr values in CSRS (length SIZE) into the note
  10793. buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
  10794. written into. Return a pointer to the new start of the note buffer, to
  10795. replace BUF which may no longer be valid. */
  10796. char *
  10797. elfcore_write_riscv_csr (bfd *abfd,
  10798. char *buf,
  10799. int *bufsiz,
  10800. const void *csrs,
  10801. int size)
  10802. {
  10803. const char *note_name = "GDB";
  10804. return elfcore_write_note (abfd, buf, bufsiz,
  10805. note_name, NT_RISCV_CSR, csrs, size);
  10806. }
  10807. /* Write the target description (a string) pointed to by TDESC, length
  10808. SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
  10809. note is being written into. Return a pointer to the new start of the
  10810. note buffer, to replace BUF which may no longer be valid. */
  10811. char *
  10812. elfcore_write_gdb_tdesc (bfd *abfd,
  10813. char *buf,
  10814. int *bufsiz,
  10815. const void *tdesc,
  10816. int size)
  10817. {
  10818. const char *note_name = "GDB";
  10819. return elfcore_write_note (abfd, buf, bufsiz,
  10820. note_name, NT_GDB_TDESC, tdesc, size);
  10821. }
  10822. char *
  10823. elfcore_write_register_note (bfd *abfd,
  10824. char *buf,
  10825. int *bufsiz,
  10826. const char *section,
  10827. const void *data,
  10828. int size)
  10829. {
  10830. if (strcmp (section, ".reg2") == 0)
  10831. return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
  10832. if (strcmp (section, ".reg-xfp") == 0)
  10833. return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
  10834. if (strcmp (section, ".reg-xstate") == 0)
  10835. return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
  10836. if (strcmp (section, ".reg-x86-segbases") == 0)
  10837. return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
  10838. if (strcmp (section, ".reg-ppc-vmx") == 0)
  10839. return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
  10840. if (strcmp (section, ".reg-ppc-vsx") == 0)
  10841. return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
  10842. if (strcmp (section, ".reg-ppc-tar") == 0)
  10843. return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
  10844. if (strcmp (section, ".reg-ppc-ppr") == 0)
  10845. return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
  10846. if (strcmp (section, ".reg-ppc-dscr") == 0)
  10847. return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
  10848. if (strcmp (section, ".reg-ppc-ebb") == 0)
  10849. return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
  10850. if (strcmp (section, ".reg-ppc-pmu") == 0)
  10851. return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
  10852. if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
  10853. return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
  10854. if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
  10855. return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
  10856. if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
  10857. return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
  10858. if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
  10859. return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
  10860. if (strcmp (section, ".reg-ppc-tm-spr") == 0)
  10861. return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
  10862. if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
  10863. return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
  10864. if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
  10865. return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
  10866. if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
  10867. return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
  10868. if (strcmp (section, ".reg-s390-high-gprs") == 0)
  10869. return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
  10870. if (strcmp (section, ".reg-s390-timer") == 0)
  10871. return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
  10872. if (strcmp (section, ".reg-s390-todcmp") == 0)
  10873. return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
  10874. if (strcmp (section, ".reg-s390-todpreg") == 0)
  10875. return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
  10876. if (strcmp (section, ".reg-s390-ctrs") == 0)
  10877. return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
  10878. if (strcmp (section, ".reg-s390-prefix") == 0)
  10879. return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
  10880. if (strcmp (section, ".reg-s390-last-break") == 0)
  10881. return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
  10882. if (strcmp (section, ".reg-s390-system-call") == 0)
  10883. return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
  10884. if (strcmp (section, ".reg-s390-tdb") == 0)
  10885. return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
  10886. if (strcmp (section, ".reg-s390-vxrs-low") == 0)
  10887. return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
  10888. if (strcmp (section, ".reg-s390-vxrs-high") == 0)
  10889. return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
  10890. if (strcmp (section, ".reg-s390-gs-cb") == 0)
  10891. return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
  10892. if (strcmp (section, ".reg-s390-gs-bc") == 0)
  10893. return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
  10894. if (strcmp (section, ".reg-arm-vfp") == 0)
  10895. return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
  10896. if (strcmp (section, ".reg-aarch-tls") == 0)
  10897. return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
  10898. if (strcmp (section, ".reg-aarch-hw-break") == 0)
  10899. return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
  10900. if (strcmp (section, ".reg-aarch-hw-watch") == 0)
  10901. return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
  10902. if (strcmp (section, ".reg-aarch-sve") == 0)
  10903. return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
  10904. if (strcmp (section, ".reg-aarch-pauth") == 0)
  10905. return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
  10906. if (strcmp (section, ".reg-aarch-mte") == 0)
  10907. return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
  10908. if (strcmp (section, ".reg-arc-v2") == 0)
  10909. return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
  10910. if (strcmp (section, ".gdb-tdesc") == 0)
  10911. return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
  10912. if (strcmp (section, ".reg-riscv-csr") == 0)
  10913. return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
  10914. if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
  10915. return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
  10916. if (strcmp (section, ".reg-loongarch-lbt") == 0)
  10917. return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
  10918. if (strcmp (section, ".reg-loongarch-lsx") == 0)
  10919. return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
  10920. if (strcmp (section, ".reg-loongarch-lasx") == 0)
  10921. return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
  10922. return NULL;
  10923. }
  10924. char *
  10925. elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
  10926. const void *buf, int bufsiz)
  10927. {
  10928. return elfcore_write_note (obfd, note_data, note_size,
  10929. "CORE", NT_FILE, buf, bufsiz);
  10930. }
  10931. static bool
  10932. elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
  10933. size_t align)
  10934. {
  10935. char *p;
  10936. /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
  10937. gABI specifies that PT_NOTE alignment should be aligned to 4
  10938. bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
  10939. align is less than 4, we use 4 byte alignment. */
  10940. if (align < 4)
  10941. align = 4;
  10942. if (align != 4 && align != 8)
  10943. return false;
  10944. p = buf;
  10945. while (p < buf + size)
  10946. {
  10947. Elf_External_Note *xnp = (Elf_External_Note *) p;
  10948. Elf_Internal_Note in;
  10949. if (offsetof (Elf_External_Note, name) > buf - p + size)
  10950. return false;
  10951. in.type = H_GET_32 (abfd, xnp->type);
  10952. in.namesz = H_GET_32 (abfd, xnp->namesz);
  10953. in.namedata = xnp->name;
  10954. if (in.namesz > buf - in.namedata + size)
  10955. return false;
  10956. in.descsz = H_GET_32 (abfd, xnp->descsz);
  10957. in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
  10958. in.descpos = offset + (in.descdata - buf);
  10959. if (in.descsz != 0
  10960. && (in.descdata >= buf + size
  10961. || in.descsz > buf - in.descdata + size))
  10962. return false;
  10963. switch (bfd_get_format (abfd))
  10964. {
  10965. default:
  10966. return true;
  10967. case bfd_core:
  10968. {
  10969. #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
  10970. struct
  10971. {
  10972. const char * string;
  10973. size_t len;
  10974. bool (*func) (bfd *, Elf_Internal_Note *);
  10975. }
  10976. grokers[] =
  10977. {
  10978. GROKER_ELEMENT ("", elfcore_grok_note),
  10979. GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
  10980. GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
  10981. GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
  10982. GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
  10983. GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
  10984. GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
  10985. GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
  10986. };
  10987. #undef GROKER_ELEMENT
  10988. int i;
  10989. for (i = ARRAY_SIZE (grokers); i--;)
  10990. {
  10991. if (in.namesz >= grokers[i].len
  10992. && strncmp (in.namedata, grokers[i].string,
  10993. grokers[i].len) == 0)
  10994. {
  10995. if (! grokers[i].func (abfd, & in))
  10996. return false;
  10997. break;
  10998. }
  10999. }
  11000. break;
  11001. }
  11002. case bfd_object:
  11003. if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
  11004. {
  11005. if (! elfobj_grok_gnu_note (abfd, &in))
  11006. return false;
  11007. }
  11008. else if (in.namesz == sizeof "stapsdt"
  11009. && strcmp (in.namedata, "stapsdt") == 0)
  11010. {
  11011. if (! elfobj_grok_stapsdt_note (abfd, &in))
  11012. return false;
  11013. }
  11014. break;
  11015. }
  11016. p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
  11017. }
  11018. return true;
  11019. }
  11020. bool
  11021. elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
  11022. size_t align)
  11023. {
  11024. char *buf;
  11025. if (size == 0 || (size + 1) == 0)
  11026. return true;
  11027. if (bfd_seek (abfd, offset, SEEK_SET) != 0)
  11028. return false;
  11029. buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
  11030. if (buf == NULL)
  11031. return false;
  11032. /* PR 17512: file: ec08f814
  11033. 0-termintate the buffer so that string searches will not overflow. */
  11034. buf[size] = 0;
  11035. if (!elf_parse_notes (abfd, buf, size, offset, align))
  11036. {
  11037. free (buf);
  11038. return false;
  11039. }
  11040. free (buf);
  11041. return true;
  11042. }
  11043. /* Providing external access to the ELF program header table. */
  11044. /* Return an upper bound on the number of bytes required to store a
  11045. copy of ABFD's program header table entries. Return -1 if an error
  11046. occurs; bfd_get_error will return an appropriate code. */
  11047. long
  11048. bfd_get_elf_phdr_upper_bound (bfd *abfd)
  11049. {
  11050. if (abfd->xvec->flavour != bfd_target_elf_flavour)
  11051. {
  11052. bfd_set_error (bfd_error_wrong_format);
  11053. return -1;
  11054. }
  11055. return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
  11056. }
  11057. /* Copy ABFD's program header table entries to *PHDRS. The entries
  11058. will be stored as an array of Elf_Internal_Phdr structures, as
  11059. defined in include/elf/internal.h. To find out how large the
  11060. buffer needs to be, call bfd_get_elf_phdr_upper_bound.
  11061. Return the number of program header table entries read, or -1 if an
  11062. error occurs; bfd_get_error will return an appropriate code. */
  11063. int
  11064. bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
  11065. {
  11066. int num_phdrs;
  11067. if (abfd->xvec->flavour != bfd_target_elf_flavour)
  11068. {
  11069. bfd_set_error (bfd_error_wrong_format);
  11070. return -1;
  11071. }
  11072. num_phdrs = elf_elfheader (abfd)->e_phnum;
  11073. if (num_phdrs != 0)
  11074. memcpy (phdrs, elf_tdata (abfd)->phdr,
  11075. num_phdrs * sizeof (Elf_Internal_Phdr));
  11076. return num_phdrs;
  11077. }
  11078. enum elf_reloc_type_class
  11079. _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  11080. const asection *rel_sec ATTRIBUTE_UNUSED,
  11081. const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
  11082. {
  11083. return reloc_class_normal;
  11084. }
  11085. /* For RELA architectures, return the relocation value for a
  11086. relocation against a local symbol. */
  11087. bfd_vma
  11088. _bfd_elf_rela_local_sym (bfd *abfd,
  11089. Elf_Internal_Sym *sym,
  11090. asection **psec,
  11091. Elf_Internal_Rela *rel)
  11092. {
  11093. asection *sec = *psec;
  11094. bfd_vma relocation;
  11095. relocation = (sec->output_section->vma
  11096. + sec->output_offset
  11097. + sym->st_value);
  11098. if ((sec->flags & SEC_MERGE)
  11099. && ELF_ST_TYPE (sym->st_info) == STT_SECTION
  11100. && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
  11101. {
  11102. rel->r_addend =
  11103. _bfd_merged_section_offset (abfd, psec,
  11104. elf_section_data (sec)->sec_info,
  11105. sym->st_value + rel->r_addend);
  11106. if (sec != *psec)
  11107. {
  11108. /* If we have changed the section, and our original section is
  11109. marked with SEC_EXCLUDE, it means that the original
  11110. SEC_MERGE section has been completely subsumed in some
  11111. other SEC_MERGE section. In this case, we need to leave
  11112. some info around for --emit-relocs. */
  11113. if ((sec->flags & SEC_EXCLUDE) != 0)
  11114. sec->kept_section = *psec;
  11115. sec = *psec;
  11116. }
  11117. rel->r_addend -= relocation;
  11118. rel->r_addend += sec->output_section->vma + sec->output_offset;
  11119. }
  11120. return relocation;
  11121. }
  11122. bfd_vma
  11123. _bfd_elf_rel_local_sym (bfd *abfd,
  11124. Elf_Internal_Sym *sym,
  11125. asection **psec,
  11126. bfd_vma addend)
  11127. {
  11128. asection *sec = *psec;
  11129. if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
  11130. return sym->st_value + addend;
  11131. return _bfd_merged_section_offset (abfd, psec,
  11132. elf_section_data (sec)->sec_info,
  11133. sym->st_value + addend);
  11134. }
  11135. /* Adjust an address within a section. Given OFFSET within SEC, return
  11136. the new offset within the section, based upon changes made to the
  11137. section. Returns -1 if the offset is now invalid.
  11138. The offset (in abnd out) is in target sized bytes, however big a
  11139. byte may be. */
  11140. bfd_vma
  11141. _bfd_elf_section_offset (bfd *abfd,
  11142. struct bfd_link_info *info,
  11143. asection *sec,
  11144. bfd_vma offset)
  11145. {
  11146. switch (sec->sec_info_type)
  11147. {
  11148. case SEC_INFO_TYPE_STABS:
  11149. return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
  11150. offset);
  11151. case SEC_INFO_TYPE_EH_FRAME:
  11152. return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
  11153. default:
  11154. if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
  11155. {
  11156. /* Reverse the offset. */
  11157. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  11158. bfd_size_type address_size = bed->s->arch_size / 8;
  11159. /* address_size and sec->size are in octets. Convert
  11160. to bytes before subtracting the original offset. */
  11161. offset = ((sec->size - address_size)
  11162. / bfd_octets_per_byte (abfd, sec) - offset);
  11163. }
  11164. return offset;
  11165. }
  11166. }
  11167. /* Create a new BFD as if by bfd_openr. Rather than opening a file,
  11168. reconstruct an ELF file by reading the segments out of remote memory
  11169. based on the ELF file header at EHDR_VMA and the ELF program headers it
  11170. points to. If not null, *LOADBASEP is filled in with the difference
  11171. between the VMAs from which the segments were read, and the VMAs the
  11172. file headers (and hence BFD's idea of each section's VMA) put them at.
  11173. The function TARGET_READ_MEMORY is called to copy LEN bytes from the
  11174. remote memory at target address VMA into the local buffer at MYADDR; it
  11175. should return zero on success or an `errno' code on failure. TEMPL must
  11176. be a BFD for an ELF target with the word size and byte order found in
  11177. the remote memory. */
  11178. bfd *
  11179. bfd_elf_bfd_from_remote_memory
  11180. (bfd *templ,
  11181. bfd_vma ehdr_vma,
  11182. bfd_size_type size,
  11183. bfd_vma *loadbasep,
  11184. int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
  11185. {
  11186. return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
  11187. (templ, ehdr_vma, size, loadbasep, target_read_memory);
  11188. }
  11189. long
  11190. _bfd_elf_get_synthetic_symtab (bfd *abfd,
  11191. long symcount ATTRIBUTE_UNUSED,
  11192. asymbol **syms ATTRIBUTE_UNUSED,
  11193. long dynsymcount,
  11194. asymbol **dynsyms,
  11195. asymbol **ret)
  11196. {
  11197. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  11198. asection *relplt;
  11199. asymbol *s;
  11200. const char *relplt_name;
  11201. bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
  11202. arelent *p;
  11203. long count, i, n;
  11204. size_t size;
  11205. Elf_Internal_Shdr *hdr;
  11206. char *names;
  11207. asection *plt;
  11208. *ret = NULL;
  11209. if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
  11210. return 0;
  11211. if (dynsymcount <= 0)
  11212. return 0;
  11213. if (!bed->plt_sym_val)
  11214. return 0;
  11215. relplt_name = bed->relplt_name;
  11216. if (relplt_name == NULL)
  11217. relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
  11218. relplt = bfd_get_section_by_name (abfd, relplt_name);
  11219. if (relplt == NULL)
  11220. return 0;
  11221. hdr = &elf_section_data (relplt)->this_hdr;
  11222. if (hdr->sh_link != elf_dynsymtab (abfd)
  11223. || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
  11224. return 0;
  11225. plt = bfd_get_section_by_name (abfd, ".plt");
  11226. if (plt == NULL)
  11227. return 0;
  11228. slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
  11229. if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
  11230. return -1;
  11231. count = relplt->size / hdr->sh_entsize;
  11232. size = count * sizeof (asymbol);
  11233. p = relplt->relocation;
  11234. for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
  11235. {
  11236. size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
  11237. if (p->addend != 0)
  11238. {
  11239. #ifdef BFD64
  11240. size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
  11241. #else
  11242. size += sizeof ("+0x") - 1 + 8;
  11243. #endif
  11244. }
  11245. }
  11246. s = *ret = (asymbol *) bfd_malloc (size);
  11247. if (s == NULL)
  11248. return -1;
  11249. names = (char *) (s + count);
  11250. p = relplt->relocation;
  11251. n = 0;
  11252. for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
  11253. {
  11254. size_t len;
  11255. bfd_vma addr;
  11256. addr = bed->plt_sym_val (i, plt, p);
  11257. if (addr == (bfd_vma) -1)
  11258. continue;
  11259. *s = **p->sym_ptr_ptr;
  11260. /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
  11261. we are defining a symbol, ensure one of them is set. */
  11262. if ((s->flags & BSF_LOCAL) == 0)
  11263. s->flags |= BSF_GLOBAL;
  11264. s->flags |= BSF_SYNTHETIC;
  11265. s->section = plt;
  11266. s->value = addr - plt->vma;
  11267. s->name = names;
  11268. s->udata.p = NULL;
  11269. len = strlen ((*p->sym_ptr_ptr)->name);
  11270. memcpy (names, (*p->sym_ptr_ptr)->name, len);
  11271. names += len;
  11272. if (p->addend != 0)
  11273. {
  11274. char buf[30], *a;
  11275. memcpy (names, "+0x", sizeof ("+0x") - 1);
  11276. names += sizeof ("+0x") - 1;
  11277. bfd_sprintf_vma (abfd, buf, p->addend);
  11278. for (a = buf; *a == '0'; ++a)
  11279. ;
  11280. len = strlen (a);
  11281. memcpy (names, a, len);
  11282. names += len;
  11283. }
  11284. memcpy (names, "@plt", sizeof ("@plt"));
  11285. names += sizeof ("@plt");
  11286. ++s, ++n;
  11287. }
  11288. return n;
  11289. }
  11290. /* It is only used by x86-64 so far.
  11291. ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
  11292. but current usage would allow all of _bfd_std_section to be zero. */
  11293. static const asymbol lcomm_sym
  11294. = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
  11295. asection _bfd_elf_large_com_section
  11296. = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
  11297. "LARGE_COMMON", 0, SEC_IS_COMMON);
  11298. bool
  11299. _bfd_elf_final_write_processing (bfd *abfd)
  11300. {
  11301. Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
  11302. i_ehdrp = elf_elfheader (abfd);
  11303. if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
  11304. i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
  11305. /* Set the osabi field to ELFOSABI_GNU if the binary contains
  11306. SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
  11307. or STB_GNU_UNIQUE binding. */
  11308. if (elf_tdata (abfd)->has_gnu_osabi != 0)
  11309. {
  11310. if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
  11311. i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
  11312. else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
  11313. && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
  11314. {
  11315. if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
  11316. _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
  11317. "and FreeBSD targets"));
  11318. if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
  11319. _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
  11320. "only by GNU and FreeBSD targets"));
  11321. if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
  11322. _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
  11323. "only by GNU and FreeBSD targets"));
  11324. if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
  11325. _bfd_error_handler (_("GNU_RETAIN section is supported "
  11326. "only by GNU and FreeBSD targets"));
  11327. bfd_set_error (bfd_error_sorry);
  11328. return false;
  11329. }
  11330. }
  11331. return true;
  11332. }
  11333. /* Return TRUE for ELF symbol types that represent functions.
  11334. This is the default version of this function, which is sufficient for
  11335. most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
  11336. bool
  11337. _bfd_elf_is_function_type (unsigned int type)
  11338. {
  11339. return (type == STT_FUNC
  11340. || type == STT_GNU_IFUNC);
  11341. }
  11342. /* If the ELF symbol SYM might be a function in SEC, return the
  11343. function size and set *CODE_OFF to the function's entry point,
  11344. otherwise return zero. */
  11345. bfd_size_type
  11346. _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
  11347. bfd_vma *code_off)
  11348. {
  11349. bfd_size_type size;
  11350. elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
  11351. if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
  11352. | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
  11353. || sym->section != sec)
  11354. return 0;
  11355. size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
  11356. /* In theory we should check that the symbol's type satisfies
  11357. _bfd_elf_is_function_type(), but there are some function-like
  11358. symbols which would fail this test. (eg _start). Instead
  11359. we check for hidden, local, notype symbols with zero size.
  11360. This type of symbol is generated by the annobin plugin for gcc
  11361. and clang, and should not be considered to be a function symbol. */
  11362. if (size == 0
  11363. && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
  11364. && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
  11365. && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
  11366. return 0;
  11367. *code_off = sym->value;
  11368. /* Do not return 0 for the function's size. */
  11369. return size ? size : 1;
  11370. }
  11371. /* Set to non-zero to enable some debug messages. */
  11372. #define DEBUG_SECONDARY_RELOCS 0
  11373. /* An internal-to-the-bfd-library only section type
  11374. used to indicate a cached secondary reloc section. */
  11375. #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
  11376. /* Create a BFD section to hold a secondary reloc section. */
  11377. bool
  11378. _bfd_elf_init_secondary_reloc_section (bfd * abfd,
  11379. Elf_Internal_Shdr *hdr,
  11380. const char * name,
  11381. unsigned int shindex)
  11382. {
  11383. /* We only support RELA secondary relocs. */
  11384. if (hdr->sh_type != SHT_RELA)
  11385. return false;
  11386. #if DEBUG_SECONDARY_RELOCS
  11387. fprintf (stderr, "secondary reloc section %s encountered\n", name);
  11388. #endif
  11389. hdr->sh_type = SHT_SECONDARY_RELOC;
  11390. return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
  11391. }
  11392. /* Read in any secondary relocs associated with SEC. */
  11393. bool
  11394. _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
  11395. asection * sec,
  11396. asymbol ** symbols,
  11397. bool dynamic)
  11398. {
  11399. const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
  11400. asection * relsec;
  11401. bool result = true;
  11402. bfd_vma (*r_sym) (bfd_vma);
  11403. #if BFD_DEFAULT_TARGET_SIZE > 32
  11404. if (bfd_arch_bits_per_address (abfd) != 32)
  11405. r_sym = elf64_r_sym;
  11406. else
  11407. #endif
  11408. r_sym = elf32_r_sym;
  11409. if (!elf_section_data (sec)->has_secondary_relocs)
  11410. return true;
  11411. /* Discover if there are any secondary reloc sections
  11412. associated with SEC. */
  11413. for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
  11414. {
  11415. Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
  11416. if (hdr->sh_type == SHT_SECONDARY_RELOC
  11417. && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
  11418. && (hdr->sh_entsize == ebd->s->sizeof_rel
  11419. || hdr->sh_entsize == ebd->s->sizeof_rela))
  11420. {
  11421. bfd_byte * native_relocs;
  11422. bfd_byte * native_reloc;
  11423. arelent * internal_relocs;
  11424. arelent * internal_reloc;
  11425. unsigned int i;
  11426. unsigned int entsize;
  11427. unsigned int symcount;
  11428. unsigned int reloc_count;
  11429. size_t amt;
  11430. if (ebd->elf_info_to_howto == NULL)
  11431. return false;
  11432. #if DEBUG_SECONDARY_RELOCS
  11433. fprintf (stderr, "read secondary relocs for %s from %s\n",
  11434. sec->name, relsec->name);
  11435. #endif
  11436. entsize = hdr->sh_entsize;
  11437. native_relocs = bfd_malloc (hdr->sh_size);
  11438. if (native_relocs == NULL)
  11439. {
  11440. result = false;
  11441. continue;
  11442. }
  11443. reloc_count = NUM_SHDR_ENTRIES (hdr);
  11444. if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
  11445. {
  11446. free (native_relocs);
  11447. bfd_set_error (bfd_error_file_too_big);
  11448. result = false;
  11449. continue;
  11450. }
  11451. internal_relocs = (arelent *) bfd_alloc (abfd, amt);
  11452. if (internal_relocs == NULL)
  11453. {
  11454. free (native_relocs);
  11455. result = false;
  11456. continue;
  11457. }
  11458. if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
  11459. || (bfd_bread (native_relocs, hdr->sh_size, abfd)
  11460. != hdr->sh_size))
  11461. {
  11462. free (native_relocs);
  11463. /* The internal_relocs will be freed when
  11464. the memory for the bfd is released. */
  11465. result = false;
  11466. continue;
  11467. }
  11468. if (dynamic)
  11469. symcount = bfd_get_dynamic_symcount (abfd);
  11470. else
  11471. symcount = bfd_get_symcount (abfd);
  11472. for (i = 0, internal_reloc = internal_relocs,
  11473. native_reloc = native_relocs;
  11474. i < reloc_count;
  11475. i++, internal_reloc++, native_reloc += entsize)
  11476. {
  11477. bool res;
  11478. Elf_Internal_Rela rela;
  11479. if (entsize == ebd->s->sizeof_rel)
  11480. ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
  11481. else /* entsize == ebd->s->sizeof_rela */
  11482. ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
  11483. /* The address of an ELF reloc is section relative for an object
  11484. file, and absolute for an executable file or shared library.
  11485. The address of a normal BFD reloc is always section relative,
  11486. and the address of a dynamic reloc is absolute.. */
  11487. if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
  11488. internal_reloc->address = rela.r_offset;
  11489. else
  11490. internal_reloc->address = rela.r_offset - sec->vma;
  11491. if (r_sym (rela.r_info) == STN_UNDEF)
  11492. {
  11493. /* FIXME: This and the error case below mean that we
  11494. have a symbol on relocs that is not elf_symbol_type. */
  11495. internal_reloc->sym_ptr_ptr =
  11496. bfd_abs_section_ptr->symbol_ptr_ptr;
  11497. }
  11498. else if (r_sym (rela.r_info) > symcount)
  11499. {
  11500. _bfd_error_handler
  11501. /* xgettext:c-format */
  11502. (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
  11503. abfd, sec, i, (long) r_sym (rela.r_info));
  11504. bfd_set_error (bfd_error_bad_value);
  11505. internal_reloc->sym_ptr_ptr =
  11506. bfd_abs_section_ptr->symbol_ptr_ptr;
  11507. result = false;
  11508. }
  11509. else
  11510. {
  11511. asymbol **ps;
  11512. ps = symbols + r_sym (rela.r_info) - 1;
  11513. internal_reloc->sym_ptr_ptr = ps;
  11514. /* Make sure that this symbol is not removed by strip. */
  11515. (*ps)->flags |= BSF_KEEP;
  11516. }
  11517. internal_reloc->addend = rela.r_addend;
  11518. res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
  11519. if (! res || internal_reloc->howto == NULL)
  11520. {
  11521. #if DEBUG_SECONDARY_RELOCS
  11522. fprintf (stderr, "there is no howto associated with reloc %lx\n",
  11523. rela.r_info);
  11524. #endif
  11525. result = false;
  11526. }
  11527. }
  11528. free (native_relocs);
  11529. /* Store the internal relocs. */
  11530. elf_section_data (relsec)->sec_info = internal_relocs;
  11531. }
  11532. }
  11533. return result;
  11534. }
  11535. /* Set the ELF section header fields of an output secondary reloc section. */
  11536. bool
  11537. _bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED,
  11538. bfd * obfd ATTRIBUTE_UNUSED,
  11539. const Elf_Internal_Shdr * isection,
  11540. Elf_Internal_Shdr * osection)
  11541. {
  11542. asection * isec;
  11543. asection * osec;
  11544. struct bfd_elf_section_data * esd;
  11545. if (isection == NULL)
  11546. return false;
  11547. if (isection->sh_type != SHT_SECONDARY_RELOC)
  11548. return true;
  11549. isec = isection->bfd_section;
  11550. if (isec == NULL)
  11551. return false;
  11552. osec = osection->bfd_section;
  11553. if (osec == NULL)
  11554. return false;
  11555. esd = elf_section_data (osec);
  11556. BFD_ASSERT (esd->sec_info == NULL);
  11557. esd->sec_info = elf_section_data (isec)->sec_info;
  11558. osection->sh_type = SHT_RELA;
  11559. osection->sh_link = elf_onesymtab (obfd);
  11560. if (osection->sh_link == 0)
  11561. {
  11562. /* There is no symbol table - we are hosed... */
  11563. _bfd_error_handler
  11564. /* xgettext:c-format */
  11565. (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
  11566. obfd, osec);
  11567. bfd_set_error (bfd_error_bad_value);
  11568. return false;
  11569. }
  11570. /* Find the output section that corresponds to the isection's sh_info link. */
  11571. if (isection->sh_info == 0
  11572. || isection->sh_info >= elf_numsections (ibfd))
  11573. {
  11574. _bfd_error_handler
  11575. /* xgettext:c-format */
  11576. (_("%pB(%pA): info section index is invalid"),
  11577. obfd, osec);
  11578. bfd_set_error (bfd_error_bad_value);
  11579. return false;
  11580. }
  11581. isection = elf_elfsections (ibfd)[isection->sh_info];
  11582. if (isection == NULL
  11583. || isection->bfd_section == NULL
  11584. || isection->bfd_section->output_section == NULL)
  11585. {
  11586. _bfd_error_handler
  11587. /* xgettext:c-format */
  11588. (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
  11589. obfd, osec);
  11590. bfd_set_error (bfd_error_bad_value);
  11591. return false;
  11592. }
  11593. esd = elf_section_data (isection->bfd_section->output_section);
  11594. BFD_ASSERT (esd != NULL);
  11595. osection->sh_info = esd->this_idx;
  11596. esd->has_secondary_relocs = true;
  11597. #if DEBUG_SECONDARY_RELOCS
  11598. fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
  11599. osec->name, osection->sh_link, osection->sh_info);
  11600. fprintf (stderr, "mark section %s as having secondary relocs\n",
  11601. bfd_section_name (isection->bfd_section->output_section));
  11602. #endif
  11603. return true;
  11604. }
  11605. /* Write out a secondary reloc section.
  11606. FIXME: Currently this function can result in a serious performance penalty
  11607. for files with secondary relocs and lots of sections. The proper way to
  11608. fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
  11609. relocs together and then to have this function just walk that chain. */
  11610. bool
  11611. _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
  11612. {
  11613. const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
  11614. bfd_vma addr_offset;
  11615. asection * relsec;
  11616. bfd_vma (*r_info) (bfd_vma, bfd_vma);
  11617. bool result = true;
  11618. if (sec == NULL)
  11619. return false;
  11620. #if BFD_DEFAULT_TARGET_SIZE > 32
  11621. if (bfd_arch_bits_per_address (abfd) != 32)
  11622. r_info = elf64_r_info;
  11623. else
  11624. #endif
  11625. r_info = elf32_r_info;
  11626. /* The address of an ELF reloc is section relative for an object
  11627. file, and absolute for an executable file or shared library.
  11628. The address of a BFD reloc is always section relative. */
  11629. addr_offset = 0;
  11630. if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
  11631. addr_offset = sec->vma;
  11632. /* Discover if there are any secondary reloc sections
  11633. associated with SEC. */
  11634. for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
  11635. {
  11636. const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
  11637. Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
  11638. if (hdr->sh_type == SHT_RELA
  11639. && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
  11640. {
  11641. asymbol * last_sym;
  11642. int last_sym_idx;
  11643. unsigned int reloc_count;
  11644. unsigned int idx;
  11645. unsigned int entsize;
  11646. arelent * src_irel;
  11647. bfd_byte * dst_rela;
  11648. if (hdr->contents != NULL)
  11649. {
  11650. _bfd_error_handler
  11651. /* xgettext:c-format */
  11652. (_("%pB(%pA): error: secondary reloc section processed twice"),
  11653. abfd, relsec);
  11654. bfd_set_error (bfd_error_bad_value);
  11655. result = false;
  11656. continue;
  11657. }
  11658. entsize = hdr->sh_entsize;
  11659. if (entsize == 0)
  11660. {
  11661. _bfd_error_handler
  11662. /* xgettext:c-format */
  11663. (_("%pB(%pA): error: secondary reloc section has zero sized entries"),
  11664. abfd, relsec);
  11665. bfd_set_error (bfd_error_bad_value);
  11666. result = false;
  11667. continue;
  11668. }
  11669. else if (entsize != ebd->s->sizeof_rel
  11670. && entsize != ebd->s->sizeof_rela)
  11671. {
  11672. _bfd_error_handler
  11673. /* xgettext:c-format */
  11674. (_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
  11675. abfd, relsec);
  11676. bfd_set_error (bfd_error_bad_value);
  11677. result = false;
  11678. continue;
  11679. }
  11680. reloc_count = hdr->sh_size / entsize;
  11681. if (reloc_count <= 0)
  11682. {
  11683. _bfd_error_handler
  11684. /* xgettext:c-format */
  11685. (_("%pB(%pA): error: secondary reloc section is empty!"),
  11686. abfd, relsec);
  11687. bfd_set_error (bfd_error_bad_value);
  11688. result = false;
  11689. continue;
  11690. }
  11691. hdr->contents = bfd_alloc (abfd, hdr->sh_size);
  11692. if (hdr->contents == NULL)
  11693. continue;
  11694. #if DEBUG_SECONDARY_RELOCS
  11695. fprintf (stderr, "write %u secondary relocs for %s from %s\n",
  11696. reloc_count, sec->name, relsec->name);
  11697. #endif
  11698. last_sym = NULL;
  11699. last_sym_idx = 0;
  11700. dst_rela = hdr->contents;
  11701. src_irel = (arelent *) esd->sec_info;
  11702. if (src_irel == NULL)
  11703. {
  11704. _bfd_error_handler
  11705. /* xgettext:c-format */
  11706. (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
  11707. abfd, relsec);
  11708. bfd_set_error (bfd_error_bad_value);
  11709. result = false;
  11710. continue;
  11711. }
  11712. for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
  11713. {
  11714. Elf_Internal_Rela src_rela;
  11715. arelent *ptr;
  11716. asymbol *sym;
  11717. int n;
  11718. ptr = src_irel + idx;
  11719. if (ptr == NULL)
  11720. {
  11721. _bfd_error_handler
  11722. /* xgettext:c-format */
  11723. (_("%pB(%pA): error: reloc table entry %u is empty"),
  11724. abfd, relsec, idx);
  11725. bfd_set_error (bfd_error_bad_value);
  11726. result = false;
  11727. break;
  11728. }
  11729. if (ptr->sym_ptr_ptr == NULL)
  11730. {
  11731. /* FIXME: Is this an error ? */
  11732. n = 0;
  11733. }
  11734. else
  11735. {
  11736. sym = *ptr->sym_ptr_ptr;
  11737. if (sym == last_sym)
  11738. n = last_sym_idx;
  11739. else
  11740. {
  11741. n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
  11742. if (n < 0)
  11743. {
  11744. _bfd_error_handler
  11745. /* xgettext:c-format */
  11746. (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
  11747. abfd, relsec, idx);
  11748. bfd_set_error (bfd_error_bad_value);
  11749. result = false;
  11750. n = 0;
  11751. }
  11752. last_sym = sym;
  11753. last_sym_idx = n;
  11754. }
  11755. if (sym->the_bfd != NULL
  11756. && sym->the_bfd->xvec != abfd->xvec
  11757. && ! _bfd_elf_validate_reloc (abfd, ptr))
  11758. {
  11759. _bfd_error_handler
  11760. /* xgettext:c-format */
  11761. (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
  11762. abfd, relsec, idx);
  11763. bfd_set_error (bfd_error_bad_value);
  11764. result = false;
  11765. n = 0;
  11766. }
  11767. }
  11768. src_rela.r_offset = ptr->address + addr_offset;
  11769. if (ptr->howto == NULL)
  11770. {
  11771. _bfd_error_handler
  11772. /* xgettext:c-format */
  11773. (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
  11774. abfd, relsec, idx);
  11775. bfd_set_error (bfd_error_bad_value);
  11776. result = false;
  11777. src_rela.r_info = r_info (0, 0);
  11778. }
  11779. else
  11780. src_rela.r_info = r_info (n, ptr->howto->type);
  11781. src_rela.r_addend = ptr->addend;
  11782. if (entsize == ebd->s->sizeof_rel)
  11783. ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
  11784. else /* entsize == ebd->s->sizeof_rela */
  11785. ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
  11786. }
  11787. }
  11788. }
  11789. return result;
  11790. }