mach-o.c 173 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290
  1. /* Mach-O support for BFD.
  2. Copyright (C) 1999-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. #include "sysdep.h"
  17. #include <limits.h>
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "libiberty.h"
  21. #include "mach-o.h"
  22. #include "aout/stab_gnu.h"
  23. #include "mach-o/reloc.h"
  24. #include "mach-o/external.h"
  25. #include <ctype.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #define bfd_mach_o_object_p bfd_mach_o_gen_object_p
  29. #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
  30. #define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject
  31. #define FILE_ALIGN(off, algn) \
  32. (((off) + ((ufile_ptr) 1 << (algn)) - 1) & ((ufile_ptr) -1 << (algn)))
  33. static bool
  34. bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd);
  35. unsigned int
  36. bfd_mach_o_version (bfd *abfd)
  37. {
  38. bfd_mach_o_data_struct *mdata = NULL;
  39. BFD_ASSERT (bfd_mach_o_valid (abfd));
  40. mdata = bfd_mach_o_get_data (abfd);
  41. return mdata->header.version;
  42. }
  43. bool
  44. bfd_mach_o_valid (bfd *abfd)
  45. {
  46. if (abfd == NULL || abfd->xvec == NULL)
  47. return false;
  48. if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
  49. return false;
  50. if (bfd_mach_o_get_data (abfd) == NULL)
  51. return false;
  52. return true;
  53. }
  54. static inline bool
  55. mach_o_wide_p (bfd_mach_o_header *header)
  56. {
  57. switch (header->version)
  58. {
  59. case 1:
  60. return false;
  61. case 2:
  62. return true;
  63. default:
  64. BFD_FAIL ();
  65. return false;
  66. }
  67. }
  68. static inline bool
  69. bfd_mach_o_wide_p (bfd *abfd)
  70. {
  71. return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
  72. }
  73. /* Tables to translate well known Mach-O segment/section names to bfd
  74. names. Use of canonical names (such as .text or .debug_frame) is required
  75. by gdb. */
  76. /* __TEXT Segment. */
  77. static const mach_o_section_name_xlat text_section_names_xlat[] =
  78. {
  79. { ".text", "__text",
  80. SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  81. BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS, 0},
  82. { ".const", "__const",
  83. SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  84. BFD_MACH_O_S_ATTR_NONE, 0},
  85. { ".static_const", "__static_const",
  86. SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  87. BFD_MACH_O_S_ATTR_NONE, 0},
  88. { ".cstring", "__cstring",
  89. SEC_READONLY | SEC_DATA | SEC_LOAD | SEC_MERGE | SEC_STRINGS,
  90. BFD_MACH_O_S_CSTRING_LITERALS,
  91. BFD_MACH_O_S_ATTR_NONE, 0},
  92. { ".literal4", "__literal4",
  93. SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_4BYTE_LITERALS,
  94. BFD_MACH_O_S_ATTR_NONE, 2},
  95. { ".literal8", "__literal8",
  96. SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_8BYTE_LITERALS,
  97. BFD_MACH_O_S_ATTR_NONE, 3},
  98. { ".literal16", "__literal16",
  99. SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_16BYTE_LITERALS,
  100. BFD_MACH_O_S_ATTR_NONE, 4},
  101. { ".constructor", "__constructor",
  102. SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  103. BFD_MACH_O_S_ATTR_NONE, 0},
  104. { ".destructor", "__destructor",
  105. SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  106. BFD_MACH_O_S_ATTR_NONE, 0},
  107. { ".eh_frame", "__eh_frame",
  108. SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_COALESCED,
  109. BFD_MACH_O_S_ATTR_LIVE_SUPPORT
  110. | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
  111. | BFD_MACH_O_S_ATTR_NO_TOC, 2},
  112. { NULL, NULL, 0, 0, 0, 0}
  113. };
  114. /* __DATA Segment. */
  115. static const mach_o_section_name_xlat data_section_names_xlat[] =
  116. {
  117. { ".data", "__data",
  118. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  119. BFD_MACH_O_S_ATTR_NONE, 0},
  120. { ".bss", "__bss",
  121. SEC_NO_FLAGS, BFD_MACH_O_S_ZEROFILL,
  122. BFD_MACH_O_S_ATTR_NONE, 0},
  123. { ".const_data", "__const",
  124. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  125. BFD_MACH_O_S_ATTR_NONE, 0},
  126. { ".static_data", "__static_data",
  127. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  128. BFD_MACH_O_S_ATTR_NONE, 0},
  129. { ".mod_init_func", "__mod_init_func",
  130. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS,
  131. BFD_MACH_O_S_ATTR_NONE, 2},
  132. { ".mod_term_func", "__mod_term_func",
  133. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS,
  134. BFD_MACH_O_S_ATTR_NONE, 2},
  135. { ".dyld", "__dyld",
  136. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  137. BFD_MACH_O_S_ATTR_NONE, 0},
  138. { ".cfstring", "__cfstring",
  139. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  140. BFD_MACH_O_S_ATTR_NONE, 2},
  141. { NULL, NULL, 0, 0, 0, 0}
  142. };
  143. /* __DWARF Segment. */
  144. static const mach_o_section_name_xlat dwarf_section_names_xlat[] =
  145. {
  146. { ".debug_frame", "__debug_frame",
  147. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  148. BFD_MACH_O_S_ATTR_DEBUG, 0},
  149. { ".debug_info", "__debug_info",
  150. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  151. BFD_MACH_O_S_ATTR_DEBUG, 0},
  152. { ".debug_abbrev", "__debug_abbrev",
  153. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  154. BFD_MACH_O_S_ATTR_DEBUG, 0},
  155. { ".debug_aranges", "__debug_aranges",
  156. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  157. BFD_MACH_O_S_ATTR_DEBUG, 0},
  158. { ".debug_macinfo", "__debug_macinfo",
  159. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  160. BFD_MACH_O_S_ATTR_DEBUG, 0},
  161. { ".debug_line", "__debug_line",
  162. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  163. BFD_MACH_O_S_ATTR_DEBUG, 0},
  164. { ".debug_loc", "__debug_loc",
  165. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  166. BFD_MACH_O_S_ATTR_DEBUG, 0},
  167. { ".debug_pubnames", "__debug_pubnames",
  168. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  169. BFD_MACH_O_S_ATTR_DEBUG, 0},
  170. { ".debug_pubtypes", "__debug_pubtypes",
  171. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  172. BFD_MACH_O_S_ATTR_DEBUG, 0},
  173. { ".debug_str", "__debug_str",
  174. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  175. BFD_MACH_O_S_ATTR_DEBUG, 0},
  176. { ".debug_ranges", "__debug_ranges",
  177. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  178. BFD_MACH_O_S_ATTR_DEBUG, 0},
  179. { ".debug_macro", "__debug_macro",
  180. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  181. BFD_MACH_O_S_ATTR_DEBUG, 0},
  182. { ".debug_gdb_scripts", "__debug_gdb_scri",
  183. SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
  184. BFD_MACH_O_S_ATTR_DEBUG, 0},
  185. { NULL, NULL, 0, 0, 0, 0}
  186. };
  187. /* __OBJC Segment. */
  188. static const mach_o_section_name_xlat objc_section_names_xlat[] =
  189. {
  190. { ".objc_class", "__class",
  191. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  192. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  193. { ".objc_meta_class", "__meta_class",
  194. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  195. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  196. { ".objc_cat_cls_meth", "__cat_cls_meth",
  197. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  198. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  199. { ".objc_cat_inst_meth", "__cat_inst_meth",
  200. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  201. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  202. { ".objc_protocol", "__protocol",
  203. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  204. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  205. { ".objc_string_object", "__string_object",
  206. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  207. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  208. { ".objc_cls_meth", "__cls_meth",
  209. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  210. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  211. { ".objc_inst_meth", "__inst_meth",
  212. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  213. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  214. { ".objc_cls_refs", "__cls_refs",
  215. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS,
  216. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  217. { ".objc_message_refs", "__message_refs",
  218. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS,
  219. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  220. { ".objc_symbols", "__symbols",
  221. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  222. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  223. { ".objc_category", "__category",
  224. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  225. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  226. { ".objc_class_vars", "__class_vars",
  227. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  228. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  229. { ".objc_instance_vars", "__instance_vars",
  230. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  231. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  232. { ".objc_module_info", "__module_info",
  233. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  234. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  235. { ".objc_selector_strs", "__selector_strs",
  236. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_CSTRING_LITERALS,
  237. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  238. { ".objc_image_info", "__image_info",
  239. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  240. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  241. { ".objc_selector_fixup", "__sel_fixup",
  242. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  243. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  244. /* Objc V1 */
  245. { ".objc1_class_ext", "__class_ext",
  246. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  247. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  248. { ".objc1_property_list", "__property",
  249. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  250. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  251. { ".objc1_protocol_ext", "__protocol_ext",
  252. SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
  253. BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
  254. { NULL, NULL, 0, 0, 0, 0}
  255. };
  256. static const mach_o_segment_name_xlat segsec_names_xlat[] =
  257. {
  258. { "__TEXT", text_section_names_xlat },
  259. { "__DATA", data_section_names_xlat },
  260. { "__DWARF", dwarf_section_names_xlat },
  261. { "__OBJC", objc_section_names_xlat },
  262. { NULL, NULL }
  263. };
  264. static const char dsym_subdir[] = ".dSYM/Contents/Resources/DWARF";
  265. /* For both cases bfd-name => mach-o name and vice versa, the specific target
  266. is checked before the generic. This allows a target (e.g. ppc for cstring)
  267. to override the generic definition with a more specific one. */
  268. /* Fetch the translation from a Mach-O section designation (segment, section)
  269. as a bfd short name, if one exists. Otherwise return NULL.
  270. Allow the segment and section names to be unterminated 16 byte arrays. */
  271. const mach_o_section_name_xlat *
  272. bfd_mach_o_section_data_for_mach_sect (bfd *abfd, const char *segname,
  273. const char *sectname)
  274. {
  275. const struct mach_o_segment_name_xlat *seg;
  276. const mach_o_section_name_xlat *sec;
  277. bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
  278. /* First try any target-specific translations defined... */
  279. if (bed->segsec_names_xlat)
  280. for (seg = bed->segsec_names_xlat; seg->segname; seg++)
  281. if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
  282. for (sec = seg->sections; sec->mach_o_name; sec++)
  283. if (strncmp (sec->mach_o_name, sectname,
  284. BFD_MACH_O_SECTNAME_SIZE) == 0)
  285. return sec;
  286. /* ... and then the Mach-O generic ones. */
  287. for (seg = segsec_names_xlat; seg->segname; seg++)
  288. if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
  289. for (sec = seg->sections; sec->mach_o_name; sec++)
  290. if (strncmp (sec->mach_o_name, sectname,
  291. BFD_MACH_O_SECTNAME_SIZE) == 0)
  292. return sec;
  293. return NULL;
  294. }
  295. /* If the bfd_name for this section is a 'canonical' form for which we
  296. know the Mach-O data, return the segment name and the data for the
  297. Mach-O equivalent. Otherwise return NULL. */
  298. const mach_o_section_name_xlat *
  299. bfd_mach_o_section_data_for_bfd_name (bfd *abfd, const char *bfd_name,
  300. const char **segname)
  301. {
  302. const struct mach_o_segment_name_xlat *seg;
  303. const mach_o_section_name_xlat *sec;
  304. bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
  305. *segname = NULL;
  306. if (bfd_name[0] != '.')
  307. return NULL;
  308. /* First try any target-specific translations defined... */
  309. if (bed->segsec_names_xlat)
  310. for (seg = bed->segsec_names_xlat; seg->segname; seg++)
  311. for (sec = seg->sections; sec->bfd_name; sec++)
  312. if (strcmp (bfd_name, sec->bfd_name) == 0)
  313. {
  314. *segname = seg->segname;
  315. return sec;
  316. }
  317. /* ... and then the Mach-O generic ones. */
  318. for (seg = segsec_names_xlat; seg->segname; seg++)
  319. for (sec = seg->sections; sec->bfd_name; sec++)
  320. if (strcmp (bfd_name, sec->bfd_name) == 0)
  321. {
  322. *segname = seg->segname;
  323. return sec;
  324. }
  325. return NULL;
  326. }
  327. /* Convert Mach-O section name to BFD.
  328. Try to use standard/canonical names, for which we have tables including
  329. default flag settings - which are returned. Otherwise forge a new name
  330. in the form "<segmentname>.<sectionname>" this will be prefixed with
  331. LC_SEGMENT. if the segment name does not begin with an underscore.
  332. SEGNAME and SECTNAME are 16 byte arrays (they do not need to be NUL-
  333. terminated if the name length is exactly 16 bytes - but must be if the name
  334. length is less than 16 characters). */
  335. void
  336. bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname,
  337. const char *secname, const char **name,
  338. flagword *flags)
  339. {
  340. const mach_o_section_name_xlat *xlat;
  341. char *res;
  342. unsigned int len;
  343. const char *pfx = "";
  344. *name = NULL;
  345. *flags = SEC_NO_FLAGS;
  346. /* First search for a canonical name...
  347. xlat will be non-null if there is an entry for segname, secname. */
  348. xlat = bfd_mach_o_section_data_for_mach_sect (abfd, segname, secname);
  349. if (xlat)
  350. {
  351. len = strlen (xlat->bfd_name);
  352. res = bfd_alloc (abfd, len + 1);
  353. if (res == NULL)
  354. return;
  355. memcpy (res, xlat->bfd_name, len+1);
  356. *name = res;
  357. *flags = xlat->bfd_flags;
  358. return;
  359. }
  360. /* ... else we make up a bfd name from the segment concatenated with the
  361. section. */
  362. len = 16 + 1 + 16 + 1;
  363. /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
  364. with an underscore. */
  365. if (segname[0] != '_')
  366. {
  367. static const char seg_pfx[] = "LC_SEGMENT.";
  368. pfx = seg_pfx;
  369. len += sizeof (seg_pfx) - 1;
  370. }
  371. res = bfd_alloc (abfd, len);
  372. if (res == NULL)
  373. return;
  374. snprintf (res, len, "%s%.16s.%.16s", pfx, segname, secname);
  375. *name = res;
  376. }
  377. /* Convert a bfd section name to a Mach-O segment + section name.
  378. If the name is a canonical one for which we have a Darwin match
  379. return the translation table - which contains defaults for flags,
  380. type, attribute and default alignment data.
  381. Otherwise, expand the bfd_name (assumed to be in the form
  382. "[LC_SEGMENT.]<segmentname>.<sectionname>") and return NULL. */
  383. static const mach_o_section_name_xlat *
  384. bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
  385. asection *sect,
  386. bfd_mach_o_section *section)
  387. {
  388. const mach_o_section_name_xlat *xlat;
  389. const char *name = bfd_section_name (sect);
  390. const char *segname;
  391. const char *dot;
  392. unsigned int len;
  393. unsigned int seglen;
  394. unsigned int seclen;
  395. memset (section->segname, 0, BFD_MACH_O_SEGNAME_SIZE + 1);
  396. memset (section->sectname, 0, BFD_MACH_O_SECTNAME_SIZE + 1);
  397. /* See if is a canonical name ... */
  398. xlat = bfd_mach_o_section_data_for_bfd_name (abfd, name, &segname);
  399. if (xlat)
  400. {
  401. strcpy (section->segname, segname);
  402. strcpy (section->sectname, xlat->mach_o_name);
  403. return xlat;
  404. }
  405. /* .. else we convert our constructed one back to Mach-O.
  406. Strip LC_SEGMENT. prefix, if present. */
  407. if (strncmp (name, "LC_SEGMENT.", 11) == 0)
  408. name += 11;
  409. /* Find a dot. */
  410. dot = strchr (name, '.');
  411. len = strlen (name);
  412. /* Try to split name into segment and section names. */
  413. if (dot && dot != name)
  414. {
  415. seglen = dot - name;
  416. seclen = len - (dot + 1 - name);
  417. if (seglen <= BFD_MACH_O_SEGNAME_SIZE
  418. && seclen <= BFD_MACH_O_SECTNAME_SIZE)
  419. {
  420. memcpy (section->segname, name, seglen);
  421. section->segname[seglen] = 0;
  422. memcpy (section->sectname, dot + 1, seclen);
  423. section->sectname[seclen] = 0;
  424. return NULL;
  425. }
  426. }
  427. /* The segment and section names are both missing - don't make them
  428. into dots. */
  429. if (dot && dot == name)
  430. return NULL;
  431. /* Just duplicate the name into both segment and section. */
  432. if (len > 16)
  433. len = 16;
  434. memcpy (section->segname, name, len);
  435. section->segname[len] = 0;
  436. memcpy (section->sectname, name, len);
  437. section->sectname[len] = 0;
  438. return NULL;
  439. }
  440. /* Return the size of an entry for section SEC.
  441. Must be called only for symbol pointer section and symbol stubs
  442. sections. */
  443. unsigned int
  444. bfd_mach_o_section_get_entry_size (bfd *abfd, bfd_mach_o_section *sec)
  445. {
  446. switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
  447. {
  448. case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
  449. case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
  450. return bfd_mach_o_wide_p (abfd) ? 8 : 4;
  451. case BFD_MACH_O_S_SYMBOL_STUBS:
  452. return sec->reserved2;
  453. default:
  454. BFD_FAIL ();
  455. return 0;
  456. }
  457. }
  458. /* Return the number of indirect symbols for a section.
  459. Must be called only for symbol pointer section and symbol stubs
  460. sections. */
  461. unsigned int
  462. bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
  463. {
  464. unsigned int elsz;
  465. elsz = bfd_mach_o_section_get_entry_size (abfd, sec);
  466. if (elsz == 0)
  467. return 0;
  468. else
  469. return sec->size / elsz;
  470. }
  471. /* Append command CMD to ABFD. Note that header.ncmds is not updated. */
  472. static void
  473. bfd_mach_o_append_command (bfd *abfd, bfd_mach_o_load_command *cmd)
  474. {
  475. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  476. if (mdata->last_command != NULL)
  477. mdata->last_command->next = cmd;
  478. else
  479. mdata->first_command = cmd;
  480. mdata->last_command = cmd;
  481. cmd->next = NULL;
  482. }
  483. /* Copy any private info we understand from the input symbol
  484. to the output symbol. */
  485. bool
  486. bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
  487. asymbol *isymbol,
  488. bfd *obfd ATTRIBUTE_UNUSED,
  489. asymbol *osymbol)
  490. {
  491. bfd_mach_o_asymbol *os, *is;
  492. os = (bfd_mach_o_asymbol *)osymbol;
  493. is = (bfd_mach_o_asymbol *)isymbol;
  494. os->n_type = is->n_type;
  495. os->n_sect = is->n_sect;
  496. os->n_desc = is->n_desc;
  497. os->symbol.udata.i = is->symbol.udata.i;
  498. return true;
  499. }
  500. /* Copy any private info we understand from the input section
  501. to the output section. */
  502. bool
  503. bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd, asection *isection,
  504. bfd *obfd, asection *osection)
  505. {
  506. bfd_mach_o_section *os = bfd_mach_o_get_mach_o_section (osection);
  507. bfd_mach_o_section *is = bfd_mach_o_get_mach_o_section (isection);
  508. if (ibfd->xvec->flavour != bfd_target_mach_o_flavour
  509. || obfd->xvec->flavour != bfd_target_mach_o_flavour)
  510. return true;
  511. BFD_ASSERT (is != NULL && os != NULL);
  512. os->flags = is->flags;
  513. os->reserved1 = is->reserved1;
  514. os->reserved2 = is->reserved2;
  515. os->reserved3 = is->reserved3;
  516. return true;
  517. }
  518. static const char *
  519. cputype (unsigned long value)
  520. {
  521. switch (value)
  522. {
  523. case BFD_MACH_O_CPU_TYPE_VAX: return "VAX";
  524. case BFD_MACH_O_CPU_TYPE_MC680x0: return "MC68k";
  525. case BFD_MACH_O_CPU_TYPE_I386: return "I386";
  526. case BFD_MACH_O_CPU_TYPE_MIPS: return "MIPS";
  527. case BFD_MACH_O_CPU_TYPE_MC98000: return "MC98k";
  528. case BFD_MACH_O_CPU_TYPE_HPPA: return "HPPA";
  529. case BFD_MACH_O_CPU_TYPE_ARM: return "ARM";
  530. case BFD_MACH_O_CPU_TYPE_MC88000: return "MC88K";
  531. case BFD_MACH_O_CPU_TYPE_SPARC: return "SPARC";
  532. case BFD_MACH_O_CPU_TYPE_I860: return "I860";
  533. case BFD_MACH_O_CPU_TYPE_ALPHA: return "ALPHA";
  534. case BFD_MACH_O_CPU_TYPE_POWERPC: return "PPC";
  535. case BFD_MACH_O_CPU_TYPE_POWERPC_64: return "PPC64";
  536. case BFD_MACH_O_CPU_TYPE_X86_64: return "X86_64";
  537. case BFD_MACH_O_CPU_TYPE_ARM64: return "ARM64";
  538. default: return _("<unknown>");
  539. }
  540. }
  541. static const char *
  542. cpusubtype (unsigned long cpu_type, unsigned long cpu_subtype, char *buffer)
  543. {
  544. buffer[0] = 0;
  545. switch (cpu_subtype & BFD_MACH_O_CPU_SUBTYPE_MASK)
  546. {
  547. case 0:
  548. break;
  549. case BFD_MACH_O_CPU_SUBTYPE_LIB64:
  550. sprintf (buffer, " (LIB64)"); break;
  551. default:
  552. sprintf (buffer, _("<unknown mask flags>")); break;
  553. }
  554. cpu_subtype &= ~ BFD_MACH_O_CPU_SUBTYPE_MASK;
  555. switch (cpu_type)
  556. {
  557. case BFD_MACH_O_CPU_TYPE_X86_64:
  558. case BFD_MACH_O_CPU_TYPE_I386:
  559. switch (cpu_subtype)
  560. {
  561. case BFD_MACH_O_CPU_SUBTYPE_X86_ALL:
  562. return strcat (buffer, " (X86_ALL)");
  563. default:
  564. break;
  565. }
  566. break;
  567. case BFD_MACH_O_CPU_TYPE_ARM:
  568. switch (cpu_subtype)
  569. {
  570. case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
  571. return strcat (buffer, " (ARM_ALL)");
  572. case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
  573. return strcat (buffer, " (ARM_V4T)");
  574. case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
  575. return strcat (buffer, " (ARM_V6)");
  576. case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
  577. return strcat (buffer, " (ARM_V5TEJ)");
  578. case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
  579. return strcat (buffer, " (ARM_XSCALE)");
  580. case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
  581. return strcat (buffer, " (ARM_V7)");
  582. default:
  583. break;
  584. }
  585. break;
  586. case BFD_MACH_O_CPU_TYPE_ARM64:
  587. switch (cpu_subtype)
  588. {
  589. case BFD_MACH_O_CPU_SUBTYPE_ARM64_ALL:
  590. return strcat (buffer, " (ARM64_ALL)");
  591. case BFD_MACH_O_CPU_SUBTYPE_ARM64_V8:
  592. return strcat (buffer, " (ARM64_V8)");
  593. default:
  594. break;
  595. }
  596. break;
  597. default:
  598. break;
  599. }
  600. if (cpu_subtype != 0)
  601. return strcat (buffer, _(" (<unknown>)"));
  602. return buffer;
  603. }
  604. bool
  605. bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, void *ptr)
  606. {
  607. FILE * file = (FILE *) ptr;
  608. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  609. char buff[128];
  610. fprintf (file, _(" MACH-O header:\n"));
  611. fprintf (file, _(" magic: %#lx\n"), (long) mdata->header.magic);
  612. fprintf (file, _(" cputype: %#lx (%s)\n"), (long) mdata->header.cputype,
  613. cputype (mdata->header.cputype));
  614. fprintf (file, _(" cpusubtype: %#lx%s\n"), (long) mdata->header.cpusubtype,
  615. cpusubtype (mdata->header.cputype, mdata->header.cpusubtype, buff));
  616. fprintf (file, _(" filetype: %#lx\n"), (long) mdata->header.filetype);
  617. fprintf (file, _(" ncmds: %#lx\n"), (long) mdata->header.ncmds);
  618. fprintf (file, _(" sizeocmds: %#lx\n"), (long) mdata->header.sizeofcmds);
  619. fprintf (file, _(" flags: %#lx\n"), (long) mdata->header.flags);
  620. fprintf (file, _(" version: %x\n"), mdata->header.version);
  621. return true;
  622. }
  623. /* Copy any private info we understand from the input bfd
  624. to the output bfd. */
  625. bool
  626. bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd)
  627. {
  628. bfd_mach_o_data_struct *imdata;
  629. bfd_mach_o_data_struct *omdata;
  630. bfd_mach_o_load_command *icmd;
  631. if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
  632. || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
  633. return true;
  634. BFD_ASSERT (bfd_mach_o_valid (ibfd));
  635. BFD_ASSERT (bfd_mach_o_valid (obfd));
  636. imdata = bfd_mach_o_get_data (ibfd);
  637. omdata = bfd_mach_o_get_data (obfd);
  638. /* Copy header flags. */
  639. omdata->header.flags = imdata->header.flags;
  640. /* PR 23299. Copy the cputype. */
  641. if (imdata->header.cputype != omdata->header.cputype)
  642. {
  643. if (omdata->header.cputype == 0)
  644. omdata->header.cputype = imdata->header.cputype;
  645. else if (imdata->header.cputype != 0)
  646. /* Urg - what has happened ? */
  647. _bfd_error_handler (_("incompatible cputypes in mach-o files: %ld vs %ld"),
  648. (long) imdata->header.cputype,
  649. (long) omdata->header.cputype);
  650. }
  651. /* Copy the cpusubtype. */
  652. omdata->header.cpusubtype = imdata->header.cpusubtype;
  653. /* Copy commands. */
  654. for (icmd = imdata->first_command; icmd != NULL; icmd = icmd->next)
  655. {
  656. bfd_mach_o_load_command *ocmd;
  657. switch (icmd->type)
  658. {
  659. case BFD_MACH_O_LC_LOAD_DYLIB:
  660. case BFD_MACH_O_LC_LOAD_DYLINKER:
  661. case BFD_MACH_O_LC_DYLD_INFO:
  662. /* Command is copied. */
  663. ocmd = bfd_alloc (obfd, sizeof (bfd_mach_o_load_command));
  664. if (ocmd == NULL)
  665. return false;
  666. /* Copy common fields. */
  667. ocmd->type = icmd->type;
  668. ocmd->type_required = icmd->type_required;
  669. ocmd->offset = 0;
  670. ocmd->len = icmd->len;
  671. break;
  672. default:
  673. /* Command is not copied. */
  674. continue;
  675. break;
  676. }
  677. switch (icmd->type)
  678. {
  679. case BFD_MACH_O_LC_LOAD_DYLIB:
  680. {
  681. bfd_mach_o_dylib_command *idy = &icmd->command.dylib;
  682. bfd_mach_o_dylib_command *ody = &ocmd->command.dylib;
  683. ody->name_offset = idy->name_offset;
  684. ody->timestamp = idy->timestamp;
  685. ody->current_version = idy->current_version;
  686. ody->compatibility_version = idy->compatibility_version;
  687. ody->name_str = idy->name_str;
  688. }
  689. break;
  690. case BFD_MACH_O_LC_LOAD_DYLINKER:
  691. {
  692. bfd_mach_o_dylinker_command *idy = &icmd->command.dylinker;
  693. bfd_mach_o_dylinker_command *ody = &ocmd->command.dylinker;
  694. ody->name_offset = idy->name_offset;
  695. ody->name_str = idy->name_str;
  696. }
  697. break;
  698. case BFD_MACH_O_LC_DYLD_INFO:
  699. {
  700. bfd_mach_o_dyld_info_command *idy = &icmd->command.dyld_info;
  701. bfd_mach_o_dyld_info_command *ody = &ocmd->command.dyld_info;
  702. if (bfd_mach_o_read_dyld_content (ibfd, idy))
  703. {
  704. ody->rebase_size = idy->rebase_size;
  705. ody->rebase_content = idy->rebase_content;
  706. ody->bind_size = idy->bind_size;
  707. ody->bind_content = idy->bind_content;
  708. ody->weak_bind_size = idy->weak_bind_size;
  709. ody->weak_bind_content = idy->weak_bind_content;
  710. ody->lazy_bind_size = idy->lazy_bind_size;
  711. ody->lazy_bind_content = idy->lazy_bind_content;
  712. ody->export_size = idy->export_size;
  713. ody->export_content = idy->export_content;
  714. }
  715. /* PR 17512L: file: 730e492d. */
  716. else
  717. {
  718. ody->rebase_size =
  719. ody->bind_size =
  720. ody->weak_bind_size =
  721. ody->lazy_bind_size =
  722. ody->export_size = 0;
  723. ody->rebase_content =
  724. ody->bind_content =
  725. ody->weak_bind_content =
  726. ody->lazy_bind_content =
  727. ody->export_content = NULL;
  728. }
  729. }
  730. break;
  731. default:
  732. /* That command should be handled. */
  733. abort ();
  734. }
  735. /* Insert command. */
  736. bfd_mach_o_append_command (obfd, ocmd);
  737. }
  738. return true;
  739. }
  740. /* This allows us to set up to 32 bits of flags (unless we invent some
  741. fiendish scheme to subdivide). For now, we'll just set the file flags
  742. without error checking - just overwrite. */
  743. bool
  744. bfd_mach_o_bfd_set_private_flags (bfd *abfd, flagword flags)
  745. {
  746. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  747. if (!mdata)
  748. return false;
  749. mdata->header.flags = flags;
  750. return true;
  751. }
  752. /* Count the total number of symbols. */
  753. static long
  754. bfd_mach_o_count_symbols (bfd *abfd)
  755. {
  756. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  757. if (mdata->symtab == NULL)
  758. return 0;
  759. return mdata->symtab->nsyms;
  760. }
  761. long
  762. bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
  763. {
  764. long nsyms = bfd_mach_o_count_symbols (abfd);
  765. return ((nsyms + 1) * sizeof (asymbol *));
  766. }
  767. long
  768. bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
  769. {
  770. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  771. long nsyms = bfd_mach_o_count_symbols (abfd);
  772. bfd_mach_o_symtab_command *sym = mdata->symtab;
  773. unsigned long j;
  774. if (nsyms < 0)
  775. return nsyms;
  776. if (nsyms == 0)
  777. {
  778. /* Do not try to read symbols if there are none. */
  779. alocation[0] = NULL;
  780. return 0;
  781. }
  782. if (!bfd_mach_o_read_symtab_symbols (abfd))
  783. {
  784. _bfd_error_handler
  785. (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
  786. return 0;
  787. }
  788. BFD_ASSERT (sym->symbols != NULL);
  789. for (j = 0; j < sym->nsyms; j++)
  790. alocation[j] = &sym->symbols[j].symbol;
  791. alocation[j] = NULL;
  792. return nsyms;
  793. }
  794. /* Create synthetic symbols for indirect symbols. */
  795. long
  796. bfd_mach_o_get_synthetic_symtab (bfd *abfd,
  797. long symcount ATTRIBUTE_UNUSED,
  798. asymbol **syms ATTRIBUTE_UNUSED,
  799. long dynsymcount ATTRIBUTE_UNUSED,
  800. asymbol **dynsyms ATTRIBUTE_UNUSED,
  801. asymbol **ret)
  802. {
  803. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  804. bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
  805. bfd_mach_o_symtab_command *symtab = mdata->symtab;
  806. asymbol *s;
  807. char * s_start;
  808. char * s_end;
  809. unsigned long count, i, j, n;
  810. size_t size;
  811. char *names;
  812. char *nul_name;
  813. const char stub [] = "$stub";
  814. *ret = NULL;
  815. /* Stop now if no symbols or no indirect symbols. */
  816. if (dysymtab == NULL || dysymtab->nindirectsyms == 0
  817. || symtab == NULL || symtab->symbols == NULL)
  818. return 0;
  819. /* We need to allocate a bfd symbol for every indirect symbol and to
  820. allocate the memory for its name. */
  821. count = dysymtab->nindirectsyms;
  822. size = count * sizeof (asymbol) + 1;
  823. for (j = 0; j < count; j++)
  824. {
  825. const char * strng;
  826. unsigned int isym = dysymtab->indirect_syms[j];
  827. /* Some indirect symbols are anonymous. */
  828. if (isym < symtab->nsyms && (strng = symtab->symbols[isym].symbol.name))
  829. /* PR 17512: file: f5b8eeba. */
  830. size += strnlen (strng, symtab->strsize - (strng - symtab->strtab)) + sizeof (stub);
  831. }
  832. s_start = bfd_malloc (size);
  833. s = *ret = (asymbol *) s_start;
  834. if (s == NULL)
  835. return -1;
  836. names = (char *) (s + count);
  837. nul_name = names;
  838. *names++ = 0;
  839. s_end = s_start + size;
  840. n = 0;
  841. for (i = 0; i < mdata->nsects; i++)
  842. {
  843. bfd_mach_o_section *sec = mdata->sections[i];
  844. unsigned int first, last;
  845. bfd_vma addr;
  846. bfd_vma entry_size;
  847. switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
  848. {
  849. case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
  850. case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
  851. case BFD_MACH_O_S_SYMBOL_STUBS:
  852. /* Only these sections have indirect symbols. */
  853. first = sec->reserved1;
  854. last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
  855. addr = sec->addr;
  856. entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
  857. /* PR 17512: file: 08e15eec. */
  858. if (first >= count || last >= count || first > last)
  859. goto fail;
  860. for (j = first; j < last; j++)
  861. {
  862. unsigned int isym = dysymtab->indirect_syms[j];
  863. /* PR 17512: file: 04d64d9b. */
  864. if (((char *) s) + sizeof (* s) > s_end)
  865. goto fail;
  866. s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
  867. s->section = sec->bfdsection;
  868. s->value = addr - sec->addr;
  869. s->udata.p = NULL;
  870. if (isym < symtab->nsyms
  871. && symtab->symbols[isym].symbol.name)
  872. {
  873. const char *sym = symtab->symbols[isym].symbol.name;
  874. size_t len;
  875. s->name = names;
  876. len = strlen (sym);
  877. /* PR 17512: file: 47dfd4d2. */
  878. if (names + len >= s_end)
  879. goto fail;
  880. memcpy (names, sym, len);
  881. names += len;
  882. /* PR 17512: file: 18f340a4. */
  883. if (names + sizeof (stub) >= s_end)
  884. goto fail;
  885. memcpy (names, stub, sizeof (stub));
  886. names += sizeof (stub);
  887. }
  888. else
  889. s->name = nul_name;
  890. addr += entry_size;
  891. s++;
  892. n++;
  893. }
  894. break;
  895. default:
  896. break;
  897. }
  898. }
  899. return n;
  900. fail:
  901. free (s_start);
  902. * ret = NULL;
  903. return -1;
  904. }
  905. void
  906. bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
  907. asymbol *symbol,
  908. symbol_info *ret)
  909. {
  910. bfd_symbol_info (symbol, ret);
  911. }
  912. void
  913. bfd_mach_o_print_symbol (bfd *abfd,
  914. void * afile,
  915. asymbol *symbol,
  916. bfd_print_symbol_type how)
  917. {
  918. FILE *file = (FILE *) afile;
  919. const char *name;
  920. bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
  921. switch (how)
  922. {
  923. case bfd_print_symbol_name:
  924. fprintf (file, "%s", symbol->name);
  925. break;
  926. default:
  927. bfd_print_symbol_vandf (abfd, (void *) file, symbol);
  928. if (asym->n_type & BFD_MACH_O_N_STAB)
  929. name = bfd_get_stab_name (asym->n_type);
  930. else
  931. switch (asym->n_type & BFD_MACH_O_N_TYPE)
  932. {
  933. case BFD_MACH_O_N_UNDF:
  934. if (symbol->value == 0)
  935. name = "UND";
  936. else
  937. name = "COM";
  938. break;
  939. case BFD_MACH_O_N_ABS:
  940. name = "ABS";
  941. break;
  942. case BFD_MACH_O_N_INDR:
  943. name = "INDR";
  944. break;
  945. case BFD_MACH_O_N_PBUD:
  946. name = "PBUD";
  947. break;
  948. case BFD_MACH_O_N_SECT:
  949. name = "SECT";
  950. break;
  951. default:
  952. name = "???";
  953. break;
  954. }
  955. if (name == NULL)
  956. name = "";
  957. fprintf (file, " %02x %-6s %02x %04x",
  958. asym->n_type, name, asym->n_sect, asym->n_desc);
  959. if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
  960. && (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
  961. fprintf (file, " [%s]", symbol->section->name);
  962. fprintf (file, " %s", symbol->name);
  963. }
  964. }
  965. static void
  966. bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
  967. bfd_mach_o_cpu_subtype msubtype,
  968. enum bfd_architecture *type,
  969. unsigned long *subtype)
  970. {
  971. *subtype = bfd_arch_unknown;
  972. switch (mtype)
  973. {
  974. case BFD_MACH_O_CPU_TYPE_VAX:
  975. *type = bfd_arch_vax;
  976. break;
  977. case BFD_MACH_O_CPU_TYPE_MC680x0:
  978. *type = bfd_arch_m68k;
  979. break;
  980. case BFD_MACH_O_CPU_TYPE_I386:
  981. *type = bfd_arch_i386;
  982. *subtype = bfd_mach_i386_i386;
  983. break;
  984. case BFD_MACH_O_CPU_TYPE_X86_64:
  985. *type = bfd_arch_i386;
  986. *subtype = bfd_mach_x86_64;
  987. break;
  988. case BFD_MACH_O_CPU_TYPE_MIPS:
  989. *type = bfd_arch_mips;
  990. break;
  991. case BFD_MACH_O_CPU_TYPE_MC98000:
  992. *type = bfd_arch_m98k;
  993. break;
  994. case BFD_MACH_O_CPU_TYPE_HPPA:
  995. *type = bfd_arch_hppa;
  996. break;
  997. case BFD_MACH_O_CPU_TYPE_ARM:
  998. *type = bfd_arch_arm;
  999. switch (msubtype)
  1000. {
  1001. case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
  1002. *subtype = bfd_mach_arm_4T;
  1003. break;
  1004. case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
  1005. *subtype = bfd_mach_arm_4T; /* Best fit ? */
  1006. break;
  1007. case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
  1008. *subtype = bfd_mach_arm_5TE;
  1009. break;
  1010. case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
  1011. *subtype = bfd_mach_arm_XScale;
  1012. break;
  1013. case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
  1014. *subtype = bfd_mach_arm_5TE; /* Best fit ? */
  1015. break;
  1016. case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
  1017. default:
  1018. break;
  1019. }
  1020. break;
  1021. case BFD_MACH_O_CPU_TYPE_SPARC:
  1022. *type = bfd_arch_sparc;
  1023. *subtype = bfd_mach_sparc;
  1024. break;
  1025. case BFD_MACH_O_CPU_TYPE_ALPHA:
  1026. *type = bfd_arch_alpha;
  1027. break;
  1028. case BFD_MACH_O_CPU_TYPE_POWERPC:
  1029. *type = bfd_arch_powerpc;
  1030. *subtype = bfd_mach_ppc;
  1031. break;
  1032. case BFD_MACH_O_CPU_TYPE_POWERPC_64:
  1033. *type = bfd_arch_powerpc;
  1034. *subtype = bfd_mach_ppc64;
  1035. break;
  1036. case BFD_MACH_O_CPU_TYPE_ARM64:
  1037. *type = bfd_arch_aarch64;
  1038. *subtype = bfd_mach_aarch64;
  1039. break;
  1040. default:
  1041. *type = bfd_arch_unknown;
  1042. break;
  1043. }
  1044. }
  1045. /* Write n NUL bytes to ABFD so that LEN + n is a multiple of 4. Return the
  1046. number of bytes written or -1 in case of error. */
  1047. static int
  1048. bfd_mach_o_pad4 (bfd *abfd, unsigned int len)
  1049. {
  1050. if (len % 4 != 0)
  1051. {
  1052. char pad[4] = {0,0,0,0};
  1053. unsigned int padlen = 4 - (len % 4);
  1054. if (bfd_bwrite (pad, padlen, abfd) != padlen)
  1055. return -1;
  1056. return padlen;
  1057. }
  1058. else
  1059. return 0;
  1060. }
  1061. /* Likewise, but for a command. */
  1062. static int
  1063. bfd_mach_o_pad_command (bfd *abfd, unsigned int len)
  1064. {
  1065. unsigned int align = bfd_mach_o_wide_p (abfd) ? 8 : 4;
  1066. if (len % align != 0)
  1067. {
  1068. char pad[8] = {0};
  1069. unsigned int padlen = align - (len % align);
  1070. if (bfd_bwrite (pad, padlen, abfd) != padlen)
  1071. return -1;
  1072. return padlen;
  1073. }
  1074. else
  1075. return 0;
  1076. }
  1077. static bool
  1078. bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
  1079. {
  1080. struct mach_o_header_external raw;
  1081. unsigned int size;
  1082. size = mach_o_wide_p (header) ?
  1083. BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
  1084. bfd_h_put_32 (abfd, header->magic, raw.magic);
  1085. bfd_h_put_32 (abfd, header->cputype, raw.cputype);
  1086. bfd_h_put_32 (abfd, header->cpusubtype, raw.cpusubtype);
  1087. bfd_h_put_32 (abfd, header->filetype, raw.filetype);
  1088. bfd_h_put_32 (abfd, header->ncmds, raw.ncmds);
  1089. bfd_h_put_32 (abfd, header->sizeofcmds, raw.sizeofcmds);
  1090. bfd_h_put_32 (abfd, header->flags, raw.flags);
  1091. if (mach_o_wide_p (header))
  1092. bfd_h_put_32 (abfd, header->reserved, raw.reserved);
  1093. if (bfd_seek (abfd, 0, SEEK_SET) != 0
  1094. || bfd_bwrite (&raw, size, abfd) != size)
  1095. return false;
  1096. return true;
  1097. }
  1098. static bool
  1099. bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
  1100. {
  1101. bfd_mach_o_thread_command *cmd = &command->command.thread;
  1102. unsigned int i;
  1103. struct mach_o_thread_command_external raw;
  1104. unsigned int offset;
  1105. BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
  1106. || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
  1107. offset = BFD_MACH_O_LC_SIZE;
  1108. for (i = 0; i < cmd->nflavours; i++)
  1109. {
  1110. BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
  1111. BFD_ASSERT (cmd->flavours[i].offset ==
  1112. (command->offset + offset + BFD_MACH_O_LC_SIZE));
  1113. bfd_h_put_32 (abfd, cmd->flavours[i].flavour, raw.flavour);
  1114. bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), raw.count);
  1115. if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
  1116. || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1117. return false;
  1118. offset += cmd->flavours[i].size + sizeof (raw);
  1119. }
  1120. return true;
  1121. }
  1122. static bool
  1123. bfd_mach_o_write_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
  1124. {
  1125. bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
  1126. struct mach_o_str_command_external raw;
  1127. unsigned int namelen;
  1128. bfd_h_put_32 (abfd, cmd->name_offset, raw.str);
  1129. if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
  1130. || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1131. return false;
  1132. namelen = strlen (cmd->name_str) + 1;
  1133. if (bfd_bwrite (cmd->name_str, namelen, abfd) != namelen)
  1134. return false;
  1135. if (bfd_mach_o_pad_command (abfd, namelen) < 0)
  1136. return false;
  1137. return true;
  1138. }
  1139. static bool
  1140. bfd_mach_o_write_dylib (bfd *abfd, bfd_mach_o_load_command *command)
  1141. {
  1142. bfd_mach_o_dylib_command *cmd = &command->command.dylib;
  1143. struct mach_o_dylib_command_external raw;
  1144. unsigned int namelen;
  1145. bfd_h_put_32 (abfd, cmd->name_offset, raw.name);
  1146. bfd_h_put_32 (abfd, cmd->timestamp, raw.timestamp);
  1147. bfd_h_put_32 (abfd, cmd->current_version, raw.current_version);
  1148. bfd_h_put_32 (abfd, cmd->compatibility_version, raw.compatibility_version);
  1149. if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
  1150. || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1151. return false;
  1152. namelen = strlen (cmd->name_str) + 1;
  1153. if (bfd_bwrite (cmd->name_str, namelen, abfd) != namelen)
  1154. return false;
  1155. if (bfd_mach_o_pad_command (abfd, namelen) < 0)
  1156. return false;
  1157. return true;
  1158. }
  1159. static bool
  1160. bfd_mach_o_write_main (bfd *abfd, bfd_mach_o_load_command *command)
  1161. {
  1162. bfd_mach_o_main_command *cmd = &command->command.main;
  1163. struct mach_o_entry_point_command_external raw;
  1164. bfd_h_put_64 (abfd, cmd->entryoff, raw.entryoff);
  1165. bfd_h_put_64 (abfd, cmd->stacksize, raw.stacksize);
  1166. if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
  1167. || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1168. return false;
  1169. return true;
  1170. }
  1171. static bool
  1172. bfd_mach_o_write_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
  1173. {
  1174. bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
  1175. struct mach_o_dyld_info_command_external raw;
  1176. bfd_h_put_32 (abfd, cmd->rebase_off, raw.rebase_off);
  1177. bfd_h_put_32 (abfd, cmd->rebase_size, raw.rebase_size);
  1178. bfd_h_put_32 (abfd, cmd->bind_off, raw.bind_off);
  1179. bfd_h_put_32 (abfd, cmd->bind_size, raw.bind_size);
  1180. bfd_h_put_32 (abfd, cmd->weak_bind_off, raw.weak_bind_off);
  1181. bfd_h_put_32 (abfd, cmd->weak_bind_size, raw.weak_bind_size);
  1182. bfd_h_put_32 (abfd, cmd->lazy_bind_off, raw.lazy_bind_off);
  1183. bfd_h_put_32 (abfd, cmd->lazy_bind_size, raw.lazy_bind_size);
  1184. bfd_h_put_32 (abfd, cmd->export_off, raw.export_off);
  1185. bfd_h_put_32 (abfd, cmd->export_size, raw.export_size);
  1186. if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
  1187. || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1188. return false;
  1189. if (cmd->rebase_size != 0)
  1190. if (bfd_seek (abfd, cmd->rebase_off, SEEK_SET) != 0
  1191. || (bfd_bwrite (cmd->rebase_content, cmd->rebase_size, abfd) !=
  1192. cmd->rebase_size))
  1193. return false;
  1194. if (cmd->bind_size != 0)
  1195. if (bfd_seek (abfd, cmd->bind_off, SEEK_SET) != 0
  1196. || (bfd_bwrite (cmd->bind_content, cmd->bind_size, abfd) !=
  1197. cmd->bind_size))
  1198. return false;
  1199. if (cmd->weak_bind_size != 0)
  1200. if (bfd_seek (abfd, cmd->weak_bind_off, SEEK_SET) != 0
  1201. || (bfd_bwrite (cmd->weak_bind_content, cmd->weak_bind_size, abfd) !=
  1202. cmd->weak_bind_size))
  1203. return false;
  1204. if (cmd->lazy_bind_size != 0)
  1205. if (bfd_seek (abfd, cmd->lazy_bind_off, SEEK_SET) != 0
  1206. || (bfd_bwrite (cmd->lazy_bind_content, cmd->lazy_bind_size, abfd) !=
  1207. cmd->lazy_bind_size))
  1208. return false;
  1209. if (cmd->export_size != 0)
  1210. if (bfd_seek (abfd, cmd->export_off, SEEK_SET) != 0
  1211. || (bfd_bwrite (cmd->export_content, cmd->export_size, abfd) !=
  1212. cmd->export_size))
  1213. return false;
  1214. return true;
  1215. }
  1216. long
  1217. bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
  1218. asection *asect)
  1219. {
  1220. #if SIZEOF_LONG == SIZEOF_INT
  1221. if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
  1222. {
  1223. bfd_set_error (bfd_error_file_too_big);
  1224. return -1;
  1225. }
  1226. #endif
  1227. return (asect->reloc_count + 1L) * sizeof (arelent *);
  1228. }
  1229. /* In addition to the need to byte-swap the symbol number, the bit positions
  1230. of the fields in the relocation information vary per target endian-ness. */
  1231. void
  1232. bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel,
  1233. unsigned char *fields)
  1234. {
  1235. unsigned char info = fields[3];
  1236. if (bfd_big_endian (abfd))
  1237. {
  1238. rel->r_value = (fields[0] << 16) | (fields[1] << 8) | fields[2];
  1239. rel->r_type = (info >> BFD_MACH_O_BE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
  1240. rel->r_pcrel = (info & BFD_MACH_O_BE_PCREL) ? 1 : 0;
  1241. rel->r_length = (info >> BFD_MACH_O_BE_LENGTH_SHIFT)
  1242. & BFD_MACH_O_LENGTH_MASK;
  1243. rel->r_extern = (info & BFD_MACH_O_BE_EXTERN) ? 1 : 0;
  1244. }
  1245. else
  1246. {
  1247. rel->r_value = (fields[2] << 16) | (fields[1] << 8) | fields[0];
  1248. rel->r_type = (info >> BFD_MACH_O_LE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
  1249. rel->r_pcrel = (info & BFD_MACH_O_LE_PCREL) ? 1 : 0;
  1250. rel->r_length = (info >> BFD_MACH_O_LE_LENGTH_SHIFT)
  1251. & BFD_MACH_O_LENGTH_MASK;
  1252. rel->r_extern = (info & BFD_MACH_O_LE_EXTERN) ? 1 : 0;
  1253. }
  1254. }
  1255. /* Set syms_ptr_ptr and addend of RES. */
  1256. bool
  1257. bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd,
  1258. bfd_mach_o_reloc_info *reloc,
  1259. arelent *res, asymbol **syms)
  1260. {
  1261. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  1262. unsigned int num;
  1263. asymbol **sym;
  1264. /* Non-scattered relocation. */
  1265. reloc->r_scattered = 0;
  1266. res->addend = 0;
  1267. num = reloc->r_value;
  1268. if (reloc->r_extern)
  1269. {
  1270. /* PR 17512: file: 8396-1185-0.004. */
  1271. if (num >= (unsigned) bfd_mach_o_count_symbols (abfd))
  1272. sym = bfd_und_section_ptr->symbol_ptr_ptr;
  1273. else if (syms == NULL)
  1274. sym = bfd_und_section_ptr->symbol_ptr_ptr;
  1275. else
  1276. /* An external symbol number. */
  1277. sym = syms + num;
  1278. }
  1279. else if (num == 0x00ffffff || num == 0)
  1280. {
  1281. /* The 'symnum' in a non-scattered PAIR is 0x00ffffff. But as this
  1282. is generic code, we don't know wether this is really a PAIR.
  1283. This value is almost certainly not a valid section number, hence
  1284. this specific case to avoid an assertion failure.
  1285. Target specific swap_reloc_in routine should adjust that. */
  1286. sym = bfd_abs_section_ptr->symbol_ptr_ptr;
  1287. }
  1288. else
  1289. {
  1290. /* PR 17512: file: 006-2964-0.004. */
  1291. if (num > mdata->nsects)
  1292. {
  1293. _bfd_error_handler (_("\
  1294. malformed mach-o reloc: section index is greater than the number of sections"));
  1295. return false;
  1296. }
  1297. /* A section number. */
  1298. sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
  1299. /* For a symbol defined in section S, the addend (stored in the
  1300. binary) contains the address of the section. To comply with
  1301. bfd convention, subtract the section address.
  1302. Use the address from the header, so that the user can modify
  1303. the vma of the section. */
  1304. res->addend = -mdata->sections[num - 1]->addr;
  1305. }
  1306. /* Note: Pairs for PPC LO/HI/HA are not scattered, but contain the offset
  1307. in the lower 16bits of the address value. So we have to find the
  1308. 'symbol' from the preceding reloc. We do this even though the
  1309. section symbol is probably not needed here, because NULL symbol
  1310. values cause an assert in generic BFD code. This must be done in
  1311. the PPC swap_reloc_in routine. */
  1312. res->sym_ptr_ptr = sym;
  1313. return true;
  1314. }
  1315. /* Do most of the work for canonicalize_relocs on RAW: create internal
  1316. representation RELOC and set most fields of RES using symbol table SYMS.
  1317. Each target still has to set the howto of RES and possibly adjust other
  1318. fields.
  1319. Previously the Mach-O hook point was simply swap_in, but some targets
  1320. (like arm64) don't follow the generic rules (symnum is a value for the
  1321. non-scattered relocation ADDEND). */
  1322. bool
  1323. bfd_mach_o_pre_canonicalize_one_reloc (bfd *abfd,
  1324. struct mach_o_reloc_info_external *raw,
  1325. bfd_mach_o_reloc_info *reloc,
  1326. arelent *res, asymbol **syms)
  1327. {
  1328. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  1329. bfd_vma addr;
  1330. addr = bfd_get_32 (abfd, raw->r_address);
  1331. res->sym_ptr_ptr = NULL;
  1332. res->addend = 0;
  1333. if (addr & BFD_MACH_O_SR_SCATTERED)
  1334. {
  1335. unsigned int j;
  1336. bfd_vma symnum = bfd_get_32 (abfd, raw->r_symbolnum);
  1337. /* Scattered relocation, can't be extern. */
  1338. reloc->r_scattered = 1;
  1339. reloc->r_extern = 0;
  1340. /* Extract section and offset from r_value (symnum). */
  1341. reloc->r_value = symnum;
  1342. /* FIXME: This breaks when a symbol in a reloc exactly follows the
  1343. end of the data for the section (e.g. in a calculation of section
  1344. data length). At present, the symbol will end up associated with
  1345. the following section or, if it falls within alignment padding, as
  1346. null - which will assert later. */
  1347. for (j = 0; j < mdata->nsects; j++)
  1348. {
  1349. bfd_mach_o_section *sect = mdata->sections[j];
  1350. if (symnum >= sect->addr && symnum < sect->addr + sect->size)
  1351. {
  1352. res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
  1353. res->addend = symnum - sect->addr;
  1354. break;
  1355. }
  1356. }
  1357. /* Extract the info and address fields from r_address. */
  1358. reloc->r_type = BFD_MACH_O_GET_SR_TYPE (addr);
  1359. reloc->r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
  1360. reloc->r_pcrel = addr & BFD_MACH_O_SR_PCREL;
  1361. reloc->r_address = BFD_MACH_O_GET_SR_TYPE (addr);
  1362. res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
  1363. }
  1364. else
  1365. {
  1366. /* Non-scattered relocation. */
  1367. reloc->r_scattered = 0;
  1368. reloc->r_address = addr;
  1369. res->address = addr;
  1370. /* The value and info fields have to be extracted dependent on target
  1371. endian-ness. */
  1372. bfd_mach_o_swap_in_non_scattered_reloc (abfd, reloc, raw->r_symbolnum);
  1373. if (!bfd_mach_o_canonicalize_non_scattered_reloc (abfd, reloc,
  1374. res, syms))
  1375. return false;
  1376. }
  1377. /* We have set up a reloc with all the information present, so the swapper
  1378. can modify address, value and addend fields, if necessary, to convey
  1379. information in the generic BFD reloc that is mach-o specific. */
  1380. return true;
  1381. }
  1382. static int
  1383. bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
  1384. unsigned long count,
  1385. arelent *res, asymbol **syms)
  1386. {
  1387. bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
  1388. unsigned long i;
  1389. struct mach_o_reloc_info_external *native_relocs = NULL;
  1390. size_t native_size;
  1391. /* Allocate and read relocs. */
  1392. if (_bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &native_size))
  1393. /* PR 17512: file: 09477b57. */
  1394. goto err;
  1395. if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
  1396. return -1;
  1397. native_relocs = (struct mach_o_reloc_info_external *)
  1398. _bfd_malloc_and_read (abfd, native_size, native_size);
  1399. if (native_relocs == NULL)
  1400. return -1;
  1401. for (i = 0; i < count; i++)
  1402. {
  1403. if (!(*bed->_bfd_mach_o_canonicalize_one_reloc)(abfd, &native_relocs[i],
  1404. &res[i], syms, res))
  1405. goto err;
  1406. }
  1407. free (native_relocs);
  1408. return i;
  1409. err:
  1410. free (native_relocs);
  1411. if (bfd_get_error () == bfd_error_no_error)
  1412. bfd_set_error (bfd_error_invalid_operation);
  1413. return -1;
  1414. }
  1415. long
  1416. bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
  1417. arelent **rels, asymbol **syms)
  1418. {
  1419. bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
  1420. unsigned long i;
  1421. arelent *res;
  1422. if (asect->reloc_count == 0)
  1423. return 0;
  1424. /* No need to go further if we don't know how to read relocs. */
  1425. if (bed->_bfd_mach_o_canonicalize_one_reloc == NULL)
  1426. return 0;
  1427. if (asect->relocation == NULL)
  1428. {
  1429. size_t amt;
  1430. if (_bfd_mul_overflow (asect->reloc_count, sizeof (arelent), &amt))
  1431. return -1;
  1432. res = bfd_malloc (amt);
  1433. if (res == NULL)
  1434. return -1;
  1435. if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
  1436. asect->reloc_count, res, syms) < 0)
  1437. {
  1438. free (res);
  1439. return -1;
  1440. }
  1441. asect->relocation = res;
  1442. }
  1443. res = asect->relocation;
  1444. for (i = 0; i < asect->reloc_count; i++)
  1445. rels[i] = &res[i];
  1446. rels[i] = NULL;
  1447. return i;
  1448. }
  1449. long
  1450. bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
  1451. {
  1452. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  1453. if (mdata->dysymtab == NULL)
  1454. return 1;
  1455. return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel + 1)
  1456. * sizeof (arelent *);
  1457. }
  1458. long
  1459. bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
  1460. struct bfd_symbol **syms)
  1461. {
  1462. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  1463. bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
  1464. bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
  1465. unsigned long i;
  1466. arelent *res;
  1467. if (dysymtab == NULL)
  1468. return 0;
  1469. if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
  1470. return 0;
  1471. /* No need to go further if we don't know how to read relocs. */
  1472. if (bed->_bfd_mach_o_canonicalize_one_reloc == NULL)
  1473. return 0;
  1474. if (mdata->dyn_reloc_cache == NULL)
  1475. {
  1476. ufile_ptr filesize = bfd_get_file_size (abfd);
  1477. size_t amt;
  1478. if (filesize != 0)
  1479. {
  1480. if (dysymtab->extreloff > filesize
  1481. || dysymtab->nextrel > ((filesize - dysymtab->extreloff)
  1482. / BFD_MACH_O_RELENT_SIZE)
  1483. || dysymtab->locreloff > filesize
  1484. || dysymtab->nlocrel > ((filesize - dysymtab->locreloff)
  1485. / BFD_MACH_O_RELENT_SIZE))
  1486. {
  1487. bfd_set_error (bfd_error_file_truncated);
  1488. return -1;
  1489. }
  1490. }
  1491. if (_bfd_mul_overflow (dysymtab->nextrel + dysymtab->nlocrel,
  1492. sizeof (arelent), &amt))
  1493. {
  1494. bfd_set_error (bfd_error_file_too_big);
  1495. return -1;
  1496. }
  1497. res = bfd_malloc (amt);
  1498. if (res == NULL)
  1499. return -1;
  1500. if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
  1501. dysymtab->nextrel, res, syms) < 0)
  1502. {
  1503. free (res);
  1504. return -1;
  1505. }
  1506. if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
  1507. dysymtab->nlocrel,
  1508. res + dysymtab->nextrel, syms) < 0)
  1509. {
  1510. free (res);
  1511. return -1;
  1512. }
  1513. mdata->dyn_reloc_cache = res;
  1514. }
  1515. res = mdata->dyn_reloc_cache;
  1516. for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
  1517. rels[i] = &res[i];
  1518. rels[i] = NULL;
  1519. return i;
  1520. }
  1521. /* In addition to the need to byte-swap the symbol number, the bit positions
  1522. of the fields in the relocation information vary per target endian-ness. */
  1523. static void
  1524. bfd_mach_o_swap_out_non_scattered_reloc (bfd *abfd, unsigned char *fields,
  1525. bfd_mach_o_reloc_info *rel)
  1526. {
  1527. unsigned char info = 0;
  1528. BFD_ASSERT (rel->r_type <= 15);
  1529. BFD_ASSERT (rel->r_length <= 3);
  1530. if (bfd_big_endian (abfd))
  1531. {
  1532. fields[0] = (rel->r_value >> 16) & 0xff;
  1533. fields[1] = (rel->r_value >> 8) & 0xff;
  1534. fields[2] = rel->r_value & 0xff;
  1535. info |= rel->r_type << BFD_MACH_O_BE_TYPE_SHIFT;
  1536. info |= rel->r_pcrel ? BFD_MACH_O_BE_PCREL : 0;
  1537. info |= rel->r_length << BFD_MACH_O_BE_LENGTH_SHIFT;
  1538. info |= rel->r_extern ? BFD_MACH_O_BE_EXTERN : 0;
  1539. }
  1540. else
  1541. {
  1542. fields[2] = (rel->r_value >> 16) & 0xff;
  1543. fields[1] = (rel->r_value >> 8) & 0xff;
  1544. fields[0] = rel->r_value & 0xff;
  1545. info |= rel->r_type << BFD_MACH_O_LE_TYPE_SHIFT;
  1546. info |= rel->r_pcrel ? BFD_MACH_O_LE_PCREL : 0;
  1547. info |= rel->r_length << BFD_MACH_O_LE_LENGTH_SHIFT;
  1548. info |= rel->r_extern ? BFD_MACH_O_LE_EXTERN : 0;
  1549. }
  1550. fields[3] = info;
  1551. }
  1552. static bool
  1553. bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
  1554. {
  1555. unsigned int i;
  1556. arelent **entries;
  1557. asection *sec;
  1558. bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
  1559. sec = section->bfdsection;
  1560. if (sec->reloc_count == 0)
  1561. return true;
  1562. if (bed->_bfd_mach_o_swap_reloc_out == NULL)
  1563. return true;
  1564. if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
  1565. return false;
  1566. /* Convert and write. */
  1567. entries = section->bfdsection->orelocation;
  1568. for (i = 0; i < section->nreloc; i++)
  1569. {
  1570. arelent *rel = entries[i];
  1571. struct mach_o_reloc_info_external raw;
  1572. bfd_mach_o_reloc_info info, *pinfo = &info;
  1573. /* Convert relocation to an intermediate representation. */
  1574. if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
  1575. return false;
  1576. /* Lower the relocation info. */
  1577. if (pinfo->r_scattered)
  1578. {
  1579. unsigned long v;
  1580. v = BFD_MACH_O_SR_SCATTERED
  1581. | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
  1582. | BFD_MACH_O_SET_SR_LENGTH (pinfo->r_length)
  1583. | BFD_MACH_O_SET_SR_TYPE (pinfo->r_type)
  1584. | BFD_MACH_O_SET_SR_ADDRESS (pinfo->r_address);
  1585. /* Note: scattered relocs have field in reverse order... */
  1586. bfd_put_32 (abfd, v, raw.r_address);
  1587. bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum);
  1588. }
  1589. else
  1590. {
  1591. bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
  1592. bfd_mach_o_swap_out_non_scattered_reloc (abfd, raw.r_symbolnum,
  1593. pinfo);
  1594. }
  1595. if (bfd_bwrite (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
  1596. != BFD_MACH_O_RELENT_SIZE)
  1597. return false;
  1598. }
  1599. return true;
  1600. }
  1601. static bool
  1602. bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
  1603. {
  1604. struct mach_o_section_32_external raw;
  1605. memcpy (raw.sectname, section->sectname, 16);
  1606. memcpy (raw.segname, section->segname, 16);
  1607. bfd_h_put_32 (abfd, section->addr, raw.addr);
  1608. bfd_h_put_32 (abfd, section->size, raw.size);
  1609. bfd_h_put_32 (abfd, section->offset, raw.offset);
  1610. bfd_h_put_32 (abfd, section->align, raw.align);
  1611. bfd_h_put_32 (abfd, section->reloff, raw.reloff);
  1612. bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
  1613. bfd_h_put_32 (abfd, section->flags, raw.flags);
  1614. bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
  1615. bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
  1616. if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
  1617. != BFD_MACH_O_SECTION_SIZE)
  1618. return false;
  1619. return true;
  1620. }
  1621. static bool
  1622. bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
  1623. {
  1624. struct mach_o_section_64_external raw;
  1625. memcpy (raw.sectname, section->sectname, 16);
  1626. memcpy (raw.segname, section->segname, 16);
  1627. bfd_h_put_64 (abfd, section->addr, raw.addr);
  1628. bfd_h_put_64 (abfd, section->size, raw.size);
  1629. bfd_h_put_32 (abfd, section->offset, raw.offset);
  1630. bfd_h_put_32 (abfd, section->align, raw.align);
  1631. bfd_h_put_32 (abfd, section->reloff, raw.reloff);
  1632. bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
  1633. bfd_h_put_32 (abfd, section->flags, raw.flags);
  1634. bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
  1635. bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
  1636. bfd_h_put_32 (abfd, section->reserved3, raw.reserved3);
  1637. if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
  1638. != BFD_MACH_O_SECTION_64_SIZE)
  1639. return false;
  1640. return true;
  1641. }
  1642. static bool
  1643. bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
  1644. {
  1645. struct mach_o_segment_command_32_external raw;
  1646. bfd_mach_o_segment_command *seg = &command->command.segment;
  1647. bfd_mach_o_section *sec;
  1648. BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
  1649. for (sec = seg->sect_head; sec != NULL; sec = sec->next)
  1650. if (!bfd_mach_o_write_relocs (abfd, sec))
  1651. return false;
  1652. memcpy (raw.segname, seg->segname, 16);
  1653. bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
  1654. bfd_h_put_32 (abfd, seg->vmsize, raw.vmsize);
  1655. bfd_h_put_32 (abfd, seg->fileoff, raw.fileoff);
  1656. bfd_h_put_32 (abfd, seg->filesize, raw.filesize);
  1657. bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
  1658. bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
  1659. bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
  1660. bfd_h_put_32 (abfd, seg->flags, raw.flags);
  1661. if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
  1662. || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1663. return false;
  1664. for (sec = seg->sect_head; sec != NULL; sec = sec->next)
  1665. if (!bfd_mach_o_write_section_32 (abfd, sec))
  1666. return false;
  1667. return true;
  1668. }
  1669. static bool
  1670. bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
  1671. {
  1672. struct mach_o_segment_command_64_external raw;
  1673. bfd_mach_o_segment_command *seg = &command->command.segment;
  1674. bfd_mach_o_section *sec;
  1675. BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
  1676. for (sec = seg->sect_head; sec != NULL; sec = sec->next)
  1677. if (!bfd_mach_o_write_relocs (abfd, sec))
  1678. return false;
  1679. memcpy (raw.segname, seg->segname, 16);
  1680. bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
  1681. bfd_h_put_64 (abfd, seg->vmsize, raw.vmsize);
  1682. bfd_h_put_64 (abfd, seg->fileoff, raw.fileoff);
  1683. bfd_h_put_64 (abfd, seg->filesize, raw.filesize);
  1684. bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
  1685. bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
  1686. bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
  1687. bfd_h_put_32 (abfd, seg->flags, raw.flags);
  1688. if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
  1689. || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1690. return false;
  1691. for (sec = seg->sect_head; sec != NULL; sec = sec->next)
  1692. if (!bfd_mach_o_write_section_64 (abfd, sec))
  1693. return false;
  1694. return true;
  1695. }
  1696. static bool
  1697. bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym)
  1698. {
  1699. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  1700. unsigned long i;
  1701. unsigned int wide = bfd_mach_o_wide_p (abfd);
  1702. struct bfd_strtab_hash *strtab;
  1703. asymbol **symbols = bfd_get_outsymbols (abfd);
  1704. int padlen;
  1705. /* Write the symbols first. */
  1706. if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
  1707. return false;
  1708. strtab = _bfd_stringtab_init ();
  1709. if (strtab == NULL)
  1710. return false;
  1711. if (sym->nsyms > 0)
  1712. /* Although we don't strictly need to do this, for compatibility with
  1713. Darwin system tools, actually output an empty string for the index
  1714. 0 entry. */
  1715. _bfd_stringtab_add (strtab, "", true, false);
  1716. for (i = 0; i < sym->nsyms; i++)
  1717. {
  1718. bfd_size_type str_index;
  1719. bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
  1720. if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
  1721. /* An index of 0 always means the empty string. */
  1722. str_index = 0;
  1723. else
  1724. {
  1725. str_index = _bfd_stringtab_add (strtab, s->symbol.name, true, false);
  1726. if (str_index == (bfd_size_type) -1)
  1727. goto err;
  1728. }
  1729. if (wide)
  1730. {
  1731. struct mach_o_nlist_64_external raw;
  1732. bfd_h_put_32 (abfd, str_index, raw.n_strx);
  1733. bfd_h_put_8 (abfd, s->n_type, raw.n_type);
  1734. bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
  1735. bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
  1736. bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value,
  1737. raw.n_value);
  1738. if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1739. goto err;
  1740. }
  1741. else
  1742. {
  1743. struct mach_o_nlist_external raw;
  1744. bfd_h_put_32 (abfd, str_index, raw.n_strx);
  1745. bfd_h_put_8 (abfd, s->n_type, raw.n_type);
  1746. bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
  1747. bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
  1748. bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value,
  1749. raw.n_value);
  1750. if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1751. goto err;
  1752. }
  1753. }
  1754. sym->strsize = _bfd_stringtab_size (strtab);
  1755. sym->stroff = mdata->filelen;
  1756. mdata->filelen += sym->strsize;
  1757. if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0)
  1758. goto err;
  1759. if (!_bfd_stringtab_emit (abfd, strtab))
  1760. goto err;
  1761. /* Pad string table. */
  1762. padlen = bfd_mach_o_pad4 (abfd, sym->strsize);
  1763. if (padlen < 0)
  1764. return false;
  1765. mdata->filelen += padlen;
  1766. sym->strsize += padlen;
  1767. return true;
  1768. err:
  1769. _bfd_stringtab_free (strtab);
  1770. sym->strsize = 0;
  1771. return false;
  1772. }
  1773. static bool
  1774. bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
  1775. {
  1776. bfd_mach_o_symtab_command *sym = &command->command.symtab;
  1777. struct mach_o_symtab_command_external raw;
  1778. BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
  1779. /* The command. */
  1780. bfd_h_put_32 (abfd, sym->symoff, raw.symoff);
  1781. bfd_h_put_32 (abfd, sym->nsyms, raw.nsyms);
  1782. bfd_h_put_32 (abfd, sym->stroff, raw.stroff);
  1783. bfd_h_put_32 (abfd, sym->strsize, raw.strsize);
  1784. if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
  1785. || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1786. return false;
  1787. return true;
  1788. }
  1789. /* Count the number of indirect symbols in the image.
  1790. Requires that the sections are in their final order. */
  1791. static unsigned int
  1792. bfd_mach_o_count_indirect_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata)
  1793. {
  1794. unsigned int i;
  1795. unsigned int nisyms = 0;
  1796. for (i = 0; i < mdata->nsects; ++i)
  1797. {
  1798. bfd_mach_o_section *sec = mdata->sections[i];
  1799. switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
  1800. {
  1801. case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
  1802. case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
  1803. case BFD_MACH_O_S_SYMBOL_STUBS:
  1804. nisyms += bfd_mach_o_section_get_nbr_indirect (abfd, sec);
  1805. break;
  1806. default:
  1807. break;
  1808. }
  1809. }
  1810. return nisyms;
  1811. }
  1812. /* Create the dysymtab. */
  1813. static bool
  1814. bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd)
  1815. {
  1816. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  1817. /* TODO:
  1818. We are not going to try and fill these in yet and, moreover, we are
  1819. going to bail if they are already set. */
  1820. if (cmd->nmodtab != 0
  1821. || cmd->ntoc != 0
  1822. || cmd->nextrefsyms != 0)
  1823. {
  1824. _bfd_error_handler (_("sorry: modtab, toc and extrefsyms are not yet"
  1825. " implemented for dysymtab commands."));
  1826. return false;
  1827. }
  1828. cmd->ilocalsym = 0;
  1829. if (bfd_get_symcount (abfd) > 0)
  1830. {
  1831. asymbol **symbols = bfd_get_outsymbols (abfd);
  1832. unsigned long i;
  1833. /* Count the number of each kind of symbol. */
  1834. for (i = 0; i < bfd_get_symcount (abfd); ++i)
  1835. {
  1836. bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
  1837. if (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT))
  1838. break;
  1839. }
  1840. cmd->nlocalsym = i;
  1841. cmd->iextdefsym = i;
  1842. for (; i < bfd_get_symcount (abfd); ++i)
  1843. {
  1844. bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
  1845. if ((s->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_UNDF)
  1846. break;
  1847. }
  1848. cmd->nextdefsym = i - cmd->nlocalsym;
  1849. cmd->iundefsym = cmd->nextdefsym + cmd->iextdefsym;
  1850. cmd->nundefsym = bfd_get_symcount (abfd)
  1851. - cmd->nlocalsym
  1852. - cmd->nextdefsym;
  1853. }
  1854. else
  1855. {
  1856. cmd->nlocalsym = 0;
  1857. cmd->iextdefsym = 0;
  1858. cmd->nextdefsym = 0;
  1859. cmd->iundefsym = 0;
  1860. cmd->nundefsym = 0;
  1861. }
  1862. cmd->nindirectsyms = bfd_mach_o_count_indirect_symbols (abfd, mdata);
  1863. if (cmd->nindirectsyms > 0)
  1864. {
  1865. unsigned i;
  1866. unsigned n;
  1867. size_t amt;
  1868. mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
  1869. cmd->indirectsymoff = mdata->filelen;
  1870. if (_bfd_mul_overflow (cmd->nindirectsyms, 4, &amt))
  1871. return false;
  1872. mdata->filelen += amt;
  1873. cmd->indirect_syms = bfd_zalloc (abfd, amt);
  1874. if (cmd->indirect_syms == NULL)
  1875. return false;
  1876. n = 0;
  1877. for (i = 0; i < mdata->nsects; ++i)
  1878. {
  1879. bfd_mach_o_section *sec = mdata->sections[i];
  1880. switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
  1881. {
  1882. case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
  1883. case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
  1884. case BFD_MACH_O_S_SYMBOL_STUBS:
  1885. {
  1886. unsigned j, num;
  1887. bfd_mach_o_asymbol **isyms = sec->indirect_syms;
  1888. num = bfd_mach_o_section_get_nbr_indirect (abfd, sec);
  1889. if (isyms == NULL || num == 0)
  1890. break;
  1891. /* Record the starting index in the reserved1 field. */
  1892. sec->reserved1 = n;
  1893. for (j = 0; j < num; j++, n++)
  1894. {
  1895. if (isyms[j] == NULL)
  1896. cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL;
  1897. else if (isyms[j]->symbol.section == bfd_abs_section_ptr
  1898. && ! (isyms[j]->n_type & BFD_MACH_O_N_EXT))
  1899. cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL
  1900. | BFD_MACH_O_INDIRECT_SYM_ABS;
  1901. else
  1902. cmd->indirect_syms[n] = isyms[j]->symbol.udata.i;
  1903. }
  1904. }
  1905. break;
  1906. default:
  1907. break;
  1908. }
  1909. }
  1910. }
  1911. return true;
  1912. }
  1913. /* Write a dysymtab command.
  1914. TODO: Possibly coalesce writes of smaller objects. */
  1915. static bool
  1916. bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
  1917. {
  1918. bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
  1919. BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
  1920. if (cmd->nmodtab != 0)
  1921. {
  1922. unsigned int i;
  1923. if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
  1924. return false;
  1925. for (i = 0; i < cmd->nmodtab; i++)
  1926. {
  1927. bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
  1928. unsigned int iinit;
  1929. unsigned int ninit;
  1930. iinit = module->iinit & 0xffff;
  1931. iinit |= ((module->iterm & 0xffff) << 16);
  1932. ninit = module->ninit & 0xffff;
  1933. ninit |= ((module->nterm & 0xffff) << 16);
  1934. if (bfd_mach_o_wide_p (abfd))
  1935. {
  1936. struct mach_o_dylib_module_64_external w;
  1937. bfd_h_put_32 (abfd, module->module_name_idx, &w.module_name);
  1938. bfd_h_put_32 (abfd, module->iextdefsym, &w.iextdefsym);
  1939. bfd_h_put_32 (abfd, module->nextdefsym, &w.nextdefsym);
  1940. bfd_h_put_32 (abfd, module->irefsym, &w.irefsym);
  1941. bfd_h_put_32 (abfd, module->nrefsym, &w.nrefsym);
  1942. bfd_h_put_32 (abfd, module->ilocalsym, &w.ilocalsym);
  1943. bfd_h_put_32 (abfd, module->nlocalsym, &w.nlocalsym);
  1944. bfd_h_put_32 (abfd, module->iextrel, &w.iextrel);
  1945. bfd_h_put_32 (abfd, module->nextrel, &w.nextrel);
  1946. bfd_h_put_32 (abfd, iinit, &w.iinit_iterm);
  1947. bfd_h_put_32 (abfd, ninit, &w.ninit_nterm);
  1948. bfd_h_put_64 (abfd, module->objc_module_info_addr,
  1949. &w.objc_module_info_addr);
  1950. bfd_h_put_32 (abfd, module->objc_module_info_size,
  1951. &w.objc_module_info_size);
  1952. if (bfd_bwrite ((void *) &w, sizeof (w), abfd) != sizeof (w))
  1953. return false;
  1954. }
  1955. else
  1956. {
  1957. struct mach_o_dylib_module_external n;
  1958. bfd_h_put_32 (abfd, module->module_name_idx, &n.module_name);
  1959. bfd_h_put_32 (abfd, module->iextdefsym, &n.iextdefsym);
  1960. bfd_h_put_32 (abfd, module->nextdefsym, &n.nextdefsym);
  1961. bfd_h_put_32 (abfd, module->irefsym, &n.irefsym);
  1962. bfd_h_put_32 (abfd, module->nrefsym, &n.nrefsym);
  1963. bfd_h_put_32 (abfd, module->ilocalsym, &n.ilocalsym);
  1964. bfd_h_put_32 (abfd, module->nlocalsym, &n.nlocalsym);
  1965. bfd_h_put_32 (abfd, module->iextrel, &n.iextrel);
  1966. bfd_h_put_32 (abfd, module->nextrel, &n.nextrel);
  1967. bfd_h_put_32 (abfd, iinit, &n.iinit_iterm);
  1968. bfd_h_put_32 (abfd, ninit, &n.ninit_nterm);
  1969. bfd_h_put_32 (abfd, module->objc_module_info_addr,
  1970. &n.objc_module_info_addr);
  1971. bfd_h_put_32 (abfd, module->objc_module_info_size,
  1972. &n.objc_module_info_size);
  1973. if (bfd_bwrite ((void *) &n, sizeof (n), abfd) != sizeof (n))
  1974. return false;
  1975. }
  1976. }
  1977. }
  1978. if (cmd->ntoc != 0)
  1979. {
  1980. unsigned int i;
  1981. if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
  1982. return false;
  1983. for (i = 0; i < cmd->ntoc; i++)
  1984. {
  1985. struct mach_o_dylib_table_of_contents_external raw;
  1986. bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
  1987. bfd_h_put_32 (abfd, toc->symbol_index, &raw.symbol_index);
  1988. bfd_h_put_32 (abfd, toc->module_index, &raw.module_index);
  1989. if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  1990. return false;
  1991. }
  1992. }
  1993. if (cmd->nindirectsyms > 0)
  1994. {
  1995. unsigned int i;
  1996. if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
  1997. return false;
  1998. for (i = 0; i < cmd->nindirectsyms; ++i)
  1999. {
  2000. unsigned char raw[4];
  2001. bfd_h_put_32 (abfd, cmd->indirect_syms[i], &raw);
  2002. if (bfd_bwrite (raw, sizeof (raw), abfd) != sizeof (raw))
  2003. return false;
  2004. }
  2005. }
  2006. if (cmd->nextrefsyms != 0)
  2007. {
  2008. unsigned int i;
  2009. if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
  2010. return false;
  2011. for (i = 0; i < cmd->nextrefsyms; i++)
  2012. {
  2013. unsigned long v;
  2014. unsigned char raw[4];
  2015. bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
  2016. /* Fields isym and flags are written as bit-fields, thus we need
  2017. a specific processing for endianness. */
  2018. if (bfd_big_endian (abfd))
  2019. {
  2020. v = ((ref->isym & 0xffffff) << 8);
  2021. v |= ref->flags & 0xff;
  2022. }
  2023. else
  2024. {
  2025. v = ref->isym & 0xffffff;
  2026. v |= ((ref->flags & 0xff) << 24);
  2027. }
  2028. bfd_h_put_32 (abfd, v, raw);
  2029. if (bfd_bwrite (raw, sizeof (raw), abfd) != sizeof (raw))
  2030. return false;
  2031. }
  2032. }
  2033. /* The command. */
  2034. if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0)
  2035. return false;
  2036. else
  2037. {
  2038. struct mach_o_dysymtab_command_external raw;
  2039. bfd_h_put_32 (abfd, cmd->ilocalsym, &raw.ilocalsym);
  2040. bfd_h_put_32 (abfd, cmd->nlocalsym, &raw.nlocalsym);
  2041. bfd_h_put_32 (abfd, cmd->iextdefsym, &raw.iextdefsym);
  2042. bfd_h_put_32 (abfd, cmd->nextdefsym, &raw.nextdefsym);
  2043. bfd_h_put_32 (abfd, cmd->iundefsym, &raw.iundefsym);
  2044. bfd_h_put_32 (abfd, cmd->nundefsym, &raw.nundefsym);
  2045. bfd_h_put_32 (abfd, cmd->tocoff, &raw.tocoff);
  2046. bfd_h_put_32 (abfd, cmd->ntoc, &raw.ntoc);
  2047. bfd_h_put_32 (abfd, cmd->modtaboff, &raw.modtaboff);
  2048. bfd_h_put_32 (abfd, cmd->nmodtab, &raw.nmodtab);
  2049. bfd_h_put_32 (abfd, cmd->extrefsymoff, &raw.extrefsymoff);
  2050. bfd_h_put_32 (abfd, cmd->nextrefsyms, &raw.nextrefsyms);
  2051. bfd_h_put_32 (abfd, cmd->indirectsymoff, &raw.indirectsymoff);
  2052. bfd_h_put_32 (abfd, cmd->nindirectsyms, &raw.nindirectsyms);
  2053. bfd_h_put_32 (abfd, cmd->extreloff, &raw.extreloff);
  2054. bfd_h_put_32 (abfd, cmd->nextrel, &raw.nextrel);
  2055. bfd_h_put_32 (abfd, cmd->locreloff, &raw.locreloff);
  2056. bfd_h_put_32 (abfd, cmd->nlocrel, &raw.nlocrel);
  2057. if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
  2058. return false;
  2059. }
  2060. return true;
  2061. }
  2062. static unsigned
  2063. bfd_mach_o_primary_symbol_sort_key (bfd_mach_o_asymbol *s)
  2064. {
  2065. unsigned mtyp = s->n_type & BFD_MACH_O_N_TYPE;
  2066. /* Just leave debug symbols where they are (pretend they are local, and
  2067. then they will just be sorted on position). */
  2068. if (s->n_type & BFD_MACH_O_N_STAB)
  2069. return 0;
  2070. /* Local (we should never see an undefined local AFAICT). */
  2071. if (! (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT)))
  2072. return 0;
  2073. /* Common symbols look like undefined externs. */
  2074. if (mtyp == BFD_MACH_O_N_UNDF)
  2075. return 2;
  2076. /* A defined non-local, non-debug symbol. */
  2077. return 1;
  2078. }
  2079. static int
  2080. bfd_mach_o_cf_symbols (const void *a, const void *b)
  2081. {
  2082. bfd_mach_o_asymbol *sa = *(bfd_mach_o_asymbol **) a;
  2083. bfd_mach_o_asymbol *sb = *(bfd_mach_o_asymbol **) b;
  2084. unsigned int soa, sob;
  2085. soa = bfd_mach_o_primary_symbol_sort_key (sa);
  2086. sob = bfd_mach_o_primary_symbol_sort_key (sb);
  2087. if (soa < sob)
  2088. return -1;
  2089. if (soa > sob)
  2090. return 1;
  2091. /* If it's local or stab, just preserve the input order. */
  2092. if (soa == 0)
  2093. {
  2094. if (sa->symbol.udata.i < sb->symbol.udata.i)
  2095. return -1;
  2096. if (sa->symbol.udata.i > sb->symbol.udata.i)
  2097. return 1;
  2098. /* This is probably an error. */
  2099. return 0;
  2100. }
  2101. /* The second sort key is name. */
  2102. return strcmp (sa->symbol.name, sb->symbol.name);
  2103. }
  2104. /* Process the symbols.
  2105. This should be OK for single-module files - but it is not likely to work
  2106. for multi-module shared libraries.
  2107. (a) If the application has not filled in the relevant mach-o fields, make
  2108. an estimate.
  2109. (b) Order them, like this:
  2110. ( i) local.
  2111. (unsorted)
  2112. ( ii) external defined
  2113. (by name)
  2114. (iii) external undefined/common
  2115. (by name)
  2116. ( iv) common
  2117. (by name)
  2118. */
  2119. static bool
  2120. bfd_mach_o_mangle_symbols (bfd *abfd)
  2121. {
  2122. unsigned long i;
  2123. asymbol **symbols = bfd_get_outsymbols (abfd);
  2124. if (symbols == NULL || bfd_get_symcount (abfd) == 0)
  2125. return true;
  2126. for (i = 0; i < bfd_get_symcount (abfd); i++)
  2127. {
  2128. bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
  2129. /* We use this value, which is out-of-range as a symbol index, to signal
  2130. that the mach-o-specific data are not filled in and need to be created
  2131. from the bfd values. It is much preferable for the application to do
  2132. this, since more meaningful diagnostics can be made that way. */
  2133. if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
  2134. {
  2135. /* No symbol information has been set - therefore determine
  2136. it from the bfd symbol flags/info. */
  2137. if (s->symbol.section == bfd_abs_section_ptr)
  2138. s->n_type = BFD_MACH_O_N_ABS;
  2139. else if (s->symbol.section == bfd_und_section_ptr)
  2140. {
  2141. s->n_type = BFD_MACH_O_N_UNDF;
  2142. if (s->symbol.flags & BSF_WEAK)
  2143. s->n_desc |= BFD_MACH_O_N_WEAK_REF;
  2144. /* mach-o automatically makes undefined symbols extern. */
  2145. s->n_type |= BFD_MACH_O_N_EXT;
  2146. s->symbol.flags |= BSF_GLOBAL;
  2147. }
  2148. else if (s->symbol.section == bfd_com_section_ptr)
  2149. {
  2150. s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
  2151. s->symbol.flags |= BSF_GLOBAL;
  2152. }
  2153. else
  2154. s->n_type = BFD_MACH_O_N_SECT;
  2155. }
  2156. /* Update external symbol bit in case objcopy changed it. */
  2157. if (s->symbol.flags & BSF_GLOBAL)
  2158. s->n_type |= BFD_MACH_O_N_EXT;
  2159. else
  2160. s->n_type &= ~BFD_MACH_O_N_EXT;
  2161. /* Put the section index in, where required. */
  2162. if ((s->symbol.section != bfd_abs_section_ptr
  2163. && s->symbol.section != bfd_und_section_ptr
  2164. && s->symbol.section != bfd_com_section_ptr)
  2165. || ((s->n_type & BFD_MACH_O_N_STAB) != 0
  2166. && s->symbol.name == NULL))
  2167. s->n_sect = s->symbol.section->output_section->target_index;
  2168. /* Number to preserve order for local and debug syms. */
  2169. s->symbol.udata.i = i;
  2170. }
  2171. /* Sort the symbols. */
  2172. qsort ((void *) symbols, (size_t) bfd_get_symcount (abfd),
  2173. sizeof (asymbol *), bfd_mach_o_cf_symbols);
  2174. for (i = 0; i < bfd_get_symcount (abfd); ++i)
  2175. {
  2176. bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
  2177. s->symbol.udata.i = i; /* renumber. */
  2178. }
  2179. return true;
  2180. }
  2181. /* We build a flat table of sections, which can be re-ordered if necessary.
  2182. Fill in the section number and other mach-o-specific data. */
  2183. static bool
  2184. bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
  2185. {
  2186. asection *sec;
  2187. unsigned target_index;
  2188. unsigned nsect;
  2189. size_t amt;
  2190. nsect = bfd_count_sections (abfd);
  2191. /* Don't do it if it's already set - assume the application knows what it's
  2192. doing. */
  2193. if (mdata->nsects == nsect
  2194. && (mdata->nsects == 0 || mdata->sections != NULL))
  2195. return true;
  2196. /* We need to check that this can be done... */
  2197. if (nsect > 255)
  2198. {
  2199. _bfd_error_handler (_("mach-o: there are too many sections (%u)"
  2200. " maximum is 255,\n"), nsect);
  2201. return false;
  2202. }
  2203. mdata->nsects = nsect;
  2204. amt = mdata->nsects * sizeof (bfd_mach_o_section *);
  2205. mdata->sections = bfd_alloc (abfd, amt);
  2206. if (mdata->sections == NULL)
  2207. return false;
  2208. /* Create Mach-O sections.
  2209. Section type, attribute and align should have been set when the
  2210. section was created - either read in or specified. */
  2211. target_index = 0;
  2212. for (sec = abfd->sections; sec; sec = sec->next)
  2213. {
  2214. unsigned bfd_align = bfd_section_alignment (sec);
  2215. bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
  2216. mdata->sections[target_index] = msect;
  2217. msect->addr = bfd_section_vma (sec);
  2218. msect->size = bfd_section_size (sec);
  2219. /* Use the largest alignment set, in case it was bumped after the
  2220. section was created. */
  2221. msect->align = msect->align > bfd_align ? msect->align : bfd_align;
  2222. msect->offset = 0;
  2223. sec->target_index = ++target_index;
  2224. }
  2225. return true;
  2226. }
  2227. bool
  2228. bfd_mach_o_write_contents (bfd *abfd)
  2229. {
  2230. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  2231. bfd_mach_o_load_command *cmd;
  2232. bfd_mach_o_symtab_command *symtab = NULL;
  2233. bfd_mach_o_dysymtab_command *dysymtab = NULL;
  2234. bfd_mach_o_segment_command *linkedit = NULL;
  2235. /* Make the commands, if not already present. */
  2236. if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd))
  2237. return false;
  2238. abfd->output_has_begun = true;
  2239. /* Write the header. */
  2240. if (!bfd_mach_o_write_header (abfd, &mdata->header))
  2241. return false;
  2242. /* First pass: allocate the linkedit segment. */
  2243. for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
  2244. switch (cmd->type)
  2245. {
  2246. case BFD_MACH_O_LC_SEGMENT_64:
  2247. case BFD_MACH_O_LC_SEGMENT:
  2248. if (strcmp (cmd->command.segment.segname, "__LINKEDIT") == 0)
  2249. linkedit = &cmd->command.segment;
  2250. break;
  2251. case BFD_MACH_O_LC_SYMTAB:
  2252. symtab = &cmd->command.symtab;
  2253. break;
  2254. case BFD_MACH_O_LC_DYSYMTAB:
  2255. dysymtab = &cmd->command.dysymtab;
  2256. break;
  2257. case BFD_MACH_O_LC_DYLD_INFO:
  2258. {
  2259. bfd_mach_o_dyld_info_command *di = &cmd->command.dyld_info;
  2260. if (di->rebase_size != 0)
  2261. {
  2262. di->rebase_off = mdata->filelen;
  2263. mdata->filelen += di->rebase_size;
  2264. }
  2265. if (di->bind_size != 0)
  2266. {
  2267. di->bind_off = mdata->filelen;
  2268. mdata->filelen += di->bind_size;
  2269. }
  2270. if (di->weak_bind_size != 0)
  2271. {
  2272. di->weak_bind_off = mdata->filelen;
  2273. mdata->filelen += di->weak_bind_size;
  2274. }
  2275. if (di->lazy_bind_size != 0)
  2276. {
  2277. di->lazy_bind_off = mdata->filelen;
  2278. mdata->filelen += di->lazy_bind_size;
  2279. }
  2280. if (di->export_size != 0)
  2281. {
  2282. di->export_off = mdata->filelen;
  2283. mdata->filelen += di->export_size;
  2284. }
  2285. }
  2286. break;
  2287. case BFD_MACH_O_LC_LOAD_DYLIB:
  2288. case BFD_MACH_O_LC_LOAD_DYLINKER:
  2289. case BFD_MACH_O_LC_MAIN:
  2290. /* Nothing to do. */
  2291. break;
  2292. default:
  2293. _bfd_error_handler
  2294. (_("unable to allocate data for load command %#x"),
  2295. cmd->type);
  2296. break;
  2297. }
  2298. /* Specially handle symtab and dysymtab. */
  2299. /* Pre-allocate the symbol table (but not the string table). The reason
  2300. is that the dysymtab is after the symbol table but before the string
  2301. table (required by the native strip tool). */
  2302. if (symtab != NULL)
  2303. {
  2304. unsigned int symlen;
  2305. unsigned int wide = bfd_mach_o_wide_p (abfd);
  2306. symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
  2307. /* Align for symbols. */
  2308. mdata->filelen = FILE_ALIGN (mdata->filelen, wide ? 3 : 2);
  2309. symtab->symoff = mdata->filelen;
  2310. symtab->nsyms = bfd_get_symcount (abfd);
  2311. mdata->filelen += symtab->nsyms * symlen;
  2312. }
  2313. /* Build the dysymtab. */
  2314. if (dysymtab != NULL)
  2315. if (!bfd_mach_o_build_dysymtab (abfd, dysymtab))
  2316. return false;
  2317. /* Write symtab and strtab. */
  2318. if (symtab != NULL)
  2319. if (!bfd_mach_o_write_symtab_content (abfd, symtab))
  2320. return false;
  2321. /* Adjust linkedit size. */
  2322. if (linkedit != NULL)
  2323. {
  2324. /* bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1; */
  2325. linkedit->vmsize = mdata->filelen - linkedit->fileoff;
  2326. /* linkedit->vmsize = (linkedit->vmsize + pagemask) & ~pagemask; */
  2327. linkedit->filesize = mdata->filelen - linkedit->fileoff;
  2328. linkedit->initprot = BFD_MACH_O_PROT_READ;
  2329. linkedit->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
  2330. | BFD_MACH_O_PROT_EXECUTE;
  2331. }
  2332. /* Second pass: write commands. */
  2333. for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
  2334. {
  2335. struct mach_o_load_command_external raw;
  2336. unsigned long typeflag;
  2337. typeflag = cmd->type | (cmd->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
  2338. bfd_h_put_32 (abfd, typeflag, raw.cmd);
  2339. bfd_h_put_32 (abfd, cmd->len, raw.cmdsize);
  2340. if (bfd_seek (abfd, cmd->offset, SEEK_SET) != 0
  2341. || bfd_bwrite (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
  2342. return false;
  2343. switch (cmd->type)
  2344. {
  2345. case BFD_MACH_O_LC_SEGMENT:
  2346. if (!bfd_mach_o_write_segment_32 (abfd, cmd))
  2347. return false;
  2348. break;
  2349. case BFD_MACH_O_LC_SEGMENT_64:
  2350. if (!bfd_mach_o_write_segment_64 (abfd, cmd))
  2351. return false;
  2352. break;
  2353. case BFD_MACH_O_LC_SYMTAB:
  2354. if (!bfd_mach_o_write_symtab (abfd, cmd))
  2355. return false;
  2356. break;
  2357. case BFD_MACH_O_LC_DYSYMTAB:
  2358. if (!bfd_mach_o_write_dysymtab (abfd, cmd))
  2359. return false;
  2360. break;
  2361. case BFD_MACH_O_LC_THREAD:
  2362. case BFD_MACH_O_LC_UNIXTHREAD:
  2363. if (!bfd_mach_o_write_thread (abfd, cmd))
  2364. return false;
  2365. break;
  2366. case BFD_MACH_O_LC_LOAD_DYLIB:
  2367. if (!bfd_mach_o_write_dylib (abfd, cmd))
  2368. return false;
  2369. break;
  2370. case BFD_MACH_O_LC_LOAD_DYLINKER:
  2371. if (!bfd_mach_o_write_dylinker (abfd, cmd))
  2372. return false;
  2373. break;
  2374. case BFD_MACH_O_LC_MAIN:
  2375. if (!bfd_mach_o_write_main (abfd, cmd))
  2376. return false;
  2377. break;
  2378. case BFD_MACH_O_LC_DYLD_INFO:
  2379. if (!bfd_mach_o_write_dyld_info (abfd, cmd))
  2380. return false;
  2381. break;
  2382. default:
  2383. _bfd_error_handler
  2384. (_("unable to write unknown load command %#x"),
  2385. cmd->type);
  2386. return false;
  2387. }
  2388. }
  2389. return true;
  2390. }
  2391. static void
  2392. bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
  2393. bfd_mach_o_section *s)
  2394. {
  2395. if (seg->sect_head == NULL)
  2396. seg->sect_head = s;
  2397. else
  2398. seg->sect_tail->next = s;
  2399. seg->sect_tail = s;
  2400. }
  2401. /* Create section Mach-O flags from BFD flags. */
  2402. static void
  2403. bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED,
  2404. asection *sec)
  2405. {
  2406. flagword bfd_flags;
  2407. bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec);
  2408. /* Create default flags. */
  2409. bfd_flags = bfd_section_flags (sec);
  2410. if ((bfd_flags & SEC_CODE) == SEC_CODE)
  2411. s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
  2412. | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
  2413. | BFD_MACH_O_S_REGULAR;
  2414. else if ((bfd_flags & (SEC_ALLOC | SEC_LOAD)) == SEC_ALLOC)
  2415. s->flags = BFD_MACH_O_S_ZEROFILL;
  2416. else if (bfd_flags & SEC_DEBUGGING)
  2417. s->flags = BFD_MACH_O_S_REGULAR | BFD_MACH_O_S_ATTR_DEBUG;
  2418. else
  2419. s->flags = BFD_MACH_O_S_REGULAR;
  2420. }
  2421. static bool
  2422. bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
  2423. {
  2424. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  2425. unsigned int i, j;
  2426. seg->vmaddr = 0;
  2427. seg->fileoff = mdata->filelen;
  2428. seg->initprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
  2429. | BFD_MACH_O_PROT_EXECUTE;
  2430. seg->maxprot = seg->initprot;
  2431. /* Append sections to the segment.
  2432. This is a little tedious, we have to honor the need to account zerofill
  2433. sections after all the rest. This forces us to do the calculation of
  2434. total vmsize in three passes so that any alignment increments are
  2435. properly accounted. */
  2436. for (i = 0; i < mdata->nsects; ++i)
  2437. {
  2438. bfd_mach_o_section *s = mdata->sections[i];
  2439. asection *sec = s->bfdsection;
  2440. /* Although we account for zerofill section sizes in vm order, they are
  2441. placed in the file in source sequence. */
  2442. bfd_mach_o_append_section_to_segment (seg, s);
  2443. s->offset = 0;
  2444. /* Zerofill sections have zero file size & offset, the only content
  2445. written to the file is the symbols. */
  2446. if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) == BFD_MACH_O_S_ZEROFILL
  2447. || ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
  2448. == BFD_MACH_O_S_GB_ZEROFILL))
  2449. continue;
  2450. /* The Darwin system tools (in MH_OBJECT files, at least) always account
  2451. sections, even those with zero size. */
  2452. if (s->size > 0)
  2453. {
  2454. seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
  2455. seg->vmsize += s->size;
  2456. /* MH_OBJECT files have unaligned content. */
  2457. if (1)
  2458. {
  2459. seg->filesize = FILE_ALIGN (seg->filesize, s->align);
  2460. mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
  2461. }
  2462. seg->filesize += s->size;
  2463. /* The system tools write even zero-sized sections with an offset
  2464. field set to the current file position. */
  2465. s->offset = mdata->filelen;
  2466. }
  2467. sec->filepos = s->offset;
  2468. mdata->filelen += s->size;
  2469. }
  2470. /* Now pass through again, for zerofill, only now we just update the
  2471. vmsize, and then for zerofill_GB. */
  2472. for (j = 0; j < 2; j++)
  2473. {
  2474. unsigned int stype;
  2475. if (j == 0)
  2476. stype = BFD_MACH_O_S_ZEROFILL;
  2477. else
  2478. stype = BFD_MACH_O_S_GB_ZEROFILL;
  2479. for (i = 0; i < mdata->nsects; ++i)
  2480. {
  2481. bfd_mach_o_section *s = mdata->sections[i];
  2482. if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != stype)
  2483. continue;
  2484. if (s->size > 0)
  2485. {
  2486. seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
  2487. seg->vmsize += s->size;
  2488. }
  2489. }
  2490. }
  2491. /* Allocate space for the relocations. */
  2492. mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
  2493. for (i = 0; i < mdata->nsects; ++i)
  2494. {
  2495. bfd_mach_o_section *ms = mdata->sections[i];
  2496. asection *sec = ms->bfdsection;
  2497. ms->nreloc = sec->reloc_count;
  2498. if (ms->nreloc == 0)
  2499. {
  2500. /* Clear nreloc and reloff if there is no relocs. */
  2501. ms->reloff = 0;
  2502. continue;
  2503. }
  2504. sec->rel_filepos = mdata->filelen;
  2505. ms->reloff = sec->rel_filepos;
  2506. mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
  2507. }
  2508. return true;
  2509. }
  2510. static bool
  2511. bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
  2512. {
  2513. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  2514. unsigned int i;
  2515. bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1;
  2516. bfd_vma vma;
  2517. bfd_mach_o_section *s;
  2518. seg->vmsize = 0;
  2519. seg->fileoff = mdata->filelen;
  2520. seg->maxprot = 0;
  2521. seg->initprot = 0;
  2522. seg->flags = 0;
  2523. /* Append sections to the segment. We assume they are properly ordered
  2524. by vma (but we check that). */
  2525. vma = 0;
  2526. for (i = 0; i < mdata->nsects; ++i)
  2527. {
  2528. s = mdata->sections[i];
  2529. /* Consider only sections for this segment. */
  2530. if (strcmp (seg->segname, s->segname) != 0)
  2531. continue;
  2532. bfd_mach_o_append_section_to_segment (seg, s);
  2533. if (s->addr < vma)
  2534. {
  2535. _bfd_error_handler
  2536. /* xgettext:c-format */
  2537. (_("section address (%#" PRIx64 ") "
  2538. "below start of segment (%#" PRIx64 ")"),
  2539. (uint64_t) s->addr, (uint64_t) vma);
  2540. return false;
  2541. }
  2542. vma = s->addr + s->size;
  2543. }
  2544. /* Set segment file offset: make it page aligned. */
  2545. vma = seg->sect_head->addr;
  2546. seg->vmaddr = vma & ~pagemask;
  2547. if ((mdata->filelen & pagemask) > (vma & pagemask))
  2548. mdata->filelen += pagemask + 1;
  2549. seg->fileoff = mdata->filelen & ~pagemask;
  2550. mdata->filelen = seg->fileoff + (vma & pagemask);
  2551. /* Set section file offset. */
  2552. for (s = seg->sect_head; s != NULL; s = s->next)
  2553. {
  2554. asection *sec = s->bfdsection;
  2555. flagword flags = bfd_section_flags (sec);
  2556. /* Adjust segment size. */
  2557. seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
  2558. seg->vmsize += s->size;
  2559. /* File offset and length. */
  2560. seg->filesize = FILE_ALIGN (seg->filesize, s->align);
  2561. if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != BFD_MACH_O_S_ZEROFILL
  2562. && ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
  2563. != BFD_MACH_O_S_GB_ZEROFILL))
  2564. {
  2565. mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
  2566. s->offset = mdata->filelen;
  2567. s->bfdsection->filepos = s->offset;
  2568. seg->filesize += s->size;
  2569. mdata->filelen += s->size;
  2570. }
  2571. else
  2572. {
  2573. s->offset = 0;
  2574. s->bfdsection->filepos = 0;
  2575. }
  2576. /* Set protection. */
  2577. if (flags & SEC_LOAD)
  2578. {
  2579. if (flags & SEC_CODE)
  2580. seg->initprot |= BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_EXECUTE;
  2581. if ((flags & (SEC_DATA | SEC_READONLY)) == SEC_DATA)
  2582. seg->initprot |= BFD_MACH_O_PROT_WRITE | BFD_MACH_O_PROT_READ;
  2583. }
  2584. /* Relocs shouldn't appear in non-object files. */
  2585. if (s->bfdsection->reloc_count != 0)
  2586. return false;
  2587. }
  2588. /* Set maxprot. */
  2589. if (seg->initprot != 0)
  2590. seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
  2591. | BFD_MACH_O_PROT_EXECUTE;
  2592. else
  2593. seg->maxprot = 0;
  2594. /* Round segment size (and file size). */
  2595. seg->vmsize = (seg->vmsize + pagemask) & ~pagemask;
  2596. seg->filesize = (seg->filesize + pagemask) & ~pagemask;
  2597. mdata->filelen = (mdata->filelen + pagemask) & ~pagemask;
  2598. return true;
  2599. }
  2600. /* Layout the commands: set commands size and offset, set ncmds and sizeofcmds
  2601. fields in header. */
  2602. static bool
  2603. bfd_mach_o_layout_commands (bfd_mach_o_data_struct *mdata)
  2604. {
  2605. unsigned wide = mach_o_wide_p (&mdata->header);
  2606. unsigned int hdrlen;
  2607. ufile_ptr offset;
  2608. bfd_mach_o_load_command *cmd;
  2609. unsigned int align;
  2610. bool ret = true;
  2611. hdrlen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
  2612. align = wide ? 8 - 1 : 4 - 1;
  2613. offset = hdrlen;
  2614. mdata->header.ncmds = 0;
  2615. for (cmd = mdata->first_command; cmd; cmd = cmd->next)
  2616. {
  2617. mdata->header.ncmds++;
  2618. cmd->offset = offset;
  2619. switch (cmd->type)
  2620. {
  2621. case BFD_MACH_O_LC_SEGMENT_64:
  2622. cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
  2623. + BFD_MACH_O_SECTION_64_SIZE * cmd->command.segment.nsects;
  2624. break;
  2625. case BFD_MACH_O_LC_SEGMENT:
  2626. cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
  2627. + BFD_MACH_O_SECTION_SIZE * cmd->command.segment.nsects;
  2628. break;
  2629. case BFD_MACH_O_LC_SYMTAB:
  2630. cmd->len = sizeof (struct mach_o_symtab_command_external)
  2631. + BFD_MACH_O_LC_SIZE;
  2632. break;
  2633. case BFD_MACH_O_LC_DYSYMTAB:
  2634. cmd->len = sizeof (struct mach_o_dysymtab_command_external)
  2635. + BFD_MACH_O_LC_SIZE;
  2636. break;
  2637. case BFD_MACH_O_LC_LOAD_DYLIB:
  2638. cmd->len = sizeof (struct mach_o_dylib_command_external)
  2639. + BFD_MACH_O_LC_SIZE;
  2640. cmd->command.dylib.name_offset = cmd->len;
  2641. cmd->len += strlen (cmd->command.dylib.name_str);
  2642. cmd->len = (cmd->len + align) & ~align;
  2643. break;
  2644. case BFD_MACH_O_LC_LOAD_DYLINKER:
  2645. cmd->len = sizeof (struct mach_o_str_command_external)
  2646. + BFD_MACH_O_LC_SIZE;
  2647. cmd->command.dylinker.name_offset = cmd->len;
  2648. cmd->len += strlen (cmd->command.dylinker.name_str);
  2649. cmd->len = (cmd->len + align) & ~align;
  2650. break;
  2651. case BFD_MACH_O_LC_MAIN:
  2652. cmd->len = sizeof (struct mach_o_entry_point_command_external)
  2653. + BFD_MACH_O_LC_SIZE;
  2654. break;
  2655. case BFD_MACH_O_LC_DYLD_INFO:
  2656. cmd->len = sizeof (struct mach_o_dyld_info_command_external)
  2657. + BFD_MACH_O_LC_SIZE;
  2658. break;
  2659. default:
  2660. _bfd_error_handler
  2661. (_("unable to layout unknown load command %#x"),
  2662. cmd->type);
  2663. ret = false;
  2664. break;
  2665. }
  2666. BFD_ASSERT (cmd->len % (align + 1) == 0);
  2667. offset += cmd->len;
  2668. }
  2669. mdata->header.sizeofcmds = offset - hdrlen;
  2670. mdata->filelen = offset;
  2671. return ret;
  2672. }
  2673. /* Subroutine of bfd_mach_o_build_commands: set type, name and nsects of a
  2674. segment. */
  2675. static void
  2676. bfd_mach_o_init_segment (bfd_mach_o_data_struct *mdata,
  2677. bfd_mach_o_load_command *cmd,
  2678. const char *segname, unsigned int nbr_sect)
  2679. {
  2680. bfd_mach_o_segment_command *seg = &cmd->command.segment;
  2681. unsigned wide = mach_o_wide_p (&mdata->header);
  2682. /* Init segment command. */
  2683. cmd->type = wide ? BFD_MACH_O_LC_SEGMENT_64 : BFD_MACH_O_LC_SEGMENT;
  2684. cmd->type_required = false;
  2685. strcpy (seg->segname, segname);
  2686. seg->nsects = nbr_sect;
  2687. seg->vmaddr = 0;
  2688. seg->vmsize = 0;
  2689. seg->fileoff = 0;
  2690. seg->filesize = 0;
  2691. seg->maxprot = 0;
  2692. seg->initprot = 0;
  2693. seg->flags = 0;
  2694. seg->sect_head = NULL;
  2695. seg->sect_tail = NULL;
  2696. }
  2697. /* Build Mach-O load commands (currently assuming an MH_OBJECT file).
  2698. TODO: Other file formats, rebuilding symtab/dysymtab commands for strip
  2699. and copy functionality. */
  2700. bool
  2701. bfd_mach_o_build_commands (bfd *abfd)
  2702. {
  2703. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  2704. unsigned wide = mach_o_wide_p (&mdata->header);
  2705. unsigned int nbr_segcmd = 0;
  2706. bfd_mach_o_load_command *commands;
  2707. unsigned int nbr_commands;
  2708. int symtab_idx = -1;
  2709. int dysymtab_idx = -1;
  2710. int main_idx = -1;
  2711. unsigned int i;
  2712. /* Return now if already built. */
  2713. if (mdata->header.ncmds != 0)
  2714. return true;
  2715. /* Fill in the file type, if not already set. */
  2716. if (mdata->header.filetype == 0)
  2717. {
  2718. if (abfd->flags & EXEC_P)
  2719. mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
  2720. else if (abfd->flags & DYNAMIC)
  2721. mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
  2722. else
  2723. mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
  2724. }
  2725. /* If hasn't already been done, flatten sections list, and sort
  2726. if/when required. Must be done before the symbol table is adjusted,
  2727. since that depends on properly numbered sections. */
  2728. if (mdata->nsects == 0 || mdata->sections == NULL)
  2729. if (! bfd_mach_o_mangle_sections (abfd, mdata))
  2730. return false;
  2731. /* Order the symbol table, fill-in/check mach-o specific fields and
  2732. partition out any indirect symbols. */
  2733. if (!bfd_mach_o_mangle_symbols (abfd))
  2734. return false;
  2735. /* Segment commands. */
  2736. if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT)
  2737. {
  2738. /* Only one segment for all the sections. But the segment is
  2739. optional if there is no sections. */
  2740. nbr_segcmd = (mdata->nsects > 0) ? 1 : 0;
  2741. }
  2742. else
  2743. {
  2744. bfd_mach_o_section *prev_sect = NULL;
  2745. /* One pagezero segment and one linkedit segment. */
  2746. nbr_segcmd = 2;
  2747. /* Create one segment for associated segment name in sections.
  2748. Assume that sections with the same segment name are consecutive. */
  2749. for (i = 0; i < mdata->nsects; i++)
  2750. {
  2751. bfd_mach_o_section *this_sect = mdata->sections[i];
  2752. if (prev_sect == NULL
  2753. || strcmp (prev_sect->segname, this_sect->segname) != 0)
  2754. {
  2755. nbr_segcmd++;
  2756. prev_sect = this_sect;
  2757. }
  2758. }
  2759. }
  2760. nbr_commands = nbr_segcmd;
  2761. /* One command for the symbol table (only if there are symbols. */
  2762. if (bfd_get_symcount (abfd) > 0)
  2763. symtab_idx = nbr_commands++;
  2764. /* FIXME:
  2765. This is a rather crude test for whether we should build a dysymtab. */
  2766. if (bfd_mach_o_should_emit_dysymtab ()
  2767. && bfd_get_symcount (abfd))
  2768. {
  2769. /* If there should be a case where a dysymtab could be emitted without
  2770. a symtab (seems improbable), this would need amending. */
  2771. dysymtab_idx = nbr_commands++;
  2772. }
  2773. /* Add an entry point command. */
  2774. if (mdata->header.filetype == BFD_MACH_O_MH_EXECUTE
  2775. && bfd_get_start_address (abfd) != 0)
  2776. main_idx = nbr_commands++;
  2777. /* Well, we must have a header, at least. */
  2778. mdata->filelen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
  2779. /* A bit unusual, but no content is valid;
  2780. as -n empty.s -o empty.o */
  2781. if (nbr_commands == 0)
  2782. {
  2783. /* Layout commands (well none...) and set headers command fields. */
  2784. return bfd_mach_o_layout_commands (mdata);
  2785. }
  2786. /* Create commands for segments (and symtabs), prepend them. */
  2787. commands = bfd_zalloc (abfd, nbr_commands * sizeof (bfd_mach_o_load_command));
  2788. if (commands == NULL)
  2789. return false;
  2790. for (i = 0; i < nbr_commands - 1; i++)
  2791. commands[i].next = &commands[i + 1];
  2792. commands[nbr_commands - 1].next = mdata->first_command;
  2793. if (mdata->first_command == NULL)
  2794. mdata->last_command = &commands[nbr_commands - 1];
  2795. mdata->first_command = &commands[0];
  2796. if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT && nbr_segcmd != 0)
  2797. {
  2798. /* For object file, there is only one segment. */
  2799. bfd_mach_o_init_segment (mdata, &commands[0], "", mdata->nsects);
  2800. }
  2801. else if (nbr_segcmd != 0)
  2802. {
  2803. bfd_mach_o_load_command *cmd;
  2804. BFD_ASSERT (nbr_segcmd >= 2);
  2805. /* The pagezero. */
  2806. cmd = &commands[0];
  2807. bfd_mach_o_init_segment (mdata, cmd, "__PAGEZERO", 0);
  2808. /* Segments from sections. */
  2809. cmd++;
  2810. for (i = 0; i < mdata->nsects;)
  2811. {
  2812. const char *segname = mdata->sections[i]->segname;
  2813. unsigned int nbr_sect = 1;
  2814. /* Count number of sections for this segment. */
  2815. for (i++; i < mdata->nsects; i++)
  2816. if (strcmp (mdata->sections[i]->segname, segname) == 0)
  2817. nbr_sect++;
  2818. else
  2819. break;
  2820. bfd_mach_o_init_segment (mdata, cmd, segname, nbr_sect);
  2821. cmd++;
  2822. }
  2823. /* The linkedit. */
  2824. bfd_mach_o_init_segment (mdata, cmd, "__LINKEDIT", 0);
  2825. }
  2826. if (symtab_idx >= 0)
  2827. {
  2828. /* Init symtab command. */
  2829. bfd_mach_o_load_command *cmd = &commands[symtab_idx];
  2830. cmd->type = BFD_MACH_O_LC_SYMTAB;
  2831. cmd->type_required = false;
  2832. }
  2833. /* If required, setup symtab command, see comment above about the quality
  2834. of this test. */
  2835. if (dysymtab_idx >= 0)
  2836. {
  2837. bfd_mach_o_load_command *cmd = &commands[dysymtab_idx];
  2838. cmd->type = BFD_MACH_O_LC_DYSYMTAB;
  2839. cmd->type_required = false;
  2840. }
  2841. /* Create the main command. */
  2842. if (main_idx >= 0)
  2843. {
  2844. bfd_mach_o_load_command *cmd = &commands[main_idx];
  2845. cmd->type = BFD_MACH_O_LC_MAIN;
  2846. cmd->type_required = true;
  2847. cmd->command.main.entryoff = 0;
  2848. cmd->command.main.stacksize = 0;
  2849. }
  2850. /* Layout commands. */
  2851. if (! bfd_mach_o_layout_commands (mdata))
  2852. return false;
  2853. /* So, now we have sized the commands and the filelen set to that.
  2854. Now we can build the segment command and set the section file offsets. */
  2855. if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT)
  2856. {
  2857. for (i = 0; i < nbr_segcmd; i++)
  2858. if (!bfd_mach_o_build_obj_seg_command
  2859. (abfd, &commands[i].command.segment))
  2860. return false;
  2861. }
  2862. else
  2863. {
  2864. bfd_vma maxvma = 0;
  2865. /* Skip pagezero and linkedit segments. */
  2866. for (i = 1; i < nbr_segcmd - 1; i++)
  2867. {
  2868. bfd_mach_o_segment_command *seg = &commands[i].command.segment;
  2869. if (!bfd_mach_o_build_exec_seg_command (abfd, seg))
  2870. return false;
  2871. if (seg->vmaddr + seg->vmsize > maxvma)
  2872. maxvma = seg->vmaddr + seg->vmsize;
  2873. }
  2874. /* Set the size of __PAGEZERO. */
  2875. commands[0].command.segment.vmsize =
  2876. commands[1].command.segment.vmaddr;
  2877. /* Set the vma and fileoff of __LINKEDIT. */
  2878. commands[nbr_segcmd - 1].command.segment.vmaddr = maxvma;
  2879. commands[nbr_segcmd - 1].command.segment.fileoff = mdata->filelen;
  2880. /* Set entry point (once segments have been laid out). */
  2881. if (main_idx >= 0)
  2882. commands[main_idx].command.main.entryoff =
  2883. bfd_get_start_address (abfd) - commands[1].command.segment.vmaddr;
  2884. }
  2885. return true;
  2886. }
  2887. /* Set the contents of a section. */
  2888. bool
  2889. bfd_mach_o_set_section_contents (bfd *abfd,
  2890. asection *section,
  2891. const void * location,
  2892. file_ptr offset,
  2893. bfd_size_type count)
  2894. {
  2895. file_ptr pos;
  2896. /* Trying to write the first section contents will trigger the creation of
  2897. the load commands if they are not already present. */
  2898. if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd))
  2899. return false;
  2900. if (count == 0)
  2901. return true;
  2902. pos = section->filepos + offset;
  2903. if (bfd_seek (abfd, pos, SEEK_SET) != 0
  2904. || bfd_bwrite (location, count, abfd) != count)
  2905. return false;
  2906. return true;
  2907. }
  2908. int
  2909. bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
  2910. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  2911. {
  2912. return 0;
  2913. }
  2914. /* Make an empty symbol. This is required only because
  2915. bfd_make_section_anyway wants to create a symbol for the section. */
  2916. asymbol *
  2917. bfd_mach_o_make_empty_symbol (bfd *abfd)
  2918. {
  2919. asymbol *new_symbol;
  2920. new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
  2921. if (new_symbol == NULL)
  2922. return new_symbol;
  2923. new_symbol->the_bfd = abfd;
  2924. new_symbol->udata.i = SYM_MACHO_FIELDS_UNSET;
  2925. return new_symbol;
  2926. }
  2927. static bool
  2928. bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header)
  2929. {
  2930. struct mach_o_header_external raw;
  2931. unsigned int size;
  2932. bfd_vma (*get32) (const void *) = NULL;
  2933. /* Just read the magic number. */
  2934. if (bfd_seek (abfd, hdr_off, SEEK_SET) != 0
  2935. || bfd_bread (raw.magic, sizeof (raw.magic), abfd) != 4)
  2936. return false;
  2937. if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
  2938. {
  2939. header->byteorder = BFD_ENDIAN_BIG;
  2940. header->magic = BFD_MACH_O_MH_MAGIC;
  2941. header->version = 1;
  2942. get32 = bfd_getb32;
  2943. }
  2944. else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
  2945. {
  2946. header->byteorder = BFD_ENDIAN_LITTLE;
  2947. header->magic = BFD_MACH_O_MH_MAGIC;
  2948. header->version = 1;
  2949. get32 = bfd_getl32;
  2950. }
  2951. else if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
  2952. {
  2953. header->byteorder = BFD_ENDIAN_BIG;
  2954. header->magic = BFD_MACH_O_MH_MAGIC_64;
  2955. header->version = 2;
  2956. get32 = bfd_getb32;
  2957. }
  2958. else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
  2959. {
  2960. header->byteorder = BFD_ENDIAN_LITTLE;
  2961. header->magic = BFD_MACH_O_MH_MAGIC_64;
  2962. header->version = 2;
  2963. get32 = bfd_getl32;
  2964. }
  2965. else
  2966. {
  2967. header->byteorder = BFD_ENDIAN_UNKNOWN;
  2968. return false;
  2969. }
  2970. /* Once the size of the header is known, read the full header. */
  2971. size = mach_o_wide_p (header) ?
  2972. BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
  2973. if (bfd_seek (abfd, hdr_off, SEEK_SET) != 0
  2974. || bfd_bread (&raw, size, abfd) != size)
  2975. return false;
  2976. header->cputype = (*get32) (raw.cputype);
  2977. header->cpusubtype = (*get32) (raw.cpusubtype);
  2978. header->filetype = (*get32) (raw.filetype);
  2979. header->ncmds = (*get32) (raw.ncmds);
  2980. header->sizeofcmds = (*get32) (raw.sizeofcmds);
  2981. header->flags = (*get32) (raw.flags);
  2982. if (mach_o_wide_p (header))
  2983. header->reserved = (*get32) (raw.reserved);
  2984. else
  2985. header->reserved = 0;
  2986. return true;
  2987. }
  2988. bool
  2989. bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
  2990. {
  2991. bfd_mach_o_section *s;
  2992. unsigned bfdalign = bfd_section_alignment (sec);
  2993. s = bfd_mach_o_get_mach_o_section (sec);
  2994. if (s == NULL)
  2995. {
  2996. flagword bfd_flags;
  2997. static const mach_o_section_name_xlat * xlat;
  2998. s = (bfd_mach_o_section *) bfd_zalloc (abfd, sizeof (*s));
  2999. if (s == NULL)
  3000. return false;
  3001. sec->used_by_bfd = s;
  3002. s->bfdsection = sec;
  3003. /* Create the Darwin seg/sect name pair from the bfd name.
  3004. If this is a canonical name for which a specific paiting exists
  3005. there will also be defined flags, type, attribute and alignment
  3006. values. */
  3007. xlat = bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, s);
  3008. if (xlat != NULL)
  3009. {
  3010. s->flags = xlat->macho_sectype | xlat->macho_secattr;
  3011. s->align = xlat->sectalign > bfdalign ? xlat->sectalign
  3012. : bfdalign;
  3013. bfd_set_section_alignment (sec, s->align);
  3014. bfd_flags = bfd_section_flags (sec);
  3015. if (bfd_flags == SEC_NO_FLAGS)
  3016. bfd_set_section_flags (sec, xlat->bfd_flags);
  3017. }
  3018. else
  3019. /* Create default flags. */
  3020. bfd_mach_o_set_section_flags_from_bfd (abfd, sec);
  3021. }
  3022. return _bfd_generic_new_section_hook (abfd, sec);
  3023. }
  3024. static void
  3025. bfd_mach_o_init_section_from_mach_o (asection *sec, unsigned long prot)
  3026. {
  3027. flagword flags;
  3028. bfd_mach_o_section *section;
  3029. flags = bfd_section_flags (sec);
  3030. section = bfd_mach_o_get_mach_o_section (sec);
  3031. /* TODO: see if we should use the xlat system for doing this by
  3032. preference and fall back to this for unknown sections. */
  3033. if (flags == SEC_NO_FLAGS)
  3034. {
  3035. /* Try to guess flags. */
  3036. if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
  3037. flags = SEC_DEBUGGING;
  3038. else
  3039. {
  3040. flags = SEC_ALLOC;
  3041. if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
  3042. != BFD_MACH_O_S_ZEROFILL)
  3043. {
  3044. flags |= SEC_LOAD;
  3045. if (prot & BFD_MACH_O_PROT_EXECUTE)
  3046. flags |= SEC_CODE;
  3047. if (prot & BFD_MACH_O_PROT_WRITE)
  3048. flags |= SEC_DATA;
  3049. else if (prot & BFD_MACH_O_PROT_READ)
  3050. flags |= SEC_READONLY;
  3051. }
  3052. }
  3053. }
  3054. else
  3055. {
  3056. if ((flags & SEC_DEBUGGING) == 0)
  3057. flags |= SEC_ALLOC;
  3058. }
  3059. if (section->offset != 0)
  3060. flags |= SEC_HAS_CONTENTS;
  3061. if (section->nreloc != 0)
  3062. flags |= SEC_RELOC;
  3063. bfd_set_section_flags (sec, flags);
  3064. sec->vma = section->addr;
  3065. sec->lma = section->addr;
  3066. sec->size = section->size;
  3067. sec->filepos = section->offset;
  3068. sec->alignment_power = section->align;
  3069. sec->segment_mark = 0;
  3070. sec->reloc_count = section->nreloc;
  3071. sec->rel_filepos = section->reloff;
  3072. }
  3073. static asection *
  3074. bfd_mach_o_make_bfd_section (bfd *abfd,
  3075. const unsigned char *segname,
  3076. const unsigned char *sectname)
  3077. {
  3078. const char *sname;
  3079. flagword flags;
  3080. bfd_mach_o_convert_section_name_to_bfd
  3081. (abfd, (const char *)segname, (const char *)sectname, &sname, &flags);
  3082. if (sname == NULL)
  3083. return NULL;
  3084. return bfd_make_section_anyway_with_flags (abfd, sname, flags);
  3085. }
  3086. static asection *
  3087. bfd_mach_o_read_section_32 (bfd *abfd, unsigned long prot)
  3088. {
  3089. struct mach_o_section_32_external raw;
  3090. asection *sec;
  3091. bfd_mach_o_section *section;
  3092. if (bfd_bread (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
  3093. != BFD_MACH_O_SECTION_SIZE)
  3094. return NULL;
  3095. sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
  3096. if (sec == NULL)
  3097. return NULL;
  3098. section = bfd_mach_o_get_mach_o_section (sec);
  3099. memcpy (section->segname, raw.segname, sizeof (raw.segname));
  3100. section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
  3101. memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
  3102. section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
  3103. section->addr = bfd_h_get_32 (abfd, raw.addr);
  3104. section->size = bfd_h_get_32 (abfd, raw.size);
  3105. section->offset = bfd_h_get_32 (abfd, raw.offset);
  3106. section->align = bfd_h_get_32 (abfd, raw.align);
  3107. /* PR 17512: file: 0017eb76. */
  3108. if (section->align >= 31)
  3109. {
  3110. _bfd_error_handler
  3111. (_("bfd_mach_o_read_section_32: overlarge alignment value: %#lx"),
  3112. section->align);
  3113. section->align = 30;
  3114. }
  3115. section->reloff = bfd_h_get_32 (abfd, raw.reloff);
  3116. section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
  3117. section->flags = bfd_h_get_32 (abfd, raw.flags);
  3118. section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
  3119. section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
  3120. section->reserved3 = 0;
  3121. bfd_mach_o_init_section_from_mach_o (sec, prot);
  3122. return sec;
  3123. }
  3124. static asection *
  3125. bfd_mach_o_read_section_64 (bfd *abfd, unsigned long prot)
  3126. {
  3127. struct mach_o_section_64_external raw;
  3128. asection *sec;
  3129. bfd_mach_o_section *section;
  3130. if (bfd_bread (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
  3131. != BFD_MACH_O_SECTION_64_SIZE)
  3132. return NULL;
  3133. sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
  3134. if (sec == NULL)
  3135. return NULL;
  3136. section = bfd_mach_o_get_mach_o_section (sec);
  3137. memcpy (section->segname, raw.segname, sizeof (raw.segname));
  3138. section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
  3139. memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
  3140. section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
  3141. section->addr = bfd_h_get_64 (abfd, raw.addr);
  3142. section->size = bfd_h_get_64 (abfd, raw.size);
  3143. section->offset = bfd_h_get_32 (abfd, raw.offset);
  3144. section->align = bfd_h_get_32 (abfd, raw.align);
  3145. if (section->align >= 63)
  3146. {
  3147. _bfd_error_handler
  3148. (_("bfd_mach_o_read_section_64: overlarge alignment value: %#lx"),
  3149. section->align);
  3150. section->align = 62;
  3151. }
  3152. section->reloff = bfd_h_get_32 (abfd, raw.reloff);
  3153. section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
  3154. section->flags = bfd_h_get_32 (abfd, raw.flags);
  3155. section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
  3156. section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
  3157. section->reserved3 = bfd_h_get_32 (abfd, raw.reserved3);
  3158. bfd_mach_o_init_section_from_mach_o (sec, prot);
  3159. return sec;
  3160. }
  3161. static asection *
  3162. bfd_mach_o_read_section (bfd *abfd, unsigned long prot, unsigned int wide)
  3163. {
  3164. if (wide)
  3165. return bfd_mach_o_read_section_64 (abfd, prot);
  3166. else
  3167. return bfd_mach_o_read_section_32 (abfd, prot);
  3168. }
  3169. static bool
  3170. bfd_mach_o_read_symtab_symbol (bfd *abfd,
  3171. bfd_mach_o_symtab_command *sym,
  3172. bfd_mach_o_asymbol *s,
  3173. unsigned long i)
  3174. {
  3175. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  3176. unsigned int wide = mach_o_wide_p (&mdata->header);
  3177. unsigned int symwidth =
  3178. wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
  3179. unsigned int symoff = sym->symoff + (i * symwidth);
  3180. struct mach_o_nlist_64_external raw;
  3181. unsigned char type = -1;
  3182. unsigned char section = -1;
  3183. short desc = -1;
  3184. symvalue value = -1;
  3185. unsigned long stroff = -1;
  3186. unsigned int symtype = -1;
  3187. BFD_ASSERT (sym->strtab != NULL);
  3188. if (bfd_seek (abfd, symoff, SEEK_SET) != 0
  3189. || bfd_bread (&raw, symwidth, abfd) != symwidth)
  3190. {
  3191. _bfd_error_handler
  3192. /* xgettext:c-format */
  3193. (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %u"),
  3194. symwidth, symoff);
  3195. return false;
  3196. }
  3197. stroff = bfd_h_get_32 (abfd, raw.n_strx);
  3198. type = bfd_h_get_8 (abfd, raw.n_type);
  3199. symtype = type & BFD_MACH_O_N_TYPE;
  3200. section = bfd_h_get_8 (abfd, raw.n_sect);
  3201. desc = bfd_h_get_16 (abfd, raw.n_desc);
  3202. if (wide)
  3203. value = bfd_h_get_64 (abfd, raw.n_value);
  3204. else
  3205. value = bfd_h_get_32 (abfd, raw.n_value);
  3206. if (stroff >= sym->strsize)
  3207. {
  3208. _bfd_error_handler
  3209. /* xgettext:c-format */
  3210. (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %u)"),
  3211. stroff,
  3212. sym->strsize);
  3213. return false;
  3214. }
  3215. s->symbol.the_bfd = abfd;
  3216. s->symbol.name = sym->strtab + stroff;
  3217. s->symbol.value = value;
  3218. s->symbol.flags = 0x0;
  3219. s->symbol.udata.i = i;
  3220. s->n_type = type;
  3221. s->n_sect = section;
  3222. s->n_desc = desc;
  3223. if (type & BFD_MACH_O_N_STAB)
  3224. {
  3225. s->symbol.flags |= BSF_DEBUGGING;
  3226. s->symbol.section = bfd_und_section_ptr;
  3227. switch (type)
  3228. {
  3229. case N_FUN:
  3230. case N_STSYM:
  3231. case N_LCSYM:
  3232. case N_BNSYM:
  3233. case N_SLINE:
  3234. case N_ENSYM:
  3235. case N_ECOMM:
  3236. case N_ECOML:
  3237. case N_GSYM:
  3238. if ((section > 0) && (section <= mdata->nsects))
  3239. {
  3240. s->symbol.section = mdata->sections[section - 1]->bfdsection;
  3241. s->symbol.value =
  3242. s->symbol.value - mdata->sections[section - 1]->addr;
  3243. }
  3244. break;
  3245. }
  3246. }
  3247. else
  3248. {
  3249. if (type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
  3250. s->symbol.flags |= BSF_GLOBAL;
  3251. else
  3252. s->symbol.flags |= BSF_LOCAL;
  3253. switch (symtype)
  3254. {
  3255. case BFD_MACH_O_N_UNDF:
  3256. if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
  3257. && s->symbol.value != 0)
  3258. {
  3259. /* A common symbol. */
  3260. s->symbol.section = bfd_com_section_ptr;
  3261. s->symbol.flags = BSF_NO_FLAGS;
  3262. }
  3263. else
  3264. {
  3265. s->symbol.section = bfd_und_section_ptr;
  3266. if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
  3267. s->symbol.flags |= BSF_WEAK;
  3268. }
  3269. break;
  3270. case BFD_MACH_O_N_PBUD:
  3271. s->symbol.section = bfd_und_section_ptr;
  3272. break;
  3273. case BFD_MACH_O_N_ABS:
  3274. s->symbol.section = bfd_abs_section_ptr;
  3275. break;
  3276. case BFD_MACH_O_N_SECT:
  3277. if ((section > 0) && (section <= mdata->nsects))
  3278. {
  3279. s->symbol.section = mdata->sections[section - 1]->bfdsection;
  3280. s->symbol.value =
  3281. s->symbol.value - mdata->sections[section - 1]->addr;
  3282. }
  3283. else
  3284. {
  3285. /* Mach-O uses 0 to mean "no section"; not an error. */
  3286. if (section != 0)
  3287. {
  3288. _bfd_error_handler
  3289. /* xgettext:c-format */
  3290. (_("bfd_mach_o_read_symtab_symbol: "
  3291. "symbol \"%s\" specified invalid section %d (max %lu): "
  3292. "setting to undefined"),
  3293. s->symbol.name, section, mdata->nsects);
  3294. }
  3295. s->symbol.section = bfd_und_section_ptr;
  3296. }
  3297. break;
  3298. case BFD_MACH_O_N_INDR:
  3299. /* FIXME: we don't follow the BFD convention as this indirect symbol
  3300. won't be followed by the referenced one. This looks harmless
  3301. unless we start using the linker. */
  3302. s->symbol.flags |= BSF_INDIRECT;
  3303. s->symbol.section = bfd_ind_section_ptr;
  3304. s->symbol.value = 0;
  3305. break;
  3306. default:
  3307. _bfd_error_handler
  3308. /* xgettext:c-format */
  3309. (_("bfd_mach_o_read_symtab_symbol: "
  3310. "symbol \"%s\" specified invalid type field 0x%x: "
  3311. "setting to undefined"), s->symbol.name, symtype);
  3312. s->symbol.section = bfd_und_section_ptr;
  3313. break;
  3314. }
  3315. }
  3316. return true;
  3317. }
  3318. bool
  3319. bfd_mach_o_read_symtab_strtab (bfd *abfd)
  3320. {
  3321. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  3322. bfd_mach_o_symtab_command *sym = mdata->symtab;
  3323. /* Fail if there is no symtab. */
  3324. if (sym == NULL)
  3325. return false;
  3326. /* Success if already loaded. */
  3327. if (sym->strtab)
  3328. return true;
  3329. if (abfd->flags & BFD_IN_MEMORY)
  3330. {
  3331. struct bfd_in_memory *b;
  3332. b = (struct bfd_in_memory *) abfd->iostream;
  3333. if ((sym->stroff + sym->strsize) > b->size)
  3334. {
  3335. bfd_set_error (bfd_error_file_truncated);
  3336. return false;
  3337. }
  3338. sym->strtab = (char *) b->buffer + sym->stroff;
  3339. }
  3340. else
  3341. {
  3342. /* See PR 21840 for a reproducer. */
  3343. if ((sym->strsize + 1) == 0)
  3344. return false;
  3345. if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0)
  3346. return false;
  3347. sym->strtab = (char *) _bfd_alloc_and_read (abfd, sym->strsize + 1,
  3348. sym->strsize);
  3349. if (sym->strtab == NULL)
  3350. return false;
  3351. /* Zero terminate the string table. */
  3352. sym->strtab[sym->strsize] = 0;
  3353. }
  3354. return true;
  3355. }
  3356. bool
  3357. bfd_mach_o_read_symtab_symbols (bfd *abfd)
  3358. {
  3359. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  3360. bfd_mach_o_symtab_command *sym = mdata->symtab;
  3361. unsigned long i;
  3362. size_t amt;
  3363. ufile_ptr filesize;
  3364. if (sym == NULL || sym->nsyms == 0 || sym->symbols)
  3365. /* Return now if there are no symbols or if already loaded. */
  3366. return true;
  3367. filesize = bfd_get_file_size (abfd);
  3368. if (filesize != 0)
  3369. {
  3370. unsigned int wide = mach_o_wide_p (&mdata->header);
  3371. unsigned int symwidth
  3372. = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
  3373. if (sym->symoff > filesize
  3374. || sym->nsyms > (filesize - sym->symoff) / symwidth)
  3375. {
  3376. bfd_set_error (bfd_error_file_truncated);
  3377. sym->nsyms = 0;
  3378. return false;
  3379. }
  3380. }
  3381. if (_bfd_mul_overflow (sym->nsyms, sizeof (bfd_mach_o_asymbol), &amt)
  3382. || (sym->symbols = bfd_alloc (abfd, amt)) == NULL)
  3383. {
  3384. bfd_set_error (bfd_error_no_memory);
  3385. sym->nsyms = 0;
  3386. return false;
  3387. }
  3388. if (!bfd_mach_o_read_symtab_strtab (abfd))
  3389. goto fail;
  3390. for (i = 0; i < sym->nsyms; i++)
  3391. if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i))
  3392. goto fail;
  3393. return true;
  3394. fail:
  3395. bfd_release (abfd, sym->symbols);
  3396. sym->symbols = NULL;
  3397. sym->nsyms = 0;
  3398. return false;
  3399. }
  3400. static const char *
  3401. bfd_mach_o_i386_flavour_string (unsigned int flavour)
  3402. {
  3403. switch ((int) flavour)
  3404. {
  3405. case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
  3406. case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
  3407. case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
  3408. case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
  3409. case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
  3410. case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
  3411. case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
  3412. case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
  3413. case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
  3414. case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
  3415. case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
  3416. case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
  3417. case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
  3418. default: return "UNKNOWN";
  3419. }
  3420. }
  3421. static const char *
  3422. bfd_mach_o_ppc_flavour_string (unsigned int flavour)
  3423. {
  3424. switch ((int) flavour)
  3425. {
  3426. case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
  3427. case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
  3428. case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
  3429. case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
  3430. case BFD_MACH_O_PPC_THREAD_STATE64: return "PPC_THREAD_STATE64";
  3431. case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
  3432. default: return "UNKNOWN";
  3433. }
  3434. }
  3435. static unsigned char *
  3436. bfd_mach_o_alloc_and_read (bfd *abfd, file_ptr filepos, size_t size)
  3437. {
  3438. if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
  3439. return NULL;
  3440. return _bfd_alloc_and_read (abfd, size, size);
  3441. }
  3442. static bool
  3443. bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
  3444. {
  3445. bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
  3446. struct mach_o_str_command_external raw;
  3447. unsigned int nameoff;
  3448. unsigned int namelen;
  3449. if (command->len < sizeof (raw) + 8)
  3450. return false;
  3451. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3452. return false;
  3453. nameoff = bfd_h_get_32 (abfd, raw.str);
  3454. if (nameoff > command->len)
  3455. return false;
  3456. cmd->name_offset = nameoff;
  3457. namelen = command->len - nameoff;
  3458. nameoff += command->offset;
  3459. cmd->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, nameoff, namelen);
  3460. return cmd->name_str != NULL;
  3461. }
  3462. static bool
  3463. bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
  3464. {
  3465. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  3466. bfd_mach_o_dylib_command *cmd = &command->command.dylib;
  3467. struct mach_o_dylib_command_external raw;
  3468. unsigned int nameoff;
  3469. unsigned int namelen;
  3470. file_ptr pos;
  3471. if (command->len < sizeof (raw) + 8)
  3472. return false;
  3473. switch (command->type)
  3474. {
  3475. case BFD_MACH_O_LC_LOAD_DYLIB:
  3476. case BFD_MACH_O_LC_LAZY_LOAD_DYLIB:
  3477. case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
  3478. case BFD_MACH_O_LC_ID_DYLIB:
  3479. case BFD_MACH_O_LC_REEXPORT_DYLIB:
  3480. case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
  3481. break;
  3482. default:
  3483. BFD_FAIL ();
  3484. return false;
  3485. }
  3486. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3487. return false;
  3488. nameoff = bfd_h_get_32 (abfd, raw.name);
  3489. if (nameoff > command->len)
  3490. return false;
  3491. cmd->timestamp = bfd_h_get_32 (abfd, raw.timestamp);
  3492. cmd->current_version = bfd_h_get_32 (abfd, raw.current_version);
  3493. cmd->compatibility_version = bfd_h_get_32 (abfd, raw.compatibility_version);
  3494. cmd->name_offset = command->offset + nameoff;
  3495. namelen = command->len - nameoff;
  3496. pos = mdata->hdr_offset + cmd->name_offset;
  3497. cmd->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, pos, namelen);
  3498. return cmd->name_str != NULL;
  3499. }
  3500. static bool
  3501. bfd_mach_o_read_prebound_dylib (bfd *abfd,
  3502. bfd_mach_o_load_command *command)
  3503. {
  3504. bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib;
  3505. struct mach_o_prebound_dylib_command_external raw;
  3506. unsigned int nameoff;
  3507. unsigned int modoff;
  3508. unsigned int str_len;
  3509. unsigned char *str;
  3510. if (command->len < sizeof (raw) + 8)
  3511. return false;
  3512. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3513. return false;
  3514. nameoff = bfd_h_get_32 (abfd, raw.name);
  3515. modoff = bfd_h_get_32 (abfd, raw.linked_modules);
  3516. if (nameoff > command->len || modoff > command->len)
  3517. return false;
  3518. str_len = command->len - sizeof (raw);
  3519. str = _bfd_alloc_and_read (abfd, str_len, str_len);
  3520. if (str == NULL)
  3521. return false;
  3522. cmd->name_offset = command->offset + nameoff;
  3523. cmd->nmodules = bfd_h_get_32 (abfd, raw.nmodules);
  3524. cmd->linked_modules_offset = command->offset + modoff;
  3525. cmd->name_str = (char *)str + nameoff - (sizeof (raw) + BFD_MACH_O_LC_SIZE);
  3526. cmd->linked_modules = str + modoff - (sizeof (raw) + BFD_MACH_O_LC_SIZE);
  3527. return true;
  3528. }
  3529. static bool
  3530. bfd_mach_o_read_prebind_cksum (bfd *abfd,
  3531. bfd_mach_o_load_command *command)
  3532. {
  3533. bfd_mach_o_prebind_cksum_command *cmd = &command->command.prebind_cksum;
  3534. struct mach_o_prebind_cksum_command_external raw;
  3535. if (command->len < sizeof (raw) + 8)
  3536. return false;
  3537. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3538. return false;
  3539. cmd->cksum = bfd_get_32 (abfd, raw.cksum);
  3540. return true;
  3541. }
  3542. static bool
  3543. bfd_mach_o_read_twolevel_hints (bfd *abfd,
  3544. bfd_mach_o_load_command *command)
  3545. {
  3546. bfd_mach_o_twolevel_hints_command *cmd = &command->command.twolevel_hints;
  3547. struct mach_o_twolevel_hints_command_external raw;
  3548. if (command->len < sizeof (raw) + 8)
  3549. return false;
  3550. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3551. return false;
  3552. cmd->offset = bfd_get_32 (abfd, raw.offset);
  3553. cmd->nhints = bfd_get_32 (abfd, raw.nhints);
  3554. return true;
  3555. }
  3556. static bool
  3557. bfd_mach_o_read_fvmlib (bfd *abfd, bfd_mach_o_load_command *command)
  3558. {
  3559. bfd_mach_o_fvmlib_command *fvm = &command->command.fvmlib;
  3560. struct mach_o_fvmlib_command_external raw;
  3561. unsigned int nameoff;
  3562. unsigned int namelen;
  3563. if (command->len < sizeof (raw) + 8)
  3564. return false;
  3565. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3566. return false;
  3567. nameoff = bfd_h_get_32 (abfd, raw.name);
  3568. if (nameoff > command->len)
  3569. return false;
  3570. fvm->minor_version = bfd_h_get_32 (abfd, raw.minor_version);
  3571. fvm->header_addr = bfd_h_get_32 (abfd, raw.header_addr);
  3572. fvm->name_offset = command->offset + nameoff;
  3573. namelen = command->len - nameoff;
  3574. fvm->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, fvm->name_offset,
  3575. namelen);
  3576. return fvm->name_str != NULL;
  3577. }
  3578. static bool
  3579. bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
  3580. {
  3581. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  3582. bfd_mach_o_thread_command *cmd = &command->command.thread;
  3583. unsigned int offset;
  3584. unsigned int nflavours;
  3585. unsigned int i;
  3586. struct mach_o_thread_command_external raw;
  3587. size_t amt;
  3588. BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
  3589. || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
  3590. /* Count the number of threads. */
  3591. offset = 8;
  3592. nflavours = 0;
  3593. while (offset + sizeof (raw) <= command->len)
  3594. {
  3595. unsigned int count;
  3596. if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
  3597. || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3598. return false;
  3599. count = bfd_h_get_32 (abfd, raw.count);
  3600. if (count > (unsigned) -1 / 4
  3601. || command->len - (offset + sizeof (raw)) < count * 4)
  3602. return false;
  3603. offset += sizeof (raw) + count * 4;
  3604. nflavours++;
  3605. }
  3606. if (nflavours == 0 || offset != command->len)
  3607. return false;
  3608. /* Allocate threads. */
  3609. if (_bfd_mul_overflow (nflavours, sizeof (bfd_mach_o_thread_flavour), &amt))
  3610. {
  3611. bfd_set_error (bfd_error_file_too_big);
  3612. return false;
  3613. }
  3614. cmd->flavours = bfd_alloc (abfd, amt);
  3615. if (cmd->flavours == NULL)
  3616. return false;
  3617. cmd->nflavours = nflavours;
  3618. offset = 8;
  3619. nflavours = 0;
  3620. while (offset != command->len)
  3621. {
  3622. if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
  3623. || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3624. return false;
  3625. cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour);
  3626. cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw);
  3627. cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, raw.count) * 4;
  3628. offset += cmd->flavours[nflavours].size + sizeof (raw);
  3629. nflavours++;
  3630. }
  3631. for (i = 0; i < nflavours; i++)
  3632. {
  3633. asection *bfdsec;
  3634. unsigned int snamelen;
  3635. char *sname;
  3636. const char *flavourstr;
  3637. const char *prefix = "LC_THREAD";
  3638. unsigned int j = 0;
  3639. switch (mdata->header.cputype)
  3640. {
  3641. case BFD_MACH_O_CPU_TYPE_POWERPC:
  3642. case BFD_MACH_O_CPU_TYPE_POWERPC_64:
  3643. flavourstr =
  3644. bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
  3645. break;
  3646. case BFD_MACH_O_CPU_TYPE_I386:
  3647. case BFD_MACH_O_CPU_TYPE_X86_64:
  3648. flavourstr =
  3649. bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
  3650. break;
  3651. default:
  3652. flavourstr = "UNKNOWN_ARCHITECTURE";
  3653. break;
  3654. }
  3655. snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
  3656. sname = bfd_alloc (abfd, snamelen);
  3657. if (sname == NULL)
  3658. return false;
  3659. for (;;)
  3660. {
  3661. sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
  3662. if (bfd_get_section_by_name (abfd, sname) == NULL)
  3663. break;
  3664. j++;
  3665. }
  3666. bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
  3667. bfdsec->vma = 0;
  3668. bfdsec->lma = 0;
  3669. bfdsec->size = cmd->flavours[i].size;
  3670. bfdsec->filepos = cmd->flavours[i].offset;
  3671. bfdsec->alignment_power = 0x0;
  3672. cmd->section = bfdsec;
  3673. }
  3674. return true;
  3675. }
  3676. static bool
  3677. bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command,
  3678. ufile_ptr filesize)
  3679. {
  3680. bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
  3681. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  3682. BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
  3683. {
  3684. struct mach_o_dysymtab_command_external raw;
  3685. if (command->len < sizeof (raw) + 8)
  3686. return false;
  3687. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3688. return false;
  3689. cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym);
  3690. cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym);
  3691. cmd->iextdefsym = bfd_h_get_32 (abfd, raw.iextdefsym);
  3692. cmd->nextdefsym = bfd_h_get_32 (abfd, raw.nextdefsym);
  3693. cmd->iundefsym = bfd_h_get_32 (abfd, raw.iundefsym);
  3694. cmd->nundefsym = bfd_h_get_32 (abfd, raw.nundefsym);
  3695. cmd->tocoff = bfd_h_get_32 (abfd, raw.tocoff);
  3696. cmd->ntoc = bfd_h_get_32 (abfd, raw.ntoc);
  3697. cmd->modtaboff = bfd_h_get_32 (abfd, raw.modtaboff);
  3698. cmd->nmodtab = bfd_h_get_32 (abfd, raw.nmodtab);
  3699. cmd->extrefsymoff = bfd_h_get_32 (abfd, raw.extrefsymoff);
  3700. cmd->nextrefsyms = bfd_h_get_32 (abfd, raw.nextrefsyms);
  3701. cmd->indirectsymoff = bfd_h_get_32 (abfd, raw.indirectsymoff);
  3702. cmd->nindirectsyms = bfd_h_get_32 (abfd, raw.nindirectsyms);
  3703. cmd->extreloff = bfd_h_get_32 (abfd, raw.extreloff);
  3704. cmd->nextrel = bfd_h_get_32 (abfd, raw.nextrel);
  3705. cmd->locreloff = bfd_h_get_32 (abfd, raw.locreloff);
  3706. cmd->nlocrel = bfd_h_get_32 (abfd, raw.nlocrel);
  3707. }
  3708. if (cmd->nmodtab != 0)
  3709. {
  3710. unsigned int i;
  3711. int wide = bfd_mach_o_wide_p (abfd);
  3712. unsigned int module_len = wide ? 56 : 52;
  3713. size_t amt;
  3714. if (cmd->modtaboff > filesize
  3715. || cmd->nmodtab > (filesize - cmd->modtaboff) / module_len)
  3716. {
  3717. bfd_set_error (bfd_error_file_truncated);
  3718. return false;
  3719. }
  3720. if (_bfd_mul_overflow (cmd->nmodtab,
  3721. sizeof (bfd_mach_o_dylib_module), &amt))
  3722. {
  3723. bfd_set_error (bfd_error_file_too_big);
  3724. return false;
  3725. }
  3726. cmd->dylib_module = bfd_alloc (abfd, amt);
  3727. if (cmd->dylib_module == NULL)
  3728. return false;
  3729. if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
  3730. return false;
  3731. for (i = 0; i < cmd->nmodtab; i++)
  3732. {
  3733. bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
  3734. unsigned long v;
  3735. unsigned char buf[56];
  3736. if (bfd_bread ((void *) buf, module_len, abfd) != module_len)
  3737. return false;
  3738. module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
  3739. module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
  3740. module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
  3741. module->irefsym = bfd_h_get_32 (abfd, buf + 12);
  3742. module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
  3743. module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
  3744. module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
  3745. module->iextrel = bfd_h_get_32 (abfd, buf + 28);
  3746. module->nextrel = bfd_h_get_32 (abfd, buf + 32);
  3747. v = bfd_h_get_32 (abfd, buf +36);
  3748. module->iinit = v & 0xffff;
  3749. module->iterm = (v >> 16) & 0xffff;
  3750. v = bfd_h_get_32 (abfd, buf + 40);
  3751. module->ninit = v & 0xffff;
  3752. module->nterm = (v >> 16) & 0xffff;
  3753. if (wide)
  3754. {
  3755. module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
  3756. module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
  3757. }
  3758. else
  3759. {
  3760. module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
  3761. module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
  3762. }
  3763. }
  3764. }
  3765. if (cmd->ntoc != 0)
  3766. {
  3767. unsigned long i;
  3768. size_t amt;
  3769. struct mach_o_dylib_table_of_contents_external raw;
  3770. if (cmd->tocoff > filesize
  3771. || cmd->ntoc > (filesize - cmd->tocoff) / sizeof (raw))
  3772. {
  3773. bfd_set_error (bfd_error_file_truncated);
  3774. return false;
  3775. }
  3776. if (_bfd_mul_overflow (cmd->ntoc,
  3777. sizeof (bfd_mach_o_dylib_table_of_content), &amt))
  3778. {
  3779. bfd_set_error (bfd_error_file_too_big);
  3780. return false;
  3781. }
  3782. cmd->dylib_toc = bfd_alloc (abfd, amt);
  3783. if (cmd->dylib_toc == NULL)
  3784. return false;
  3785. if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
  3786. return false;
  3787. for (i = 0; i < cmd->ntoc; i++)
  3788. {
  3789. bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
  3790. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3791. return false;
  3792. toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index);
  3793. toc->module_index = bfd_h_get_32 (abfd, raw.module_index);
  3794. }
  3795. }
  3796. if (cmd->nindirectsyms != 0)
  3797. {
  3798. unsigned int i;
  3799. size_t amt;
  3800. if (cmd->indirectsymoff > filesize
  3801. || cmd->nindirectsyms > (filesize - cmd->indirectsymoff) / 4)
  3802. {
  3803. bfd_set_error (bfd_error_file_truncated);
  3804. return false;
  3805. }
  3806. if (_bfd_mul_overflow (cmd->nindirectsyms, sizeof (unsigned int), &amt))
  3807. {
  3808. bfd_set_error (bfd_error_file_too_big);
  3809. return false;
  3810. }
  3811. cmd->indirect_syms = bfd_alloc (abfd, amt);
  3812. if (cmd->indirect_syms == NULL)
  3813. return false;
  3814. if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
  3815. return false;
  3816. for (i = 0; i < cmd->nindirectsyms; i++)
  3817. {
  3818. unsigned char raw[4];
  3819. unsigned int *is = &cmd->indirect_syms[i];
  3820. if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
  3821. return false;
  3822. *is = bfd_h_get_32 (abfd, raw);
  3823. }
  3824. }
  3825. if (cmd->nextrefsyms != 0)
  3826. {
  3827. unsigned long v;
  3828. unsigned int i;
  3829. size_t amt;
  3830. if (cmd->extrefsymoff > filesize
  3831. || cmd->nextrefsyms > (filesize - cmd->extrefsymoff) / 4)
  3832. {
  3833. bfd_set_error (bfd_error_file_truncated);
  3834. return false;
  3835. }
  3836. if (_bfd_mul_overflow (cmd->nextrefsyms,
  3837. sizeof (bfd_mach_o_dylib_reference), &amt))
  3838. {
  3839. bfd_set_error (bfd_error_file_too_big);
  3840. return false;
  3841. }
  3842. cmd->ext_refs = bfd_alloc (abfd, amt);
  3843. if (cmd->ext_refs == NULL)
  3844. return false;
  3845. if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
  3846. return false;
  3847. for (i = 0; i < cmd->nextrefsyms; i++)
  3848. {
  3849. unsigned char raw[4];
  3850. bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
  3851. if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
  3852. return false;
  3853. /* Fields isym and flags are written as bit-fields, thus we need
  3854. a specific processing for endianness. */
  3855. v = bfd_h_get_32 (abfd, raw);
  3856. if (bfd_big_endian (abfd))
  3857. {
  3858. ref->isym = (v >> 8) & 0xffffff;
  3859. ref->flags = v & 0xff;
  3860. }
  3861. else
  3862. {
  3863. ref->isym = v & 0xffffff;
  3864. ref->flags = (v >> 24) & 0xff;
  3865. }
  3866. }
  3867. }
  3868. if (mdata->dysymtab)
  3869. return false;
  3870. mdata->dysymtab = cmd;
  3871. return true;
  3872. }
  3873. static bool
  3874. bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command,
  3875. ufile_ptr filesize)
  3876. {
  3877. bfd_mach_o_symtab_command *symtab = &command->command.symtab;
  3878. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  3879. struct mach_o_symtab_command_external raw;
  3880. BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
  3881. if (command->len < sizeof (raw) + 8)
  3882. return false;
  3883. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3884. return false;
  3885. symtab->symoff = bfd_h_get_32 (abfd, raw.symoff);
  3886. symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms);
  3887. symtab->stroff = bfd_h_get_32 (abfd, raw.stroff);
  3888. symtab->strsize = bfd_h_get_32 (abfd, raw.strsize);
  3889. symtab->symbols = NULL;
  3890. symtab->strtab = NULL;
  3891. if (symtab->symoff > filesize
  3892. || symtab->nsyms > (filesize - symtab->symoff) / BFD_MACH_O_NLIST_SIZE
  3893. || symtab->stroff > filesize
  3894. || symtab->strsize > filesize - symtab->stroff)
  3895. {
  3896. bfd_set_error (bfd_error_file_truncated);
  3897. return false;
  3898. }
  3899. if (symtab->nsyms != 0)
  3900. abfd->flags |= HAS_SYMS;
  3901. if (mdata->symtab)
  3902. return false;
  3903. mdata->symtab = symtab;
  3904. return true;
  3905. }
  3906. static bool
  3907. bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
  3908. {
  3909. bfd_mach_o_uuid_command *cmd = &command->command.uuid;
  3910. BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
  3911. if (command->len < 16 + 8)
  3912. return false;
  3913. if (bfd_bread (cmd->uuid, 16, abfd) != 16)
  3914. return false;
  3915. return true;
  3916. }
  3917. static bool
  3918. bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
  3919. {
  3920. bfd_mach_o_linkedit_command *cmd = &command->command.linkedit;
  3921. struct mach_o_linkedit_data_command_external raw;
  3922. if (command->len < sizeof (raw) + 8)
  3923. return false;
  3924. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3925. return false;
  3926. cmd->dataoff = bfd_get_32 (abfd, raw.dataoff);
  3927. cmd->datasize = bfd_get_32 (abfd, raw.datasize);
  3928. return true;
  3929. }
  3930. static bool
  3931. bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
  3932. {
  3933. bfd_mach_o_str_command *cmd = &command->command.str;
  3934. struct mach_o_str_command_external raw;
  3935. unsigned long off;
  3936. if (command->len < sizeof (raw) + 8)
  3937. return false;
  3938. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  3939. return false;
  3940. off = bfd_get_32 (abfd, raw.str);
  3941. if (off > command->len)
  3942. return false;
  3943. cmd->stroff = command->offset + off;
  3944. cmd->str_len = command->len - off;
  3945. cmd->str = (char *) bfd_mach_o_alloc_and_read (abfd, cmd->stroff,
  3946. cmd->str_len);
  3947. return cmd->str != NULL;
  3948. }
  3949. static bool
  3950. bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd)
  3951. {
  3952. /* Read rebase content. */
  3953. if (cmd->rebase_content == NULL && cmd->rebase_size != 0)
  3954. {
  3955. cmd->rebase_content
  3956. = bfd_mach_o_alloc_and_read (abfd, cmd->rebase_off, cmd->rebase_size);
  3957. if (cmd->rebase_content == NULL)
  3958. return false;
  3959. }
  3960. /* Read bind content. */
  3961. if (cmd->bind_content == NULL && cmd->bind_size != 0)
  3962. {
  3963. cmd->bind_content
  3964. = bfd_mach_o_alloc_and_read (abfd, cmd->bind_off, cmd->bind_size);
  3965. if (cmd->bind_content == NULL)
  3966. return false;
  3967. }
  3968. /* Read weak bind content. */
  3969. if (cmd->weak_bind_content == NULL && cmd->weak_bind_size != 0)
  3970. {
  3971. cmd->weak_bind_content = bfd_mach_o_alloc_and_read
  3972. (abfd, cmd->weak_bind_off, cmd->weak_bind_size);
  3973. if (cmd->weak_bind_content == NULL)
  3974. return false;
  3975. }
  3976. /* Read lazy bind content. */
  3977. if (cmd->lazy_bind_content == NULL && cmd->lazy_bind_size != 0)
  3978. {
  3979. cmd->lazy_bind_content = bfd_mach_o_alloc_and_read
  3980. (abfd, cmd->lazy_bind_off, cmd->lazy_bind_size);
  3981. if (cmd->lazy_bind_content == NULL)
  3982. return false;
  3983. }
  3984. /* Read export content. */
  3985. if (cmd->export_content == NULL && cmd->export_size != 0)
  3986. {
  3987. cmd->export_content = bfd_mach_o_alloc_and_read
  3988. (abfd, cmd->export_off, cmd->export_size);
  3989. if (cmd->export_content == NULL)
  3990. return false;
  3991. }
  3992. return true;
  3993. }
  3994. static bool
  3995. bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
  3996. {
  3997. bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
  3998. struct mach_o_dyld_info_command_external raw;
  3999. if (command->len < sizeof (raw) + 8)
  4000. return false;
  4001. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  4002. return false;
  4003. cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off);
  4004. cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size);
  4005. cmd->rebase_content = NULL;
  4006. cmd->bind_off = bfd_get_32 (abfd, raw.bind_off);
  4007. cmd->bind_size = bfd_get_32 (abfd, raw.bind_size);
  4008. cmd->bind_content = NULL;
  4009. cmd->weak_bind_off = bfd_get_32 (abfd, raw.weak_bind_off);
  4010. cmd->weak_bind_size = bfd_get_32 (abfd, raw.weak_bind_size);
  4011. cmd->weak_bind_content = NULL;
  4012. cmd->lazy_bind_off = bfd_get_32 (abfd, raw.lazy_bind_off);
  4013. cmd->lazy_bind_size = bfd_get_32 (abfd, raw.lazy_bind_size);
  4014. cmd->lazy_bind_content = NULL;
  4015. cmd->export_off = bfd_get_32 (abfd, raw.export_off);
  4016. cmd->export_size = bfd_get_32 (abfd, raw.export_size);
  4017. cmd->export_content = NULL;
  4018. return true;
  4019. }
  4020. static bool
  4021. bfd_mach_o_read_version_min (bfd *abfd, bfd_mach_o_load_command *command)
  4022. {
  4023. bfd_mach_o_version_min_command *cmd = &command->command.version_min;
  4024. struct mach_o_version_min_command_external raw;
  4025. if (command->len < sizeof (raw) + 8)
  4026. return false;
  4027. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  4028. return false;
  4029. cmd->version = bfd_get_32 (abfd, raw.version);
  4030. cmd->sdk = bfd_get_32 (abfd, raw.sdk);
  4031. return true;
  4032. }
  4033. static bool
  4034. bfd_mach_o_read_encryption_info (bfd *abfd, bfd_mach_o_load_command *command)
  4035. {
  4036. bfd_mach_o_encryption_info_command *cmd = &command->command.encryption_info;
  4037. struct mach_o_encryption_info_command_external raw;
  4038. if (command->len < sizeof (raw) + 8)
  4039. return false;
  4040. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  4041. return false;
  4042. cmd->cryptoff = bfd_get_32 (abfd, raw.cryptoff);
  4043. cmd->cryptsize = bfd_get_32 (abfd, raw.cryptsize);
  4044. cmd->cryptid = bfd_get_32 (abfd, raw.cryptid);
  4045. return true;
  4046. }
  4047. static bool
  4048. bfd_mach_o_read_encryption_info_64 (bfd *abfd, bfd_mach_o_load_command *command)
  4049. {
  4050. bfd_mach_o_encryption_info_command *cmd = &command->command.encryption_info;
  4051. struct mach_o_encryption_info_64_command_external raw;
  4052. if (command->len < sizeof (raw) + 8)
  4053. return false;
  4054. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  4055. return false;
  4056. cmd->cryptoff = bfd_get_32 (abfd, raw.cryptoff);
  4057. cmd->cryptsize = bfd_get_32 (abfd, raw.cryptsize);
  4058. cmd->cryptid = bfd_get_32 (abfd, raw.cryptid);
  4059. return true;
  4060. }
  4061. static bool
  4062. bfd_mach_o_read_main (bfd *abfd, bfd_mach_o_load_command *command)
  4063. {
  4064. bfd_mach_o_main_command *cmd = &command->command.main;
  4065. struct mach_o_entry_point_command_external raw;
  4066. if (command->len < sizeof (raw) + 8)
  4067. return false;
  4068. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  4069. return false;
  4070. cmd->entryoff = bfd_get_64 (abfd, raw.entryoff);
  4071. cmd->stacksize = bfd_get_64 (abfd, raw.stacksize);
  4072. return true;
  4073. }
  4074. static bool
  4075. bfd_mach_o_read_source_version (bfd *abfd, bfd_mach_o_load_command *command)
  4076. {
  4077. bfd_mach_o_source_version_command *cmd = &command->command.source_version;
  4078. struct mach_o_source_version_command_external raw;
  4079. bfd_uint64_t ver;
  4080. if (command->len < sizeof (raw) + 8)
  4081. return false;
  4082. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  4083. return false;
  4084. ver = bfd_get_64 (abfd, raw.version);
  4085. /* Note: we use a serie of shift to avoid shift > 32 (for which gcc
  4086. generates warnings) in case of the host doesn't support 64 bit
  4087. integers. */
  4088. cmd->e = ver & 0x3ff;
  4089. ver >>= 10;
  4090. cmd->d = ver & 0x3ff;
  4091. ver >>= 10;
  4092. cmd->c = ver & 0x3ff;
  4093. ver >>= 10;
  4094. cmd->b = ver & 0x3ff;
  4095. ver >>= 10;
  4096. cmd->a = ver & 0xffffff;
  4097. return true;
  4098. }
  4099. static bool
  4100. bfd_mach_o_read_note (bfd *abfd, bfd_mach_o_load_command *command)
  4101. {
  4102. bfd_mach_o_note_command *cmd = &command->command.note;
  4103. struct mach_o_note_command_external raw;
  4104. if (command->len < sizeof (raw) + 8)
  4105. return false;
  4106. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  4107. return false;
  4108. memcpy (cmd->data_owner, raw.data_owner, 16);
  4109. cmd->offset = bfd_get_64 (abfd, raw.offset);
  4110. cmd->size = bfd_get_64 (abfd, raw.size);
  4111. return true;
  4112. }
  4113. static bool
  4114. bfd_mach_o_read_build_version (bfd *abfd, bfd_mach_o_load_command *command)
  4115. {
  4116. bfd_mach_o_build_version_command *cmd = &command->command.build_version;
  4117. struct mach_o_build_version_command_external raw;
  4118. if (command->len < sizeof (raw) + 8)
  4119. return false;
  4120. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  4121. return false;
  4122. cmd->platform = bfd_get_32 (abfd, raw.platform);
  4123. cmd->minos = bfd_get_32 (abfd, raw.minos);
  4124. cmd->sdk = bfd_get_32 (abfd, raw.sdk);
  4125. cmd->ntools = bfd_get_32 (abfd, raw.ntools);
  4126. return true;
  4127. }
  4128. static bool
  4129. bfd_mach_o_read_segment (bfd *abfd,
  4130. bfd_mach_o_load_command *command,
  4131. unsigned int wide)
  4132. {
  4133. bfd_mach_o_segment_command *seg = &command->command.segment;
  4134. unsigned long i;
  4135. if (wide)
  4136. {
  4137. struct mach_o_segment_command_64_external raw;
  4138. BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
  4139. if (command->len < sizeof (raw) + 8)
  4140. return false;
  4141. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  4142. return false;
  4143. memcpy (seg->segname, raw.segname, 16);
  4144. seg->segname[16] = '\0';
  4145. seg->vmaddr = bfd_h_get_64 (abfd, raw.vmaddr);
  4146. seg->vmsize = bfd_h_get_64 (abfd, raw.vmsize);
  4147. seg->fileoff = bfd_h_get_64 (abfd, raw.fileoff);
  4148. seg->filesize = bfd_h_get_64 (abfd, raw.filesize);
  4149. seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
  4150. seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
  4151. seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
  4152. seg->flags = bfd_h_get_32 (abfd, raw.flags);
  4153. }
  4154. else
  4155. {
  4156. struct mach_o_segment_command_32_external raw;
  4157. BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
  4158. if (command->len < sizeof (raw) + 8)
  4159. return false;
  4160. if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
  4161. return false;
  4162. memcpy (seg->segname, raw.segname, 16);
  4163. seg->segname[16] = '\0';
  4164. seg->vmaddr = bfd_h_get_32 (abfd, raw.vmaddr);
  4165. seg->vmsize = bfd_h_get_32 (abfd, raw.vmsize);
  4166. seg->fileoff = bfd_h_get_32 (abfd, raw.fileoff);
  4167. seg->filesize = bfd_h_get_32 (abfd, raw.filesize);
  4168. seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
  4169. seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
  4170. seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
  4171. seg->flags = bfd_h_get_32 (abfd, raw.flags);
  4172. }
  4173. seg->sect_head = NULL;
  4174. seg->sect_tail = NULL;
  4175. for (i = 0; i < seg->nsects; i++)
  4176. {
  4177. asection *sec;
  4178. sec = bfd_mach_o_read_section (abfd, seg->initprot, wide);
  4179. if (sec == NULL)
  4180. return false;
  4181. bfd_mach_o_append_section_to_segment
  4182. (seg, bfd_mach_o_get_mach_o_section (sec));
  4183. }
  4184. return true;
  4185. }
  4186. static bool
  4187. bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
  4188. {
  4189. return bfd_mach_o_read_segment (abfd, command, 0);
  4190. }
  4191. static bool
  4192. bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
  4193. {
  4194. return bfd_mach_o_read_segment (abfd, command, 1);
  4195. }
  4196. static bool
  4197. bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command,
  4198. ufile_ptr filesize)
  4199. {
  4200. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  4201. struct mach_o_load_command_external raw;
  4202. unsigned int cmd;
  4203. /* Read command type and length. */
  4204. if (bfd_seek (abfd, mdata->hdr_offset + command->offset, SEEK_SET) != 0
  4205. || bfd_bread (&raw, BFD_MACH_O_LC_SIZE, abfd) != BFD_MACH_O_LC_SIZE)
  4206. return false;
  4207. cmd = bfd_h_get_32 (abfd, raw.cmd);
  4208. command->type = cmd & ~BFD_MACH_O_LC_REQ_DYLD;
  4209. command->type_required = (cmd & BFD_MACH_O_LC_REQ_DYLD) != 0;
  4210. command->len = bfd_h_get_32 (abfd, raw.cmdsize);
  4211. if (command->len < 8 || command->len % 4 != 0)
  4212. return false;
  4213. switch (command->type)
  4214. {
  4215. case BFD_MACH_O_LC_SEGMENT:
  4216. if (!bfd_mach_o_read_segment_32 (abfd, command))
  4217. return false;
  4218. break;
  4219. case BFD_MACH_O_LC_SEGMENT_64:
  4220. if (!bfd_mach_o_read_segment_64 (abfd, command))
  4221. return false;
  4222. break;
  4223. case BFD_MACH_O_LC_SYMTAB:
  4224. if (!bfd_mach_o_read_symtab (abfd, command, filesize))
  4225. return false;
  4226. break;
  4227. case BFD_MACH_O_LC_SYMSEG:
  4228. break;
  4229. case BFD_MACH_O_LC_THREAD:
  4230. case BFD_MACH_O_LC_UNIXTHREAD:
  4231. if (!bfd_mach_o_read_thread (abfd, command))
  4232. return false;
  4233. break;
  4234. case BFD_MACH_O_LC_LOAD_DYLINKER:
  4235. case BFD_MACH_O_LC_ID_DYLINKER:
  4236. case BFD_MACH_O_LC_DYLD_ENVIRONMENT:
  4237. if (!bfd_mach_o_read_dylinker (abfd, command))
  4238. return false;
  4239. break;
  4240. case BFD_MACH_O_LC_LOAD_DYLIB:
  4241. case BFD_MACH_O_LC_LAZY_LOAD_DYLIB:
  4242. case BFD_MACH_O_LC_ID_DYLIB:
  4243. case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
  4244. case BFD_MACH_O_LC_REEXPORT_DYLIB:
  4245. case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
  4246. if (!bfd_mach_o_read_dylib (abfd, command))
  4247. return false;
  4248. break;
  4249. case BFD_MACH_O_LC_PREBOUND_DYLIB:
  4250. if (!bfd_mach_o_read_prebound_dylib (abfd, command))
  4251. return false;
  4252. break;
  4253. case BFD_MACH_O_LC_LOADFVMLIB:
  4254. case BFD_MACH_O_LC_IDFVMLIB:
  4255. if (!bfd_mach_o_read_fvmlib (abfd, command))
  4256. return false;
  4257. break;
  4258. case BFD_MACH_O_LC_IDENT:
  4259. case BFD_MACH_O_LC_FVMFILE:
  4260. case BFD_MACH_O_LC_PREPAGE:
  4261. case BFD_MACH_O_LC_ROUTINES:
  4262. case BFD_MACH_O_LC_ROUTINES_64:
  4263. break;
  4264. case BFD_MACH_O_LC_SUB_FRAMEWORK:
  4265. case BFD_MACH_O_LC_SUB_UMBRELLA:
  4266. case BFD_MACH_O_LC_SUB_LIBRARY:
  4267. case BFD_MACH_O_LC_SUB_CLIENT:
  4268. case BFD_MACH_O_LC_RPATH:
  4269. if (!bfd_mach_o_read_str (abfd, command))
  4270. return false;
  4271. break;
  4272. case BFD_MACH_O_LC_DYSYMTAB:
  4273. if (!bfd_mach_o_read_dysymtab (abfd, command, filesize))
  4274. return false;
  4275. break;
  4276. case BFD_MACH_O_LC_PREBIND_CKSUM:
  4277. if (!bfd_mach_o_read_prebind_cksum (abfd, command))
  4278. return false;
  4279. break;
  4280. case BFD_MACH_O_LC_TWOLEVEL_HINTS:
  4281. if (!bfd_mach_o_read_twolevel_hints (abfd, command))
  4282. return false;
  4283. break;
  4284. case BFD_MACH_O_LC_UUID:
  4285. if (!bfd_mach_o_read_uuid (abfd, command))
  4286. return false;
  4287. break;
  4288. case BFD_MACH_O_LC_CODE_SIGNATURE:
  4289. case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
  4290. case BFD_MACH_O_LC_FUNCTION_STARTS:
  4291. case BFD_MACH_O_LC_DATA_IN_CODE:
  4292. case BFD_MACH_O_LC_DYLIB_CODE_SIGN_DRS:
  4293. case BFD_MACH_O_LC_LINKER_OPTIMIZATION_HINT:
  4294. case BFD_MACH_O_LC_DYLD_EXPORTS_TRIE:
  4295. case BFD_MACH_O_LC_DYLD_CHAINED_FIXUPS:
  4296. if (!bfd_mach_o_read_linkedit (abfd, command))
  4297. return false;
  4298. break;
  4299. case BFD_MACH_O_LC_ENCRYPTION_INFO:
  4300. if (!bfd_mach_o_read_encryption_info (abfd, command))
  4301. return false;
  4302. break;
  4303. case BFD_MACH_O_LC_ENCRYPTION_INFO_64:
  4304. if (!bfd_mach_o_read_encryption_info_64 (abfd, command))
  4305. return false;
  4306. break;
  4307. case BFD_MACH_O_LC_DYLD_INFO:
  4308. if (!bfd_mach_o_read_dyld_info (abfd, command))
  4309. return false;
  4310. break;
  4311. case BFD_MACH_O_LC_VERSION_MIN_MACOSX:
  4312. case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS:
  4313. case BFD_MACH_O_LC_VERSION_MIN_WATCHOS:
  4314. case BFD_MACH_O_LC_VERSION_MIN_TVOS:
  4315. if (!bfd_mach_o_read_version_min (abfd, command))
  4316. return false;
  4317. break;
  4318. case BFD_MACH_O_LC_MAIN:
  4319. if (!bfd_mach_o_read_main (abfd, command))
  4320. return false;
  4321. break;
  4322. case BFD_MACH_O_LC_SOURCE_VERSION:
  4323. if (!bfd_mach_o_read_source_version (abfd, command))
  4324. return false;
  4325. break;
  4326. case BFD_MACH_O_LC_LINKER_OPTIONS:
  4327. break;
  4328. case BFD_MACH_O_LC_NOTE:
  4329. if (!bfd_mach_o_read_note (abfd, command))
  4330. return false;
  4331. break;
  4332. case BFD_MACH_O_LC_BUILD_VERSION:
  4333. if (!bfd_mach_o_read_build_version (abfd, command))
  4334. return false;
  4335. break;
  4336. default:
  4337. command->len = 0;
  4338. _bfd_error_handler (_("%pB: unknown load command %#x"),
  4339. abfd, command->type);
  4340. return false;
  4341. }
  4342. return true;
  4343. }
  4344. static bool
  4345. bfd_mach_o_flatten_sections (bfd *abfd)
  4346. {
  4347. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  4348. bfd_mach_o_load_command *cmd;
  4349. long csect = 0;
  4350. size_t amt;
  4351. /* Count total number of sections. */
  4352. mdata->nsects = 0;
  4353. for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
  4354. {
  4355. if (cmd->type == BFD_MACH_O_LC_SEGMENT
  4356. || cmd->type == BFD_MACH_O_LC_SEGMENT_64)
  4357. {
  4358. bfd_mach_o_segment_command *seg = &cmd->command.segment;
  4359. mdata->nsects += seg->nsects;
  4360. }
  4361. }
  4362. /* Allocate sections array. */
  4363. if (_bfd_mul_overflow (mdata->nsects, sizeof (bfd_mach_o_section *), &amt))
  4364. {
  4365. bfd_set_error (bfd_error_file_too_big);
  4366. return false;
  4367. }
  4368. mdata->sections = bfd_alloc (abfd, amt);
  4369. if (mdata->sections == NULL && mdata->nsects != 0)
  4370. return false;
  4371. /* Fill the array. */
  4372. csect = 0;
  4373. for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
  4374. {
  4375. if (cmd->type == BFD_MACH_O_LC_SEGMENT
  4376. || cmd->type == BFD_MACH_O_LC_SEGMENT_64)
  4377. {
  4378. bfd_mach_o_segment_command *seg = &cmd->command.segment;
  4379. bfd_mach_o_section *sec;
  4380. BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
  4381. for (sec = seg->sect_head; sec != NULL; sec = sec->next)
  4382. mdata->sections[csect++] = sec;
  4383. }
  4384. }
  4385. return true;
  4386. }
  4387. static bool
  4388. bfd_mach_o_scan_start_address (bfd *abfd)
  4389. {
  4390. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  4391. bfd_mach_o_thread_command *thr = NULL;
  4392. bfd_mach_o_load_command *cmd;
  4393. unsigned long i;
  4394. for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
  4395. if (cmd->type == BFD_MACH_O_LC_THREAD
  4396. || cmd->type == BFD_MACH_O_LC_UNIXTHREAD)
  4397. {
  4398. thr = &cmd->command.thread;
  4399. break;
  4400. }
  4401. else if (cmd->type == BFD_MACH_O_LC_MAIN && mdata->nsects > 1)
  4402. {
  4403. bfd_mach_o_main_command *main_cmd = &cmd->command.main;
  4404. bfd_mach_o_section *text_sect = mdata->sections[0];
  4405. if (text_sect)
  4406. {
  4407. abfd->start_address = main_cmd->entryoff
  4408. + (text_sect->addr - text_sect->offset);
  4409. return true;
  4410. }
  4411. }
  4412. /* An object file has no start address, so do not fail if not found. */
  4413. if (thr == NULL)
  4414. return true;
  4415. /* FIXME: create a subtarget hook ? */
  4416. for (i = 0; i < thr->nflavours; i++)
  4417. {
  4418. if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
  4419. && (thr->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE32))
  4420. {
  4421. unsigned char buf[4];
  4422. if (bfd_seek (abfd, thr->flavours[i].offset + 40, SEEK_SET) != 0
  4423. || bfd_bread (buf, 4, abfd) != 4)
  4424. return false;
  4425. abfd->start_address = bfd_h_get_32 (abfd, buf);
  4426. }
  4427. else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
  4428. && (thr->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
  4429. {
  4430. unsigned char buf[4];
  4431. if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0
  4432. || bfd_bread (buf, 4, abfd) != 4)
  4433. return false;
  4434. abfd->start_address = bfd_h_get_32 (abfd, buf);
  4435. }
  4436. else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
  4437. && (thr->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
  4438. {
  4439. unsigned char buf[8];
  4440. if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0
  4441. || bfd_bread (buf, 8, abfd) != 8)
  4442. return false;
  4443. abfd->start_address = bfd_h_get_64 (abfd, buf);
  4444. }
  4445. else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
  4446. && (thr->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
  4447. {
  4448. unsigned char buf[8];
  4449. if (bfd_seek (abfd, thr->flavours[i].offset + (16 * 8), SEEK_SET) != 0
  4450. || bfd_bread (buf, 8, abfd) != 8)
  4451. return false;
  4452. abfd->start_address = bfd_h_get_64 (abfd, buf);
  4453. }
  4454. }
  4455. return true;
  4456. }
  4457. bool
  4458. bfd_mach_o_set_arch_mach (bfd *abfd,
  4459. enum bfd_architecture arch,
  4460. unsigned long machine)
  4461. {
  4462. bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
  4463. /* If this isn't the right architecture for this backend, and this
  4464. isn't the generic backend, fail. */
  4465. if (arch != bed->arch
  4466. && arch != bfd_arch_unknown
  4467. && bed->arch != bfd_arch_unknown)
  4468. return false;
  4469. return bfd_default_set_arch_mach (abfd, arch, machine);
  4470. }
  4471. static bool
  4472. bfd_mach_o_scan (bfd *abfd,
  4473. bfd_mach_o_header *header,
  4474. bfd_mach_o_data_struct *mdata)
  4475. {
  4476. unsigned int i;
  4477. enum bfd_architecture cpu_type;
  4478. unsigned long cpu_subtype;
  4479. unsigned int hdrsize;
  4480. hdrsize = mach_o_wide_p (header) ?
  4481. BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
  4482. mdata->header = *header;
  4483. abfd->flags = abfd->flags & BFD_IN_MEMORY;
  4484. switch (header->filetype)
  4485. {
  4486. case BFD_MACH_O_MH_OBJECT:
  4487. abfd->flags |= HAS_RELOC;
  4488. break;
  4489. case BFD_MACH_O_MH_EXECUTE:
  4490. abfd->flags |= EXEC_P;
  4491. break;
  4492. case BFD_MACH_O_MH_DYLIB:
  4493. case BFD_MACH_O_MH_BUNDLE:
  4494. abfd->flags |= DYNAMIC;
  4495. break;
  4496. }
  4497. abfd->tdata.mach_o_data = mdata;
  4498. bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
  4499. &cpu_type, &cpu_subtype);
  4500. if (cpu_type == bfd_arch_unknown)
  4501. {
  4502. _bfd_error_handler
  4503. /* xgettext:c-format */
  4504. (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
  4505. header->cputype, header->cpusubtype);
  4506. return false;
  4507. }
  4508. bfd_set_arch_mach (abfd, cpu_type, cpu_subtype);
  4509. if (header->ncmds != 0)
  4510. {
  4511. bfd_mach_o_load_command *cmd;
  4512. size_t amt;
  4513. ufile_ptr filesize = bfd_get_file_size (abfd);
  4514. if (filesize == 0)
  4515. filesize = (ufile_ptr) -1;
  4516. mdata->first_command = NULL;
  4517. mdata->last_command = NULL;
  4518. if (header->ncmds > (filesize - hdrsize) / BFD_MACH_O_LC_SIZE)
  4519. {
  4520. bfd_set_error (bfd_error_file_truncated);
  4521. return false;
  4522. }
  4523. if (_bfd_mul_overflow (header->ncmds,
  4524. sizeof (bfd_mach_o_load_command), &amt))
  4525. {
  4526. bfd_set_error (bfd_error_file_too_big);
  4527. return false;
  4528. }
  4529. cmd = bfd_alloc (abfd, amt);
  4530. if (cmd == NULL)
  4531. return false;
  4532. for (i = 0; i < header->ncmds; i++)
  4533. {
  4534. bfd_mach_o_load_command *cur = &cmd[i];
  4535. bfd_mach_o_append_command (abfd, cur);
  4536. if (i == 0)
  4537. cur->offset = hdrsize;
  4538. else
  4539. {
  4540. bfd_mach_o_load_command *prev = &cmd[i - 1];
  4541. cur->offset = prev->offset + prev->len;
  4542. }
  4543. if (!bfd_mach_o_read_command (abfd, cur, filesize))
  4544. return false;
  4545. }
  4546. }
  4547. /* Sections should be flatten before scanning start address. */
  4548. if (!bfd_mach_o_flatten_sections (abfd))
  4549. return false;
  4550. if (!bfd_mach_o_scan_start_address (abfd))
  4551. return false;
  4552. return true;
  4553. }
  4554. bool
  4555. bfd_mach_o_mkobject_init (bfd *abfd)
  4556. {
  4557. bfd_mach_o_data_struct *mdata = NULL;
  4558. mdata = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
  4559. if (mdata == NULL)
  4560. return false;
  4561. abfd->tdata.mach_o_data = mdata;
  4562. mdata->header.magic = 0;
  4563. mdata->header.cputype = 0;
  4564. mdata->header.cpusubtype = 0;
  4565. mdata->header.filetype = 0;
  4566. mdata->header.ncmds = 0;
  4567. mdata->header.sizeofcmds = 0;
  4568. mdata->header.flags = 0;
  4569. mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
  4570. mdata->first_command = NULL;
  4571. mdata->last_command = NULL;
  4572. mdata->nsects = 0;
  4573. mdata->sections = NULL;
  4574. mdata->dyn_reloc_cache = NULL;
  4575. return true;
  4576. }
  4577. static bool
  4578. bfd_mach_o_gen_mkobject (bfd *abfd)
  4579. {
  4580. bfd_mach_o_data_struct *mdata;
  4581. if (!bfd_mach_o_mkobject_init (abfd))
  4582. return false;
  4583. mdata = bfd_mach_o_get_data (abfd);
  4584. mdata->header.magic = BFD_MACH_O_MH_MAGIC;
  4585. mdata->header.cputype = 0;
  4586. mdata->header.cpusubtype = 0;
  4587. mdata->header.byteorder = abfd->xvec->byteorder;
  4588. mdata->header.version = 1;
  4589. return true;
  4590. }
  4591. bfd_cleanup
  4592. bfd_mach_o_header_p (bfd *abfd,
  4593. file_ptr hdr_off,
  4594. bfd_mach_o_filetype file_type,
  4595. bfd_mach_o_cpu_type cpu_type)
  4596. {
  4597. bfd_mach_o_header header;
  4598. bfd_mach_o_data_struct *mdata;
  4599. if (!bfd_mach_o_read_header (abfd, hdr_off, &header))
  4600. goto wrong;
  4601. if (! (header.byteorder == BFD_ENDIAN_BIG
  4602. || header.byteorder == BFD_ENDIAN_LITTLE))
  4603. {
  4604. _bfd_error_handler (_("unknown header byte-order value %#x"),
  4605. header.byteorder);
  4606. goto wrong;
  4607. }
  4608. if (! ((header.byteorder == BFD_ENDIAN_BIG
  4609. && abfd->xvec->byteorder == BFD_ENDIAN_BIG
  4610. && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
  4611. || (header.byteorder == BFD_ENDIAN_LITTLE
  4612. && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
  4613. && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
  4614. goto wrong;
  4615. /* Check cputype and filetype.
  4616. In case of wildcard, do not accept magics that are handled by existing
  4617. targets. */
  4618. if (cpu_type)
  4619. {
  4620. if (header.cputype != cpu_type)
  4621. goto wrong;
  4622. }
  4623. else
  4624. {
  4625. #ifndef BFD64
  4626. /* Do not recognize 64 architectures if not configured for 64bit targets.
  4627. This could happen only for generic targets. */
  4628. if (mach_o_wide_p (&header))
  4629. goto wrong;
  4630. #endif
  4631. }
  4632. if (file_type)
  4633. {
  4634. if (header.filetype != file_type)
  4635. goto wrong;
  4636. }
  4637. else
  4638. {
  4639. switch (header.filetype)
  4640. {
  4641. case BFD_MACH_O_MH_CORE:
  4642. /* Handled by core_p */
  4643. goto wrong;
  4644. default:
  4645. break;
  4646. }
  4647. }
  4648. mdata = (bfd_mach_o_data_struct *) bfd_zalloc (abfd, sizeof (*mdata));
  4649. if (mdata == NULL)
  4650. goto fail;
  4651. mdata->hdr_offset = hdr_off;
  4652. if (!bfd_mach_o_scan (abfd, &header, mdata))
  4653. goto wrong;
  4654. return _bfd_no_cleanup;
  4655. wrong:
  4656. bfd_set_error (bfd_error_wrong_format);
  4657. fail:
  4658. return NULL;
  4659. }
  4660. static bfd_cleanup
  4661. bfd_mach_o_gen_object_p (bfd *abfd)
  4662. {
  4663. return bfd_mach_o_header_p (abfd, 0, 0, 0);
  4664. }
  4665. static bfd_cleanup
  4666. bfd_mach_o_gen_core_p (bfd *abfd)
  4667. {
  4668. return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_MH_CORE, 0);
  4669. }
  4670. /* Return the base address of ABFD, ie the address at which the image is
  4671. mapped. The possible initial pagezero is ignored. */
  4672. bfd_vma
  4673. bfd_mach_o_get_base_address (bfd *abfd)
  4674. {
  4675. bfd_mach_o_data_struct *mdata;
  4676. bfd_mach_o_load_command *cmd;
  4677. /* Check for Mach-O. */
  4678. if (!bfd_mach_o_valid (abfd))
  4679. return 0;
  4680. mdata = bfd_mach_o_get_data (abfd);
  4681. for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
  4682. {
  4683. if ((cmd->type == BFD_MACH_O_LC_SEGMENT
  4684. || cmd->type == BFD_MACH_O_LC_SEGMENT_64))
  4685. {
  4686. struct bfd_mach_o_segment_command *segcmd = &cmd->command.segment;
  4687. if (segcmd->initprot != 0)
  4688. return segcmd->vmaddr;
  4689. }
  4690. }
  4691. return 0;
  4692. }
  4693. typedef struct mach_o_fat_archentry
  4694. {
  4695. unsigned long cputype;
  4696. unsigned long cpusubtype;
  4697. unsigned long offset;
  4698. unsigned long size;
  4699. unsigned long align;
  4700. } mach_o_fat_archentry;
  4701. typedef struct mach_o_fat_data_struct
  4702. {
  4703. unsigned long magic;
  4704. unsigned long nfat_arch;
  4705. mach_o_fat_archentry *archentries;
  4706. } mach_o_fat_data_struct;
  4707. bfd_cleanup
  4708. bfd_mach_o_fat_archive_p (bfd *abfd)
  4709. {
  4710. mach_o_fat_data_struct *adata = NULL;
  4711. struct mach_o_fat_header_external hdr;
  4712. unsigned long i;
  4713. size_t amt;
  4714. ufile_ptr filesize;
  4715. if (bfd_seek (abfd, 0, SEEK_SET) != 0
  4716. || bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
  4717. goto error;
  4718. adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
  4719. if (adata == NULL)
  4720. goto error;
  4721. adata->magic = bfd_getb32 (hdr.magic);
  4722. adata->nfat_arch = bfd_getb32 (hdr.nfat_arch);
  4723. if (adata->magic != 0xcafebabe)
  4724. goto error;
  4725. /* Avoid matching Java bytecode files, which have the same magic number.
  4726. In the Java bytecode file format this field contains the JVM version,
  4727. which starts at 43.0. */
  4728. if (adata->nfat_arch > 30)
  4729. goto error;
  4730. if (_bfd_mul_overflow (adata->nfat_arch,
  4731. sizeof (mach_o_fat_archentry), &amt))
  4732. {
  4733. bfd_set_error (bfd_error_file_too_big);
  4734. goto error;
  4735. }
  4736. adata->archentries = bfd_alloc (abfd, amt);
  4737. if (adata->archentries == NULL)
  4738. goto error;
  4739. filesize = bfd_get_file_size (abfd);
  4740. for (i = 0; i < adata->nfat_arch; i++)
  4741. {
  4742. struct mach_o_fat_arch_external arch;
  4743. if (bfd_bread (&arch, sizeof (arch), abfd) != sizeof (arch))
  4744. goto error;
  4745. adata->archentries[i].cputype = bfd_getb32 (arch.cputype);
  4746. adata->archentries[i].cpusubtype = bfd_getb32 (arch.cpusubtype);
  4747. adata->archentries[i].offset = bfd_getb32 (arch.offset);
  4748. adata->archentries[i].size = bfd_getb32 (arch.size);
  4749. adata->archentries[i].align = bfd_getb32 (arch.align);
  4750. if (filesize != 0
  4751. && (adata->archentries[i].offset > filesize
  4752. || (adata->archentries[i].size
  4753. > filesize - adata->archentries[i].offset)))
  4754. {
  4755. bfd_release (abfd, adata);
  4756. bfd_set_error (bfd_error_malformed_archive);
  4757. return NULL;
  4758. }
  4759. }
  4760. abfd->tdata.mach_o_fat_data = adata;
  4761. return _bfd_no_cleanup;
  4762. error:
  4763. if (adata != NULL)
  4764. bfd_release (abfd, adata);
  4765. bfd_set_error (bfd_error_wrong_format);
  4766. return NULL;
  4767. }
  4768. /* Set the filename for a fat binary member ABFD, whose bfd architecture is
  4769. ARCH_TYPE/ARCH_SUBTYPE and corresponding entry in header is ENTRY.
  4770. Set arelt_data and origin fields too. */
  4771. static bool
  4772. bfd_mach_o_fat_member_init (bfd *abfd,
  4773. enum bfd_architecture arch_type,
  4774. unsigned long arch_subtype,
  4775. mach_o_fat_archentry *entry)
  4776. {
  4777. struct areltdata *areltdata;
  4778. /* Create the member filename. Use ARCH_NAME. */
  4779. const bfd_arch_info_type *ap = bfd_lookup_arch (arch_type, arch_subtype);
  4780. const char *filename;
  4781. if (ap)
  4782. {
  4783. /* Use the architecture name if known. */
  4784. filename = bfd_set_filename (abfd, ap->printable_name);
  4785. }
  4786. else
  4787. {
  4788. /* Forge a uniq id. */
  4789. char buf[2 + 8 + 1 + 2 + 8 + 1];
  4790. snprintf (buf, sizeof (buf), "0x%lx-0x%lx",
  4791. entry->cputype, entry->cpusubtype);
  4792. filename = bfd_set_filename (abfd, buf);
  4793. }
  4794. if (!filename)
  4795. return false;
  4796. areltdata = bfd_zmalloc (sizeof (struct areltdata));
  4797. if (areltdata == NULL)
  4798. return false;
  4799. areltdata->parsed_size = entry->size;
  4800. abfd->arelt_data = areltdata;
  4801. abfd->iostream = NULL;
  4802. abfd->origin = entry->offset;
  4803. return true;
  4804. }
  4805. bfd *
  4806. bfd_mach_o_fat_openr_next_archived_file (bfd *archive, bfd *prev)
  4807. {
  4808. mach_o_fat_data_struct *adata;
  4809. mach_o_fat_archentry *entry = NULL;
  4810. unsigned long i;
  4811. bfd *nbfd;
  4812. enum bfd_architecture arch_type;
  4813. unsigned long arch_subtype;
  4814. adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
  4815. BFD_ASSERT (adata != NULL);
  4816. /* Find index of previous entry. */
  4817. if (prev == NULL)
  4818. {
  4819. /* Start at first one. */
  4820. i = 0;
  4821. }
  4822. else
  4823. {
  4824. /* Find index of PREV. */
  4825. for (i = 0; i < adata->nfat_arch; i++)
  4826. {
  4827. if (adata->archentries[i].offset == prev->origin)
  4828. break;
  4829. }
  4830. if (i == adata->nfat_arch)
  4831. {
  4832. /* Not found. */
  4833. bfd_set_error (bfd_error_bad_value);
  4834. return NULL;
  4835. }
  4836. /* Get next entry. */
  4837. i++;
  4838. }
  4839. if (i >= adata->nfat_arch)
  4840. {
  4841. bfd_set_error (bfd_error_no_more_archived_files);
  4842. return NULL;
  4843. }
  4844. entry = &adata->archentries[i];
  4845. nbfd = _bfd_new_bfd_contained_in (archive);
  4846. if (nbfd == NULL)
  4847. return NULL;
  4848. bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
  4849. &arch_type, &arch_subtype);
  4850. if (!bfd_mach_o_fat_member_init (nbfd, arch_type, arch_subtype, entry))
  4851. {
  4852. bfd_close (nbfd);
  4853. return NULL;
  4854. }
  4855. bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
  4856. return nbfd;
  4857. }
  4858. /* Analogous to stat call. */
  4859. static int
  4860. bfd_mach_o_fat_stat_arch_elt (bfd *abfd, struct stat *buf)
  4861. {
  4862. if (abfd->arelt_data == NULL)
  4863. {
  4864. bfd_set_error (bfd_error_invalid_operation);
  4865. return -1;
  4866. }
  4867. buf->st_mtime = 0;
  4868. buf->st_uid = 0;
  4869. buf->st_gid = 0;
  4870. buf->st_mode = 0644;
  4871. buf->st_size = arelt_size (abfd);
  4872. return 0;
  4873. }
  4874. /* If ABFD format is FORMAT and architecture is ARCH, return it.
  4875. If ABFD is a fat image containing a member that corresponds to FORMAT
  4876. and ARCH, returns it.
  4877. In other case, returns NULL.
  4878. This function allows transparent uses of fat images. */
  4879. bfd *
  4880. bfd_mach_o_fat_extract (bfd *abfd,
  4881. bfd_format format,
  4882. const bfd_arch_info_type *arch)
  4883. {
  4884. bfd *res;
  4885. mach_o_fat_data_struct *adata;
  4886. unsigned int i;
  4887. if (bfd_check_format (abfd, format))
  4888. {
  4889. if (bfd_get_arch_info (abfd) == arch)
  4890. return abfd;
  4891. return NULL;
  4892. }
  4893. if (!bfd_check_format (abfd, bfd_archive)
  4894. || abfd->xvec != &mach_o_fat_vec)
  4895. return NULL;
  4896. /* This is a Mach-O fat image. */
  4897. adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
  4898. BFD_ASSERT (adata != NULL);
  4899. for (i = 0; i < adata->nfat_arch; i++)
  4900. {
  4901. struct mach_o_fat_archentry *e = &adata->archentries[i];
  4902. enum bfd_architecture cpu_type;
  4903. unsigned long cpu_subtype;
  4904. bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
  4905. &cpu_type, &cpu_subtype);
  4906. if (cpu_type != arch->arch || cpu_subtype != arch->mach)
  4907. continue;
  4908. /* The architecture is found. */
  4909. res = _bfd_new_bfd_contained_in (abfd);
  4910. if (res == NULL)
  4911. return NULL;
  4912. if (bfd_mach_o_fat_member_init (res, cpu_type, cpu_subtype, e)
  4913. && bfd_check_format (res, format))
  4914. {
  4915. BFD_ASSERT (bfd_get_arch_info (res) == arch);
  4916. return res;
  4917. }
  4918. bfd_close (res);
  4919. return NULL;
  4920. }
  4921. return NULL;
  4922. }
  4923. static bool
  4924. bfd_mach_o_fat_close_and_cleanup (bfd *abfd)
  4925. {
  4926. _bfd_unlink_from_archive_parent (abfd);
  4927. return true;
  4928. }
  4929. int
  4930. bfd_mach_o_lookup_command (bfd *abfd,
  4931. bfd_mach_o_load_command_type type,
  4932. bfd_mach_o_load_command **mcommand)
  4933. {
  4934. struct mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  4935. struct bfd_mach_o_load_command *cmd;
  4936. unsigned int num;
  4937. BFD_ASSERT (mdata != NULL);
  4938. BFD_ASSERT (mcommand != NULL);
  4939. num = 0;
  4940. for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
  4941. {
  4942. if (cmd->type != type)
  4943. continue;
  4944. if (num == 0)
  4945. *mcommand = cmd;
  4946. num++;
  4947. }
  4948. return num;
  4949. }
  4950. unsigned long
  4951. bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
  4952. {
  4953. switch (type)
  4954. {
  4955. case BFD_MACH_O_CPU_TYPE_MC680x0:
  4956. return 0x04000000;
  4957. case BFD_MACH_O_CPU_TYPE_POWERPC:
  4958. return 0xc0000000;
  4959. case BFD_MACH_O_CPU_TYPE_I386:
  4960. return 0xc0000000;
  4961. case BFD_MACH_O_CPU_TYPE_SPARC:
  4962. return 0xf0000000;
  4963. case BFD_MACH_O_CPU_TYPE_HPPA:
  4964. return 0xc0000000 - 0x04000000;
  4965. default:
  4966. return 0;
  4967. }
  4968. }
  4969. /* The following two tables should be kept, as far as possible, in order of
  4970. most frequently used entries to optimize their use from gas. */
  4971. const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
  4972. {
  4973. { "regular", BFD_MACH_O_S_REGULAR},
  4974. { "coalesced", BFD_MACH_O_S_COALESCED},
  4975. { "zerofill", BFD_MACH_O_S_ZEROFILL},
  4976. { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
  4977. { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
  4978. { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
  4979. { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
  4980. { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
  4981. { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
  4982. { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
  4983. { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
  4984. { "interposing", BFD_MACH_O_S_INTERPOSING},
  4985. { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
  4986. { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
  4987. { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
  4988. { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
  4989. { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
  4990. { NULL, 0}
  4991. };
  4992. const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
  4993. {
  4994. { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
  4995. { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
  4996. { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
  4997. { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
  4998. { "debug", BFD_MACH_O_S_ATTR_DEBUG },
  4999. { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
  5000. { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
  5001. { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
  5002. { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
  5003. { "self_modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
  5004. { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
  5005. { NULL, 0}
  5006. };
  5007. /* Get the section type from NAME. Return 256 if NAME is unknown. */
  5008. unsigned int
  5009. bfd_mach_o_get_section_type_from_name (bfd *abfd, const char *name)
  5010. {
  5011. const bfd_mach_o_xlat_name *x;
  5012. bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
  5013. for (x = bfd_mach_o_section_type_name; x->name; x++)
  5014. if (strcmp (x->name, name) == 0)
  5015. {
  5016. /* We found it... does the target support it? */
  5017. if (bed->bfd_mach_o_section_type_valid_for_target == NULL
  5018. || bed->bfd_mach_o_section_type_valid_for_target (x->val))
  5019. return x->val; /* OK. */
  5020. else
  5021. break; /* Not supported. */
  5022. }
  5023. /* Maximum section ID = 0xff. */
  5024. return 256;
  5025. }
  5026. /* Get the section attribute from NAME. Return -1 if NAME is unknown. */
  5027. unsigned int
  5028. bfd_mach_o_get_section_attribute_from_name (const char *name)
  5029. {
  5030. const bfd_mach_o_xlat_name *x;
  5031. for (x = bfd_mach_o_section_attribute_name; x->name; x++)
  5032. if (strcmp (x->name, name) == 0)
  5033. return x->val;
  5034. return (unsigned int)-1;
  5035. }
  5036. int
  5037. bfd_mach_o_core_fetch_environment (bfd *abfd,
  5038. unsigned char **rbuf,
  5039. unsigned int *rlen)
  5040. {
  5041. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  5042. unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
  5043. bfd_mach_o_load_command *cmd;
  5044. for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
  5045. {
  5046. bfd_mach_o_segment_command *seg;
  5047. if (cmd->type != BFD_MACH_O_LC_SEGMENT)
  5048. continue;
  5049. seg = &cmd->command.segment;
  5050. if ((seg->vmaddr + seg->vmsize) == stackaddr)
  5051. {
  5052. unsigned long start = seg->fileoff;
  5053. unsigned long end = seg->fileoff + seg->filesize;
  5054. unsigned char *buf = bfd_malloc (1024);
  5055. unsigned long size = 1024;
  5056. if (buf == NULL)
  5057. return -1;
  5058. for (;;)
  5059. {
  5060. bfd_size_type nread = 0;
  5061. unsigned long offset;
  5062. int found_nonnull = 0;
  5063. if (size > (end - start))
  5064. size = (end - start);
  5065. buf = bfd_realloc_or_free (buf, size);
  5066. if (buf == NULL)
  5067. return -1;
  5068. if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
  5069. {
  5070. free (buf);
  5071. return -1;
  5072. }
  5073. nread = bfd_bread (buf, size, abfd);
  5074. if (nread != size)
  5075. {
  5076. free (buf);
  5077. return -1;
  5078. }
  5079. for (offset = 4; offset <= size; offset += 4)
  5080. {
  5081. unsigned long val;
  5082. val = bfd_get_32(abfd, buf + size - offset);
  5083. if (! found_nonnull)
  5084. {
  5085. if (val != 0)
  5086. found_nonnull = 1;
  5087. }
  5088. else if (val == 0x0)
  5089. {
  5090. unsigned long bottom;
  5091. unsigned long top;
  5092. bottom = seg->fileoff + seg->filesize - offset;
  5093. top = seg->fileoff + seg->filesize - 4;
  5094. *rbuf = bfd_malloc (top - bottom);
  5095. if (*rbuf == NULL)
  5096. return -1;
  5097. *rlen = top - bottom;
  5098. memcpy (*rbuf, buf + size - *rlen, *rlen);
  5099. free (buf);
  5100. return 0;
  5101. }
  5102. }
  5103. if (size == (end - start))
  5104. break;
  5105. size *= 2;
  5106. }
  5107. free (buf);
  5108. }
  5109. }
  5110. return -1;
  5111. }
  5112. char *
  5113. bfd_mach_o_core_file_failing_command (bfd *abfd)
  5114. {
  5115. unsigned char *buf = NULL;
  5116. unsigned int len = 0;
  5117. int ret;
  5118. ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
  5119. if (ret < 0)
  5120. return NULL;
  5121. return (char *) buf;
  5122. }
  5123. int
  5124. bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
  5125. {
  5126. return 0;
  5127. }
  5128. static bfd_mach_o_uuid_command *
  5129. bfd_mach_o_lookup_uuid_command (bfd *abfd)
  5130. {
  5131. bfd_mach_o_load_command *uuid_cmd = NULL;
  5132. int ncmd = bfd_mach_o_lookup_command (abfd, BFD_MACH_O_LC_UUID, &uuid_cmd);
  5133. if (ncmd != 1 || uuid_cmd == NULL)
  5134. return false;
  5135. return &uuid_cmd->command.uuid;
  5136. }
  5137. /* Return true if ABFD is a dSYM file and its UUID matches UUID_CMD. */
  5138. static bool
  5139. bfd_mach_o_dsym_for_uuid_p (bfd *abfd, const bfd_mach_o_uuid_command *uuid_cmd)
  5140. {
  5141. bfd_mach_o_uuid_command *dsym_uuid_cmd;
  5142. BFD_ASSERT (abfd);
  5143. BFD_ASSERT (uuid_cmd);
  5144. if (!bfd_check_format (abfd, bfd_object))
  5145. return false;
  5146. if (bfd_get_flavour (abfd) != bfd_target_mach_o_flavour
  5147. || bfd_mach_o_get_data (abfd) == NULL
  5148. || bfd_mach_o_get_data (abfd)->header.filetype != BFD_MACH_O_MH_DSYM)
  5149. return false;
  5150. dsym_uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd);
  5151. if (dsym_uuid_cmd == NULL)
  5152. return false;
  5153. if (memcmp (uuid_cmd->uuid, dsym_uuid_cmd->uuid,
  5154. sizeof (uuid_cmd->uuid)) != 0)
  5155. return false;
  5156. return true;
  5157. }
  5158. /* Find a BFD in DSYM_FILENAME which matches ARCH and UUID_CMD.
  5159. The caller is responsible for closing the returned BFD object and
  5160. its my_archive if the returned BFD is in a fat dSYM. */
  5161. static bfd *
  5162. bfd_mach_o_find_dsym (const char *dsym_filename,
  5163. const bfd_mach_o_uuid_command *uuid_cmd,
  5164. const bfd_arch_info_type *arch)
  5165. {
  5166. bfd *base_dsym_bfd, *dsym_bfd;
  5167. BFD_ASSERT (uuid_cmd);
  5168. base_dsym_bfd = bfd_openr (dsym_filename, NULL);
  5169. if (base_dsym_bfd == NULL)
  5170. return NULL;
  5171. dsym_bfd = bfd_mach_o_fat_extract (base_dsym_bfd, bfd_object, arch);
  5172. if (bfd_mach_o_dsym_for_uuid_p (dsym_bfd, uuid_cmd))
  5173. return dsym_bfd;
  5174. bfd_close (dsym_bfd);
  5175. if (base_dsym_bfd != dsym_bfd)
  5176. bfd_close (base_dsym_bfd);
  5177. return NULL;
  5178. }
  5179. /* Return a BFD created from a dSYM file for ABFD.
  5180. The caller is responsible for closing the returned BFD object, its
  5181. filename, and its my_archive if the returned BFD is in a fat dSYM. */
  5182. static bfd *
  5183. bfd_mach_o_follow_dsym (bfd *abfd)
  5184. {
  5185. char *dsym_filename;
  5186. bfd_mach_o_uuid_command *uuid_cmd;
  5187. bfd *dsym_bfd, *base_bfd = abfd;
  5188. const char *base_basename;
  5189. if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_mach_o_flavour)
  5190. return NULL;
  5191. if (abfd->my_archive && !bfd_is_thin_archive (abfd->my_archive))
  5192. base_bfd = abfd->my_archive;
  5193. /* BFD may have been opened from a stream. */
  5194. if (bfd_get_filename (base_bfd) == NULL)
  5195. {
  5196. bfd_set_error (bfd_error_invalid_operation);
  5197. return NULL;
  5198. }
  5199. base_basename = lbasename (bfd_get_filename (base_bfd));
  5200. uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd);
  5201. if (uuid_cmd == NULL)
  5202. return NULL;
  5203. /* TODO: We assume the DWARF file has the same as the binary's.
  5204. It seems apple's GDB checks all files in the dSYM bundle directory.
  5205. http://opensource.apple.com/source/gdb/gdb-1708/src/gdb/macosx/macosx-tdep.c
  5206. */
  5207. dsym_filename = (char *)bfd_malloc (strlen (bfd_get_filename (base_bfd))
  5208. + strlen (dsym_subdir) + 1
  5209. + strlen (base_basename) + 1);
  5210. if (dsym_filename == NULL)
  5211. return NULL;
  5212. sprintf (dsym_filename, "%s%s/%s",
  5213. bfd_get_filename (base_bfd), dsym_subdir, base_basename);
  5214. dsym_bfd = bfd_mach_o_find_dsym (dsym_filename, uuid_cmd,
  5215. bfd_get_arch_info (abfd));
  5216. if (dsym_bfd == NULL)
  5217. free (dsym_filename);
  5218. return dsym_bfd;
  5219. }
  5220. bool
  5221. bfd_mach_o_find_nearest_line (bfd *abfd,
  5222. asymbol **symbols,
  5223. asection *section,
  5224. bfd_vma offset,
  5225. const char **filename_ptr,
  5226. const char **functionname_ptr,
  5227. unsigned int *line_ptr,
  5228. unsigned int *discriminator_ptr)
  5229. {
  5230. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  5231. if (mdata == NULL)
  5232. return false;
  5233. switch (mdata->header.filetype)
  5234. {
  5235. case BFD_MACH_O_MH_OBJECT:
  5236. break;
  5237. case BFD_MACH_O_MH_EXECUTE:
  5238. case BFD_MACH_O_MH_DYLIB:
  5239. case BFD_MACH_O_MH_BUNDLE:
  5240. case BFD_MACH_O_MH_KEXT_BUNDLE:
  5241. if (mdata->dwarf2_find_line_info == NULL)
  5242. {
  5243. mdata->dsym_bfd = bfd_mach_o_follow_dsym (abfd);
  5244. /* When we couldn't find dSYM for this binary, we look for
  5245. the debug information in the binary itself. In this way,
  5246. we won't try finding separated dSYM again because
  5247. mdata->dwarf2_find_line_info will be filled. */
  5248. if (! mdata->dsym_bfd)
  5249. break;
  5250. if (! _bfd_dwarf2_slurp_debug_info (abfd, mdata->dsym_bfd,
  5251. dwarf_debug_sections, symbols,
  5252. &mdata->dwarf2_find_line_info,
  5253. false))
  5254. return false;
  5255. }
  5256. break;
  5257. default:
  5258. return false;
  5259. }
  5260. return _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
  5261. filename_ptr, functionname_ptr,
  5262. line_ptr, discriminator_ptr,
  5263. dwarf_debug_sections,
  5264. &mdata->dwarf2_find_line_info);
  5265. }
  5266. bool
  5267. bfd_mach_o_close_and_cleanup (bfd *abfd)
  5268. {
  5269. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  5270. if (bfd_get_format (abfd) == bfd_object && mdata != NULL)
  5271. {
  5272. _bfd_dwarf2_cleanup_debug_info (abfd, &mdata->dwarf2_find_line_info);
  5273. bfd_mach_o_free_cached_info (abfd);
  5274. if (mdata->dsym_bfd != NULL)
  5275. {
  5276. bfd *fat_bfd = mdata->dsym_bfd->my_archive;
  5277. #if 0
  5278. /* FIXME: PR 19435: This calculation to find the memory allocated by
  5279. bfd_mach_o_follow_dsym for the filename does not always end up
  5280. selecting the correct pointer. Unfortunately this problem is
  5281. very hard to reproduce on a non Mach-O native system, so until it
  5282. can be traced and fixed on such a system, this code will remain
  5283. commented out. This does mean that there will be a memory leak,
  5284. but it is small, and happens when we are closing down, so it
  5285. should not matter too much. */
  5286. char *dsym_filename = (char *)(fat_bfd
  5287. ? bfd_get_filename (fat_bfd)
  5288. : bfd_get_filename (mdata->dsym_bfd));
  5289. #endif
  5290. bfd_close (mdata->dsym_bfd);
  5291. mdata->dsym_bfd = NULL;
  5292. if (fat_bfd)
  5293. bfd_close (fat_bfd);
  5294. #if 0
  5295. free (dsym_filename);
  5296. #endif
  5297. }
  5298. }
  5299. return _bfd_generic_close_and_cleanup (abfd);
  5300. }
  5301. bool
  5302. bfd_mach_o_free_cached_info (bfd *abfd)
  5303. {
  5304. bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
  5305. asection *asect;
  5306. free (mdata->dyn_reloc_cache);
  5307. mdata->dyn_reloc_cache = NULL;
  5308. for (asect = abfd->sections; asect != NULL; asect = asect->next)
  5309. {
  5310. free (asect->relocation);
  5311. asect->relocation = NULL;
  5312. }
  5313. return true;
  5314. }
  5315. #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
  5316. #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
  5317. #define bfd_mach_o_canonicalize_one_reloc NULL
  5318. #define bfd_mach_o_swap_reloc_out NULL
  5319. #define bfd_mach_o_print_thread NULL
  5320. #define bfd_mach_o_tgt_seg_table NULL
  5321. #define bfd_mach_o_section_type_valid_for_tgt NULL
  5322. #define TARGET_NAME mach_o_be_vec
  5323. #define TARGET_STRING "mach-o-be"
  5324. #define TARGET_ARCHITECTURE bfd_arch_unknown
  5325. #define TARGET_PAGESIZE 1
  5326. #define TARGET_BIG_ENDIAN 1
  5327. #define TARGET_ARCHIVE 0
  5328. #define TARGET_PRIORITY 1
  5329. #include "mach-o-target.c"
  5330. #undef TARGET_NAME
  5331. #undef TARGET_STRING
  5332. #undef TARGET_ARCHITECTURE
  5333. #undef TARGET_PAGESIZE
  5334. #undef TARGET_BIG_ENDIAN
  5335. #undef TARGET_ARCHIVE
  5336. #undef TARGET_PRIORITY
  5337. #define TARGET_NAME mach_o_le_vec
  5338. #define TARGET_STRING "mach-o-le"
  5339. #define TARGET_ARCHITECTURE bfd_arch_unknown
  5340. #define TARGET_PAGESIZE 1
  5341. #define TARGET_BIG_ENDIAN 0
  5342. #define TARGET_ARCHIVE 0
  5343. #define TARGET_PRIORITY 1
  5344. #include "mach-o-target.c"
  5345. #undef TARGET_NAME
  5346. #undef TARGET_STRING
  5347. #undef TARGET_ARCHITECTURE
  5348. #undef TARGET_PAGESIZE
  5349. #undef TARGET_BIG_ENDIAN
  5350. #undef TARGET_ARCHIVE
  5351. #undef TARGET_PRIORITY
  5352. /* Not yet handled: creating an archive. */
  5353. #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
  5354. #define bfd_mach_o_close_and_cleanup bfd_mach_o_fat_close_and_cleanup
  5355. /* Not used. */
  5356. #define bfd_mach_o_generic_stat_arch_elt bfd_mach_o_fat_stat_arch_elt
  5357. #define bfd_mach_o_openr_next_archived_file bfd_mach_o_fat_openr_next_archived_file
  5358. #define bfd_mach_o_archive_p bfd_mach_o_fat_archive_p
  5359. #define TARGET_NAME mach_o_fat_vec
  5360. #define TARGET_STRING "mach-o-fat"
  5361. #define TARGET_ARCHITECTURE bfd_arch_unknown
  5362. #define TARGET_PAGESIZE 1
  5363. #define TARGET_BIG_ENDIAN 1
  5364. #define TARGET_ARCHIVE 1
  5365. #define TARGET_PRIORITY 0
  5366. #include "mach-o-target.c"
  5367. #undef TARGET_NAME
  5368. #undef TARGET_STRING
  5369. #undef TARGET_ARCHITECTURE
  5370. #undef TARGET_PAGESIZE
  5371. #undef TARGET_BIG_ENDIAN
  5372. #undef TARGET_ARCHIVE
  5373. #undef TARGET_PRIORITY