MachODump.cpp 373 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314
  1. //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the MachO-specific dumper for llvm-objdump.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm-objdump.h"
  13. #include "llvm-c/Disassembler.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/StringExtras.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/BinaryFormat/MachO.h"
  18. #include "llvm/Config/config.h"
  19. #include "llvm/DebugInfo/DIContext.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  21. #include "llvm/Demangle/Demangle.h"
  22. #include "llvm/MC/MCAsmInfo.h"
  23. #include "llvm/MC/MCContext.h"
  24. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  25. #include "llvm/MC/MCInst.h"
  26. #include "llvm/MC/MCInstPrinter.h"
  27. #include "llvm/MC/MCInstrDesc.h"
  28. #include "llvm/MC/MCInstrInfo.h"
  29. #include "llvm/MC/MCRegisterInfo.h"
  30. #include "llvm/MC/MCSubtargetInfo.h"
  31. #include "llvm/Object/MachO.h"
  32. #include "llvm/Object/MachOUniversal.h"
  33. #include "llvm/Support/Casting.h"
  34. #include "llvm/Support/CommandLine.h"
  35. #include "llvm/Support/Debug.h"
  36. #include "llvm/Support/Endian.h"
  37. #include "llvm/Support/Format.h"
  38. #include "llvm/Support/FormattedStream.h"
  39. #include "llvm/Support/GraphWriter.h"
  40. #include "llvm/Support/LEB128.h"
  41. #include "llvm/Support/MemoryBuffer.h"
  42. #include "llvm/Support/TargetRegistry.h"
  43. #include "llvm/Support/TargetSelect.h"
  44. #include "llvm/Support/ToolOutputFile.h"
  45. #include "llvm/Support/WithColor.h"
  46. #include "llvm/Support/raw_ostream.h"
  47. #include <algorithm>
  48. #include <cstring>
  49. #include <system_error>
  50. #ifdef HAVE_LIBXAR
  51. extern "C" {
  52. #include <xar/xar.h>
  53. }
  54. #endif
  55. using namespace llvm::object;
  56. namespace llvm {
  57. extern cl::opt<bool> ArchiveHeaders;
  58. extern cl::opt<bool> Disassemble;
  59. extern cl::opt<bool> DisassembleAll;
  60. extern cl::opt<DIDumpType> DwarfDumpType;
  61. extern cl::list<std::string> FilterSections;
  62. extern cl::list<std::string> MAttrs;
  63. extern cl::opt<std::string> MCPU;
  64. extern cl::opt<bool> NoShowRawInsn;
  65. extern cl::opt<bool> NoLeadingAddr;
  66. extern cl::opt<bool> PrintImmHex;
  67. extern cl::opt<bool> PrivateHeaders;
  68. extern cl::opt<bool> Relocations;
  69. extern cl::opt<bool> SectionHeaders;
  70. extern cl::opt<bool> SectionContents;
  71. extern cl::opt<bool> SymbolTable;
  72. extern cl::opt<std::string> TripleName;
  73. extern cl::opt<bool> UnwindInfo;
  74. cl::opt<bool>
  75. FirstPrivateHeader("private-header",
  76. cl::desc("Display only the first format specific file "
  77. "header"));
  78. cl::opt<bool> ExportsTrie("exports-trie",
  79. cl::desc("Display mach-o exported symbols"));
  80. cl::opt<bool> Rebase("rebase", cl::desc("Display mach-o rebasing info"));
  81. cl::opt<bool> Bind("bind", cl::desc("Display mach-o binding info"));
  82. cl::opt<bool> LazyBind("lazy-bind",
  83. cl::desc("Display mach-o lazy binding info"));
  84. cl::opt<bool> WeakBind("weak-bind",
  85. cl::desc("Display mach-o weak binding info"));
  86. static cl::opt<bool>
  87. UseDbg("g", cl::Grouping,
  88. cl::desc("Print line information from debug info if available"));
  89. static cl::opt<std::string> DSYMFile("dsym",
  90. cl::desc("Use .dSYM file for debug info"));
  91. static cl::opt<bool> FullLeadingAddr("full-leading-addr",
  92. cl::desc("Print full leading address"));
  93. static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
  94. cl::desc("Print no leading headers"));
  95. cl::opt<bool> UniversalHeaders("universal-headers",
  96. cl::desc("Print Mach-O universal headers "
  97. "(requires -macho)"));
  98. cl::opt<bool>
  99. ArchiveMemberOffsets("archive-member-offsets",
  100. cl::desc("Print the offset to each archive member for "
  101. "Mach-O archives (requires -macho and "
  102. "-archive-headers)"));
  103. cl::opt<bool> IndirectSymbols("indirect-symbols",
  104. cl::desc("Print indirect symbol table for Mach-O "
  105. "objects (requires -macho)"));
  106. cl::opt<bool>
  107. DataInCode("data-in-code",
  108. cl::desc("Print the data in code table for Mach-O objects "
  109. "(requires -macho)"));
  110. cl::opt<bool> LinkOptHints("link-opt-hints",
  111. cl::desc("Print the linker optimization hints for "
  112. "Mach-O objects (requires -macho)"));
  113. cl::opt<bool> InfoPlist("info-plist",
  114. cl::desc("Print the info plist section as strings for "
  115. "Mach-O objects (requires -macho)"));
  116. cl::opt<bool> DylibsUsed("dylibs-used",
  117. cl::desc("Print the shared libraries used for linked "
  118. "Mach-O files (requires -macho)"));
  119. cl::opt<bool>
  120. DylibId("dylib-id",
  121. cl::desc("Print the shared library's id for the dylib Mach-O "
  122. "file (requires -macho)"));
  123. cl::opt<bool>
  124. NonVerbose("non-verbose",
  125. cl::desc("Print the info for Mach-O objects in "
  126. "non-verbose or numeric form (requires -macho)"));
  127. cl::opt<bool>
  128. ObjcMetaData("objc-meta-data",
  129. cl::desc("Print the Objective-C runtime meta data for "
  130. "Mach-O files (requires -macho)"));
  131. cl::opt<std::string> DisSymName(
  132. "dis-symname",
  133. cl::desc("disassemble just this symbol's instructions (requires -macho)"));
  134. static cl::opt<bool> NoSymbolicOperands(
  135. "no-symbolic-operands",
  136. cl::desc("do not symbolic operands when disassembling (requires -macho)"));
  137. static cl::list<std::string>
  138. ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
  139. cl::ZeroOrMore);
  140. bool ArchAll = false;
  141. static std::string ThumbTripleName;
  142. static const Target *GetTarget(const MachOObjectFile *MachOObj,
  143. const char **McpuDefault,
  144. const Target **ThumbTarget) {
  145. // Figure out the target triple.
  146. Triple TT(TripleName);
  147. if (TripleName.empty()) {
  148. TT = MachOObj->getArchTriple(McpuDefault);
  149. TripleName = TT.str();
  150. }
  151. if (TT.getArch() == Triple::arm) {
  152. // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
  153. // that support ARM are also capable of Thumb mode.
  154. Triple ThumbTriple = TT;
  155. std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
  156. ThumbTriple.setArchName(ThumbName);
  157. ThumbTripleName = ThumbTriple.str();
  158. }
  159. // Get the target specific parser.
  160. std::string Error;
  161. const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
  162. if (TheTarget && ThumbTripleName.empty())
  163. return TheTarget;
  164. *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
  165. if (*ThumbTarget)
  166. return TheTarget;
  167. WithColor::error(errs(), "llvm-objdump") << "unable to get target for '";
  168. if (!TheTarget)
  169. errs() << TripleName;
  170. else
  171. errs() << ThumbTripleName;
  172. errs() << "', see --version and --triple.\n";
  173. return nullptr;
  174. }
  175. struct SymbolSorter {
  176. bool operator()(const SymbolRef &A, const SymbolRef &B) {
  177. Expected<SymbolRef::Type> ATypeOrErr = A.getType();
  178. if (!ATypeOrErr)
  179. report_error(ATypeOrErr.takeError(), A.getObject()->getFileName());
  180. SymbolRef::Type AType = *ATypeOrErr;
  181. Expected<SymbolRef::Type> BTypeOrErr = B.getType();
  182. if (!BTypeOrErr)
  183. report_error(BTypeOrErr.takeError(), B.getObject()->getFileName());
  184. SymbolRef::Type BType = *BTypeOrErr;
  185. uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
  186. uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
  187. return AAddr < BAddr;
  188. }
  189. };
  190. // Types for the storted data in code table that is built before disassembly
  191. // and the predicate function to sort them.
  192. typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
  193. typedef std::vector<DiceTableEntry> DiceTable;
  194. typedef DiceTable::iterator dice_table_iterator;
  195. #ifdef HAVE_LIBXAR
  196. namespace {
  197. struct ScopedXarFile {
  198. xar_t xar;
  199. ScopedXarFile(const char *filename, int32_t flags)
  200. : xar(xar_open(filename, flags)) {}
  201. ~ScopedXarFile() {
  202. if (xar)
  203. xar_close(xar);
  204. }
  205. ScopedXarFile(const ScopedXarFile &) = delete;
  206. ScopedXarFile &operator=(const ScopedXarFile &) = delete;
  207. operator xar_t() { return xar; }
  208. };
  209. struct ScopedXarIter {
  210. xar_iter_t iter;
  211. ScopedXarIter() : iter(xar_iter_new()) {}
  212. ~ScopedXarIter() {
  213. if (iter)
  214. xar_iter_free(iter);
  215. }
  216. ScopedXarIter(const ScopedXarIter &) = delete;
  217. ScopedXarIter &operator=(const ScopedXarIter &) = delete;
  218. operator xar_iter_t() { return iter; }
  219. };
  220. } // namespace
  221. #endif // defined(HAVE_LIBXAR)
  222. // This is used to search for a data in code table entry for the PC being
  223. // disassembled. The j parameter has the PC in j.first. A single data in code
  224. // table entry can cover many bytes for each of its Kind's. So if the offset,
  225. // aka the i.first value, of the data in code table entry plus its Length
  226. // covers the PC being searched for this will return true. If not it will
  227. // return false.
  228. static bool compareDiceTableEntries(const DiceTableEntry &i,
  229. const DiceTableEntry &j) {
  230. uint16_t Length;
  231. i.second.getLength(Length);
  232. return j.first >= i.first && j.first < i.first + Length;
  233. }
  234. static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
  235. unsigned short Kind) {
  236. uint32_t Value, Size = 1;
  237. switch (Kind) {
  238. default:
  239. case MachO::DICE_KIND_DATA:
  240. if (Length >= 4) {
  241. if (!NoShowRawInsn)
  242. dumpBytes(makeArrayRef(bytes, 4), outs());
  243. Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
  244. outs() << "\t.long " << Value;
  245. Size = 4;
  246. } else if (Length >= 2) {
  247. if (!NoShowRawInsn)
  248. dumpBytes(makeArrayRef(bytes, 2), outs());
  249. Value = bytes[1] << 8 | bytes[0];
  250. outs() << "\t.short " << Value;
  251. Size = 2;
  252. } else {
  253. if (!NoShowRawInsn)
  254. dumpBytes(makeArrayRef(bytes, 2), outs());
  255. Value = bytes[0];
  256. outs() << "\t.byte " << Value;
  257. Size = 1;
  258. }
  259. if (Kind == MachO::DICE_KIND_DATA)
  260. outs() << "\t@ KIND_DATA\n";
  261. else
  262. outs() << "\t@ data in code kind = " << Kind << "\n";
  263. break;
  264. case MachO::DICE_KIND_JUMP_TABLE8:
  265. if (!NoShowRawInsn)
  266. dumpBytes(makeArrayRef(bytes, 1), outs());
  267. Value = bytes[0];
  268. outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
  269. Size = 1;
  270. break;
  271. case MachO::DICE_KIND_JUMP_TABLE16:
  272. if (!NoShowRawInsn)
  273. dumpBytes(makeArrayRef(bytes, 2), outs());
  274. Value = bytes[1] << 8 | bytes[0];
  275. outs() << "\t.short " << format("%5u", Value & 0xffff)
  276. << "\t@ KIND_JUMP_TABLE16\n";
  277. Size = 2;
  278. break;
  279. case MachO::DICE_KIND_JUMP_TABLE32:
  280. case MachO::DICE_KIND_ABS_JUMP_TABLE32:
  281. if (!NoShowRawInsn)
  282. dumpBytes(makeArrayRef(bytes, 4), outs());
  283. Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
  284. outs() << "\t.long " << Value;
  285. if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
  286. outs() << "\t@ KIND_JUMP_TABLE32\n";
  287. else
  288. outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
  289. Size = 4;
  290. break;
  291. }
  292. return Size;
  293. }
  294. static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
  295. std::vector<SectionRef> &Sections,
  296. std::vector<SymbolRef> &Symbols,
  297. SmallVectorImpl<uint64_t> &FoundFns,
  298. uint64_t &BaseSegmentAddress) {
  299. const StringRef FileName = MachOObj->getFileName();
  300. for (const SymbolRef &Symbol : MachOObj->symbols()) {
  301. StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
  302. if (!SymName.startswith("ltmp"))
  303. Symbols.push_back(Symbol);
  304. }
  305. for (const SectionRef &Section : MachOObj->sections()) {
  306. StringRef SectName;
  307. Section.getName(SectName);
  308. Sections.push_back(Section);
  309. }
  310. bool BaseSegmentAddressSet = false;
  311. for (const auto &Command : MachOObj->load_commands()) {
  312. if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
  313. // We found a function starts segment, parse the addresses for later
  314. // consumption.
  315. MachO::linkedit_data_command LLC =
  316. MachOObj->getLinkeditDataLoadCommand(Command);
  317. MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
  318. } else if (Command.C.cmd == MachO::LC_SEGMENT) {
  319. MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
  320. StringRef SegName = SLC.segname;
  321. if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
  322. BaseSegmentAddressSet = true;
  323. BaseSegmentAddress = SLC.vmaddr;
  324. }
  325. }
  326. }
  327. }
  328. static void printRelocationTargetName(const MachOObjectFile *O,
  329. const MachO::any_relocation_info &RE,
  330. raw_string_ostream &Fmt) {
  331. // Target of a scattered relocation is an address. In the interest of
  332. // generating pretty output, scan through the symbol table looking for a
  333. // symbol that aligns with that address. If we find one, print it.
  334. // Otherwise, we just print the hex address of the target.
  335. const StringRef FileName = O->getFileName();
  336. if (O->isRelocationScattered(RE)) {
  337. uint32_t Val = O->getPlainRelocationSymbolNum(RE);
  338. for (const SymbolRef &Symbol : O->symbols()) {
  339. uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
  340. if (Addr != Val)
  341. continue;
  342. Fmt << unwrapOrError(Symbol.getName(), FileName);
  343. return;
  344. }
  345. // If we couldn't find a symbol that this relocation refers to, try
  346. // to find a section beginning instead.
  347. for (const SectionRef &Section : ToolSectionFilter(*O)) {
  348. StringRef Name;
  349. uint64_t Addr = Section.getAddress();
  350. if (Addr != Val)
  351. continue;
  352. if (std::error_code EC = Section.getName(Name))
  353. report_error(errorCodeToError(EC), O->getFileName());
  354. Fmt << Name;
  355. return;
  356. }
  357. Fmt << format("0x%x", Val);
  358. return;
  359. }
  360. StringRef S;
  361. bool isExtern = O->getPlainRelocationExternal(RE);
  362. uint64_t Val = O->getPlainRelocationSymbolNum(RE);
  363. if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND) {
  364. Fmt << format("0x%0" PRIx64, Val);
  365. return;
  366. }
  367. if (isExtern) {
  368. symbol_iterator SI = O->symbol_begin();
  369. advance(SI, Val);
  370. S = unwrapOrError(SI->getName(), FileName);
  371. } else {
  372. section_iterator SI = O->section_begin();
  373. // Adjust for the fact that sections are 1-indexed.
  374. if (Val == 0) {
  375. Fmt << "0 (?,?)";
  376. return;
  377. }
  378. uint32_t I = Val - 1;
  379. while (I != 0 && SI != O->section_end()) {
  380. --I;
  381. advance(SI, 1);
  382. }
  383. if (SI == O->section_end())
  384. Fmt << Val << " (?,?)";
  385. else
  386. SI->getName(S);
  387. }
  388. Fmt << S;
  389. }
  390. Error getMachORelocationValueString(const MachOObjectFile *Obj,
  391. const RelocationRef &RelRef,
  392. SmallVectorImpl<char> &Result) {
  393. DataRefImpl Rel = RelRef.getRawDataRefImpl();
  394. MachO::any_relocation_info RE = Obj->getRelocation(Rel);
  395. unsigned Arch = Obj->getArch();
  396. std::string FmtBuf;
  397. raw_string_ostream Fmt(FmtBuf);
  398. unsigned Type = Obj->getAnyRelocationType(RE);
  399. bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
  400. // Determine any addends that should be displayed with the relocation.
  401. // These require decoding the relocation type, which is triple-specific.
  402. // X86_64 has entirely custom relocation types.
  403. if (Arch == Triple::x86_64) {
  404. switch (Type) {
  405. case MachO::X86_64_RELOC_GOT_LOAD:
  406. case MachO::X86_64_RELOC_GOT: {
  407. printRelocationTargetName(Obj, RE, Fmt);
  408. Fmt << "@GOT";
  409. if (IsPCRel)
  410. Fmt << "PCREL";
  411. break;
  412. }
  413. case MachO::X86_64_RELOC_SUBTRACTOR: {
  414. DataRefImpl RelNext = Rel;
  415. Obj->moveRelocationNext(RelNext);
  416. MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
  417. // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
  418. // X86_64_RELOC_UNSIGNED.
  419. // NOTE: Scattered relocations don't exist on x86_64.
  420. unsigned RType = Obj->getAnyRelocationType(RENext);
  421. if (RType != MachO::X86_64_RELOC_UNSIGNED)
  422. report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
  423. "X86_64_RELOC_SUBTRACTOR.");
  424. // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
  425. // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
  426. printRelocationTargetName(Obj, RENext, Fmt);
  427. Fmt << "-";
  428. printRelocationTargetName(Obj, RE, Fmt);
  429. break;
  430. }
  431. case MachO::X86_64_RELOC_TLV:
  432. printRelocationTargetName(Obj, RE, Fmt);
  433. Fmt << "@TLV";
  434. if (IsPCRel)
  435. Fmt << "P";
  436. break;
  437. case MachO::X86_64_RELOC_SIGNED_1:
  438. printRelocationTargetName(Obj, RE, Fmt);
  439. Fmt << "-1";
  440. break;
  441. case MachO::X86_64_RELOC_SIGNED_2:
  442. printRelocationTargetName(Obj, RE, Fmt);
  443. Fmt << "-2";
  444. break;
  445. case MachO::X86_64_RELOC_SIGNED_4:
  446. printRelocationTargetName(Obj, RE, Fmt);
  447. Fmt << "-4";
  448. break;
  449. default:
  450. printRelocationTargetName(Obj, RE, Fmt);
  451. break;
  452. }
  453. // X86 and ARM share some relocation types in common.
  454. } else if (Arch == Triple::x86 || Arch == Triple::arm ||
  455. Arch == Triple::ppc) {
  456. // Generic relocation types...
  457. switch (Type) {
  458. case MachO::GENERIC_RELOC_PAIR: // prints no info
  459. return Error::success();
  460. case MachO::GENERIC_RELOC_SECTDIFF: {
  461. DataRefImpl RelNext = Rel;
  462. Obj->moveRelocationNext(RelNext);
  463. MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
  464. // X86 sect diff's must be followed by a relocation of type
  465. // GENERIC_RELOC_PAIR.
  466. unsigned RType = Obj->getAnyRelocationType(RENext);
  467. if (RType != MachO::GENERIC_RELOC_PAIR)
  468. report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
  469. "GENERIC_RELOC_SECTDIFF.");
  470. printRelocationTargetName(Obj, RE, Fmt);
  471. Fmt << "-";
  472. printRelocationTargetName(Obj, RENext, Fmt);
  473. break;
  474. }
  475. }
  476. if (Arch == Triple::x86 || Arch == Triple::ppc) {
  477. switch (Type) {
  478. case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
  479. DataRefImpl RelNext = Rel;
  480. Obj->moveRelocationNext(RelNext);
  481. MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
  482. // X86 sect diff's must be followed by a relocation of type
  483. // GENERIC_RELOC_PAIR.
  484. unsigned RType = Obj->getAnyRelocationType(RENext);
  485. if (RType != MachO::GENERIC_RELOC_PAIR)
  486. report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
  487. "GENERIC_RELOC_LOCAL_SECTDIFF.");
  488. printRelocationTargetName(Obj, RE, Fmt);
  489. Fmt << "-";
  490. printRelocationTargetName(Obj, RENext, Fmt);
  491. break;
  492. }
  493. case MachO::GENERIC_RELOC_TLV: {
  494. printRelocationTargetName(Obj, RE, Fmt);
  495. Fmt << "@TLV";
  496. if (IsPCRel)
  497. Fmt << "P";
  498. break;
  499. }
  500. default:
  501. printRelocationTargetName(Obj, RE, Fmt);
  502. }
  503. } else { // ARM-specific relocations
  504. switch (Type) {
  505. case MachO::ARM_RELOC_HALF:
  506. case MachO::ARM_RELOC_HALF_SECTDIFF: {
  507. // Half relocations steal a bit from the length field to encode
  508. // whether this is an upper16 or a lower16 relocation.
  509. bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1;
  510. if (isUpper)
  511. Fmt << ":upper16:(";
  512. else
  513. Fmt << ":lower16:(";
  514. printRelocationTargetName(Obj, RE, Fmt);
  515. DataRefImpl RelNext = Rel;
  516. Obj->moveRelocationNext(RelNext);
  517. MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
  518. // ARM half relocs must be followed by a relocation of type
  519. // ARM_RELOC_PAIR.
  520. unsigned RType = Obj->getAnyRelocationType(RENext);
  521. if (RType != MachO::ARM_RELOC_PAIR)
  522. report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after "
  523. "ARM_RELOC_HALF");
  524. // NOTE: The half of the target virtual address is stashed in the
  525. // address field of the secondary relocation, but we can't reverse
  526. // engineer the constant offset from it without decoding the movw/movt
  527. // instruction to find the other half in its immediate field.
  528. // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
  529. // symbol/section pointer of the follow-on relocation.
  530. if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  531. Fmt << "-";
  532. printRelocationTargetName(Obj, RENext, Fmt);
  533. }
  534. Fmt << ")";
  535. break;
  536. }
  537. default: {
  538. printRelocationTargetName(Obj, RE, Fmt);
  539. }
  540. }
  541. }
  542. } else
  543. printRelocationTargetName(Obj, RE, Fmt);
  544. Fmt.flush();
  545. Result.append(FmtBuf.begin(), FmtBuf.end());
  546. return Error::success();
  547. }
  548. static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
  549. uint32_t n, uint32_t count,
  550. uint32_t stride, uint64_t addr) {
  551. MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
  552. uint32_t nindirectsyms = Dysymtab.nindirectsyms;
  553. if (n > nindirectsyms)
  554. outs() << " (entries start past the end of the indirect symbol "
  555. "table) (reserved1 field greater than the table size)";
  556. else if (n + count > nindirectsyms)
  557. outs() << " (entries extends past the end of the indirect symbol "
  558. "table)";
  559. outs() << "\n";
  560. uint32_t cputype = O->getHeader().cputype;
  561. if (cputype & MachO::CPU_ARCH_ABI64)
  562. outs() << "address index";
  563. else
  564. outs() << "address index";
  565. if (verbose)
  566. outs() << " name\n";
  567. else
  568. outs() << "\n";
  569. for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
  570. if (cputype & MachO::CPU_ARCH_ABI64)
  571. outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
  572. else
  573. outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
  574. MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
  575. uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
  576. if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
  577. outs() << "LOCAL\n";
  578. continue;
  579. }
  580. if (indirect_symbol ==
  581. (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
  582. outs() << "LOCAL ABSOLUTE\n";
  583. continue;
  584. }
  585. if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
  586. outs() << "ABSOLUTE\n";
  587. continue;
  588. }
  589. outs() << format("%5u ", indirect_symbol);
  590. if (verbose) {
  591. MachO::symtab_command Symtab = O->getSymtabLoadCommand();
  592. if (indirect_symbol < Symtab.nsyms) {
  593. symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
  594. SymbolRef Symbol = *Sym;
  595. outs() << unwrapOrError(Symbol.getName(), O->getFileName());
  596. } else {
  597. outs() << "?";
  598. }
  599. }
  600. outs() << "\n";
  601. }
  602. }
  603. static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
  604. for (const auto &Load : O->load_commands()) {
  605. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  606. MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
  607. for (unsigned J = 0; J < Seg.nsects; ++J) {
  608. MachO::section_64 Sec = O->getSection64(Load, J);
  609. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  610. if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  611. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  612. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  613. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  614. section_type == MachO::S_SYMBOL_STUBS) {
  615. uint32_t stride;
  616. if (section_type == MachO::S_SYMBOL_STUBS)
  617. stride = Sec.reserved2;
  618. else
  619. stride = 8;
  620. if (stride == 0) {
  621. outs() << "Can't print indirect symbols for (" << Sec.segname << ","
  622. << Sec.sectname << ") "
  623. << "(size of stubs in reserved2 field is zero)\n";
  624. continue;
  625. }
  626. uint32_t count = Sec.size / stride;
  627. outs() << "Indirect symbols for (" << Sec.segname << ","
  628. << Sec.sectname << ") " << count << " entries";
  629. uint32_t n = Sec.reserved1;
  630. PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
  631. }
  632. }
  633. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  634. MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
  635. for (unsigned J = 0; J < Seg.nsects; ++J) {
  636. MachO::section Sec = O->getSection(Load, J);
  637. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  638. if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  639. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  640. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  641. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  642. section_type == MachO::S_SYMBOL_STUBS) {
  643. uint32_t stride;
  644. if (section_type == MachO::S_SYMBOL_STUBS)
  645. stride = Sec.reserved2;
  646. else
  647. stride = 4;
  648. if (stride == 0) {
  649. outs() << "Can't print indirect symbols for (" << Sec.segname << ","
  650. << Sec.sectname << ") "
  651. << "(size of stubs in reserved2 field is zero)\n";
  652. continue;
  653. }
  654. uint32_t count = Sec.size / stride;
  655. outs() << "Indirect symbols for (" << Sec.segname << ","
  656. << Sec.sectname << ") " << count << " entries";
  657. uint32_t n = Sec.reserved1;
  658. PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
  659. }
  660. }
  661. }
  662. }
  663. }
  664. static void PrintRType(const uint64_t cputype, const unsigned r_type) {
  665. static char const *generic_r_types[] = {
  666. "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ",
  667. " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ",
  668. " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
  669. };
  670. static char const *x86_64_r_types[] = {
  671. "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ",
  672. "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ",
  673. " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
  674. };
  675. static char const *arm_r_types[] = {
  676. "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
  677. "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ",
  678. " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
  679. };
  680. static char const *arm64_r_types[] = {
  681. "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ",
  682. "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF",
  683. "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
  684. };
  685. if (r_type > 0xf){
  686. outs() << format("%-7u", r_type) << " ";
  687. return;
  688. }
  689. switch (cputype) {
  690. case MachO::CPU_TYPE_I386:
  691. outs() << generic_r_types[r_type];
  692. break;
  693. case MachO::CPU_TYPE_X86_64:
  694. outs() << x86_64_r_types[r_type];
  695. break;
  696. case MachO::CPU_TYPE_ARM:
  697. outs() << arm_r_types[r_type];
  698. break;
  699. case MachO::CPU_TYPE_ARM64:
  700. case MachO::CPU_TYPE_ARM64_32:
  701. outs() << arm64_r_types[r_type];
  702. break;
  703. default:
  704. outs() << format("%-7u ", r_type);
  705. }
  706. }
  707. static void PrintRLength(const uint64_t cputype, const unsigned r_type,
  708. const unsigned r_length, const bool previous_arm_half){
  709. if (cputype == MachO::CPU_TYPE_ARM &&
  710. (r_type == MachO::ARM_RELOC_HALF ||
  711. r_type == MachO::ARM_RELOC_HALF_SECTDIFF || previous_arm_half == true)) {
  712. if ((r_length & 0x1) == 0)
  713. outs() << "lo/";
  714. else
  715. outs() << "hi/";
  716. if ((r_length & 0x1) == 0)
  717. outs() << "arm ";
  718. else
  719. outs() << "thm ";
  720. } else {
  721. switch (r_length) {
  722. case 0:
  723. outs() << "byte ";
  724. break;
  725. case 1:
  726. outs() << "word ";
  727. break;
  728. case 2:
  729. outs() << "long ";
  730. break;
  731. case 3:
  732. if (cputype == MachO::CPU_TYPE_X86_64)
  733. outs() << "quad ";
  734. else
  735. outs() << format("?(%2d) ", r_length);
  736. break;
  737. default:
  738. outs() << format("?(%2d) ", r_length);
  739. }
  740. }
  741. }
  742. static void PrintRelocationEntries(const MachOObjectFile *O,
  743. const relocation_iterator Begin,
  744. const relocation_iterator End,
  745. const uint64_t cputype,
  746. const bool verbose) {
  747. const MachO::symtab_command Symtab = O->getSymtabLoadCommand();
  748. bool previous_arm_half = false;
  749. bool previous_sectdiff = false;
  750. uint32_t sectdiff_r_type = 0;
  751. for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) {
  752. const DataRefImpl Rel = Reloc->getRawDataRefImpl();
  753. const MachO::any_relocation_info RE = O->getRelocation(Rel);
  754. const unsigned r_type = O->getAnyRelocationType(RE);
  755. const bool r_scattered = O->isRelocationScattered(RE);
  756. const unsigned r_pcrel = O->getAnyRelocationPCRel(RE);
  757. const unsigned r_length = O->getAnyRelocationLength(RE);
  758. const unsigned r_address = O->getAnyRelocationAddress(RE);
  759. const bool r_extern = (r_scattered ? false :
  760. O->getPlainRelocationExternal(RE));
  761. const uint32_t r_value = (r_scattered ?
  762. O->getScatteredRelocationValue(RE) : 0);
  763. const unsigned r_symbolnum = (r_scattered ? 0 :
  764. O->getPlainRelocationSymbolNum(RE));
  765. if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) {
  766. if (verbose) {
  767. // scattered: address
  768. if ((cputype == MachO::CPU_TYPE_I386 &&
  769. r_type == MachO::GENERIC_RELOC_PAIR) ||
  770. (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR))
  771. outs() << " ";
  772. else
  773. outs() << format("%08x ", (unsigned int)r_address);
  774. // scattered: pcrel
  775. if (r_pcrel)
  776. outs() << "True ";
  777. else
  778. outs() << "False ";
  779. // scattered: length
  780. PrintRLength(cputype, r_type, r_length, previous_arm_half);
  781. // scattered: extern & type
  782. outs() << "n/a ";
  783. PrintRType(cputype, r_type);
  784. // scattered: scattered & value
  785. outs() << format("True 0x%08x", (unsigned int)r_value);
  786. if (previous_sectdiff == false) {
  787. if ((cputype == MachO::CPU_TYPE_ARM &&
  788. r_type == MachO::ARM_RELOC_PAIR))
  789. outs() << format(" half = 0x%04x ", (unsigned int)r_address);
  790. } else if (cputype == MachO::CPU_TYPE_ARM &&
  791. sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF)
  792. outs() << format(" other_half = 0x%04x ", (unsigned int)r_address);
  793. if ((cputype == MachO::CPU_TYPE_I386 &&
  794. (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
  795. r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) ||
  796. (cputype == MachO::CPU_TYPE_ARM &&
  797. (sectdiff_r_type == MachO::ARM_RELOC_SECTDIFF ||
  798. sectdiff_r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
  799. sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF))) {
  800. previous_sectdiff = true;
  801. sectdiff_r_type = r_type;
  802. } else {
  803. previous_sectdiff = false;
  804. sectdiff_r_type = 0;
  805. }
  806. if (cputype == MachO::CPU_TYPE_ARM &&
  807. (r_type == MachO::ARM_RELOC_HALF ||
  808. r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
  809. previous_arm_half = true;
  810. else
  811. previous_arm_half = false;
  812. outs() << "\n";
  813. }
  814. else {
  815. // scattered: address pcrel length extern type scattered value
  816. outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n",
  817. (unsigned int)r_address, r_pcrel, r_length, r_type,
  818. (unsigned int)r_value);
  819. }
  820. }
  821. else {
  822. if (verbose) {
  823. // plain: address
  824. if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
  825. outs() << " ";
  826. else
  827. outs() << format("%08x ", (unsigned int)r_address);
  828. // plain: pcrel
  829. if (r_pcrel)
  830. outs() << "True ";
  831. else
  832. outs() << "False ";
  833. // plain: length
  834. PrintRLength(cputype, r_type, r_length, previous_arm_half);
  835. if (r_extern) {
  836. // plain: extern & type & scattered
  837. outs() << "True ";
  838. PrintRType(cputype, r_type);
  839. outs() << "False ";
  840. // plain: symbolnum/value
  841. if (r_symbolnum > Symtab.nsyms)
  842. outs() << format("?(%d)\n", r_symbolnum);
  843. else {
  844. SymbolRef Symbol = *O->getSymbolByIndex(r_symbolnum);
  845. Expected<StringRef> SymNameNext = Symbol.getName();
  846. const char *name = NULL;
  847. if (SymNameNext)
  848. name = SymNameNext->data();
  849. if (name == NULL)
  850. outs() << format("?(%d)\n", r_symbolnum);
  851. else
  852. outs() << name << "\n";
  853. }
  854. }
  855. else {
  856. // plain: extern & type & scattered
  857. outs() << "False ";
  858. PrintRType(cputype, r_type);
  859. outs() << "False ";
  860. // plain: symbolnum/value
  861. if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
  862. outs() << format("other_half = 0x%04x\n", (unsigned int)r_address);
  863. else if ((cputype == MachO::CPU_TYPE_ARM64 ||
  864. cputype == MachO::CPU_TYPE_ARM64_32) &&
  865. r_type == MachO::ARM64_RELOC_ADDEND)
  866. outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum);
  867. else {
  868. outs() << format("%d ", r_symbolnum);
  869. if (r_symbolnum == MachO::R_ABS)
  870. outs() << "R_ABS\n";
  871. else {
  872. // in this case, r_symbolnum is actually a 1-based section number
  873. uint32_t nsects = O->section_end()->getRawDataRefImpl().d.a;
  874. if (r_symbolnum > 0 && r_symbolnum <= nsects) {
  875. object::DataRefImpl DRI;
  876. DRI.d.a = r_symbolnum-1;
  877. StringRef SegName = O->getSectionFinalSegmentName(DRI);
  878. if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
  879. outs() << "(" << SegName << "," << *NameOrErr << ")\n";
  880. else
  881. outs() << "(?,?)\n";
  882. }
  883. else {
  884. outs() << "(?,?)\n";
  885. }
  886. }
  887. }
  888. }
  889. if (cputype == MachO::CPU_TYPE_ARM &&
  890. (r_type == MachO::ARM_RELOC_HALF ||
  891. r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
  892. previous_arm_half = true;
  893. else
  894. previous_arm_half = false;
  895. }
  896. else {
  897. // plain: address pcrel length extern type scattered symbolnum/section
  898. outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n",
  899. (unsigned int)r_address, r_pcrel, r_length, r_extern,
  900. r_type, r_symbolnum);
  901. }
  902. }
  903. }
  904. }
  905. static void PrintRelocations(const MachOObjectFile *O, const bool verbose) {
  906. const uint64_t cputype = O->getHeader().cputype;
  907. const MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
  908. if (Dysymtab.nextrel != 0) {
  909. outs() << "External relocation information " << Dysymtab.nextrel
  910. << " entries";
  911. outs() << "\naddress pcrel length extern type scattered "
  912. "symbolnum/value\n";
  913. PrintRelocationEntries(O, O->extrel_begin(), O->extrel_end(), cputype,
  914. verbose);
  915. }
  916. if (Dysymtab.nlocrel != 0) {
  917. outs() << format("Local relocation information %u entries",
  918. Dysymtab.nlocrel);
  919. outs() << "\naddress pcrel length extern type scattered "
  920. "symbolnum/value\n";
  921. PrintRelocationEntries(O, O->locrel_begin(), O->locrel_end(), cputype,
  922. verbose);
  923. }
  924. for (const auto &Load : O->load_commands()) {
  925. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  926. const MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
  927. for (unsigned J = 0; J < Seg.nsects; ++J) {
  928. const MachO::section_64 Sec = O->getSection64(Load, J);
  929. if (Sec.nreloc != 0) {
  930. DataRefImpl DRI;
  931. DRI.d.a = J;
  932. const StringRef SegName = O->getSectionFinalSegmentName(DRI);
  933. if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
  934. outs() << "Relocation information (" << SegName << "," << *NameOrErr
  935. << format(") %u entries", Sec.nreloc);
  936. else
  937. outs() << "Relocation information (" << SegName << ",?) "
  938. << format("%u entries", Sec.nreloc);
  939. outs() << "\naddress pcrel length extern type scattered "
  940. "symbolnum/value\n";
  941. PrintRelocationEntries(O, O->section_rel_begin(DRI),
  942. O->section_rel_end(DRI), cputype, verbose);
  943. }
  944. }
  945. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  946. const MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
  947. for (unsigned J = 0; J < Seg.nsects; ++J) {
  948. const MachO::section Sec = O->getSection(Load, J);
  949. if (Sec.nreloc != 0) {
  950. DataRefImpl DRI;
  951. DRI.d.a = J;
  952. const StringRef SegName = O->getSectionFinalSegmentName(DRI);
  953. if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
  954. outs() << "Relocation information (" << SegName << "," << *NameOrErr
  955. << format(") %u entries", Sec.nreloc);
  956. else
  957. outs() << "Relocation information (" << SegName << ",?) "
  958. << format("%u entries", Sec.nreloc);
  959. outs() << "\naddress pcrel length extern type scattered "
  960. "symbolnum/value\n";
  961. PrintRelocationEntries(O, O->section_rel_begin(DRI),
  962. O->section_rel_end(DRI), cputype, verbose);
  963. }
  964. }
  965. }
  966. }
  967. }
  968. static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
  969. MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
  970. uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
  971. outs() << "Data in code table (" << nentries << " entries)\n";
  972. outs() << "offset length kind\n";
  973. for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
  974. ++DI) {
  975. uint32_t Offset;
  976. DI->getOffset(Offset);
  977. outs() << format("0x%08" PRIx32, Offset) << " ";
  978. uint16_t Length;
  979. DI->getLength(Length);
  980. outs() << format("%6u", Length) << " ";
  981. uint16_t Kind;
  982. DI->getKind(Kind);
  983. if (verbose) {
  984. switch (Kind) {
  985. case MachO::DICE_KIND_DATA:
  986. outs() << "DATA";
  987. break;
  988. case MachO::DICE_KIND_JUMP_TABLE8:
  989. outs() << "JUMP_TABLE8";
  990. break;
  991. case MachO::DICE_KIND_JUMP_TABLE16:
  992. outs() << "JUMP_TABLE16";
  993. break;
  994. case MachO::DICE_KIND_JUMP_TABLE32:
  995. outs() << "JUMP_TABLE32";
  996. break;
  997. case MachO::DICE_KIND_ABS_JUMP_TABLE32:
  998. outs() << "ABS_JUMP_TABLE32";
  999. break;
  1000. default:
  1001. outs() << format("0x%04" PRIx32, Kind);
  1002. break;
  1003. }
  1004. } else
  1005. outs() << format("0x%04" PRIx32, Kind);
  1006. outs() << "\n";
  1007. }
  1008. }
  1009. static void PrintLinkOptHints(MachOObjectFile *O) {
  1010. MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
  1011. const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
  1012. uint32_t nloh = LohLC.datasize;
  1013. outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
  1014. for (uint32_t i = 0; i < nloh;) {
  1015. unsigned n;
  1016. uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
  1017. i += n;
  1018. outs() << " identifier " << identifier << " ";
  1019. if (i >= nloh)
  1020. return;
  1021. switch (identifier) {
  1022. case 1:
  1023. outs() << "AdrpAdrp\n";
  1024. break;
  1025. case 2:
  1026. outs() << "AdrpLdr\n";
  1027. break;
  1028. case 3:
  1029. outs() << "AdrpAddLdr\n";
  1030. break;
  1031. case 4:
  1032. outs() << "AdrpLdrGotLdr\n";
  1033. break;
  1034. case 5:
  1035. outs() << "AdrpAddStr\n";
  1036. break;
  1037. case 6:
  1038. outs() << "AdrpLdrGotStr\n";
  1039. break;
  1040. case 7:
  1041. outs() << "AdrpAdd\n";
  1042. break;
  1043. case 8:
  1044. outs() << "AdrpLdrGot\n";
  1045. break;
  1046. default:
  1047. outs() << "Unknown identifier value\n";
  1048. break;
  1049. }
  1050. uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
  1051. i += n;
  1052. outs() << " narguments " << narguments << "\n";
  1053. if (i >= nloh)
  1054. return;
  1055. for (uint32_t j = 0; j < narguments; j++) {
  1056. uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
  1057. i += n;
  1058. outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
  1059. if (i >= nloh)
  1060. return;
  1061. }
  1062. }
  1063. }
  1064. static void PrintDylibs(MachOObjectFile *O, bool JustId) {
  1065. unsigned Index = 0;
  1066. for (const auto &Load : O->load_commands()) {
  1067. if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
  1068. (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
  1069. Load.C.cmd == MachO::LC_LOAD_DYLIB ||
  1070. Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
  1071. Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
  1072. Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
  1073. Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
  1074. MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
  1075. if (dl.dylib.name < dl.cmdsize) {
  1076. const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
  1077. if (JustId)
  1078. outs() << p << "\n";
  1079. else {
  1080. outs() << "\t" << p;
  1081. outs() << " (compatibility version "
  1082. << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
  1083. << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
  1084. << (dl.dylib.compatibility_version & 0xff) << ",";
  1085. outs() << " current version "
  1086. << ((dl.dylib.current_version >> 16) & 0xffff) << "."
  1087. << ((dl.dylib.current_version >> 8) & 0xff) << "."
  1088. << (dl.dylib.current_version & 0xff) << ")\n";
  1089. }
  1090. } else {
  1091. outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
  1092. if (Load.C.cmd == MachO::LC_ID_DYLIB)
  1093. outs() << "LC_ID_DYLIB ";
  1094. else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
  1095. outs() << "LC_LOAD_DYLIB ";
  1096. else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
  1097. outs() << "LC_LOAD_WEAK_DYLIB ";
  1098. else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
  1099. outs() << "LC_LAZY_LOAD_DYLIB ";
  1100. else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
  1101. outs() << "LC_REEXPORT_DYLIB ";
  1102. else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
  1103. outs() << "LC_LOAD_UPWARD_DYLIB ";
  1104. else
  1105. outs() << "LC_??? ";
  1106. outs() << "command " << Index++ << "\n";
  1107. }
  1108. }
  1109. }
  1110. }
  1111. typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
  1112. static void CreateSymbolAddressMap(MachOObjectFile *O,
  1113. SymbolAddressMap *AddrMap) {
  1114. // Create a map of symbol addresses to symbol names.
  1115. const StringRef FileName = O->getFileName();
  1116. for (const SymbolRef &Symbol : O->symbols()) {
  1117. SymbolRef::Type ST = unwrapOrError(Symbol.getType(), FileName);
  1118. if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
  1119. ST == SymbolRef::ST_Other) {
  1120. uint64_t Address = Symbol.getValue();
  1121. StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
  1122. if (!SymName.startswith(".objc"))
  1123. (*AddrMap)[Address] = SymName;
  1124. }
  1125. }
  1126. }
  1127. // GuessSymbolName is passed the address of what might be a symbol and a
  1128. // pointer to the SymbolAddressMap. It returns the name of a symbol
  1129. // with that address or nullptr if no symbol is found with that address.
  1130. static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
  1131. const char *SymbolName = nullptr;
  1132. // A DenseMap can't lookup up some values.
  1133. if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
  1134. StringRef name = AddrMap->lookup(value);
  1135. if (!name.empty())
  1136. SymbolName = name.data();
  1137. }
  1138. return SymbolName;
  1139. }
  1140. static void DumpCstringChar(const char c) {
  1141. char p[2];
  1142. p[0] = c;
  1143. p[1] = '\0';
  1144. outs().write_escaped(p);
  1145. }
  1146. static void DumpCstringSection(MachOObjectFile *O, const char *sect,
  1147. uint32_t sect_size, uint64_t sect_addr,
  1148. bool print_addresses) {
  1149. for (uint32_t i = 0; i < sect_size; i++) {
  1150. if (print_addresses) {
  1151. if (O->is64Bit())
  1152. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  1153. else
  1154. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  1155. }
  1156. for (; i < sect_size && sect[i] != '\0'; i++)
  1157. DumpCstringChar(sect[i]);
  1158. if (i < sect_size && sect[i] == '\0')
  1159. outs() << "\n";
  1160. }
  1161. }
  1162. static void DumpLiteral4(uint32_t l, float f) {
  1163. outs() << format("0x%08" PRIx32, l);
  1164. if ((l & 0x7f800000) != 0x7f800000)
  1165. outs() << format(" (%.16e)\n", f);
  1166. else {
  1167. if (l == 0x7f800000)
  1168. outs() << " (+Infinity)\n";
  1169. else if (l == 0xff800000)
  1170. outs() << " (-Infinity)\n";
  1171. else if ((l & 0x00400000) == 0x00400000)
  1172. outs() << " (non-signaling Not-a-Number)\n";
  1173. else
  1174. outs() << " (signaling Not-a-Number)\n";
  1175. }
  1176. }
  1177. static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
  1178. uint32_t sect_size, uint64_t sect_addr,
  1179. bool print_addresses) {
  1180. for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
  1181. if (print_addresses) {
  1182. if (O->is64Bit())
  1183. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  1184. else
  1185. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  1186. }
  1187. float f;
  1188. memcpy(&f, sect + i, sizeof(float));
  1189. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  1190. sys::swapByteOrder(f);
  1191. uint32_t l;
  1192. memcpy(&l, sect + i, sizeof(uint32_t));
  1193. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  1194. sys::swapByteOrder(l);
  1195. DumpLiteral4(l, f);
  1196. }
  1197. }
  1198. static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
  1199. double d) {
  1200. outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
  1201. uint32_t Hi, Lo;
  1202. Hi = (O->isLittleEndian()) ? l1 : l0;
  1203. Lo = (O->isLittleEndian()) ? l0 : l1;
  1204. // Hi is the high word, so this is equivalent to if(isfinite(d))
  1205. if ((Hi & 0x7ff00000) != 0x7ff00000)
  1206. outs() << format(" (%.16e)\n", d);
  1207. else {
  1208. if (Hi == 0x7ff00000 && Lo == 0)
  1209. outs() << " (+Infinity)\n";
  1210. else if (Hi == 0xfff00000 && Lo == 0)
  1211. outs() << " (-Infinity)\n";
  1212. else if ((Hi & 0x00080000) == 0x00080000)
  1213. outs() << " (non-signaling Not-a-Number)\n";
  1214. else
  1215. outs() << " (signaling Not-a-Number)\n";
  1216. }
  1217. }
  1218. static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
  1219. uint32_t sect_size, uint64_t sect_addr,
  1220. bool print_addresses) {
  1221. for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
  1222. if (print_addresses) {
  1223. if (O->is64Bit())
  1224. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  1225. else
  1226. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  1227. }
  1228. double d;
  1229. memcpy(&d, sect + i, sizeof(double));
  1230. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  1231. sys::swapByteOrder(d);
  1232. uint32_t l0, l1;
  1233. memcpy(&l0, sect + i, sizeof(uint32_t));
  1234. memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
  1235. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  1236. sys::swapByteOrder(l0);
  1237. sys::swapByteOrder(l1);
  1238. }
  1239. DumpLiteral8(O, l0, l1, d);
  1240. }
  1241. }
  1242. static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
  1243. outs() << format("0x%08" PRIx32, l0) << " ";
  1244. outs() << format("0x%08" PRIx32, l1) << " ";
  1245. outs() << format("0x%08" PRIx32, l2) << " ";
  1246. outs() << format("0x%08" PRIx32, l3) << "\n";
  1247. }
  1248. static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
  1249. uint32_t sect_size, uint64_t sect_addr,
  1250. bool print_addresses) {
  1251. for (uint32_t i = 0; i < sect_size; i += 16) {
  1252. if (print_addresses) {
  1253. if (O->is64Bit())
  1254. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  1255. else
  1256. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  1257. }
  1258. uint32_t l0, l1, l2, l3;
  1259. memcpy(&l0, sect + i, sizeof(uint32_t));
  1260. memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
  1261. memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
  1262. memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
  1263. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  1264. sys::swapByteOrder(l0);
  1265. sys::swapByteOrder(l1);
  1266. sys::swapByteOrder(l2);
  1267. sys::swapByteOrder(l3);
  1268. }
  1269. DumpLiteral16(l0, l1, l2, l3);
  1270. }
  1271. }
  1272. static void DumpLiteralPointerSection(MachOObjectFile *O,
  1273. const SectionRef &Section,
  1274. const char *sect, uint32_t sect_size,
  1275. uint64_t sect_addr,
  1276. bool print_addresses) {
  1277. // Collect the literal sections in this Mach-O file.
  1278. std::vector<SectionRef> LiteralSections;
  1279. for (const SectionRef &Section : O->sections()) {
  1280. DataRefImpl Ref = Section.getRawDataRefImpl();
  1281. uint32_t section_type;
  1282. if (O->is64Bit()) {
  1283. const MachO::section_64 Sec = O->getSection64(Ref);
  1284. section_type = Sec.flags & MachO::SECTION_TYPE;
  1285. } else {
  1286. const MachO::section Sec = O->getSection(Ref);
  1287. section_type = Sec.flags & MachO::SECTION_TYPE;
  1288. }
  1289. if (section_type == MachO::S_CSTRING_LITERALS ||
  1290. section_type == MachO::S_4BYTE_LITERALS ||
  1291. section_type == MachO::S_8BYTE_LITERALS ||
  1292. section_type == MachO::S_16BYTE_LITERALS)
  1293. LiteralSections.push_back(Section);
  1294. }
  1295. // Set the size of the literal pointer.
  1296. uint32_t lp_size = O->is64Bit() ? 8 : 4;
  1297. // Collect the external relocation symbols for the literal pointers.
  1298. std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
  1299. for (const RelocationRef &Reloc : Section.relocations()) {
  1300. DataRefImpl Rel;
  1301. MachO::any_relocation_info RE;
  1302. bool isExtern = false;
  1303. Rel = Reloc.getRawDataRefImpl();
  1304. RE = O->getRelocation(Rel);
  1305. isExtern = O->getPlainRelocationExternal(RE);
  1306. if (isExtern) {
  1307. uint64_t RelocOffset = Reloc.getOffset();
  1308. symbol_iterator RelocSym = Reloc.getSymbol();
  1309. Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
  1310. }
  1311. }
  1312. array_pod_sort(Relocs.begin(), Relocs.end());
  1313. // Dump each literal pointer.
  1314. for (uint32_t i = 0; i < sect_size; i += lp_size) {
  1315. if (print_addresses) {
  1316. if (O->is64Bit())
  1317. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  1318. else
  1319. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  1320. }
  1321. uint64_t lp;
  1322. if (O->is64Bit()) {
  1323. memcpy(&lp, sect + i, sizeof(uint64_t));
  1324. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  1325. sys::swapByteOrder(lp);
  1326. } else {
  1327. uint32_t li;
  1328. memcpy(&li, sect + i, sizeof(uint32_t));
  1329. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  1330. sys::swapByteOrder(li);
  1331. lp = li;
  1332. }
  1333. // First look for an external relocation entry for this literal pointer.
  1334. auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
  1335. return P.first == i;
  1336. });
  1337. if (Reloc != Relocs.end()) {
  1338. symbol_iterator RelocSym = Reloc->second;
  1339. StringRef SymName = unwrapOrError(RelocSym->getName(), O->getFileName());
  1340. outs() << "external relocation entry for symbol:" << SymName << "\n";
  1341. continue;
  1342. }
  1343. // For local references see what the section the literal pointer points to.
  1344. auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
  1345. return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
  1346. });
  1347. if (Sect == LiteralSections.end()) {
  1348. outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
  1349. continue;
  1350. }
  1351. uint64_t SectAddress = Sect->getAddress();
  1352. uint64_t SectSize = Sect->getSize();
  1353. StringRef SectName;
  1354. Sect->getName(SectName);
  1355. DataRefImpl Ref = Sect->getRawDataRefImpl();
  1356. StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
  1357. outs() << SegmentName << ":" << SectName << ":";
  1358. uint32_t section_type;
  1359. if (O->is64Bit()) {
  1360. const MachO::section_64 Sec = O->getSection64(Ref);
  1361. section_type = Sec.flags & MachO::SECTION_TYPE;
  1362. } else {
  1363. const MachO::section Sec = O->getSection(Ref);
  1364. section_type = Sec.flags & MachO::SECTION_TYPE;
  1365. }
  1366. StringRef BytesStr = unwrapOrError(Sect->getContents(), O->getFileName());
  1367. const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
  1368. switch (section_type) {
  1369. case MachO::S_CSTRING_LITERALS:
  1370. for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
  1371. i++) {
  1372. DumpCstringChar(Contents[i]);
  1373. }
  1374. outs() << "\n";
  1375. break;
  1376. case MachO::S_4BYTE_LITERALS:
  1377. float f;
  1378. memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
  1379. uint32_t l;
  1380. memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
  1381. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  1382. sys::swapByteOrder(f);
  1383. sys::swapByteOrder(l);
  1384. }
  1385. DumpLiteral4(l, f);
  1386. break;
  1387. case MachO::S_8BYTE_LITERALS: {
  1388. double d;
  1389. memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
  1390. uint32_t l0, l1;
  1391. memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
  1392. memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
  1393. sizeof(uint32_t));
  1394. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  1395. sys::swapByteOrder(f);
  1396. sys::swapByteOrder(l0);
  1397. sys::swapByteOrder(l1);
  1398. }
  1399. DumpLiteral8(O, l0, l1, d);
  1400. break;
  1401. }
  1402. case MachO::S_16BYTE_LITERALS: {
  1403. uint32_t l0, l1, l2, l3;
  1404. memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
  1405. memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
  1406. sizeof(uint32_t));
  1407. memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
  1408. sizeof(uint32_t));
  1409. memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
  1410. sizeof(uint32_t));
  1411. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  1412. sys::swapByteOrder(l0);
  1413. sys::swapByteOrder(l1);
  1414. sys::swapByteOrder(l2);
  1415. sys::swapByteOrder(l3);
  1416. }
  1417. DumpLiteral16(l0, l1, l2, l3);
  1418. break;
  1419. }
  1420. }
  1421. }
  1422. }
  1423. static void DumpInitTermPointerSection(MachOObjectFile *O,
  1424. const SectionRef &Section,
  1425. const char *sect,
  1426. uint32_t sect_size, uint64_t sect_addr,
  1427. SymbolAddressMap *AddrMap,
  1428. bool verbose) {
  1429. uint32_t stride;
  1430. stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
  1431. // Collect the external relocation symbols for the pointers.
  1432. std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
  1433. for (const RelocationRef &Reloc : Section.relocations()) {
  1434. DataRefImpl Rel;
  1435. MachO::any_relocation_info RE;
  1436. bool isExtern = false;
  1437. Rel = Reloc.getRawDataRefImpl();
  1438. RE = O->getRelocation(Rel);
  1439. isExtern = O->getPlainRelocationExternal(RE);
  1440. if (isExtern) {
  1441. uint64_t RelocOffset = Reloc.getOffset();
  1442. symbol_iterator RelocSym = Reloc.getSymbol();
  1443. Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
  1444. }
  1445. }
  1446. array_pod_sort(Relocs.begin(), Relocs.end());
  1447. for (uint32_t i = 0; i < sect_size; i += stride) {
  1448. const char *SymbolName = nullptr;
  1449. uint64_t p;
  1450. if (O->is64Bit()) {
  1451. outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
  1452. uint64_t pointer_value;
  1453. memcpy(&pointer_value, sect + i, stride);
  1454. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  1455. sys::swapByteOrder(pointer_value);
  1456. outs() << format("0x%016" PRIx64, pointer_value);
  1457. p = pointer_value;
  1458. } else {
  1459. outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
  1460. uint32_t pointer_value;
  1461. memcpy(&pointer_value, sect + i, stride);
  1462. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  1463. sys::swapByteOrder(pointer_value);
  1464. outs() << format("0x%08" PRIx32, pointer_value);
  1465. p = pointer_value;
  1466. }
  1467. if (verbose) {
  1468. // First look for an external relocation entry for this pointer.
  1469. auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
  1470. return P.first == i;
  1471. });
  1472. if (Reloc != Relocs.end()) {
  1473. symbol_iterator RelocSym = Reloc->second;
  1474. outs() << " " << unwrapOrError(RelocSym->getName(), O->getFileName());
  1475. } else {
  1476. SymbolName = GuessSymbolName(p, AddrMap);
  1477. if (SymbolName)
  1478. outs() << " " << SymbolName;
  1479. }
  1480. }
  1481. outs() << "\n";
  1482. }
  1483. }
  1484. static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
  1485. uint32_t size, uint64_t addr) {
  1486. uint32_t cputype = O->getHeader().cputype;
  1487. if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
  1488. uint32_t j;
  1489. for (uint32_t i = 0; i < size; i += j, addr += j) {
  1490. if (O->is64Bit())
  1491. outs() << format("%016" PRIx64, addr) << "\t";
  1492. else
  1493. outs() << format("%08" PRIx64, addr) << "\t";
  1494. for (j = 0; j < 16 && i + j < size; j++) {
  1495. uint8_t byte_word = *(sect + i + j);
  1496. outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
  1497. }
  1498. outs() << "\n";
  1499. }
  1500. } else {
  1501. uint32_t j;
  1502. for (uint32_t i = 0; i < size; i += j, addr += j) {
  1503. if (O->is64Bit())
  1504. outs() << format("%016" PRIx64, addr) << "\t";
  1505. else
  1506. outs() << format("%08" PRIx64, addr) << "\t";
  1507. for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
  1508. j += sizeof(int32_t)) {
  1509. if (i + j + sizeof(int32_t) <= size) {
  1510. uint32_t long_word;
  1511. memcpy(&long_word, sect + i + j, sizeof(int32_t));
  1512. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  1513. sys::swapByteOrder(long_word);
  1514. outs() << format("%08" PRIx32, long_word) << " ";
  1515. } else {
  1516. for (uint32_t k = 0; i + j + k < size; k++) {
  1517. uint8_t byte_word = *(sect + i + j + k);
  1518. outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
  1519. }
  1520. }
  1521. }
  1522. outs() << "\n";
  1523. }
  1524. }
  1525. }
  1526. static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
  1527. StringRef DisSegName, StringRef DisSectName);
  1528. static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
  1529. uint32_t size, uint32_t addr);
  1530. #ifdef HAVE_LIBXAR
  1531. static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
  1532. uint32_t size, bool verbose,
  1533. bool PrintXarHeader, bool PrintXarFileHeaders,
  1534. std::string XarMemberName);
  1535. #endif // defined(HAVE_LIBXAR)
  1536. static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
  1537. bool verbose) {
  1538. SymbolAddressMap AddrMap;
  1539. if (verbose)
  1540. CreateSymbolAddressMap(O, &AddrMap);
  1541. for (unsigned i = 0; i < FilterSections.size(); ++i) {
  1542. StringRef DumpSection = FilterSections[i];
  1543. std::pair<StringRef, StringRef> DumpSegSectName;
  1544. DumpSegSectName = DumpSection.split(',');
  1545. StringRef DumpSegName, DumpSectName;
  1546. if (!DumpSegSectName.second.empty()) {
  1547. DumpSegName = DumpSegSectName.first;
  1548. DumpSectName = DumpSegSectName.second;
  1549. } else {
  1550. DumpSegName = "";
  1551. DumpSectName = DumpSegSectName.first;
  1552. }
  1553. for (const SectionRef &Section : O->sections()) {
  1554. StringRef SectName;
  1555. Section.getName(SectName);
  1556. DataRefImpl Ref = Section.getRawDataRefImpl();
  1557. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  1558. if ((DumpSegName.empty() || SegName == DumpSegName) &&
  1559. (SectName == DumpSectName)) {
  1560. uint32_t section_flags;
  1561. if (O->is64Bit()) {
  1562. const MachO::section_64 Sec = O->getSection64(Ref);
  1563. section_flags = Sec.flags;
  1564. } else {
  1565. const MachO::section Sec = O->getSection(Ref);
  1566. section_flags = Sec.flags;
  1567. }
  1568. uint32_t section_type = section_flags & MachO::SECTION_TYPE;
  1569. StringRef BytesStr =
  1570. unwrapOrError(Section.getContents(), O->getFileName());
  1571. const char *sect = reinterpret_cast<const char *>(BytesStr.data());
  1572. uint32_t sect_size = BytesStr.size();
  1573. uint64_t sect_addr = Section.getAddress();
  1574. outs() << "Contents of (" << SegName << "," << SectName
  1575. << ") section\n";
  1576. if (verbose) {
  1577. if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
  1578. (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
  1579. DisassembleMachO(Filename, O, SegName, SectName);
  1580. continue;
  1581. }
  1582. if (SegName == "__TEXT" && SectName == "__info_plist") {
  1583. outs() << sect;
  1584. continue;
  1585. }
  1586. if (SegName == "__OBJC" && SectName == "__protocol") {
  1587. DumpProtocolSection(O, sect, sect_size, sect_addr);
  1588. continue;
  1589. }
  1590. #ifdef HAVE_LIBXAR
  1591. if (SegName == "__LLVM" && SectName == "__bundle") {
  1592. DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
  1593. ArchiveHeaders, "");
  1594. continue;
  1595. }
  1596. #endif // defined(HAVE_LIBXAR)
  1597. switch (section_type) {
  1598. case MachO::S_REGULAR:
  1599. DumpRawSectionContents(O, sect, sect_size, sect_addr);
  1600. break;
  1601. case MachO::S_ZEROFILL:
  1602. outs() << "zerofill section and has no contents in the file\n";
  1603. break;
  1604. case MachO::S_CSTRING_LITERALS:
  1605. DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1606. break;
  1607. case MachO::S_4BYTE_LITERALS:
  1608. DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1609. break;
  1610. case MachO::S_8BYTE_LITERALS:
  1611. DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1612. break;
  1613. case MachO::S_16BYTE_LITERALS:
  1614. DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1615. break;
  1616. case MachO::S_LITERAL_POINTERS:
  1617. DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
  1618. !NoLeadingAddr);
  1619. break;
  1620. case MachO::S_MOD_INIT_FUNC_POINTERS:
  1621. case MachO::S_MOD_TERM_FUNC_POINTERS:
  1622. DumpInitTermPointerSection(O, Section, sect, sect_size, sect_addr,
  1623. &AddrMap, verbose);
  1624. break;
  1625. default:
  1626. outs() << "Unknown section type ("
  1627. << format("0x%08" PRIx32, section_type) << ")\n";
  1628. DumpRawSectionContents(O, sect, sect_size, sect_addr);
  1629. break;
  1630. }
  1631. } else {
  1632. if (section_type == MachO::S_ZEROFILL)
  1633. outs() << "zerofill section and has no contents in the file\n";
  1634. else
  1635. DumpRawSectionContents(O, sect, sect_size, sect_addr);
  1636. }
  1637. }
  1638. }
  1639. }
  1640. }
  1641. static void DumpInfoPlistSectionContents(StringRef Filename,
  1642. MachOObjectFile *O) {
  1643. for (const SectionRef &Section : O->sections()) {
  1644. StringRef SectName;
  1645. Section.getName(SectName);
  1646. DataRefImpl Ref = Section.getRawDataRefImpl();
  1647. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  1648. if (SegName == "__TEXT" && SectName == "__info_plist") {
  1649. if (!NoLeadingHeaders)
  1650. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  1651. StringRef BytesStr =
  1652. unwrapOrError(Section.getContents(), O->getFileName());
  1653. const char *sect = reinterpret_cast<const char *>(BytesStr.data());
  1654. outs() << format("%.*s", BytesStr.size(), sect) << "\n";
  1655. return;
  1656. }
  1657. }
  1658. }
  1659. // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
  1660. // and if it is and there is a list of architecture flags is specified then
  1661. // check to make sure this Mach-O file is one of those architectures or all
  1662. // architectures were specified. If not then an error is generated and this
  1663. // routine returns false. Else it returns true.
  1664. static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
  1665. auto *MachO = dyn_cast<MachOObjectFile>(O);
  1666. if (!MachO || ArchAll || ArchFlags.empty())
  1667. return true;
  1668. MachO::mach_header H;
  1669. MachO::mach_header_64 H_64;
  1670. Triple T;
  1671. const char *McpuDefault, *ArchFlag;
  1672. if (MachO->is64Bit()) {
  1673. H_64 = MachO->MachOObjectFile::getHeader64();
  1674. T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
  1675. &McpuDefault, &ArchFlag);
  1676. } else {
  1677. H = MachO->MachOObjectFile::getHeader();
  1678. T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
  1679. &McpuDefault, &ArchFlag);
  1680. }
  1681. const std::string ArchFlagName(ArchFlag);
  1682. if (none_of(ArchFlags, [&](const std::string &Name) {
  1683. return Name == ArchFlagName;
  1684. })) {
  1685. WithColor::error(errs(), "llvm-objdump")
  1686. << Filename << ": no architecture specified.\n";
  1687. return false;
  1688. }
  1689. return true;
  1690. }
  1691. static void printObjcMetaData(MachOObjectFile *O, bool verbose);
  1692. // ProcessMachO() is passed a single opened Mach-O file, which may be an
  1693. // archive member and or in a slice of a universal file. It prints the
  1694. // the file name and header info and then processes it according to the
  1695. // command line options.
  1696. static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
  1697. StringRef ArchiveMemberName = StringRef(),
  1698. StringRef ArchitectureName = StringRef()) {
  1699. // If we are doing some processing here on the Mach-O file print the header
  1700. // info. And don't print it otherwise like in the case of printing the
  1701. // UniversalHeaders or ArchiveHeaders.
  1702. if (Disassemble || Relocations || PrivateHeaders || ExportsTrie || Rebase ||
  1703. Bind || SymbolTable || LazyBind || WeakBind || IndirectSymbols ||
  1704. DataInCode || LinkOptHints || DylibsUsed || DylibId || ObjcMetaData ||
  1705. (!FilterSections.empty())) {
  1706. if (!NoLeadingHeaders) {
  1707. outs() << Name;
  1708. if (!ArchiveMemberName.empty())
  1709. outs() << '(' << ArchiveMemberName << ')';
  1710. if (!ArchitectureName.empty())
  1711. outs() << " (architecture " << ArchitectureName << ")";
  1712. outs() << ":\n";
  1713. }
  1714. }
  1715. // To use the report_error() form with an ArchiveName and FileName set
  1716. // these up based on what is passed for Name and ArchiveMemberName.
  1717. StringRef ArchiveName;
  1718. StringRef FileName;
  1719. if (!ArchiveMemberName.empty()) {
  1720. ArchiveName = Name;
  1721. FileName = ArchiveMemberName;
  1722. } else {
  1723. ArchiveName = StringRef();
  1724. FileName = Name;
  1725. }
  1726. // If we need the symbol table to do the operation then check it here to
  1727. // produce a good error message as to where the Mach-O file comes from in
  1728. // the error message.
  1729. if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo)
  1730. if (Error Err = MachOOF->checkSymbolTable())
  1731. report_error(std::move(Err), ArchiveName, FileName, ArchitectureName);
  1732. if (DisassembleAll) {
  1733. for (const SectionRef &Section : MachOOF->sections()) {
  1734. StringRef SectName;
  1735. Section.getName(SectName);
  1736. if (SectName.equals("__text")) {
  1737. DataRefImpl Ref = Section.getRawDataRefImpl();
  1738. StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref);
  1739. DisassembleMachO(FileName, MachOOF, SegName, SectName);
  1740. }
  1741. }
  1742. }
  1743. else if (Disassemble) {
  1744. if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
  1745. MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
  1746. DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
  1747. else
  1748. DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
  1749. }
  1750. if (IndirectSymbols)
  1751. PrintIndirectSymbols(MachOOF, !NonVerbose);
  1752. if (DataInCode)
  1753. PrintDataInCodeTable(MachOOF, !NonVerbose);
  1754. if (LinkOptHints)
  1755. PrintLinkOptHints(MachOOF);
  1756. if (Relocations)
  1757. PrintRelocations(MachOOF, !NonVerbose);
  1758. if (SectionHeaders)
  1759. printSectionHeaders(MachOOF);
  1760. if (SectionContents)
  1761. printSectionContents(MachOOF);
  1762. if (!FilterSections.empty())
  1763. DumpSectionContents(FileName, MachOOF, !NonVerbose);
  1764. if (InfoPlist)
  1765. DumpInfoPlistSectionContents(FileName, MachOOF);
  1766. if (DylibsUsed)
  1767. PrintDylibs(MachOOF, false);
  1768. if (DylibId)
  1769. PrintDylibs(MachOOF, true);
  1770. if (SymbolTable)
  1771. printSymbolTable(MachOOF, ArchiveName, ArchitectureName);
  1772. if (UnwindInfo)
  1773. printMachOUnwindInfo(MachOOF);
  1774. if (PrivateHeaders) {
  1775. printMachOFileHeader(MachOOF);
  1776. printMachOLoadCommands(MachOOF);
  1777. }
  1778. if (FirstPrivateHeader)
  1779. printMachOFileHeader(MachOOF);
  1780. if (ObjcMetaData)
  1781. printObjcMetaData(MachOOF, !NonVerbose);
  1782. if (ExportsTrie)
  1783. printExportsTrie(MachOOF);
  1784. if (Rebase)
  1785. printRebaseTable(MachOOF);
  1786. if (Bind)
  1787. printBindTable(MachOOF);
  1788. if (LazyBind)
  1789. printLazyBindTable(MachOOF);
  1790. if (WeakBind)
  1791. printWeakBindTable(MachOOF);
  1792. if (DwarfDumpType != DIDT_Null) {
  1793. std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
  1794. // Dump the complete DWARF structure.
  1795. DIDumpOptions DumpOpts;
  1796. DumpOpts.DumpType = DwarfDumpType;
  1797. DICtx->dump(outs(), DumpOpts);
  1798. }
  1799. }
  1800. // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
  1801. static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
  1802. outs() << " cputype (" << cputype << ")\n";
  1803. outs() << " cpusubtype (" << cpusubtype << ")\n";
  1804. }
  1805. // printCPUType() helps print_fat_headers by printing the cputype and
  1806. // pusubtype (symbolically for the one's it knows about).
  1807. static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
  1808. switch (cputype) {
  1809. case MachO::CPU_TYPE_I386:
  1810. switch (cpusubtype) {
  1811. case MachO::CPU_SUBTYPE_I386_ALL:
  1812. outs() << " cputype CPU_TYPE_I386\n";
  1813. outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
  1814. break;
  1815. default:
  1816. printUnknownCPUType(cputype, cpusubtype);
  1817. break;
  1818. }
  1819. break;
  1820. case MachO::CPU_TYPE_X86_64:
  1821. switch (cpusubtype) {
  1822. case MachO::CPU_SUBTYPE_X86_64_ALL:
  1823. outs() << " cputype CPU_TYPE_X86_64\n";
  1824. outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
  1825. break;
  1826. case MachO::CPU_SUBTYPE_X86_64_H:
  1827. outs() << " cputype CPU_TYPE_X86_64\n";
  1828. outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
  1829. break;
  1830. default:
  1831. printUnknownCPUType(cputype, cpusubtype);
  1832. break;
  1833. }
  1834. break;
  1835. case MachO::CPU_TYPE_ARM:
  1836. switch (cpusubtype) {
  1837. case MachO::CPU_SUBTYPE_ARM_ALL:
  1838. outs() << " cputype CPU_TYPE_ARM\n";
  1839. outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
  1840. break;
  1841. case MachO::CPU_SUBTYPE_ARM_V4T:
  1842. outs() << " cputype CPU_TYPE_ARM\n";
  1843. outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
  1844. break;
  1845. case MachO::CPU_SUBTYPE_ARM_V5TEJ:
  1846. outs() << " cputype CPU_TYPE_ARM\n";
  1847. outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
  1848. break;
  1849. case MachO::CPU_SUBTYPE_ARM_XSCALE:
  1850. outs() << " cputype CPU_TYPE_ARM\n";
  1851. outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
  1852. break;
  1853. case MachO::CPU_SUBTYPE_ARM_V6:
  1854. outs() << " cputype CPU_TYPE_ARM\n";
  1855. outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
  1856. break;
  1857. case MachO::CPU_SUBTYPE_ARM_V6M:
  1858. outs() << " cputype CPU_TYPE_ARM\n";
  1859. outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
  1860. break;
  1861. case MachO::CPU_SUBTYPE_ARM_V7:
  1862. outs() << " cputype CPU_TYPE_ARM\n";
  1863. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
  1864. break;
  1865. case MachO::CPU_SUBTYPE_ARM_V7EM:
  1866. outs() << " cputype CPU_TYPE_ARM\n";
  1867. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
  1868. break;
  1869. case MachO::CPU_SUBTYPE_ARM_V7K:
  1870. outs() << " cputype CPU_TYPE_ARM\n";
  1871. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
  1872. break;
  1873. case MachO::CPU_SUBTYPE_ARM_V7M:
  1874. outs() << " cputype CPU_TYPE_ARM\n";
  1875. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
  1876. break;
  1877. case MachO::CPU_SUBTYPE_ARM_V7S:
  1878. outs() << " cputype CPU_TYPE_ARM\n";
  1879. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
  1880. break;
  1881. default:
  1882. printUnknownCPUType(cputype, cpusubtype);
  1883. break;
  1884. }
  1885. break;
  1886. case MachO::CPU_TYPE_ARM64:
  1887. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  1888. case MachO::CPU_SUBTYPE_ARM64_ALL:
  1889. outs() << " cputype CPU_TYPE_ARM64\n";
  1890. outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
  1891. break;
  1892. case MachO::CPU_SUBTYPE_ARM64E:
  1893. outs() << " cputype CPU_TYPE_ARM64\n";
  1894. outs() << " cpusubtype CPU_SUBTYPE_ARM64E\n";
  1895. break;
  1896. default:
  1897. printUnknownCPUType(cputype, cpusubtype);
  1898. break;
  1899. }
  1900. break;
  1901. case MachO::CPU_TYPE_ARM64_32:
  1902. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  1903. case MachO::CPU_SUBTYPE_ARM64_32_V8:
  1904. outs() << " cputype CPU_TYPE_ARM64_32\n";
  1905. outs() << " cpusubtype CPU_SUBTYPE_ARM64_32_V8\n";
  1906. break;
  1907. default:
  1908. printUnknownCPUType(cputype, cpusubtype);
  1909. break;
  1910. }
  1911. break;
  1912. default:
  1913. printUnknownCPUType(cputype, cpusubtype);
  1914. break;
  1915. }
  1916. }
  1917. static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
  1918. bool verbose) {
  1919. outs() << "Fat headers\n";
  1920. if (verbose) {
  1921. if (UB->getMagic() == MachO::FAT_MAGIC)
  1922. outs() << "fat_magic FAT_MAGIC\n";
  1923. else // UB->getMagic() == MachO::FAT_MAGIC_64
  1924. outs() << "fat_magic FAT_MAGIC_64\n";
  1925. } else
  1926. outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
  1927. uint32_t nfat_arch = UB->getNumberOfObjects();
  1928. StringRef Buf = UB->getData();
  1929. uint64_t size = Buf.size();
  1930. uint64_t big_size = sizeof(struct MachO::fat_header) +
  1931. nfat_arch * sizeof(struct MachO::fat_arch);
  1932. outs() << "nfat_arch " << UB->getNumberOfObjects();
  1933. if (nfat_arch == 0)
  1934. outs() << " (malformed, contains zero architecture types)\n";
  1935. else if (big_size > size)
  1936. outs() << " (malformed, architectures past end of file)\n";
  1937. else
  1938. outs() << "\n";
  1939. for (uint32_t i = 0; i < nfat_arch; ++i) {
  1940. MachOUniversalBinary::ObjectForArch OFA(UB, i);
  1941. uint32_t cputype = OFA.getCPUType();
  1942. uint32_t cpusubtype = OFA.getCPUSubType();
  1943. outs() << "architecture ";
  1944. for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
  1945. MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
  1946. uint32_t other_cputype = other_OFA.getCPUType();
  1947. uint32_t other_cpusubtype = other_OFA.getCPUSubType();
  1948. if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
  1949. (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
  1950. (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
  1951. outs() << "(illegal duplicate architecture) ";
  1952. break;
  1953. }
  1954. }
  1955. if (verbose) {
  1956. outs() << OFA.getArchFlagName() << "\n";
  1957. printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  1958. } else {
  1959. outs() << i << "\n";
  1960. outs() << " cputype " << cputype << "\n";
  1961. outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
  1962. << "\n";
  1963. }
  1964. if (verbose &&
  1965. (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
  1966. outs() << " capabilities CPU_SUBTYPE_LIB64\n";
  1967. else
  1968. outs() << " capabilities "
  1969. << format("0x%" PRIx32,
  1970. (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
  1971. outs() << " offset " << OFA.getOffset();
  1972. if (OFA.getOffset() > size)
  1973. outs() << " (past end of file)";
  1974. if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
  1975. outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
  1976. outs() << "\n";
  1977. outs() << " size " << OFA.getSize();
  1978. big_size = OFA.getOffset() + OFA.getSize();
  1979. if (big_size > size)
  1980. outs() << " (past end of file)";
  1981. outs() << "\n";
  1982. outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
  1983. << ")\n";
  1984. }
  1985. }
  1986. static void printArchiveChild(StringRef Filename, const Archive::Child &C,
  1987. bool verbose, bool print_offset,
  1988. StringRef ArchitectureName = StringRef()) {
  1989. if (print_offset)
  1990. outs() << C.getChildOffset() << "\t";
  1991. sys::fs::perms Mode =
  1992. unwrapOrError(C.getAccessMode(), Filename, C, ArchitectureName);
  1993. if (verbose) {
  1994. // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
  1995. // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
  1996. outs() << "-";
  1997. outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
  1998. outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
  1999. outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
  2000. outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
  2001. outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
  2002. outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
  2003. outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
  2004. outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
  2005. outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
  2006. } else {
  2007. outs() << format("0%o ", Mode);
  2008. }
  2009. outs() << format(
  2010. "%3d/%-3d %5" PRId64 " ",
  2011. unwrapOrError(C.getUID(), Filename, C, ArchitectureName),
  2012. unwrapOrError(C.getGID(), Filename, C, ArchitectureName),
  2013. unwrapOrError(C.getRawSize(), Filename, C, ArchitectureName));
  2014. StringRef RawLastModified = C.getRawLastModified();
  2015. if (verbose) {
  2016. unsigned Seconds;
  2017. if (RawLastModified.getAsInteger(10, Seconds))
  2018. outs() << "(date: \"" << RawLastModified
  2019. << "\" contains non-decimal chars) ";
  2020. else {
  2021. // Since cime(3) returns a 26 character string of the form:
  2022. // "Sun Sep 16 01:03:52 1973\n\0"
  2023. // just print 24 characters.
  2024. time_t t = Seconds;
  2025. outs() << format("%.24s ", ctime(&t));
  2026. }
  2027. } else {
  2028. outs() << RawLastModified << " ";
  2029. }
  2030. if (verbose) {
  2031. Expected<StringRef> NameOrErr = C.getName();
  2032. if (!NameOrErr) {
  2033. consumeError(NameOrErr.takeError());
  2034. outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName)
  2035. << "\n";
  2036. } else {
  2037. StringRef Name = NameOrErr.get();
  2038. outs() << Name << "\n";
  2039. }
  2040. } else {
  2041. outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName)
  2042. << "\n";
  2043. }
  2044. }
  2045. static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
  2046. bool print_offset,
  2047. StringRef ArchitectureName = StringRef()) {
  2048. Error Err = Error::success();
  2049. for (const auto &C : A->children(Err, false))
  2050. printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
  2051. if (Err)
  2052. report_error(std::move(Err), StringRef(), Filename, ArchitectureName);
  2053. }
  2054. static bool ValidateArchFlags() {
  2055. // Check for -arch all and verifiy the -arch flags are valid.
  2056. for (unsigned i = 0; i < ArchFlags.size(); ++i) {
  2057. if (ArchFlags[i] == "all") {
  2058. ArchAll = true;
  2059. } else {
  2060. if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
  2061. WithColor::error(errs(), "llvm-objdump")
  2062. << "unknown architecture named '" + ArchFlags[i] +
  2063. "'for the -arch option\n";
  2064. return false;
  2065. }
  2066. }
  2067. }
  2068. return true;
  2069. }
  2070. // ParseInputMachO() parses the named Mach-O file in Filename and handles the
  2071. // -arch flags selecting just those slices as specified by them and also parses
  2072. // archive files. Then for each individual Mach-O file ProcessMachO() is
  2073. // called to process the file based on the command line options.
  2074. void parseInputMachO(StringRef Filename) {
  2075. if (!ValidateArchFlags())
  2076. return;
  2077. // Attempt to open the binary.
  2078. Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
  2079. if (!BinaryOrErr) {
  2080. if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
  2081. report_error(std::move(E), Filename);
  2082. else
  2083. outs() << Filename << ": is not an object file\n";
  2084. return;
  2085. }
  2086. Binary &Bin = *BinaryOrErr.get().getBinary();
  2087. if (Archive *A = dyn_cast<Archive>(&Bin)) {
  2088. outs() << "Archive : " << Filename << "\n";
  2089. if (ArchiveHeaders)
  2090. printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
  2091. Error Err = Error::success();
  2092. for (auto &C : A->children(Err)) {
  2093. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  2094. if (!ChildOrErr) {
  2095. if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  2096. report_error(std::move(E), Filename, C);
  2097. continue;
  2098. }
  2099. if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
  2100. if (!checkMachOAndArchFlags(O, Filename))
  2101. return;
  2102. ProcessMachO(Filename, O, O->getFileName());
  2103. }
  2104. }
  2105. if (Err)
  2106. report_error(std::move(Err), Filename);
  2107. return;
  2108. }
  2109. if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
  2110. parseInputMachO(UB);
  2111. return;
  2112. }
  2113. if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
  2114. if (!checkMachOAndArchFlags(O, Filename))
  2115. return;
  2116. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O))
  2117. ProcessMachO(Filename, MachOOF);
  2118. else
  2119. WithColor::error(errs(), "llvm-objdump")
  2120. << Filename << "': "
  2121. << "object is not a Mach-O file type.\n";
  2122. return;
  2123. }
  2124. llvm_unreachable("Input object can't be invalid at this point");
  2125. }
  2126. void parseInputMachO(MachOUniversalBinary *UB) {
  2127. if (!ValidateArchFlags())
  2128. return;
  2129. auto Filename = UB->getFileName();
  2130. if (UniversalHeaders)
  2131. printMachOUniversalHeaders(UB, !NonVerbose);
  2132. // If we have a list of architecture flags specified dump only those.
  2133. if (!ArchAll && !ArchFlags.empty()) {
  2134. // Look for a slice in the universal binary that matches each ArchFlag.
  2135. bool ArchFound;
  2136. for (unsigned i = 0; i < ArchFlags.size(); ++i) {
  2137. ArchFound = false;
  2138. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  2139. E = UB->end_objects();
  2140. I != E; ++I) {
  2141. if (ArchFlags[i] == I->getArchFlagName()) {
  2142. ArchFound = true;
  2143. Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
  2144. I->getAsObjectFile();
  2145. std::string ArchitectureName = "";
  2146. if (ArchFlags.size() > 1)
  2147. ArchitectureName = I->getArchFlagName();
  2148. if (ObjOrErr) {
  2149. ObjectFile &O = *ObjOrErr.get();
  2150. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
  2151. ProcessMachO(Filename, MachOOF, "", ArchitectureName);
  2152. } else if (Error E = isNotObjectErrorInvalidFileType(
  2153. ObjOrErr.takeError())) {
  2154. report_error(std::move(E), Filename, StringRef(), ArchitectureName);
  2155. continue;
  2156. } else if (Expected<std::unique_ptr<Archive>> AOrErr =
  2157. I->getAsArchive()) {
  2158. std::unique_ptr<Archive> &A = *AOrErr;
  2159. outs() << "Archive : " << Filename;
  2160. if (!ArchitectureName.empty())
  2161. outs() << " (architecture " << ArchitectureName << ")";
  2162. outs() << "\n";
  2163. if (ArchiveHeaders)
  2164. printArchiveHeaders(Filename, A.get(), !NonVerbose,
  2165. ArchiveMemberOffsets, ArchitectureName);
  2166. Error Err = Error::success();
  2167. for (auto &C : A->children(Err)) {
  2168. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  2169. if (!ChildOrErr) {
  2170. if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  2171. report_error(std::move(E), Filename, C, ArchitectureName);
  2172. continue;
  2173. }
  2174. if (MachOObjectFile *O =
  2175. dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
  2176. ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
  2177. }
  2178. if (Err)
  2179. report_error(std::move(Err), Filename);
  2180. } else {
  2181. consumeError(AOrErr.takeError());
  2182. error("Mach-O universal file: " + Filename + " for " +
  2183. "architecture " + StringRef(I->getArchFlagName()) +
  2184. " is not a Mach-O file or an archive file");
  2185. }
  2186. }
  2187. }
  2188. if (!ArchFound) {
  2189. WithColor::error(errs(), "llvm-objdump")
  2190. << "file: " + Filename + " does not contain "
  2191. << "architecture: " + ArchFlags[i] + "\n";
  2192. return;
  2193. }
  2194. }
  2195. return;
  2196. }
  2197. // No architecture flags were specified so if this contains a slice that
  2198. // matches the host architecture dump only that.
  2199. if (!ArchAll) {
  2200. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  2201. E = UB->end_objects();
  2202. I != E; ++I) {
  2203. if (MachOObjectFile::getHostArch().getArchName() ==
  2204. I->getArchFlagName()) {
  2205. Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
  2206. std::string ArchiveName;
  2207. ArchiveName.clear();
  2208. if (ObjOrErr) {
  2209. ObjectFile &O = *ObjOrErr.get();
  2210. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
  2211. ProcessMachO(Filename, MachOOF);
  2212. } else if (Error E =
  2213. isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
  2214. report_error(std::move(E), Filename);
  2215. } else if (Expected<std::unique_ptr<Archive>> AOrErr =
  2216. I->getAsArchive()) {
  2217. std::unique_ptr<Archive> &A = *AOrErr;
  2218. outs() << "Archive : " << Filename << "\n";
  2219. if (ArchiveHeaders)
  2220. printArchiveHeaders(Filename, A.get(), !NonVerbose,
  2221. ArchiveMemberOffsets);
  2222. Error Err = Error::success();
  2223. for (auto &C : A->children(Err)) {
  2224. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  2225. if (!ChildOrErr) {
  2226. if (Error E =
  2227. isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  2228. report_error(std::move(E), Filename, C);
  2229. continue;
  2230. }
  2231. if (MachOObjectFile *O =
  2232. dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
  2233. ProcessMachO(Filename, O, O->getFileName());
  2234. }
  2235. if (Err)
  2236. report_error(std::move(Err), Filename);
  2237. } else {
  2238. consumeError(AOrErr.takeError());
  2239. error("Mach-O universal file: " + Filename + " for architecture " +
  2240. StringRef(I->getArchFlagName()) +
  2241. " is not a Mach-O file or an archive file");
  2242. }
  2243. return;
  2244. }
  2245. }
  2246. }
  2247. // Either all architectures have been specified or none have been specified
  2248. // and this does not contain the host architecture so dump all the slices.
  2249. bool moreThanOneArch = UB->getNumberOfObjects() > 1;
  2250. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  2251. E = UB->end_objects();
  2252. I != E; ++I) {
  2253. Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
  2254. std::string ArchitectureName = "";
  2255. if (moreThanOneArch)
  2256. ArchitectureName = I->getArchFlagName();
  2257. if (ObjOrErr) {
  2258. ObjectFile &Obj = *ObjOrErr.get();
  2259. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
  2260. ProcessMachO(Filename, MachOOF, "", ArchitectureName);
  2261. } else if (Error E =
  2262. isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
  2263. report_error(std::move(E), StringRef(), Filename, ArchitectureName);
  2264. } else if (Expected<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
  2265. std::unique_ptr<Archive> &A = *AOrErr;
  2266. outs() << "Archive : " << Filename;
  2267. if (!ArchitectureName.empty())
  2268. outs() << " (architecture " << ArchitectureName << ")";
  2269. outs() << "\n";
  2270. if (ArchiveHeaders)
  2271. printArchiveHeaders(Filename, A.get(), !NonVerbose,
  2272. ArchiveMemberOffsets, ArchitectureName);
  2273. Error Err = Error::success();
  2274. for (auto &C : A->children(Err)) {
  2275. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  2276. if (!ChildOrErr) {
  2277. if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  2278. report_error(std::move(E), Filename, C, ArchitectureName);
  2279. continue;
  2280. }
  2281. if (MachOObjectFile *O =
  2282. dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
  2283. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
  2284. ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
  2285. ArchitectureName);
  2286. }
  2287. }
  2288. if (Err)
  2289. report_error(std::move(Err), Filename);
  2290. } else {
  2291. consumeError(AOrErr.takeError());
  2292. error("Mach-O universal file: " + Filename + " for architecture " +
  2293. StringRef(I->getArchFlagName()) +
  2294. " is not a Mach-O file or an archive file");
  2295. }
  2296. }
  2297. }
  2298. // The block of info used by the Symbolizer call backs.
  2299. struct DisassembleInfo {
  2300. DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap,
  2301. std::vector<SectionRef> *Sections, bool verbose)
  2302. : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {}
  2303. bool verbose;
  2304. MachOObjectFile *O;
  2305. SectionRef S;
  2306. SymbolAddressMap *AddrMap;
  2307. std::vector<SectionRef> *Sections;
  2308. const char *class_name = nullptr;
  2309. const char *selector_name = nullptr;
  2310. std::unique_ptr<char[]> method = nullptr;
  2311. char *demangled_name = nullptr;
  2312. uint64_t adrp_addr = 0;
  2313. uint32_t adrp_inst = 0;
  2314. std::unique_ptr<SymbolAddressMap> bindtable;
  2315. uint32_t depth = 0;
  2316. };
  2317. // SymbolizerGetOpInfo() is the operand information call back function.
  2318. // This is called to get the symbolic information for operand(s) of an
  2319. // instruction when it is being done. This routine does this from
  2320. // the relocation information, symbol table, etc. That block of information
  2321. // is a pointer to the struct DisassembleInfo that was passed when the
  2322. // disassembler context was created and passed to back to here when
  2323. // called back by the disassembler for instruction operands that could have
  2324. // relocation information. The address of the instruction containing operand is
  2325. // at the Pc parameter. The immediate value the operand has is passed in
  2326. // op_info->Value and is at Offset past the start of the instruction and has a
  2327. // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
  2328. // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
  2329. // names and addends of the symbolic expression to add for the operand. The
  2330. // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
  2331. // information is returned then this function returns 1 else it returns 0.
  2332. static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
  2333. uint64_t Size, int TagType, void *TagBuf) {
  2334. struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
  2335. struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
  2336. uint64_t value = op_info->Value;
  2337. // Make sure all fields returned are zero if we don't set them.
  2338. memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
  2339. op_info->Value = value;
  2340. // If the TagType is not the value 1 which it code knows about or if no
  2341. // verbose symbolic information is wanted then just return 0, indicating no
  2342. // information is being returned.
  2343. if (TagType != 1 || !info->verbose)
  2344. return 0;
  2345. unsigned int Arch = info->O->getArch();
  2346. if (Arch == Triple::x86) {
  2347. if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
  2348. return 0;
  2349. if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
  2350. // TODO:
  2351. // Search the external relocation entries of a fully linked image
  2352. // (if any) for an entry that matches this segment offset.
  2353. // uint32_t seg_offset = (Pc + Offset);
  2354. return 0;
  2355. }
  2356. // In MH_OBJECT filetypes search the section's relocation entries (if any)
  2357. // for an entry for this section offset.
  2358. uint32_t sect_addr = info->S.getAddress();
  2359. uint32_t sect_offset = (Pc + Offset) - sect_addr;
  2360. bool reloc_found = false;
  2361. DataRefImpl Rel;
  2362. MachO::any_relocation_info RE;
  2363. bool isExtern = false;
  2364. SymbolRef Symbol;
  2365. bool r_scattered = false;
  2366. uint32_t r_value, pair_r_value, r_type;
  2367. for (const RelocationRef &Reloc : info->S.relocations()) {
  2368. uint64_t RelocOffset = Reloc.getOffset();
  2369. if (RelocOffset == sect_offset) {
  2370. Rel = Reloc.getRawDataRefImpl();
  2371. RE = info->O->getRelocation(Rel);
  2372. r_type = info->O->getAnyRelocationType(RE);
  2373. r_scattered = info->O->isRelocationScattered(RE);
  2374. if (r_scattered) {
  2375. r_value = info->O->getScatteredRelocationValue(RE);
  2376. if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
  2377. r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
  2378. DataRefImpl RelNext = Rel;
  2379. info->O->moveRelocationNext(RelNext);
  2380. MachO::any_relocation_info RENext;
  2381. RENext = info->O->getRelocation(RelNext);
  2382. if (info->O->isRelocationScattered(RENext))
  2383. pair_r_value = info->O->getScatteredRelocationValue(RENext);
  2384. else
  2385. return 0;
  2386. }
  2387. } else {
  2388. isExtern = info->O->getPlainRelocationExternal(RE);
  2389. if (isExtern) {
  2390. symbol_iterator RelocSym = Reloc.getSymbol();
  2391. Symbol = *RelocSym;
  2392. }
  2393. }
  2394. reloc_found = true;
  2395. break;
  2396. }
  2397. }
  2398. if (reloc_found && isExtern) {
  2399. op_info->AddSymbol.Present = 1;
  2400. op_info->AddSymbol.Name =
  2401. unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
  2402. // For i386 extern relocation entries the value in the instruction is
  2403. // the offset from the symbol, and value is already set in op_info->Value.
  2404. return 1;
  2405. }
  2406. if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
  2407. r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
  2408. const char *add = GuessSymbolName(r_value, info->AddrMap);
  2409. const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
  2410. uint32_t offset = value - (r_value - pair_r_value);
  2411. op_info->AddSymbol.Present = 1;
  2412. if (add != nullptr)
  2413. op_info->AddSymbol.Name = add;
  2414. else
  2415. op_info->AddSymbol.Value = r_value;
  2416. op_info->SubtractSymbol.Present = 1;
  2417. if (sub != nullptr)
  2418. op_info->SubtractSymbol.Name = sub;
  2419. else
  2420. op_info->SubtractSymbol.Value = pair_r_value;
  2421. op_info->Value = offset;
  2422. return 1;
  2423. }
  2424. return 0;
  2425. }
  2426. if (Arch == Triple::x86_64) {
  2427. if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
  2428. return 0;
  2429. // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
  2430. // relocation entries of a linked image (if any) for an entry that matches
  2431. // this segment offset.
  2432. if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
  2433. uint64_t seg_offset = Pc + Offset;
  2434. bool reloc_found = false;
  2435. DataRefImpl Rel;
  2436. MachO::any_relocation_info RE;
  2437. bool isExtern = false;
  2438. SymbolRef Symbol;
  2439. for (const RelocationRef &Reloc : info->O->external_relocations()) {
  2440. uint64_t RelocOffset = Reloc.getOffset();
  2441. if (RelocOffset == seg_offset) {
  2442. Rel = Reloc.getRawDataRefImpl();
  2443. RE = info->O->getRelocation(Rel);
  2444. // external relocation entries should always be external.
  2445. isExtern = info->O->getPlainRelocationExternal(RE);
  2446. if (isExtern) {
  2447. symbol_iterator RelocSym = Reloc.getSymbol();
  2448. Symbol = *RelocSym;
  2449. }
  2450. reloc_found = true;
  2451. break;
  2452. }
  2453. }
  2454. if (reloc_found && isExtern) {
  2455. // The Value passed in will be adjusted by the Pc if the instruction
  2456. // adds the Pc. But for x86_64 external relocation entries the Value
  2457. // is the offset from the external symbol.
  2458. if (info->O->getAnyRelocationPCRel(RE))
  2459. op_info->Value -= Pc + Offset + Size;
  2460. const char *name =
  2461. unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
  2462. op_info->AddSymbol.Present = 1;
  2463. op_info->AddSymbol.Name = name;
  2464. return 1;
  2465. }
  2466. return 0;
  2467. }
  2468. // In MH_OBJECT filetypes search the section's relocation entries (if any)
  2469. // for an entry for this section offset.
  2470. uint64_t sect_addr = info->S.getAddress();
  2471. uint64_t sect_offset = (Pc + Offset) - sect_addr;
  2472. bool reloc_found = false;
  2473. DataRefImpl Rel;
  2474. MachO::any_relocation_info RE;
  2475. bool isExtern = false;
  2476. SymbolRef Symbol;
  2477. for (const RelocationRef &Reloc : info->S.relocations()) {
  2478. uint64_t RelocOffset = Reloc.getOffset();
  2479. if (RelocOffset == sect_offset) {
  2480. Rel = Reloc.getRawDataRefImpl();
  2481. RE = info->O->getRelocation(Rel);
  2482. // NOTE: Scattered relocations don't exist on x86_64.
  2483. isExtern = info->O->getPlainRelocationExternal(RE);
  2484. if (isExtern) {
  2485. symbol_iterator RelocSym = Reloc.getSymbol();
  2486. Symbol = *RelocSym;
  2487. }
  2488. reloc_found = true;
  2489. break;
  2490. }
  2491. }
  2492. if (reloc_found && isExtern) {
  2493. // The Value passed in will be adjusted by the Pc if the instruction
  2494. // adds the Pc. But for x86_64 external relocation entries the Value
  2495. // is the offset from the external symbol.
  2496. if (info->O->getAnyRelocationPCRel(RE))
  2497. op_info->Value -= Pc + Offset + Size;
  2498. const char *name =
  2499. unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
  2500. unsigned Type = info->O->getAnyRelocationType(RE);
  2501. if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
  2502. DataRefImpl RelNext = Rel;
  2503. info->O->moveRelocationNext(RelNext);
  2504. MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
  2505. unsigned TypeNext = info->O->getAnyRelocationType(RENext);
  2506. bool isExternNext = info->O->getPlainRelocationExternal(RENext);
  2507. unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
  2508. if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
  2509. op_info->SubtractSymbol.Present = 1;
  2510. op_info->SubtractSymbol.Name = name;
  2511. symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
  2512. Symbol = *RelocSymNext;
  2513. name = unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
  2514. }
  2515. }
  2516. // TODO: add the VariantKinds to op_info->VariantKind for relocation types
  2517. // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
  2518. op_info->AddSymbol.Present = 1;
  2519. op_info->AddSymbol.Name = name;
  2520. return 1;
  2521. }
  2522. return 0;
  2523. }
  2524. if (Arch == Triple::arm) {
  2525. if (Offset != 0 || (Size != 4 && Size != 2))
  2526. return 0;
  2527. if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
  2528. // TODO:
  2529. // Search the external relocation entries of a fully linked image
  2530. // (if any) for an entry that matches this segment offset.
  2531. // uint32_t seg_offset = (Pc + Offset);
  2532. return 0;
  2533. }
  2534. // In MH_OBJECT filetypes search the section's relocation entries (if any)
  2535. // for an entry for this section offset.
  2536. uint32_t sect_addr = info->S.getAddress();
  2537. uint32_t sect_offset = (Pc + Offset) - sect_addr;
  2538. DataRefImpl Rel;
  2539. MachO::any_relocation_info RE;
  2540. bool isExtern = false;
  2541. SymbolRef Symbol;
  2542. bool r_scattered = false;
  2543. uint32_t r_value, pair_r_value, r_type, r_length, other_half;
  2544. auto Reloc =
  2545. find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
  2546. uint64_t RelocOffset = Reloc.getOffset();
  2547. return RelocOffset == sect_offset;
  2548. });
  2549. if (Reloc == info->S.relocations().end())
  2550. return 0;
  2551. Rel = Reloc->getRawDataRefImpl();
  2552. RE = info->O->getRelocation(Rel);
  2553. r_length = info->O->getAnyRelocationLength(RE);
  2554. r_scattered = info->O->isRelocationScattered(RE);
  2555. if (r_scattered) {
  2556. r_value = info->O->getScatteredRelocationValue(RE);
  2557. r_type = info->O->getScatteredRelocationType(RE);
  2558. } else {
  2559. r_type = info->O->getAnyRelocationType(RE);
  2560. isExtern = info->O->getPlainRelocationExternal(RE);
  2561. if (isExtern) {
  2562. symbol_iterator RelocSym = Reloc->getSymbol();
  2563. Symbol = *RelocSym;
  2564. }
  2565. }
  2566. if (r_type == MachO::ARM_RELOC_HALF ||
  2567. r_type == MachO::ARM_RELOC_SECTDIFF ||
  2568. r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
  2569. r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  2570. DataRefImpl RelNext = Rel;
  2571. info->O->moveRelocationNext(RelNext);
  2572. MachO::any_relocation_info RENext;
  2573. RENext = info->O->getRelocation(RelNext);
  2574. other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
  2575. if (info->O->isRelocationScattered(RENext))
  2576. pair_r_value = info->O->getScatteredRelocationValue(RENext);
  2577. }
  2578. if (isExtern) {
  2579. const char *name =
  2580. unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
  2581. op_info->AddSymbol.Present = 1;
  2582. op_info->AddSymbol.Name = name;
  2583. switch (r_type) {
  2584. case MachO::ARM_RELOC_HALF:
  2585. if ((r_length & 0x1) == 1) {
  2586. op_info->Value = value << 16 | other_half;
  2587. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
  2588. } else {
  2589. op_info->Value = other_half << 16 | value;
  2590. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
  2591. }
  2592. break;
  2593. default:
  2594. break;
  2595. }
  2596. return 1;
  2597. }
  2598. // If we have a branch that is not an external relocation entry then
  2599. // return 0 so the code in tryAddingSymbolicOperand() can use the
  2600. // SymbolLookUp call back with the branch target address to look up the
  2601. // symbol and possibility add an annotation for a symbol stub.
  2602. if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
  2603. r_type == MachO::ARM_THUMB_RELOC_BR22))
  2604. return 0;
  2605. uint32_t offset = 0;
  2606. if (r_type == MachO::ARM_RELOC_HALF ||
  2607. r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  2608. if ((r_length & 0x1) == 1)
  2609. value = value << 16 | other_half;
  2610. else
  2611. value = other_half << 16 | value;
  2612. }
  2613. if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
  2614. r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
  2615. offset = value - r_value;
  2616. value = r_value;
  2617. }
  2618. if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  2619. if ((r_length & 0x1) == 1)
  2620. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
  2621. else
  2622. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
  2623. const char *add = GuessSymbolName(r_value, info->AddrMap);
  2624. const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
  2625. int32_t offset = value - (r_value - pair_r_value);
  2626. op_info->AddSymbol.Present = 1;
  2627. if (add != nullptr)
  2628. op_info->AddSymbol.Name = add;
  2629. else
  2630. op_info->AddSymbol.Value = r_value;
  2631. op_info->SubtractSymbol.Present = 1;
  2632. if (sub != nullptr)
  2633. op_info->SubtractSymbol.Name = sub;
  2634. else
  2635. op_info->SubtractSymbol.Value = pair_r_value;
  2636. op_info->Value = offset;
  2637. return 1;
  2638. }
  2639. op_info->AddSymbol.Present = 1;
  2640. op_info->Value = offset;
  2641. if (r_type == MachO::ARM_RELOC_HALF) {
  2642. if ((r_length & 0x1) == 1)
  2643. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
  2644. else
  2645. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
  2646. }
  2647. const char *add = GuessSymbolName(value, info->AddrMap);
  2648. if (add != nullptr) {
  2649. op_info->AddSymbol.Name = add;
  2650. return 1;
  2651. }
  2652. op_info->AddSymbol.Value = value;
  2653. return 1;
  2654. }
  2655. if (Arch == Triple::aarch64) {
  2656. if (Offset != 0 || Size != 4)
  2657. return 0;
  2658. if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
  2659. // TODO:
  2660. // Search the external relocation entries of a fully linked image
  2661. // (if any) for an entry that matches this segment offset.
  2662. // uint64_t seg_offset = (Pc + Offset);
  2663. return 0;
  2664. }
  2665. // In MH_OBJECT filetypes search the section's relocation entries (if any)
  2666. // for an entry for this section offset.
  2667. uint64_t sect_addr = info->S.getAddress();
  2668. uint64_t sect_offset = (Pc + Offset) - sect_addr;
  2669. auto Reloc =
  2670. find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
  2671. uint64_t RelocOffset = Reloc.getOffset();
  2672. return RelocOffset == sect_offset;
  2673. });
  2674. if (Reloc == info->S.relocations().end())
  2675. return 0;
  2676. DataRefImpl Rel = Reloc->getRawDataRefImpl();
  2677. MachO::any_relocation_info RE = info->O->getRelocation(Rel);
  2678. uint32_t r_type = info->O->getAnyRelocationType(RE);
  2679. if (r_type == MachO::ARM64_RELOC_ADDEND) {
  2680. DataRefImpl RelNext = Rel;
  2681. info->O->moveRelocationNext(RelNext);
  2682. MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
  2683. if (value == 0) {
  2684. value = info->O->getPlainRelocationSymbolNum(RENext);
  2685. op_info->Value = value;
  2686. }
  2687. }
  2688. // NOTE: Scattered relocations don't exist on arm64.
  2689. if (!info->O->getPlainRelocationExternal(RE))
  2690. return 0;
  2691. const char *name =
  2692. unwrapOrError(Reloc->getSymbol()->getName(), info->O->getFileName())
  2693. .data();
  2694. op_info->AddSymbol.Present = 1;
  2695. op_info->AddSymbol.Name = name;
  2696. switch (r_type) {
  2697. case MachO::ARM64_RELOC_PAGE21:
  2698. /* @page */
  2699. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
  2700. break;
  2701. case MachO::ARM64_RELOC_PAGEOFF12:
  2702. /* @pageoff */
  2703. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
  2704. break;
  2705. case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
  2706. /* @gotpage */
  2707. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
  2708. break;
  2709. case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
  2710. /* @gotpageoff */
  2711. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
  2712. break;
  2713. case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
  2714. /* @tvlppage is not implemented in llvm-mc */
  2715. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
  2716. break;
  2717. case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
  2718. /* @tvlppageoff is not implemented in llvm-mc */
  2719. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
  2720. break;
  2721. default:
  2722. case MachO::ARM64_RELOC_BRANCH26:
  2723. op_info->VariantKind = LLVMDisassembler_VariantKind_None;
  2724. break;
  2725. }
  2726. return 1;
  2727. }
  2728. return 0;
  2729. }
  2730. // GuessCstringPointer is passed the address of what might be a pointer to a
  2731. // literal string in a cstring section. If that address is in a cstring section
  2732. // it returns a pointer to that string. Else it returns nullptr.
  2733. static const char *GuessCstringPointer(uint64_t ReferenceValue,
  2734. struct DisassembleInfo *info) {
  2735. for (const auto &Load : info->O->load_commands()) {
  2736. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  2737. MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
  2738. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2739. MachO::section_64 Sec = info->O->getSection64(Load, J);
  2740. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2741. if (section_type == MachO::S_CSTRING_LITERALS &&
  2742. ReferenceValue >= Sec.addr &&
  2743. ReferenceValue < Sec.addr + Sec.size) {
  2744. uint64_t sect_offset = ReferenceValue - Sec.addr;
  2745. uint64_t object_offset = Sec.offset + sect_offset;
  2746. StringRef MachOContents = info->O->getData();
  2747. uint64_t object_size = MachOContents.size();
  2748. const char *object_addr = (const char *)MachOContents.data();
  2749. if (object_offset < object_size) {
  2750. const char *name = object_addr + object_offset;
  2751. return name;
  2752. } else {
  2753. return nullptr;
  2754. }
  2755. }
  2756. }
  2757. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  2758. MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
  2759. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2760. MachO::section Sec = info->O->getSection(Load, J);
  2761. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2762. if (section_type == MachO::S_CSTRING_LITERALS &&
  2763. ReferenceValue >= Sec.addr &&
  2764. ReferenceValue < Sec.addr + Sec.size) {
  2765. uint64_t sect_offset = ReferenceValue - Sec.addr;
  2766. uint64_t object_offset = Sec.offset + sect_offset;
  2767. StringRef MachOContents = info->O->getData();
  2768. uint64_t object_size = MachOContents.size();
  2769. const char *object_addr = (const char *)MachOContents.data();
  2770. if (object_offset < object_size) {
  2771. const char *name = object_addr + object_offset;
  2772. return name;
  2773. } else {
  2774. return nullptr;
  2775. }
  2776. }
  2777. }
  2778. }
  2779. }
  2780. return nullptr;
  2781. }
  2782. // GuessIndirectSymbol returns the name of the indirect symbol for the
  2783. // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
  2784. // an address of a symbol stub or a lazy or non-lazy pointer to associate the
  2785. // symbol name being referenced by the stub or pointer.
  2786. static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
  2787. struct DisassembleInfo *info) {
  2788. MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
  2789. MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
  2790. for (const auto &Load : info->O->load_commands()) {
  2791. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  2792. MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
  2793. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2794. MachO::section_64 Sec = info->O->getSection64(Load, J);
  2795. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2796. if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  2797. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  2798. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  2799. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  2800. section_type == MachO::S_SYMBOL_STUBS) &&
  2801. ReferenceValue >= Sec.addr &&
  2802. ReferenceValue < Sec.addr + Sec.size) {
  2803. uint32_t stride;
  2804. if (section_type == MachO::S_SYMBOL_STUBS)
  2805. stride = Sec.reserved2;
  2806. else
  2807. stride = 8;
  2808. if (stride == 0)
  2809. return nullptr;
  2810. uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
  2811. if (index < Dysymtab.nindirectsyms) {
  2812. uint32_t indirect_symbol =
  2813. info->O->getIndirectSymbolTableEntry(Dysymtab, index);
  2814. if (indirect_symbol < Symtab.nsyms) {
  2815. symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
  2816. return unwrapOrError(Sym->getName(), info->O->getFileName())
  2817. .data();
  2818. }
  2819. }
  2820. }
  2821. }
  2822. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  2823. MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
  2824. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2825. MachO::section Sec = info->O->getSection(Load, J);
  2826. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2827. if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  2828. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  2829. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  2830. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  2831. section_type == MachO::S_SYMBOL_STUBS) &&
  2832. ReferenceValue >= Sec.addr &&
  2833. ReferenceValue < Sec.addr + Sec.size) {
  2834. uint32_t stride;
  2835. if (section_type == MachO::S_SYMBOL_STUBS)
  2836. stride = Sec.reserved2;
  2837. else
  2838. stride = 4;
  2839. if (stride == 0)
  2840. return nullptr;
  2841. uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
  2842. if (index < Dysymtab.nindirectsyms) {
  2843. uint32_t indirect_symbol =
  2844. info->O->getIndirectSymbolTableEntry(Dysymtab, index);
  2845. if (indirect_symbol < Symtab.nsyms) {
  2846. symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
  2847. return unwrapOrError(Sym->getName(), info->O->getFileName())
  2848. .data();
  2849. }
  2850. }
  2851. }
  2852. }
  2853. }
  2854. }
  2855. return nullptr;
  2856. }
  2857. // method_reference() is called passing it the ReferenceName that might be
  2858. // a reference it to an Objective-C method call. If so then it allocates and
  2859. // assembles a method call string with the values last seen and saved in
  2860. // the DisassembleInfo's class_name and selector_name fields. This is saved
  2861. // into the method field of the info and any previous string is free'ed.
  2862. // Then the class_name field in the info is set to nullptr. The method call
  2863. // string is set into ReferenceName and ReferenceType is set to
  2864. // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
  2865. // then both ReferenceType and ReferenceName are left unchanged.
  2866. static void method_reference(struct DisassembleInfo *info,
  2867. uint64_t *ReferenceType,
  2868. const char **ReferenceName) {
  2869. unsigned int Arch = info->O->getArch();
  2870. if (*ReferenceName != nullptr) {
  2871. if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
  2872. if (info->selector_name != nullptr) {
  2873. if (info->class_name != nullptr) {
  2874. info->method = llvm::make_unique<char[]>(
  2875. 5 + strlen(info->class_name) + strlen(info->selector_name));
  2876. char *method = info->method.get();
  2877. if (method != nullptr) {
  2878. strcpy(method, "+[");
  2879. strcat(method, info->class_name);
  2880. strcat(method, " ");
  2881. strcat(method, info->selector_name);
  2882. strcat(method, "]");
  2883. *ReferenceName = method;
  2884. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
  2885. }
  2886. } else {
  2887. info->method =
  2888. llvm::make_unique<char[]>(9 + strlen(info->selector_name));
  2889. char *method = info->method.get();
  2890. if (method != nullptr) {
  2891. if (Arch == Triple::x86_64)
  2892. strcpy(method, "-[%rdi ");
  2893. else if (Arch == Triple::aarch64)
  2894. strcpy(method, "-[x0 ");
  2895. else
  2896. strcpy(method, "-[r? ");
  2897. strcat(method, info->selector_name);
  2898. strcat(method, "]");
  2899. *ReferenceName = method;
  2900. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
  2901. }
  2902. }
  2903. info->class_name = nullptr;
  2904. }
  2905. } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
  2906. if (info->selector_name != nullptr) {
  2907. info->method =
  2908. llvm::make_unique<char[]>(17 + strlen(info->selector_name));
  2909. char *method = info->method.get();
  2910. if (method != nullptr) {
  2911. if (Arch == Triple::x86_64)
  2912. strcpy(method, "-[[%rdi super] ");
  2913. else if (Arch == Triple::aarch64)
  2914. strcpy(method, "-[[x0 super] ");
  2915. else
  2916. strcpy(method, "-[[r? super] ");
  2917. strcat(method, info->selector_name);
  2918. strcat(method, "]");
  2919. *ReferenceName = method;
  2920. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
  2921. }
  2922. info->class_name = nullptr;
  2923. }
  2924. }
  2925. }
  2926. }
  2927. // GuessPointerPointer() is passed the address of what might be a pointer to
  2928. // a reference to an Objective-C class, selector, message ref or cfstring.
  2929. // If so the value of the pointer is returned and one of the booleans are set
  2930. // to true. If not zero is returned and all the booleans are set to false.
  2931. static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
  2932. struct DisassembleInfo *info,
  2933. bool &classref, bool &selref, bool &msgref,
  2934. bool &cfstring) {
  2935. classref = false;
  2936. selref = false;
  2937. msgref = false;
  2938. cfstring = false;
  2939. for (const auto &Load : info->O->load_commands()) {
  2940. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  2941. MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
  2942. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2943. MachO::section_64 Sec = info->O->getSection64(Load, J);
  2944. if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
  2945. strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
  2946. strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
  2947. strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
  2948. strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
  2949. ReferenceValue >= Sec.addr &&
  2950. ReferenceValue < Sec.addr + Sec.size) {
  2951. uint64_t sect_offset = ReferenceValue - Sec.addr;
  2952. uint64_t object_offset = Sec.offset + sect_offset;
  2953. StringRef MachOContents = info->O->getData();
  2954. uint64_t object_size = MachOContents.size();
  2955. const char *object_addr = (const char *)MachOContents.data();
  2956. if (object_offset < object_size) {
  2957. uint64_t pointer_value;
  2958. memcpy(&pointer_value, object_addr + object_offset,
  2959. sizeof(uint64_t));
  2960. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  2961. sys::swapByteOrder(pointer_value);
  2962. if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
  2963. selref = true;
  2964. else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
  2965. strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
  2966. classref = true;
  2967. else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
  2968. ReferenceValue + 8 < Sec.addr + Sec.size) {
  2969. msgref = true;
  2970. memcpy(&pointer_value, object_addr + object_offset + 8,
  2971. sizeof(uint64_t));
  2972. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  2973. sys::swapByteOrder(pointer_value);
  2974. } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
  2975. cfstring = true;
  2976. return pointer_value;
  2977. } else {
  2978. return 0;
  2979. }
  2980. }
  2981. }
  2982. }
  2983. // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
  2984. }
  2985. return 0;
  2986. }
  2987. // get_pointer_64 returns a pointer to the bytes in the object file at the
  2988. // Address from a section in the Mach-O file. And indirectly returns the
  2989. // offset into the section, number of bytes left in the section past the offset
  2990. // and which section is was being referenced. If the Address is not in a
  2991. // section nullptr is returned.
  2992. static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
  2993. uint32_t &left, SectionRef &S,
  2994. DisassembleInfo *info,
  2995. bool objc_only = false) {
  2996. offset = 0;
  2997. left = 0;
  2998. S = SectionRef();
  2999. for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
  3000. uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
  3001. uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
  3002. if (SectSize == 0)
  3003. continue;
  3004. if (objc_only) {
  3005. StringRef SectName;
  3006. ((*(info->Sections))[SectIdx]).getName(SectName);
  3007. DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
  3008. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  3009. if (SegName != "__OBJC" && SectName != "__cstring")
  3010. continue;
  3011. }
  3012. if (Address >= SectAddress && Address < SectAddress + SectSize) {
  3013. S = (*(info->Sections))[SectIdx];
  3014. offset = Address - SectAddress;
  3015. left = SectSize - offset;
  3016. StringRef SectContents = unwrapOrError(
  3017. ((*(info->Sections))[SectIdx]).getContents(), info->O->getFileName());
  3018. return SectContents.data() + offset;
  3019. }
  3020. }
  3021. return nullptr;
  3022. }
  3023. static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
  3024. uint32_t &left, SectionRef &S,
  3025. DisassembleInfo *info,
  3026. bool objc_only = false) {
  3027. return get_pointer_64(Address, offset, left, S, info, objc_only);
  3028. }
  3029. // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
  3030. // the symbol indirectly through n_value. Based on the relocation information
  3031. // for the specified section offset in the specified section reference.
  3032. // If no relocation information is found and a non-zero ReferenceValue for the
  3033. // symbol is passed, look up that address in the info's AddrMap.
  3034. static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
  3035. DisassembleInfo *info, uint64_t &n_value,
  3036. uint64_t ReferenceValue = 0) {
  3037. n_value = 0;
  3038. if (!info->verbose)
  3039. return nullptr;
  3040. // See if there is an external relocation entry at the sect_offset.
  3041. bool reloc_found = false;
  3042. DataRefImpl Rel;
  3043. MachO::any_relocation_info RE;
  3044. bool isExtern = false;
  3045. SymbolRef Symbol;
  3046. for (const RelocationRef &Reloc : S.relocations()) {
  3047. uint64_t RelocOffset = Reloc.getOffset();
  3048. if (RelocOffset == sect_offset) {
  3049. Rel = Reloc.getRawDataRefImpl();
  3050. RE = info->O->getRelocation(Rel);
  3051. if (info->O->isRelocationScattered(RE))
  3052. continue;
  3053. isExtern = info->O->getPlainRelocationExternal(RE);
  3054. if (isExtern) {
  3055. symbol_iterator RelocSym = Reloc.getSymbol();
  3056. Symbol = *RelocSym;
  3057. }
  3058. reloc_found = true;
  3059. break;
  3060. }
  3061. }
  3062. // If there is an external relocation entry for a symbol in this section
  3063. // at this section_offset then use that symbol's value for the n_value
  3064. // and return its name.
  3065. const char *SymbolName = nullptr;
  3066. if (reloc_found && isExtern) {
  3067. n_value = Symbol.getValue();
  3068. StringRef Name = unwrapOrError(Symbol.getName(), info->O->getFileName());
  3069. if (!Name.empty()) {
  3070. SymbolName = Name.data();
  3071. return SymbolName;
  3072. }
  3073. }
  3074. // TODO: For fully linked images, look through the external relocation
  3075. // entries off the dynamic symtab command. For these the r_offset is from the
  3076. // start of the first writeable segment in the Mach-O file. So the offset
  3077. // to this section from that segment is passed to this routine by the caller,
  3078. // as the database_offset. Which is the difference of the section's starting
  3079. // address and the first writable segment.
  3080. //
  3081. // NOTE: need add passing the database_offset to this routine.
  3082. // We did not find an external relocation entry so look up the ReferenceValue
  3083. // as an address of a symbol and if found return that symbol's name.
  3084. SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
  3085. return SymbolName;
  3086. }
  3087. static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
  3088. DisassembleInfo *info,
  3089. uint32_t ReferenceValue) {
  3090. uint64_t n_value64;
  3091. return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
  3092. }
  3093. // These are structs in the Objective-C meta data and read to produce the
  3094. // comments for disassembly. While these are part of the ABI they are no
  3095. // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
  3096. // .
  3097. // The cfstring object in a 64-bit Mach-O file.
  3098. struct cfstring64_t {
  3099. uint64_t isa; // class64_t * (64-bit pointer)
  3100. uint64_t flags; // flag bits
  3101. uint64_t characters; // char * (64-bit pointer)
  3102. uint64_t length; // number of non-NULL characters in above
  3103. };
  3104. // The class object in a 64-bit Mach-O file.
  3105. struct class64_t {
  3106. uint64_t isa; // class64_t * (64-bit pointer)
  3107. uint64_t superclass; // class64_t * (64-bit pointer)
  3108. uint64_t cache; // Cache (64-bit pointer)
  3109. uint64_t vtable; // IMP * (64-bit pointer)
  3110. uint64_t data; // class_ro64_t * (64-bit pointer)
  3111. };
  3112. struct class32_t {
  3113. uint32_t isa; /* class32_t * (32-bit pointer) */
  3114. uint32_t superclass; /* class32_t * (32-bit pointer) */
  3115. uint32_t cache; /* Cache (32-bit pointer) */
  3116. uint32_t vtable; /* IMP * (32-bit pointer) */
  3117. uint32_t data; /* class_ro32_t * (32-bit pointer) */
  3118. };
  3119. struct class_ro64_t {
  3120. uint32_t flags;
  3121. uint32_t instanceStart;
  3122. uint32_t instanceSize;
  3123. uint32_t reserved;
  3124. uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
  3125. uint64_t name; // const char * (64-bit pointer)
  3126. uint64_t baseMethods; // const method_list_t * (64-bit pointer)
  3127. uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
  3128. uint64_t ivars; // const ivar_list_t * (64-bit pointer)
  3129. uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
  3130. uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
  3131. };
  3132. struct class_ro32_t {
  3133. uint32_t flags;
  3134. uint32_t instanceStart;
  3135. uint32_t instanceSize;
  3136. uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
  3137. uint32_t name; /* const char * (32-bit pointer) */
  3138. uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
  3139. uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
  3140. uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
  3141. uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
  3142. uint32_t baseProperties; /* const struct objc_property_list *
  3143. (32-bit pointer) */
  3144. };
  3145. /* Values for class_ro{64,32}_t->flags */
  3146. #define RO_META (1 << 0)
  3147. #define RO_ROOT (1 << 1)
  3148. #define RO_HAS_CXX_STRUCTORS (1 << 2)
  3149. struct method_list64_t {
  3150. uint32_t entsize;
  3151. uint32_t count;
  3152. /* struct method64_t first; These structures follow inline */
  3153. };
  3154. struct method_list32_t {
  3155. uint32_t entsize;
  3156. uint32_t count;
  3157. /* struct method32_t first; These structures follow inline */
  3158. };
  3159. struct method64_t {
  3160. uint64_t name; /* SEL (64-bit pointer) */
  3161. uint64_t types; /* const char * (64-bit pointer) */
  3162. uint64_t imp; /* IMP (64-bit pointer) */
  3163. };
  3164. struct method32_t {
  3165. uint32_t name; /* SEL (32-bit pointer) */
  3166. uint32_t types; /* const char * (32-bit pointer) */
  3167. uint32_t imp; /* IMP (32-bit pointer) */
  3168. };
  3169. struct protocol_list64_t {
  3170. uint64_t count; /* uintptr_t (a 64-bit value) */
  3171. /* struct protocol64_t * list[0]; These pointers follow inline */
  3172. };
  3173. struct protocol_list32_t {
  3174. uint32_t count; /* uintptr_t (a 32-bit value) */
  3175. /* struct protocol32_t * list[0]; These pointers follow inline */
  3176. };
  3177. struct protocol64_t {
  3178. uint64_t isa; /* id * (64-bit pointer) */
  3179. uint64_t name; /* const char * (64-bit pointer) */
  3180. uint64_t protocols; /* struct protocol_list64_t *
  3181. (64-bit pointer) */
  3182. uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
  3183. uint64_t classMethods; /* method_list_t * (64-bit pointer) */
  3184. uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
  3185. uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
  3186. uint64_t instanceProperties; /* struct objc_property_list *
  3187. (64-bit pointer) */
  3188. };
  3189. struct protocol32_t {
  3190. uint32_t isa; /* id * (32-bit pointer) */
  3191. uint32_t name; /* const char * (32-bit pointer) */
  3192. uint32_t protocols; /* struct protocol_list_t *
  3193. (32-bit pointer) */
  3194. uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
  3195. uint32_t classMethods; /* method_list_t * (32-bit pointer) */
  3196. uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
  3197. uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
  3198. uint32_t instanceProperties; /* struct objc_property_list *
  3199. (32-bit pointer) */
  3200. };
  3201. struct ivar_list64_t {
  3202. uint32_t entsize;
  3203. uint32_t count;
  3204. /* struct ivar64_t first; These structures follow inline */
  3205. };
  3206. struct ivar_list32_t {
  3207. uint32_t entsize;
  3208. uint32_t count;
  3209. /* struct ivar32_t first; These structures follow inline */
  3210. };
  3211. struct ivar64_t {
  3212. uint64_t offset; /* uintptr_t * (64-bit pointer) */
  3213. uint64_t name; /* const char * (64-bit pointer) */
  3214. uint64_t type; /* const char * (64-bit pointer) */
  3215. uint32_t alignment;
  3216. uint32_t size;
  3217. };
  3218. struct ivar32_t {
  3219. uint32_t offset; /* uintptr_t * (32-bit pointer) */
  3220. uint32_t name; /* const char * (32-bit pointer) */
  3221. uint32_t type; /* const char * (32-bit pointer) */
  3222. uint32_t alignment;
  3223. uint32_t size;
  3224. };
  3225. struct objc_property_list64 {
  3226. uint32_t entsize;
  3227. uint32_t count;
  3228. /* struct objc_property64 first; These structures follow inline */
  3229. };
  3230. struct objc_property_list32 {
  3231. uint32_t entsize;
  3232. uint32_t count;
  3233. /* struct objc_property32 first; These structures follow inline */
  3234. };
  3235. struct objc_property64 {
  3236. uint64_t name; /* const char * (64-bit pointer) */
  3237. uint64_t attributes; /* const char * (64-bit pointer) */
  3238. };
  3239. struct objc_property32 {
  3240. uint32_t name; /* const char * (32-bit pointer) */
  3241. uint32_t attributes; /* const char * (32-bit pointer) */
  3242. };
  3243. struct category64_t {
  3244. uint64_t name; /* const char * (64-bit pointer) */
  3245. uint64_t cls; /* struct class_t * (64-bit pointer) */
  3246. uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
  3247. uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
  3248. uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
  3249. uint64_t instanceProperties; /* struct objc_property_list *
  3250. (64-bit pointer) */
  3251. };
  3252. struct category32_t {
  3253. uint32_t name; /* const char * (32-bit pointer) */
  3254. uint32_t cls; /* struct class_t * (32-bit pointer) */
  3255. uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
  3256. uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
  3257. uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
  3258. uint32_t instanceProperties; /* struct objc_property_list *
  3259. (32-bit pointer) */
  3260. };
  3261. struct objc_image_info64 {
  3262. uint32_t version;
  3263. uint32_t flags;
  3264. };
  3265. struct objc_image_info32 {
  3266. uint32_t version;
  3267. uint32_t flags;
  3268. };
  3269. struct imageInfo_t {
  3270. uint32_t version;
  3271. uint32_t flags;
  3272. };
  3273. /* masks for objc_image_info.flags */
  3274. #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
  3275. #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
  3276. #define OBJC_IMAGE_IS_SIMULATED (1 << 5)
  3277. #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6)
  3278. struct message_ref64 {
  3279. uint64_t imp; /* IMP (64-bit pointer) */
  3280. uint64_t sel; /* SEL (64-bit pointer) */
  3281. };
  3282. struct message_ref32 {
  3283. uint32_t imp; /* IMP (32-bit pointer) */
  3284. uint32_t sel; /* SEL (32-bit pointer) */
  3285. };
  3286. // Objective-C 1 (32-bit only) meta data structs.
  3287. struct objc_module_t {
  3288. uint32_t version;
  3289. uint32_t size;
  3290. uint32_t name; /* char * (32-bit pointer) */
  3291. uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
  3292. };
  3293. struct objc_symtab_t {
  3294. uint32_t sel_ref_cnt;
  3295. uint32_t refs; /* SEL * (32-bit pointer) */
  3296. uint16_t cls_def_cnt;
  3297. uint16_t cat_def_cnt;
  3298. // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
  3299. };
  3300. struct objc_class_t {
  3301. uint32_t isa; /* struct objc_class * (32-bit pointer) */
  3302. uint32_t super_class; /* struct objc_class * (32-bit pointer) */
  3303. uint32_t name; /* const char * (32-bit pointer) */
  3304. int32_t version;
  3305. int32_t info;
  3306. int32_t instance_size;
  3307. uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
  3308. uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
  3309. uint32_t cache; /* struct objc_cache * (32-bit pointer) */
  3310. uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
  3311. };
  3312. #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
  3313. // class is not a metaclass
  3314. #define CLS_CLASS 0x1
  3315. // class is a metaclass
  3316. #define CLS_META 0x2
  3317. struct objc_category_t {
  3318. uint32_t category_name; /* char * (32-bit pointer) */
  3319. uint32_t class_name; /* char * (32-bit pointer) */
  3320. uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
  3321. uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
  3322. uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
  3323. };
  3324. struct objc_ivar_t {
  3325. uint32_t ivar_name; /* char * (32-bit pointer) */
  3326. uint32_t ivar_type; /* char * (32-bit pointer) */
  3327. int32_t ivar_offset;
  3328. };
  3329. struct objc_ivar_list_t {
  3330. int32_t ivar_count;
  3331. // struct objc_ivar_t ivar_list[1]; /* variable length structure */
  3332. };
  3333. struct objc_method_list_t {
  3334. uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
  3335. int32_t method_count;
  3336. // struct objc_method_t method_list[1]; /* variable length structure */
  3337. };
  3338. struct objc_method_t {
  3339. uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
  3340. uint32_t method_types; /* char * (32-bit pointer) */
  3341. uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
  3342. (32-bit pointer) */
  3343. };
  3344. struct objc_protocol_list_t {
  3345. uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
  3346. int32_t count;
  3347. // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
  3348. // (32-bit pointer) */
  3349. };
  3350. struct objc_protocol_t {
  3351. uint32_t isa; /* struct objc_class * (32-bit pointer) */
  3352. uint32_t protocol_name; /* char * (32-bit pointer) */
  3353. uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
  3354. uint32_t instance_methods; /* struct objc_method_description_list *
  3355. (32-bit pointer) */
  3356. uint32_t class_methods; /* struct objc_method_description_list *
  3357. (32-bit pointer) */
  3358. };
  3359. struct objc_method_description_list_t {
  3360. int32_t count;
  3361. // struct objc_method_description_t list[1];
  3362. };
  3363. struct objc_method_description_t {
  3364. uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
  3365. uint32_t types; /* char * (32-bit pointer) */
  3366. };
  3367. inline void swapStruct(struct cfstring64_t &cfs) {
  3368. sys::swapByteOrder(cfs.isa);
  3369. sys::swapByteOrder(cfs.flags);
  3370. sys::swapByteOrder(cfs.characters);
  3371. sys::swapByteOrder(cfs.length);
  3372. }
  3373. inline void swapStruct(struct class64_t &c) {
  3374. sys::swapByteOrder(c.isa);
  3375. sys::swapByteOrder(c.superclass);
  3376. sys::swapByteOrder(c.cache);
  3377. sys::swapByteOrder(c.vtable);
  3378. sys::swapByteOrder(c.data);
  3379. }
  3380. inline void swapStruct(struct class32_t &c) {
  3381. sys::swapByteOrder(c.isa);
  3382. sys::swapByteOrder(c.superclass);
  3383. sys::swapByteOrder(c.cache);
  3384. sys::swapByteOrder(c.vtable);
  3385. sys::swapByteOrder(c.data);
  3386. }
  3387. inline void swapStruct(struct class_ro64_t &cro) {
  3388. sys::swapByteOrder(cro.flags);
  3389. sys::swapByteOrder(cro.instanceStart);
  3390. sys::swapByteOrder(cro.instanceSize);
  3391. sys::swapByteOrder(cro.reserved);
  3392. sys::swapByteOrder(cro.ivarLayout);
  3393. sys::swapByteOrder(cro.name);
  3394. sys::swapByteOrder(cro.baseMethods);
  3395. sys::swapByteOrder(cro.baseProtocols);
  3396. sys::swapByteOrder(cro.ivars);
  3397. sys::swapByteOrder(cro.weakIvarLayout);
  3398. sys::swapByteOrder(cro.baseProperties);
  3399. }
  3400. inline void swapStruct(struct class_ro32_t &cro) {
  3401. sys::swapByteOrder(cro.flags);
  3402. sys::swapByteOrder(cro.instanceStart);
  3403. sys::swapByteOrder(cro.instanceSize);
  3404. sys::swapByteOrder(cro.ivarLayout);
  3405. sys::swapByteOrder(cro.name);
  3406. sys::swapByteOrder(cro.baseMethods);
  3407. sys::swapByteOrder(cro.baseProtocols);
  3408. sys::swapByteOrder(cro.ivars);
  3409. sys::swapByteOrder(cro.weakIvarLayout);
  3410. sys::swapByteOrder(cro.baseProperties);
  3411. }
  3412. inline void swapStruct(struct method_list64_t &ml) {
  3413. sys::swapByteOrder(ml.entsize);
  3414. sys::swapByteOrder(ml.count);
  3415. }
  3416. inline void swapStruct(struct method_list32_t &ml) {
  3417. sys::swapByteOrder(ml.entsize);
  3418. sys::swapByteOrder(ml.count);
  3419. }
  3420. inline void swapStruct(struct method64_t &m) {
  3421. sys::swapByteOrder(m.name);
  3422. sys::swapByteOrder(m.types);
  3423. sys::swapByteOrder(m.imp);
  3424. }
  3425. inline void swapStruct(struct method32_t &m) {
  3426. sys::swapByteOrder(m.name);
  3427. sys::swapByteOrder(m.types);
  3428. sys::swapByteOrder(m.imp);
  3429. }
  3430. inline void swapStruct(struct protocol_list64_t &pl) {
  3431. sys::swapByteOrder(pl.count);
  3432. }
  3433. inline void swapStruct(struct protocol_list32_t &pl) {
  3434. sys::swapByteOrder(pl.count);
  3435. }
  3436. inline void swapStruct(struct protocol64_t &p) {
  3437. sys::swapByteOrder(p.isa);
  3438. sys::swapByteOrder(p.name);
  3439. sys::swapByteOrder(p.protocols);
  3440. sys::swapByteOrder(p.instanceMethods);
  3441. sys::swapByteOrder(p.classMethods);
  3442. sys::swapByteOrder(p.optionalInstanceMethods);
  3443. sys::swapByteOrder(p.optionalClassMethods);
  3444. sys::swapByteOrder(p.instanceProperties);
  3445. }
  3446. inline void swapStruct(struct protocol32_t &p) {
  3447. sys::swapByteOrder(p.isa);
  3448. sys::swapByteOrder(p.name);
  3449. sys::swapByteOrder(p.protocols);
  3450. sys::swapByteOrder(p.instanceMethods);
  3451. sys::swapByteOrder(p.classMethods);
  3452. sys::swapByteOrder(p.optionalInstanceMethods);
  3453. sys::swapByteOrder(p.optionalClassMethods);
  3454. sys::swapByteOrder(p.instanceProperties);
  3455. }
  3456. inline void swapStruct(struct ivar_list64_t &il) {
  3457. sys::swapByteOrder(il.entsize);
  3458. sys::swapByteOrder(il.count);
  3459. }
  3460. inline void swapStruct(struct ivar_list32_t &il) {
  3461. sys::swapByteOrder(il.entsize);
  3462. sys::swapByteOrder(il.count);
  3463. }
  3464. inline void swapStruct(struct ivar64_t &i) {
  3465. sys::swapByteOrder(i.offset);
  3466. sys::swapByteOrder(i.name);
  3467. sys::swapByteOrder(i.type);
  3468. sys::swapByteOrder(i.alignment);
  3469. sys::swapByteOrder(i.size);
  3470. }
  3471. inline void swapStruct(struct ivar32_t &i) {
  3472. sys::swapByteOrder(i.offset);
  3473. sys::swapByteOrder(i.name);
  3474. sys::swapByteOrder(i.type);
  3475. sys::swapByteOrder(i.alignment);
  3476. sys::swapByteOrder(i.size);
  3477. }
  3478. inline void swapStruct(struct objc_property_list64 &pl) {
  3479. sys::swapByteOrder(pl.entsize);
  3480. sys::swapByteOrder(pl.count);
  3481. }
  3482. inline void swapStruct(struct objc_property_list32 &pl) {
  3483. sys::swapByteOrder(pl.entsize);
  3484. sys::swapByteOrder(pl.count);
  3485. }
  3486. inline void swapStruct(struct objc_property64 &op) {
  3487. sys::swapByteOrder(op.name);
  3488. sys::swapByteOrder(op.attributes);
  3489. }
  3490. inline void swapStruct(struct objc_property32 &op) {
  3491. sys::swapByteOrder(op.name);
  3492. sys::swapByteOrder(op.attributes);
  3493. }
  3494. inline void swapStruct(struct category64_t &c) {
  3495. sys::swapByteOrder(c.name);
  3496. sys::swapByteOrder(c.cls);
  3497. sys::swapByteOrder(c.instanceMethods);
  3498. sys::swapByteOrder(c.classMethods);
  3499. sys::swapByteOrder(c.protocols);
  3500. sys::swapByteOrder(c.instanceProperties);
  3501. }
  3502. inline void swapStruct(struct category32_t &c) {
  3503. sys::swapByteOrder(c.name);
  3504. sys::swapByteOrder(c.cls);
  3505. sys::swapByteOrder(c.instanceMethods);
  3506. sys::swapByteOrder(c.classMethods);
  3507. sys::swapByteOrder(c.protocols);
  3508. sys::swapByteOrder(c.instanceProperties);
  3509. }
  3510. inline void swapStruct(struct objc_image_info64 &o) {
  3511. sys::swapByteOrder(o.version);
  3512. sys::swapByteOrder(o.flags);
  3513. }
  3514. inline void swapStruct(struct objc_image_info32 &o) {
  3515. sys::swapByteOrder(o.version);
  3516. sys::swapByteOrder(o.flags);
  3517. }
  3518. inline void swapStruct(struct imageInfo_t &o) {
  3519. sys::swapByteOrder(o.version);
  3520. sys::swapByteOrder(o.flags);
  3521. }
  3522. inline void swapStruct(struct message_ref64 &mr) {
  3523. sys::swapByteOrder(mr.imp);
  3524. sys::swapByteOrder(mr.sel);
  3525. }
  3526. inline void swapStruct(struct message_ref32 &mr) {
  3527. sys::swapByteOrder(mr.imp);
  3528. sys::swapByteOrder(mr.sel);
  3529. }
  3530. inline void swapStruct(struct objc_module_t &module) {
  3531. sys::swapByteOrder(module.version);
  3532. sys::swapByteOrder(module.size);
  3533. sys::swapByteOrder(module.name);
  3534. sys::swapByteOrder(module.symtab);
  3535. }
  3536. inline void swapStruct(struct objc_symtab_t &symtab) {
  3537. sys::swapByteOrder(symtab.sel_ref_cnt);
  3538. sys::swapByteOrder(symtab.refs);
  3539. sys::swapByteOrder(symtab.cls_def_cnt);
  3540. sys::swapByteOrder(symtab.cat_def_cnt);
  3541. }
  3542. inline void swapStruct(struct objc_class_t &objc_class) {
  3543. sys::swapByteOrder(objc_class.isa);
  3544. sys::swapByteOrder(objc_class.super_class);
  3545. sys::swapByteOrder(objc_class.name);
  3546. sys::swapByteOrder(objc_class.version);
  3547. sys::swapByteOrder(objc_class.info);
  3548. sys::swapByteOrder(objc_class.instance_size);
  3549. sys::swapByteOrder(objc_class.ivars);
  3550. sys::swapByteOrder(objc_class.methodLists);
  3551. sys::swapByteOrder(objc_class.cache);
  3552. sys::swapByteOrder(objc_class.protocols);
  3553. }
  3554. inline void swapStruct(struct objc_category_t &objc_category) {
  3555. sys::swapByteOrder(objc_category.category_name);
  3556. sys::swapByteOrder(objc_category.class_name);
  3557. sys::swapByteOrder(objc_category.instance_methods);
  3558. sys::swapByteOrder(objc_category.class_methods);
  3559. sys::swapByteOrder(objc_category.protocols);
  3560. }
  3561. inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
  3562. sys::swapByteOrder(objc_ivar_list.ivar_count);
  3563. }
  3564. inline void swapStruct(struct objc_ivar_t &objc_ivar) {
  3565. sys::swapByteOrder(objc_ivar.ivar_name);
  3566. sys::swapByteOrder(objc_ivar.ivar_type);
  3567. sys::swapByteOrder(objc_ivar.ivar_offset);
  3568. }
  3569. inline void swapStruct(struct objc_method_list_t &method_list) {
  3570. sys::swapByteOrder(method_list.obsolete);
  3571. sys::swapByteOrder(method_list.method_count);
  3572. }
  3573. inline void swapStruct(struct objc_method_t &method) {
  3574. sys::swapByteOrder(method.method_name);
  3575. sys::swapByteOrder(method.method_types);
  3576. sys::swapByteOrder(method.method_imp);
  3577. }
  3578. inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
  3579. sys::swapByteOrder(protocol_list.next);
  3580. sys::swapByteOrder(protocol_list.count);
  3581. }
  3582. inline void swapStruct(struct objc_protocol_t &protocol) {
  3583. sys::swapByteOrder(protocol.isa);
  3584. sys::swapByteOrder(protocol.protocol_name);
  3585. sys::swapByteOrder(protocol.protocol_list);
  3586. sys::swapByteOrder(protocol.instance_methods);
  3587. sys::swapByteOrder(protocol.class_methods);
  3588. }
  3589. inline void swapStruct(struct objc_method_description_list_t &mdl) {
  3590. sys::swapByteOrder(mdl.count);
  3591. }
  3592. inline void swapStruct(struct objc_method_description_t &md) {
  3593. sys::swapByteOrder(md.name);
  3594. sys::swapByteOrder(md.types);
  3595. }
  3596. static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
  3597. struct DisassembleInfo *info);
  3598. // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
  3599. // to an Objective-C class and returns the class name. It is also passed the
  3600. // address of the pointer, so when the pointer is zero as it can be in an .o
  3601. // file, that is used to look for an external relocation entry with a symbol
  3602. // name.
  3603. static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
  3604. uint64_t ReferenceValue,
  3605. struct DisassembleInfo *info) {
  3606. const char *r;
  3607. uint32_t offset, left;
  3608. SectionRef S;
  3609. // The pointer_value can be 0 in an object file and have a relocation
  3610. // entry for the class symbol at the ReferenceValue (the address of the
  3611. // pointer).
  3612. if (pointer_value == 0) {
  3613. r = get_pointer_64(ReferenceValue, offset, left, S, info);
  3614. if (r == nullptr || left < sizeof(uint64_t))
  3615. return nullptr;
  3616. uint64_t n_value;
  3617. const char *symbol_name = get_symbol_64(offset, S, info, n_value);
  3618. if (symbol_name == nullptr)
  3619. return nullptr;
  3620. const char *class_name = strrchr(symbol_name, '$');
  3621. if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
  3622. return class_name + 2;
  3623. else
  3624. return nullptr;
  3625. }
  3626. // The case were the pointer_value is non-zero and points to a class defined
  3627. // in this Mach-O file.
  3628. r = get_pointer_64(pointer_value, offset, left, S, info);
  3629. if (r == nullptr || left < sizeof(struct class64_t))
  3630. return nullptr;
  3631. struct class64_t c;
  3632. memcpy(&c, r, sizeof(struct class64_t));
  3633. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3634. swapStruct(c);
  3635. if (c.data == 0)
  3636. return nullptr;
  3637. r = get_pointer_64(c.data, offset, left, S, info);
  3638. if (r == nullptr || left < sizeof(struct class_ro64_t))
  3639. return nullptr;
  3640. struct class_ro64_t cro;
  3641. memcpy(&cro, r, sizeof(struct class_ro64_t));
  3642. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3643. swapStruct(cro);
  3644. if (cro.name == 0)
  3645. return nullptr;
  3646. const char *name = get_pointer_64(cro.name, offset, left, S, info);
  3647. return name;
  3648. }
  3649. // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
  3650. // pointer to a cfstring and returns its name or nullptr.
  3651. static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
  3652. struct DisassembleInfo *info) {
  3653. const char *r, *name;
  3654. uint32_t offset, left;
  3655. SectionRef S;
  3656. struct cfstring64_t cfs;
  3657. uint64_t cfs_characters;
  3658. r = get_pointer_64(ReferenceValue, offset, left, S, info);
  3659. if (r == nullptr || left < sizeof(struct cfstring64_t))
  3660. return nullptr;
  3661. memcpy(&cfs, r, sizeof(struct cfstring64_t));
  3662. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3663. swapStruct(cfs);
  3664. if (cfs.characters == 0) {
  3665. uint64_t n_value;
  3666. const char *symbol_name = get_symbol_64(
  3667. offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
  3668. if (symbol_name == nullptr)
  3669. return nullptr;
  3670. cfs_characters = n_value;
  3671. } else
  3672. cfs_characters = cfs.characters;
  3673. name = get_pointer_64(cfs_characters, offset, left, S, info);
  3674. return name;
  3675. }
  3676. // get_objc2_64bit_selref() is used for disassembly and is passed a the address
  3677. // of a pointer to an Objective-C selector reference when the pointer value is
  3678. // zero as in a .o file and is likely to have a external relocation entry with
  3679. // who's symbol's n_value is the real pointer to the selector name. If that is
  3680. // the case the real pointer to the selector name is returned else 0 is
  3681. // returned
  3682. static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
  3683. struct DisassembleInfo *info) {
  3684. uint32_t offset, left;
  3685. SectionRef S;
  3686. const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
  3687. if (r == nullptr || left < sizeof(uint64_t))
  3688. return 0;
  3689. uint64_t n_value;
  3690. const char *symbol_name = get_symbol_64(offset, S, info, n_value);
  3691. if (symbol_name == nullptr)
  3692. return 0;
  3693. return n_value;
  3694. }
  3695. static const SectionRef get_section(MachOObjectFile *O, const char *segname,
  3696. const char *sectname) {
  3697. for (const SectionRef &Section : O->sections()) {
  3698. StringRef SectName;
  3699. Section.getName(SectName);
  3700. DataRefImpl Ref = Section.getRawDataRefImpl();
  3701. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  3702. if (SegName == segname && SectName == sectname)
  3703. return Section;
  3704. }
  3705. return SectionRef();
  3706. }
  3707. static void
  3708. walk_pointer_list_64(const char *listname, const SectionRef S,
  3709. MachOObjectFile *O, struct DisassembleInfo *info,
  3710. void (*func)(uint64_t, struct DisassembleInfo *info)) {
  3711. if (S == SectionRef())
  3712. return;
  3713. StringRef SectName;
  3714. S.getName(SectName);
  3715. DataRefImpl Ref = S.getRawDataRefImpl();
  3716. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  3717. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  3718. StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
  3719. const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
  3720. for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
  3721. uint32_t left = S.getSize() - i;
  3722. uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
  3723. uint64_t p = 0;
  3724. memcpy(&p, Contents + i, size);
  3725. if (i + sizeof(uint64_t) > S.getSize())
  3726. outs() << listname << " list pointer extends past end of (" << SegName
  3727. << "," << SectName << ") section\n";
  3728. outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
  3729. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  3730. sys::swapByteOrder(p);
  3731. uint64_t n_value = 0;
  3732. const char *name = get_symbol_64(i, S, info, n_value, p);
  3733. if (name == nullptr)
  3734. name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
  3735. if (n_value != 0) {
  3736. outs() << format("0x%" PRIx64, n_value);
  3737. if (p != 0)
  3738. outs() << " + " << format("0x%" PRIx64, p);
  3739. } else
  3740. outs() << format("0x%" PRIx64, p);
  3741. if (name != nullptr)
  3742. outs() << " " << name;
  3743. outs() << "\n";
  3744. p += n_value;
  3745. if (func)
  3746. func(p, info);
  3747. }
  3748. }
  3749. static void
  3750. walk_pointer_list_32(const char *listname, const SectionRef S,
  3751. MachOObjectFile *O, struct DisassembleInfo *info,
  3752. void (*func)(uint32_t, struct DisassembleInfo *info)) {
  3753. if (S == SectionRef())
  3754. return;
  3755. StringRef SectName;
  3756. S.getName(SectName);
  3757. DataRefImpl Ref = S.getRawDataRefImpl();
  3758. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  3759. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  3760. StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
  3761. const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
  3762. for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
  3763. uint32_t left = S.getSize() - i;
  3764. uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
  3765. uint32_t p = 0;
  3766. memcpy(&p, Contents + i, size);
  3767. if (i + sizeof(uint32_t) > S.getSize())
  3768. outs() << listname << " list pointer extends past end of (" << SegName
  3769. << "," << SectName << ") section\n";
  3770. uint32_t Address = S.getAddress() + i;
  3771. outs() << format("%08" PRIx32, Address) << " ";
  3772. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  3773. sys::swapByteOrder(p);
  3774. outs() << format("0x%" PRIx32, p);
  3775. const char *name = get_symbol_32(i, S, info, p);
  3776. if (name != nullptr)
  3777. outs() << " " << name;
  3778. outs() << "\n";
  3779. if (func)
  3780. func(p, info);
  3781. }
  3782. }
  3783. static void print_layout_map(const char *layout_map, uint32_t left) {
  3784. if (layout_map == nullptr)
  3785. return;
  3786. outs() << " layout map: ";
  3787. do {
  3788. outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
  3789. left--;
  3790. layout_map++;
  3791. } while (*layout_map != '\0' && left != 0);
  3792. outs() << "\n";
  3793. }
  3794. static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
  3795. uint32_t offset, left;
  3796. SectionRef S;
  3797. const char *layout_map;
  3798. if (p == 0)
  3799. return;
  3800. layout_map = get_pointer_64(p, offset, left, S, info);
  3801. print_layout_map(layout_map, left);
  3802. }
  3803. static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
  3804. uint32_t offset, left;
  3805. SectionRef S;
  3806. const char *layout_map;
  3807. if (p == 0)
  3808. return;
  3809. layout_map = get_pointer_32(p, offset, left, S, info);
  3810. print_layout_map(layout_map, left);
  3811. }
  3812. static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
  3813. const char *indent) {
  3814. struct method_list64_t ml;
  3815. struct method64_t m;
  3816. const char *r;
  3817. uint32_t offset, xoffset, left, i;
  3818. SectionRef S, xS;
  3819. const char *name, *sym_name;
  3820. uint64_t n_value;
  3821. r = get_pointer_64(p, offset, left, S, info);
  3822. if (r == nullptr)
  3823. return;
  3824. memset(&ml, '\0', sizeof(struct method_list64_t));
  3825. if (left < sizeof(struct method_list64_t)) {
  3826. memcpy(&ml, r, left);
  3827. outs() << " (method_list_t entends past the end of the section)\n";
  3828. } else
  3829. memcpy(&ml, r, sizeof(struct method_list64_t));
  3830. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3831. swapStruct(ml);
  3832. outs() << indent << "\t\t entsize " << ml.entsize << "\n";
  3833. outs() << indent << "\t\t count " << ml.count << "\n";
  3834. p += sizeof(struct method_list64_t);
  3835. offset += sizeof(struct method_list64_t);
  3836. for (i = 0; i < ml.count; i++) {
  3837. r = get_pointer_64(p, offset, left, S, info);
  3838. if (r == nullptr)
  3839. return;
  3840. memset(&m, '\0', sizeof(struct method64_t));
  3841. if (left < sizeof(struct method64_t)) {
  3842. memcpy(&m, r, left);
  3843. outs() << indent << " (method_t extends past the end of the section)\n";
  3844. } else
  3845. memcpy(&m, r, sizeof(struct method64_t));
  3846. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3847. swapStruct(m);
  3848. outs() << indent << "\t\t name ";
  3849. sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
  3850. info, n_value, m.name);
  3851. if (n_value != 0) {
  3852. if (info->verbose && sym_name != nullptr)
  3853. outs() << sym_name;
  3854. else
  3855. outs() << format("0x%" PRIx64, n_value);
  3856. if (m.name != 0)
  3857. outs() << " + " << format("0x%" PRIx64, m.name);
  3858. } else
  3859. outs() << format("0x%" PRIx64, m.name);
  3860. name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
  3861. if (name != nullptr)
  3862. outs() << format(" %.*s", left, name);
  3863. outs() << "\n";
  3864. outs() << indent << "\t\t types ";
  3865. sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
  3866. info, n_value, m.types);
  3867. if (n_value != 0) {
  3868. if (info->verbose && sym_name != nullptr)
  3869. outs() << sym_name;
  3870. else
  3871. outs() << format("0x%" PRIx64, n_value);
  3872. if (m.types != 0)
  3873. outs() << " + " << format("0x%" PRIx64, m.types);
  3874. } else
  3875. outs() << format("0x%" PRIx64, m.types);
  3876. name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
  3877. if (name != nullptr)
  3878. outs() << format(" %.*s", left, name);
  3879. outs() << "\n";
  3880. outs() << indent << "\t\t imp ";
  3881. name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
  3882. n_value, m.imp);
  3883. if (info->verbose && name == nullptr) {
  3884. if (n_value != 0) {
  3885. outs() << format("0x%" PRIx64, n_value) << " ";
  3886. if (m.imp != 0)
  3887. outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
  3888. } else
  3889. outs() << format("0x%" PRIx64, m.imp) << " ";
  3890. }
  3891. if (name != nullptr)
  3892. outs() << name;
  3893. outs() << "\n";
  3894. p += sizeof(struct method64_t);
  3895. offset += sizeof(struct method64_t);
  3896. }
  3897. }
  3898. static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
  3899. const char *indent) {
  3900. struct method_list32_t ml;
  3901. struct method32_t m;
  3902. const char *r, *name;
  3903. uint32_t offset, xoffset, left, i;
  3904. SectionRef S, xS;
  3905. r = get_pointer_32(p, offset, left, S, info);
  3906. if (r == nullptr)
  3907. return;
  3908. memset(&ml, '\0', sizeof(struct method_list32_t));
  3909. if (left < sizeof(struct method_list32_t)) {
  3910. memcpy(&ml, r, left);
  3911. outs() << " (method_list_t entends past the end of the section)\n";
  3912. } else
  3913. memcpy(&ml, r, sizeof(struct method_list32_t));
  3914. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3915. swapStruct(ml);
  3916. outs() << indent << "\t\t entsize " << ml.entsize << "\n";
  3917. outs() << indent << "\t\t count " << ml.count << "\n";
  3918. p += sizeof(struct method_list32_t);
  3919. offset += sizeof(struct method_list32_t);
  3920. for (i = 0; i < ml.count; i++) {
  3921. r = get_pointer_32(p, offset, left, S, info);
  3922. if (r == nullptr)
  3923. return;
  3924. memset(&m, '\0', sizeof(struct method32_t));
  3925. if (left < sizeof(struct method32_t)) {
  3926. memcpy(&ml, r, left);
  3927. outs() << indent << " (method_t entends past the end of the section)\n";
  3928. } else
  3929. memcpy(&m, r, sizeof(struct method32_t));
  3930. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3931. swapStruct(m);
  3932. outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
  3933. name = get_pointer_32(m.name, xoffset, left, xS, info);
  3934. if (name != nullptr)
  3935. outs() << format(" %.*s", left, name);
  3936. outs() << "\n";
  3937. outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
  3938. name = get_pointer_32(m.types, xoffset, left, xS, info);
  3939. if (name != nullptr)
  3940. outs() << format(" %.*s", left, name);
  3941. outs() << "\n";
  3942. outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
  3943. name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
  3944. m.imp);
  3945. if (name != nullptr)
  3946. outs() << " " << name;
  3947. outs() << "\n";
  3948. p += sizeof(struct method32_t);
  3949. offset += sizeof(struct method32_t);
  3950. }
  3951. }
  3952. static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
  3953. uint32_t offset, left, xleft;
  3954. SectionRef S;
  3955. struct objc_method_list_t method_list;
  3956. struct objc_method_t method;
  3957. const char *r, *methods, *name, *SymbolName;
  3958. int32_t i;
  3959. r = get_pointer_32(p, offset, left, S, info, true);
  3960. if (r == nullptr)
  3961. return true;
  3962. outs() << "\n";
  3963. if (left > sizeof(struct objc_method_list_t)) {
  3964. memcpy(&method_list, r, sizeof(struct objc_method_list_t));
  3965. } else {
  3966. outs() << "\t\t objc_method_list extends past end of the section\n";
  3967. memset(&method_list, '\0', sizeof(struct objc_method_list_t));
  3968. memcpy(&method_list, r, left);
  3969. }
  3970. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3971. swapStruct(method_list);
  3972. outs() << "\t\t obsolete "
  3973. << format("0x%08" PRIx32, method_list.obsolete) << "\n";
  3974. outs() << "\t\t method_count " << method_list.method_count << "\n";
  3975. methods = r + sizeof(struct objc_method_list_t);
  3976. for (i = 0; i < method_list.method_count; i++) {
  3977. if ((i + 1) * sizeof(struct objc_method_t) > left) {
  3978. outs() << "\t\t remaining method's extend past the of the section\n";
  3979. break;
  3980. }
  3981. memcpy(&method, methods + i * sizeof(struct objc_method_t),
  3982. sizeof(struct objc_method_t));
  3983. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3984. swapStruct(method);
  3985. outs() << "\t\t method_name "
  3986. << format("0x%08" PRIx32, method.method_name);
  3987. if (info->verbose) {
  3988. name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
  3989. if (name != nullptr)
  3990. outs() << format(" %.*s", xleft, name);
  3991. else
  3992. outs() << " (not in an __OBJC section)";
  3993. }
  3994. outs() << "\n";
  3995. outs() << "\t\t method_types "
  3996. << format("0x%08" PRIx32, method.method_types);
  3997. if (info->verbose) {
  3998. name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
  3999. if (name != nullptr)
  4000. outs() << format(" %.*s", xleft, name);
  4001. else
  4002. outs() << " (not in an __OBJC section)";
  4003. }
  4004. outs() << "\n";
  4005. outs() << "\t\t method_imp "
  4006. << format("0x%08" PRIx32, method.method_imp) << " ";
  4007. if (info->verbose) {
  4008. SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
  4009. if (SymbolName != nullptr)
  4010. outs() << SymbolName;
  4011. }
  4012. outs() << "\n";
  4013. }
  4014. return false;
  4015. }
  4016. static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
  4017. struct protocol_list64_t pl;
  4018. uint64_t q, n_value;
  4019. struct protocol64_t pc;
  4020. const char *r;
  4021. uint32_t offset, xoffset, left, i;
  4022. SectionRef S, xS;
  4023. const char *name, *sym_name;
  4024. r = get_pointer_64(p, offset, left, S, info);
  4025. if (r == nullptr)
  4026. return;
  4027. memset(&pl, '\0', sizeof(struct protocol_list64_t));
  4028. if (left < sizeof(struct protocol_list64_t)) {
  4029. memcpy(&pl, r, left);
  4030. outs() << " (protocol_list_t entends past the end of the section)\n";
  4031. } else
  4032. memcpy(&pl, r, sizeof(struct protocol_list64_t));
  4033. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4034. swapStruct(pl);
  4035. outs() << " count " << pl.count << "\n";
  4036. p += sizeof(struct protocol_list64_t);
  4037. offset += sizeof(struct protocol_list64_t);
  4038. for (i = 0; i < pl.count; i++) {
  4039. r = get_pointer_64(p, offset, left, S, info);
  4040. if (r == nullptr)
  4041. return;
  4042. q = 0;
  4043. if (left < sizeof(uint64_t)) {
  4044. memcpy(&q, r, left);
  4045. outs() << " (protocol_t * entends past the end of the section)\n";
  4046. } else
  4047. memcpy(&q, r, sizeof(uint64_t));
  4048. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4049. sys::swapByteOrder(q);
  4050. outs() << "\t\t list[" << i << "] ";
  4051. sym_name = get_symbol_64(offset, S, info, n_value, q);
  4052. if (n_value != 0) {
  4053. if (info->verbose && sym_name != nullptr)
  4054. outs() << sym_name;
  4055. else
  4056. outs() << format("0x%" PRIx64, n_value);
  4057. if (q != 0)
  4058. outs() << " + " << format("0x%" PRIx64, q);
  4059. } else
  4060. outs() << format("0x%" PRIx64, q);
  4061. outs() << " (struct protocol_t *)\n";
  4062. r = get_pointer_64(q + n_value, offset, left, S, info);
  4063. if (r == nullptr)
  4064. return;
  4065. memset(&pc, '\0', sizeof(struct protocol64_t));
  4066. if (left < sizeof(struct protocol64_t)) {
  4067. memcpy(&pc, r, left);
  4068. outs() << " (protocol_t entends past the end of the section)\n";
  4069. } else
  4070. memcpy(&pc, r, sizeof(struct protocol64_t));
  4071. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4072. swapStruct(pc);
  4073. outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
  4074. outs() << "\t\t\t name ";
  4075. sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
  4076. info, n_value, pc.name);
  4077. if (n_value != 0) {
  4078. if (info->verbose && sym_name != nullptr)
  4079. outs() << sym_name;
  4080. else
  4081. outs() << format("0x%" PRIx64, n_value);
  4082. if (pc.name != 0)
  4083. outs() << " + " << format("0x%" PRIx64, pc.name);
  4084. } else
  4085. outs() << format("0x%" PRIx64, pc.name);
  4086. name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
  4087. if (name != nullptr)
  4088. outs() << format(" %.*s", left, name);
  4089. outs() << "\n";
  4090. outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
  4091. outs() << "\t\t instanceMethods ";
  4092. sym_name =
  4093. get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
  4094. S, info, n_value, pc.instanceMethods);
  4095. if (n_value != 0) {
  4096. if (info->verbose && sym_name != nullptr)
  4097. outs() << sym_name;
  4098. else
  4099. outs() << format("0x%" PRIx64, n_value);
  4100. if (pc.instanceMethods != 0)
  4101. outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
  4102. } else
  4103. outs() << format("0x%" PRIx64, pc.instanceMethods);
  4104. outs() << " (struct method_list_t *)\n";
  4105. if (pc.instanceMethods + n_value != 0)
  4106. print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
  4107. outs() << "\t\t classMethods ";
  4108. sym_name =
  4109. get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
  4110. info, n_value, pc.classMethods);
  4111. if (n_value != 0) {
  4112. if (info->verbose && sym_name != nullptr)
  4113. outs() << sym_name;
  4114. else
  4115. outs() << format("0x%" PRIx64, n_value);
  4116. if (pc.classMethods != 0)
  4117. outs() << " + " << format("0x%" PRIx64, pc.classMethods);
  4118. } else
  4119. outs() << format("0x%" PRIx64, pc.classMethods);
  4120. outs() << " (struct method_list_t *)\n";
  4121. if (pc.classMethods + n_value != 0)
  4122. print_method_list64_t(pc.classMethods + n_value, info, "\t");
  4123. outs() << "\t optionalInstanceMethods "
  4124. << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
  4125. outs() << "\t optionalClassMethods "
  4126. << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
  4127. outs() << "\t instanceProperties "
  4128. << format("0x%" PRIx64, pc.instanceProperties) << "\n";
  4129. p += sizeof(uint64_t);
  4130. offset += sizeof(uint64_t);
  4131. }
  4132. }
  4133. static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
  4134. struct protocol_list32_t pl;
  4135. uint32_t q;
  4136. struct protocol32_t pc;
  4137. const char *r;
  4138. uint32_t offset, xoffset, left, i;
  4139. SectionRef S, xS;
  4140. const char *name;
  4141. r = get_pointer_32(p, offset, left, S, info);
  4142. if (r == nullptr)
  4143. return;
  4144. memset(&pl, '\0', sizeof(struct protocol_list32_t));
  4145. if (left < sizeof(struct protocol_list32_t)) {
  4146. memcpy(&pl, r, left);
  4147. outs() << " (protocol_list_t entends past the end of the section)\n";
  4148. } else
  4149. memcpy(&pl, r, sizeof(struct protocol_list32_t));
  4150. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4151. swapStruct(pl);
  4152. outs() << " count " << pl.count << "\n";
  4153. p += sizeof(struct protocol_list32_t);
  4154. offset += sizeof(struct protocol_list32_t);
  4155. for (i = 0; i < pl.count; i++) {
  4156. r = get_pointer_32(p, offset, left, S, info);
  4157. if (r == nullptr)
  4158. return;
  4159. q = 0;
  4160. if (left < sizeof(uint32_t)) {
  4161. memcpy(&q, r, left);
  4162. outs() << " (protocol_t * entends past the end of the section)\n";
  4163. } else
  4164. memcpy(&q, r, sizeof(uint32_t));
  4165. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4166. sys::swapByteOrder(q);
  4167. outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
  4168. << " (struct protocol_t *)\n";
  4169. r = get_pointer_32(q, offset, left, S, info);
  4170. if (r == nullptr)
  4171. return;
  4172. memset(&pc, '\0', sizeof(struct protocol32_t));
  4173. if (left < sizeof(struct protocol32_t)) {
  4174. memcpy(&pc, r, left);
  4175. outs() << " (protocol_t entends past the end of the section)\n";
  4176. } else
  4177. memcpy(&pc, r, sizeof(struct protocol32_t));
  4178. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4179. swapStruct(pc);
  4180. outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
  4181. outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
  4182. name = get_pointer_32(pc.name, xoffset, left, xS, info);
  4183. if (name != nullptr)
  4184. outs() << format(" %.*s", left, name);
  4185. outs() << "\n";
  4186. outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
  4187. outs() << "\t\t instanceMethods "
  4188. << format("0x%" PRIx32, pc.instanceMethods)
  4189. << " (struct method_list_t *)\n";
  4190. if (pc.instanceMethods != 0)
  4191. print_method_list32_t(pc.instanceMethods, info, "\t");
  4192. outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
  4193. << " (struct method_list_t *)\n";
  4194. if (pc.classMethods != 0)
  4195. print_method_list32_t(pc.classMethods, info, "\t");
  4196. outs() << "\t optionalInstanceMethods "
  4197. << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
  4198. outs() << "\t optionalClassMethods "
  4199. << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
  4200. outs() << "\t instanceProperties "
  4201. << format("0x%" PRIx32, pc.instanceProperties) << "\n";
  4202. p += sizeof(uint32_t);
  4203. offset += sizeof(uint32_t);
  4204. }
  4205. }
  4206. static void print_indent(uint32_t indent) {
  4207. for (uint32_t i = 0; i < indent;) {
  4208. if (indent - i >= 8) {
  4209. outs() << "\t";
  4210. i += 8;
  4211. } else {
  4212. for (uint32_t j = i; j < indent; j++)
  4213. outs() << " ";
  4214. return;
  4215. }
  4216. }
  4217. }
  4218. static bool print_method_description_list(uint32_t p, uint32_t indent,
  4219. struct DisassembleInfo *info) {
  4220. uint32_t offset, left, xleft;
  4221. SectionRef S;
  4222. struct objc_method_description_list_t mdl;
  4223. struct objc_method_description_t md;
  4224. const char *r, *list, *name;
  4225. int32_t i;
  4226. r = get_pointer_32(p, offset, left, S, info, true);
  4227. if (r == nullptr)
  4228. return true;
  4229. outs() << "\n";
  4230. if (left > sizeof(struct objc_method_description_list_t)) {
  4231. memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
  4232. } else {
  4233. print_indent(indent);
  4234. outs() << " objc_method_description_list extends past end of the section\n";
  4235. memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
  4236. memcpy(&mdl, r, left);
  4237. }
  4238. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4239. swapStruct(mdl);
  4240. print_indent(indent);
  4241. outs() << " count " << mdl.count << "\n";
  4242. list = r + sizeof(struct objc_method_description_list_t);
  4243. for (i = 0; i < mdl.count; i++) {
  4244. if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
  4245. print_indent(indent);
  4246. outs() << " remaining list entries extend past the of the section\n";
  4247. break;
  4248. }
  4249. print_indent(indent);
  4250. outs() << " list[" << i << "]\n";
  4251. memcpy(&md, list + i * sizeof(struct objc_method_description_t),
  4252. sizeof(struct objc_method_description_t));
  4253. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4254. swapStruct(md);
  4255. print_indent(indent);
  4256. outs() << " name " << format("0x%08" PRIx32, md.name);
  4257. if (info->verbose) {
  4258. name = get_pointer_32(md.name, offset, xleft, S, info, true);
  4259. if (name != nullptr)
  4260. outs() << format(" %.*s", xleft, name);
  4261. else
  4262. outs() << " (not in an __OBJC section)";
  4263. }
  4264. outs() << "\n";
  4265. print_indent(indent);
  4266. outs() << " types " << format("0x%08" PRIx32, md.types);
  4267. if (info->verbose) {
  4268. name = get_pointer_32(md.types, offset, xleft, S, info, true);
  4269. if (name != nullptr)
  4270. outs() << format(" %.*s", xleft, name);
  4271. else
  4272. outs() << " (not in an __OBJC section)";
  4273. }
  4274. outs() << "\n";
  4275. }
  4276. return false;
  4277. }
  4278. static bool print_protocol_list(uint32_t p, uint32_t indent,
  4279. struct DisassembleInfo *info);
  4280. static bool print_protocol(uint32_t p, uint32_t indent,
  4281. struct DisassembleInfo *info) {
  4282. uint32_t offset, left;
  4283. SectionRef S;
  4284. struct objc_protocol_t protocol;
  4285. const char *r, *name;
  4286. r = get_pointer_32(p, offset, left, S, info, true);
  4287. if (r == nullptr)
  4288. return true;
  4289. outs() << "\n";
  4290. if (left >= sizeof(struct objc_protocol_t)) {
  4291. memcpy(&protocol, r, sizeof(struct objc_protocol_t));
  4292. } else {
  4293. print_indent(indent);
  4294. outs() << " Protocol extends past end of the section\n";
  4295. memset(&protocol, '\0', sizeof(struct objc_protocol_t));
  4296. memcpy(&protocol, r, left);
  4297. }
  4298. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4299. swapStruct(protocol);
  4300. print_indent(indent);
  4301. outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
  4302. << "\n";
  4303. print_indent(indent);
  4304. outs() << " protocol_name "
  4305. << format("0x%08" PRIx32, protocol.protocol_name);
  4306. if (info->verbose) {
  4307. name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
  4308. if (name != nullptr)
  4309. outs() << format(" %.*s", left, name);
  4310. else
  4311. outs() << " (not in an __OBJC section)";
  4312. }
  4313. outs() << "\n";
  4314. print_indent(indent);
  4315. outs() << " protocol_list "
  4316. << format("0x%08" PRIx32, protocol.protocol_list);
  4317. if (print_protocol_list(protocol.protocol_list, indent + 4, info))
  4318. outs() << " (not in an __OBJC section)\n";
  4319. print_indent(indent);
  4320. outs() << " instance_methods "
  4321. << format("0x%08" PRIx32, protocol.instance_methods);
  4322. if (print_method_description_list(protocol.instance_methods, indent, info))
  4323. outs() << " (not in an __OBJC section)\n";
  4324. print_indent(indent);
  4325. outs() << " class_methods "
  4326. << format("0x%08" PRIx32, protocol.class_methods);
  4327. if (print_method_description_list(protocol.class_methods, indent, info))
  4328. outs() << " (not in an __OBJC section)\n";
  4329. return false;
  4330. }
  4331. static bool print_protocol_list(uint32_t p, uint32_t indent,
  4332. struct DisassembleInfo *info) {
  4333. uint32_t offset, left, l;
  4334. SectionRef S;
  4335. struct objc_protocol_list_t protocol_list;
  4336. const char *r, *list;
  4337. int32_t i;
  4338. r = get_pointer_32(p, offset, left, S, info, true);
  4339. if (r == nullptr)
  4340. return true;
  4341. outs() << "\n";
  4342. if (left > sizeof(struct objc_protocol_list_t)) {
  4343. memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
  4344. } else {
  4345. outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
  4346. memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
  4347. memcpy(&protocol_list, r, left);
  4348. }
  4349. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4350. swapStruct(protocol_list);
  4351. print_indent(indent);
  4352. outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
  4353. << "\n";
  4354. print_indent(indent);
  4355. outs() << " count " << protocol_list.count << "\n";
  4356. list = r + sizeof(struct objc_protocol_list_t);
  4357. for (i = 0; i < protocol_list.count; i++) {
  4358. if ((i + 1) * sizeof(uint32_t) > left) {
  4359. outs() << "\t\t remaining list entries extend past the of the section\n";
  4360. break;
  4361. }
  4362. memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
  4363. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4364. sys::swapByteOrder(l);
  4365. print_indent(indent);
  4366. outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
  4367. if (print_protocol(l, indent, info))
  4368. outs() << "(not in an __OBJC section)\n";
  4369. }
  4370. return false;
  4371. }
  4372. static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
  4373. struct ivar_list64_t il;
  4374. struct ivar64_t i;
  4375. const char *r;
  4376. uint32_t offset, xoffset, left, j;
  4377. SectionRef S, xS;
  4378. const char *name, *sym_name, *ivar_offset_p;
  4379. uint64_t ivar_offset, n_value;
  4380. r = get_pointer_64(p, offset, left, S, info);
  4381. if (r == nullptr)
  4382. return;
  4383. memset(&il, '\0', sizeof(struct ivar_list64_t));
  4384. if (left < sizeof(struct ivar_list64_t)) {
  4385. memcpy(&il, r, left);
  4386. outs() << " (ivar_list_t entends past the end of the section)\n";
  4387. } else
  4388. memcpy(&il, r, sizeof(struct ivar_list64_t));
  4389. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4390. swapStruct(il);
  4391. outs() << " entsize " << il.entsize << "\n";
  4392. outs() << " count " << il.count << "\n";
  4393. p += sizeof(struct ivar_list64_t);
  4394. offset += sizeof(struct ivar_list64_t);
  4395. for (j = 0; j < il.count; j++) {
  4396. r = get_pointer_64(p, offset, left, S, info);
  4397. if (r == nullptr)
  4398. return;
  4399. memset(&i, '\0', sizeof(struct ivar64_t));
  4400. if (left < sizeof(struct ivar64_t)) {
  4401. memcpy(&i, r, left);
  4402. outs() << " (ivar_t entends past the end of the section)\n";
  4403. } else
  4404. memcpy(&i, r, sizeof(struct ivar64_t));
  4405. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4406. swapStruct(i);
  4407. outs() << "\t\t\t offset ";
  4408. sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
  4409. info, n_value, i.offset);
  4410. if (n_value != 0) {
  4411. if (info->verbose && sym_name != nullptr)
  4412. outs() << sym_name;
  4413. else
  4414. outs() << format("0x%" PRIx64, n_value);
  4415. if (i.offset != 0)
  4416. outs() << " + " << format("0x%" PRIx64, i.offset);
  4417. } else
  4418. outs() << format("0x%" PRIx64, i.offset);
  4419. ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
  4420. if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
  4421. memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
  4422. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4423. sys::swapByteOrder(ivar_offset);
  4424. outs() << " " << ivar_offset << "\n";
  4425. } else
  4426. outs() << "\n";
  4427. outs() << "\t\t\t name ";
  4428. sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
  4429. n_value, i.name);
  4430. if (n_value != 0) {
  4431. if (info->verbose && sym_name != nullptr)
  4432. outs() << sym_name;
  4433. else
  4434. outs() << format("0x%" PRIx64, n_value);
  4435. if (i.name != 0)
  4436. outs() << " + " << format("0x%" PRIx64, i.name);
  4437. } else
  4438. outs() << format("0x%" PRIx64, i.name);
  4439. name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
  4440. if (name != nullptr)
  4441. outs() << format(" %.*s", left, name);
  4442. outs() << "\n";
  4443. outs() << "\t\t\t type ";
  4444. sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
  4445. n_value, i.name);
  4446. name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
  4447. if (n_value != 0) {
  4448. if (info->verbose && sym_name != nullptr)
  4449. outs() << sym_name;
  4450. else
  4451. outs() << format("0x%" PRIx64, n_value);
  4452. if (i.type != 0)
  4453. outs() << " + " << format("0x%" PRIx64, i.type);
  4454. } else
  4455. outs() << format("0x%" PRIx64, i.type);
  4456. if (name != nullptr)
  4457. outs() << format(" %.*s", left, name);
  4458. outs() << "\n";
  4459. outs() << "\t\t\talignment " << i.alignment << "\n";
  4460. outs() << "\t\t\t size " << i.size << "\n";
  4461. p += sizeof(struct ivar64_t);
  4462. offset += sizeof(struct ivar64_t);
  4463. }
  4464. }
  4465. static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
  4466. struct ivar_list32_t il;
  4467. struct ivar32_t i;
  4468. const char *r;
  4469. uint32_t offset, xoffset, left, j;
  4470. SectionRef S, xS;
  4471. const char *name, *ivar_offset_p;
  4472. uint32_t ivar_offset;
  4473. r = get_pointer_32(p, offset, left, S, info);
  4474. if (r == nullptr)
  4475. return;
  4476. memset(&il, '\0', sizeof(struct ivar_list32_t));
  4477. if (left < sizeof(struct ivar_list32_t)) {
  4478. memcpy(&il, r, left);
  4479. outs() << " (ivar_list_t entends past the end of the section)\n";
  4480. } else
  4481. memcpy(&il, r, sizeof(struct ivar_list32_t));
  4482. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4483. swapStruct(il);
  4484. outs() << " entsize " << il.entsize << "\n";
  4485. outs() << " count " << il.count << "\n";
  4486. p += sizeof(struct ivar_list32_t);
  4487. offset += sizeof(struct ivar_list32_t);
  4488. for (j = 0; j < il.count; j++) {
  4489. r = get_pointer_32(p, offset, left, S, info);
  4490. if (r == nullptr)
  4491. return;
  4492. memset(&i, '\0', sizeof(struct ivar32_t));
  4493. if (left < sizeof(struct ivar32_t)) {
  4494. memcpy(&i, r, left);
  4495. outs() << " (ivar_t entends past the end of the section)\n";
  4496. } else
  4497. memcpy(&i, r, sizeof(struct ivar32_t));
  4498. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4499. swapStruct(i);
  4500. outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
  4501. ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
  4502. if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
  4503. memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
  4504. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4505. sys::swapByteOrder(ivar_offset);
  4506. outs() << " " << ivar_offset << "\n";
  4507. } else
  4508. outs() << "\n";
  4509. outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
  4510. name = get_pointer_32(i.name, xoffset, left, xS, info);
  4511. if (name != nullptr)
  4512. outs() << format(" %.*s", left, name);
  4513. outs() << "\n";
  4514. outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
  4515. name = get_pointer_32(i.type, xoffset, left, xS, info);
  4516. if (name != nullptr)
  4517. outs() << format(" %.*s", left, name);
  4518. outs() << "\n";
  4519. outs() << "\t\t\talignment " << i.alignment << "\n";
  4520. outs() << "\t\t\t size " << i.size << "\n";
  4521. p += sizeof(struct ivar32_t);
  4522. offset += sizeof(struct ivar32_t);
  4523. }
  4524. }
  4525. static void print_objc_property_list64(uint64_t p,
  4526. struct DisassembleInfo *info) {
  4527. struct objc_property_list64 opl;
  4528. struct objc_property64 op;
  4529. const char *r;
  4530. uint32_t offset, xoffset, left, j;
  4531. SectionRef S, xS;
  4532. const char *name, *sym_name;
  4533. uint64_t n_value;
  4534. r = get_pointer_64(p, offset, left, S, info);
  4535. if (r == nullptr)
  4536. return;
  4537. memset(&opl, '\0', sizeof(struct objc_property_list64));
  4538. if (left < sizeof(struct objc_property_list64)) {
  4539. memcpy(&opl, r, left);
  4540. outs() << " (objc_property_list entends past the end of the section)\n";
  4541. } else
  4542. memcpy(&opl, r, sizeof(struct objc_property_list64));
  4543. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4544. swapStruct(opl);
  4545. outs() << " entsize " << opl.entsize << "\n";
  4546. outs() << " count " << opl.count << "\n";
  4547. p += sizeof(struct objc_property_list64);
  4548. offset += sizeof(struct objc_property_list64);
  4549. for (j = 0; j < opl.count; j++) {
  4550. r = get_pointer_64(p, offset, left, S, info);
  4551. if (r == nullptr)
  4552. return;
  4553. memset(&op, '\0', sizeof(struct objc_property64));
  4554. if (left < sizeof(struct objc_property64)) {
  4555. memcpy(&op, r, left);
  4556. outs() << " (objc_property entends past the end of the section)\n";
  4557. } else
  4558. memcpy(&op, r, sizeof(struct objc_property64));
  4559. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4560. swapStruct(op);
  4561. outs() << "\t\t\t name ";
  4562. sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
  4563. info, n_value, op.name);
  4564. if (n_value != 0) {
  4565. if (info->verbose && sym_name != nullptr)
  4566. outs() << sym_name;
  4567. else
  4568. outs() << format("0x%" PRIx64, n_value);
  4569. if (op.name != 0)
  4570. outs() << " + " << format("0x%" PRIx64, op.name);
  4571. } else
  4572. outs() << format("0x%" PRIx64, op.name);
  4573. name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
  4574. if (name != nullptr)
  4575. outs() << format(" %.*s", left, name);
  4576. outs() << "\n";
  4577. outs() << "\t\t\tattributes ";
  4578. sym_name =
  4579. get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
  4580. info, n_value, op.attributes);
  4581. if (n_value != 0) {
  4582. if (info->verbose && sym_name != nullptr)
  4583. outs() << sym_name;
  4584. else
  4585. outs() << format("0x%" PRIx64, n_value);
  4586. if (op.attributes != 0)
  4587. outs() << " + " << format("0x%" PRIx64, op.attributes);
  4588. } else
  4589. outs() << format("0x%" PRIx64, op.attributes);
  4590. name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
  4591. if (name != nullptr)
  4592. outs() << format(" %.*s", left, name);
  4593. outs() << "\n";
  4594. p += sizeof(struct objc_property64);
  4595. offset += sizeof(struct objc_property64);
  4596. }
  4597. }
  4598. static void print_objc_property_list32(uint32_t p,
  4599. struct DisassembleInfo *info) {
  4600. struct objc_property_list32 opl;
  4601. struct objc_property32 op;
  4602. const char *r;
  4603. uint32_t offset, xoffset, left, j;
  4604. SectionRef S, xS;
  4605. const char *name;
  4606. r = get_pointer_32(p, offset, left, S, info);
  4607. if (r == nullptr)
  4608. return;
  4609. memset(&opl, '\0', sizeof(struct objc_property_list32));
  4610. if (left < sizeof(struct objc_property_list32)) {
  4611. memcpy(&opl, r, left);
  4612. outs() << " (objc_property_list entends past the end of the section)\n";
  4613. } else
  4614. memcpy(&opl, r, sizeof(struct objc_property_list32));
  4615. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4616. swapStruct(opl);
  4617. outs() << " entsize " << opl.entsize << "\n";
  4618. outs() << " count " << opl.count << "\n";
  4619. p += sizeof(struct objc_property_list32);
  4620. offset += sizeof(struct objc_property_list32);
  4621. for (j = 0; j < opl.count; j++) {
  4622. r = get_pointer_32(p, offset, left, S, info);
  4623. if (r == nullptr)
  4624. return;
  4625. memset(&op, '\0', sizeof(struct objc_property32));
  4626. if (left < sizeof(struct objc_property32)) {
  4627. memcpy(&op, r, left);
  4628. outs() << " (objc_property entends past the end of the section)\n";
  4629. } else
  4630. memcpy(&op, r, sizeof(struct objc_property32));
  4631. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4632. swapStruct(op);
  4633. outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
  4634. name = get_pointer_32(op.name, xoffset, left, xS, info);
  4635. if (name != nullptr)
  4636. outs() << format(" %.*s", left, name);
  4637. outs() << "\n";
  4638. outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
  4639. name = get_pointer_32(op.attributes, xoffset, left, xS, info);
  4640. if (name != nullptr)
  4641. outs() << format(" %.*s", left, name);
  4642. outs() << "\n";
  4643. p += sizeof(struct objc_property32);
  4644. offset += sizeof(struct objc_property32);
  4645. }
  4646. }
  4647. static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
  4648. bool &is_meta_class) {
  4649. struct class_ro64_t cro;
  4650. const char *r;
  4651. uint32_t offset, xoffset, left;
  4652. SectionRef S, xS;
  4653. const char *name, *sym_name;
  4654. uint64_t n_value;
  4655. r = get_pointer_64(p, offset, left, S, info);
  4656. if (r == nullptr || left < sizeof(struct class_ro64_t))
  4657. return false;
  4658. memcpy(&cro, r, sizeof(struct class_ro64_t));
  4659. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4660. swapStruct(cro);
  4661. outs() << " flags " << format("0x%" PRIx32, cro.flags);
  4662. if (cro.flags & RO_META)
  4663. outs() << " RO_META";
  4664. if (cro.flags & RO_ROOT)
  4665. outs() << " RO_ROOT";
  4666. if (cro.flags & RO_HAS_CXX_STRUCTORS)
  4667. outs() << " RO_HAS_CXX_STRUCTORS";
  4668. outs() << "\n";
  4669. outs() << " instanceStart " << cro.instanceStart << "\n";
  4670. outs() << " instanceSize " << cro.instanceSize << "\n";
  4671. outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
  4672. << "\n";
  4673. outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
  4674. << "\n";
  4675. print_layout_map64(cro.ivarLayout, info);
  4676. outs() << " name ";
  4677. sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
  4678. info, n_value, cro.name);
  4679. if (n_value != 0) {
  4680. if (info->verbose && sym_name != nullptr)
  4681. outs() << sym_name;
  4682. else
  4683. outs() << format("0x%" PRIx64, n_value);
  4684. if (cro.name != 0)
  4685. outs() << " + " << format("0x%" PRIx64, cro.name);
  4686. } else
  4687. outs() << format("0x%" PRIx64, cro.name);
  4688. name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
  4689. if (name != nullptr)
  4690. outs() << format(" %.*s", left, name);
  4691. outs() << "\n";
  4692. outs() << " baseMethods ";
  4693. sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
  4694. S, info, n_value, cro.baseMethods);
  4695. if (n_value != 0) {
  4696. if (info->verbose && sym_name != nullptr)
  4697. outs() << sym_name;
  4698. else
  4699. outs() << format("0x%" PRIx64, n_value);
  4700. if (cro.baseMethods != 0)
  4701. outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
  4702. } else
  4703. outs() << format("0x%" PRIx64, cro.baseMethods);
  4704. outs() << " (struct method_list_t *)\n";
  4705. if (cro.baseMethods + n_value != 0)
  4706. print_method_list64_t(cro.baseMethods + n_value, info, "");
  4707. outs() << " baseProtocols ";
  4708. sym_name =
  4709. get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
  4710. info, n_value, cro.baseProtocols);
  4711. if (n_value != 0) {
  4712. if (info->verbose && sym_name != nullptr)
  4713. outs() << sym_name;
  4714. else
  4715. outs() << format("0x%" PRIx64, n_value);
  4716. if (cro.baseProtocols != 0)
  4717. outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
  4718. } else
  4719. outs() << format("0x%" PRIx64, cro.baseProtocols);
  4720. outs() << "\n";
  4721. if (cro.baseProtocols + n_value != 0)
  4722. print_protocol_list64_t(cro.baseProtocols + n_value, info);
  4723. outs() << " ivars ";
  4724. sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
  4725. info, n_value, cro.ivars);
  4726. if (n_value != 0) {
  4727. if (info->verbose && sym_name != nullptr)
  4728. outs() << sym_name;
  4729. else
  4730. outs() << format("0x%" PRIx64, n_value);
  4731. if (cro.ivars != 0)
  4732. outs() << " + " << format("0x%" PRIx64, cro.ivars);
  4733. } else
  4734. outs() << format("0x%" PRIx64, cro.ivars);
  4735. outs() << "\n";
  4736. if (cro.ivars + n_value != 0)
  4737. print_ivar_list64_t(cro.ivars + n_value, info);
  4738. outs() << " weakIvarLayout ";
  4739. sym_name =
  4740. get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
  4741. info, n_value, cro.weakIvarLayout);
  4742. if (n_value != 0) {
  4743. if (info->verbose && sym_name != nullptr)
  4744. outs() << sym_name;
  4745. else
  4746. outs() << format("0x%" PRIx64, n_value);
  4747. if (cro.weakIvarLayout != 0)
  4748. outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
  4749. } else
  4750. outs() << format("0x%" PRIx64, cro.weakIvarLayout);
  4751. outs() << "\n";
  4752. print_layout_map64(cro.weakIvarLayout + n_value, info);
  4753. outs() << " baseProperties ";
  4754. sym_name =
  4755. get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
  4756. info, n_value, cro.baseProperties);
  4757. if (n_value != 0) {
  4758. if (info->verbose && sym_name != nullptr)
  4759. outs() << sym_name;
  4760. else
  4761. outs() << format("0x%" PRIx64, n_value);
  4762. if (cro.baseProperties != 0)
  4763. outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
  4764. } else
  4765. outs() << format("0x%" PRIx64, cro.baseProperties);
  4766. outs() << "\n";
  4767. if (cro.baseProperties + n_value != 0)
  4768. print_objc_property_list64(cro.baseProperties + n_value, info);
  4769. is_meta_class = (cro.flags & RO_META) != 0;
  4770. return true;
  4771. }
  4772. static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
  4773. bool &is_meta_class) {
  4774. struct class_ro32_t cro;
  4775. const char *r;
  4776. uint32_t offset, xoffset, left;
  4777. SectionRef S, xS;
  4778. const char *name;
  4779. r = get_pointer_32(p, offset, left, S, info);
  4780. if (r == nullptr)
  4781. return false;
  4782. memset(&cro, '\0', sizeof(struct class_ro32_t));
  4783. if (left < sizeof(struct class_ro32_t)) {
  4784. memcpy(&cro, r, left);
  4785. outs() << " (class_ro_t entends past the end of the section)\n";
  4786. } else
  4787. memcpy(&cro, r, sizeof(struct class_ro32_t));
  4788. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4789. swapStruct(cro);
  4790. outs() << " flags " << format("0x%" PRIx32, cro.flags);
  4791. if (cro.flags & RO_META)
  4792. outs() << " RO_META";
  4793. if (cro.flags & RO_ROOT)
  4794. outs() << " RO_ROOT";
  4795. if (cro.flags & RO_HAS_CXX_STRUCTORS)
  4796. outs() << " RO_HAS_CXX_STRUCTORS";
  4797. outs() << "\n";
  4798. outs() << " instanceStart " << cro.instanceStart << "\n";
  4799. outs() << " instanceSize " << cro.instanceSize << "\n";
  4800. outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
  4801. << "\n";
  4802. print_layout_map32(cro.ivarLayout, info);
  4803. outs() << " name " << format("0x%" PRIx32, cro.name);
  4804. name = get_pointer_32(cro.name, xoffset, left, xS, info);
  4805. if (name != nullptr)
  4806. outs() << format(" %.*s", left, name);
  4807. outs() << "\n";
  4808. outs() << " baseMethods "
  4809. << format("0x%" PRIx32, cro.baseMethods)
  4810. << " (struct method_list_t *)\n";
  4811. if (cro.baseMethods != 0)
  4812. print_method_list32_t(cro.baseMethods, info, "");
  4813. outs() << " baseProtocols "
  4814. << format("0x%" PRIx32, cro.baseProtocols) << "\n";
  4815. if (cro.baseProtocols != 0)
  4816. print_protocol_list32_t(cro.baseProtocols, info);
  4817. outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
  4818. << "\n";
  4819. if (cro.ivars != 0)
  4820. print_ivar_list32_t(cro.ivars, info);
  4821. outs() << " weakIvarLayout "
  4822. << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
  4823. print_layout_map32(cro.weakIvarLayout, info);
  4824. outs() << " baseProperties "
  4825. << format("0x%" PRIx32, cro.baseProperties) << "\n";
  4826. if (cro.baseProperties != 0)
  4827. print_objc_property_list32(cro.baseProperties, info);
  4828. is_meta_class = (cro.flags & RO_META) != 0;
  4829. return true;
  4830. }
  4831. static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
  4832. struct class64_t c;
  4833. const char *r;
  4834. uint32_t offset, left;
  4835. SectionRef S;
  4836. const char *name;
  4837. uint64_t isa_n_value, n_value;
  4838. r = get_pointer_64(p, offset, left, S, info);
  4839. if (r == nullptr || left < sizeof(struct class64_t))
  4840. return;
  4841. memcpy(&c, r, sizeof(struct class64_t));
  4842. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4843. swapStruct(c);
  4844. outs() << " isa " << format("0x%" PRIx64, c.isa);
  4845. name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
  4846. isa_n_value, c.isa);
  4847. if (name != nullptr)
  4848. outs() << " " << name;
  4849. outs() << "\n";
  4850. outs() << " superclass " << format("0x%" PRIx64, c.superclass);
  4851. name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
  4852. n_value, c.superclass);
  4853. if (name != nullptr)
  4854. outs() << " " << name;
  4855. else {
  4856. name = get_dyld_bind_info_symbolname(S.getAddress() +
  4857. offset + offsetof(struct class64_t, superclass), info);
  4858. if (name != nullptr)
  4859. outs() << " " << name;
  4860. }
  4861. outs() << "\n";
  4862. outs() << " cache " << format("0x%" PRIx64, c.cache);
  4863. name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
  4864. n_value, c.cache);
  4865. if (name != nullptr)
  4866. outs() << " " << name;
  4867. outs() << "\n";
  4868. outs() << " vtable " << format("0x%" PRIx64, c.vtable);
  4869. name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
  4870. n_value, c.vtable);
  4871. if (name != nullptr)
  4872. outs() << " " << name;
  4873. outs() << "\n";
  4874. name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
  4875. n_value, c.data);
  4876. outs() << " data ";
  4877. if (n_value != 0) {
  4878. if (info->verbose && name != nullptr)
  4879. outs() << name;
  4880. else
  4881. outs() << format("0x%" PRIx64, n_value);
  4882. if (c.data != 0)
  4883. outs() << " + " << format("0x%" PRIx64, c.data);
  4884. } else
  4885. outs() << format("0x%" PRIx64, c.data);
  4886. outs() << " (struct class_ro_t *)";
  4887. // This is a Swift class if some of the low bits of the pointer are set.
  4888. if ((c.data + n_value) & 0x7)
  4889. outs() << " Swift class";
  4890. outs() << "\n";
  4891. bool is_meta_class;
  4892. if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
  4893. return;
  4894. if (!is_meta_class &&
  4895. c.isa + isa_n_value != p &&
  4896. c.isa + isa_n_value != 0 &&
  4897. info->depth < 100) {
  4898. info->depth++;
  4899. outs() << "Meta Class\n";
  4900. print_class64_t(c.isa + isa_n_value, info);
  4901. }
  4902. }
  4903. static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
  4904. struct class32_t c;
  4905. const char *r;
  4906. uint32_t offset, left;
  4907. SectionRef S;
  4908. const char *name;
  4909. r = get_pointer_32(p, offset, left, S, info);
  4910. if (r == nullptr)
  4911. return;
  4912. memset(&c, '\0', sizeof(struct class32_t));
  4913. if (left < sizeof(struct class32_t)) {
  4914. memcpy(&c, r, left);
  4915. outs() << " (class_t entends past the end of the section)\n";
  4916. } else
  4917. memcpy(&c, r, sizeof(struct class32_t));
  4918. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4919. swapStruct(c);
  4920. outs() << " isa " << format("0x%" PRIx32, c.isa);
  4921. name =
  4922. get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
  4923. if (name != nullptr)
  4924. outs() << " " << name;
  4925. outs() << "\n";
  4926. outs() << " superclass " << format("0x%" PRIx32, c.superclass);
  4927. name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
  4928. c.superclass);
  4929. if (name != nullptr)
  4930. outs() << " " << name;
  4931. outs() << "\n";
  4932. outs() << " cache " << format("0x%" PRIx32, c.cache);
  4933. name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
  4934. c.cache);
  4935. if (name != nullptr)
  4936. outs() << " " << name;
  4937. outs() << "\n";
  4938. outs() << " vtable " << format("0x%" PRIx32, c.vtable);
  4939. name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
  4940. c.vtable);
  4941. if (name != nullptr)
  4942. outs() << " " << name;
  4943. outs() << "\n";
  4944. name =
  4945. get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
  4946. outs() << " data " << format("0x%" PRIx32, c.data)
  4947. << " (struct class_ro_t *)";
  4948. // This is a Swift class if some of the low bits of the pointer are set.
  4949. if (c.data & 0x3)
  4950. outs() << " Swift class";
  4951. outs() << "\n";
  4952. bool is_meta_class;
  4953. if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
  4954. return;
  4955. if (!is_meta_class) {
  4956. outs() << "Meta Class\n";
  4957. print_class32_t(c.isa, info);
  4958. }
  4959. }
  4960. static void print_objc_class_t(struct objc_class_t *objc_class,
  4961. struct DisassembleInfo *info) {
  4962. uint32_t offset, left, xleft;
  4963. const char *name, *p, *ivar_list;
  4964. SectionRef S;
  4965. int32_t i;
  4966. struct objc_ivar_list_t objc_ivar_list;
  4967. struct objc_ivar_t ivar;
  4968. outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
  4969. if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
  4970. name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
  4971. if (name != nullptr)
  4972. outs() << format(" %.*s", left, name);
  4973. else
  4974. outs() << " (not in an __OBJC section)";
  4975. }
  4976. outs() << "\n";
  4977. outs() << "\t super_class "
  4978. << format("0x%08" PRIx32, objc_class->super_class);
  4979. if (info->verbose) {
  4980. name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
  4981. if (name != nullptr)
  4982. outs() << format(" %.*s", left, name);
  4983. else
  4984. outs() << " (not in an __OBJC section)";
  4985. }
  4986. outs() << "\n";
  4987. outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
  4988. if (info->verbose) {
  4989. name = get_pointer_32(objc_class->name, offset, left, S, info, true);
  4990. if (name != nullptr)
  4991. outs() << format(" %.*s", left, name);
  4992. else
  4993. outs() << " (not in an __OBJC section)";
  4994. }
  4995. outs() << "\n";
  4996. outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
  4997. << "\n";
  4998. outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
  4999. if (info->verbose) {
  5000. if (CLS_GETINFO(objc_class, CLS_CLASS))
  5001. outs() << " CLS_CLASS";
  5002. else if (CLS_GETINFO(objc_class, CLS_META))
  5003. outs() << " CLS_META";
  5004. }
  5005. outs() << "\n";
  5006. outs() << "\t instance_size "
  5007. << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
  5008. p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
  5009. outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
  5010. if (p != nullptr) {
  5011. if (left > sizeof(struct objc_ivar_list_t)) {
  5012. outs() << "\n";
  5013. memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
  5014. } else {
  5015. outs() << " (entends past the end of the section)\n";
  5016. memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
  5017. memcpy(&objc_ivar_list, p, left);
  5018. }
  5019. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  5020. swapStruct(objc_ivar_list);
  5021. outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
  5022. ivar_list = p + sizeof(struct objc_ivar_list_t);
  5023. for (i = 0; i < objc_ivar_list.ivar_count; i++) {
  5024. if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
  5025. outs() << "\t\t remaining ivar's extend past the of the section\n";
  5026. break;
  5027. }
  5028. memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
  5029. sizeof(struct objc_ivar_t));
  5030. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  5031. swapStruct(ivar);
  5032. outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
  5033. if (info->verbose) {
  5034. name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
  5035. if (name != nullptr)
  5036. outs() << format(" %.*s", xleft, name);
  5037. else
  5038. outs() << " (not in an __OBJC section)";
  5039. }
  5040. outs() << "\n";
  5041. outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
  5042. if (info->verbose) {
  5043. name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
  5044. if (name != nullptr)
  5045. outs() << format(" %.*s", xleft, name);
  5046. else
  5047. outs() << " (not in an __OBJC section)";
  5048. }
  5049. outs() << "\n";
  5050. outs() << "\t\t ivar_offset "
  5051. << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
  5052. }
  5053. } else {
  5054. outs() << " (not in an __OBJC section)\n";
  5055. }
  5056. outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
  5057. if (print_method_list(objc_class->methodLists, info))
  5058. outs() << " (not in an __OBJC section)\n";
  5059. outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
  5060. << "\n";
  5061. outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
  5062. if (print_protocol_list(objc_class->protocols, 16, info))
  5063. outs() << " (not in an __OBJC section)\n";
  5064. }
  5065. static void print_objc_objc_category_t(struct objc_category_t *objc_category,
  5066. struct DisassembleInfo *info) {
  5067. uint32_t offset, left;
  5068. const char *name;
  5069. SectionRef S;
  5070. outs() << "\t category name "
  5071. << format("0x%08" PRIx32, objc_category->category_name);
  5072. if (info->verbose) {
  5073. name = get_pointer_32(objc_category->category_name, offset, left, S, info,
  5074. true);
  5075. if (name != nullptr)
  5076. outs() << format(" %.*s", left, name);
  5077. else
  5078. outs() << " (not in an __OBJC section)";
  5079. }
  5080. outs() << "\n";
  5081. outs() << "\t\t class name "
  5082. << format("0x%08" PRIx32, objc_category->class_name);
  5083. if (info->verbose) {
  5084. name =
  5085. get_pointer_32(objc_category->class_name, offset, left, S, info, true);
  5086. if (name != nullptr)
  5087. outs() << format(" %.*s", left, name);
  5088. else
  5089. outs() << " (not in an __OBJC section)";
  5090. }
  5091. outs() << "\n";
  5092. outs() << "\t instance methods "
  5093. << format("0x%08" PRIx32, objc_category->instance_methods);
  5094. if (print_method_list(objc_category->instance_methods, info))
  5095. outs() << " (not in an __OBJC section)\n";
  5096. outs() << "\t class methods "
  5097. << format("0x%08" PRIx32, objc_category->class_methods);
  5098. if (print_method_list(objc_category->class_methods, info))
  5099. outs() << " (not in an __OBJC section)\n";
  5100. }
  5101. static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
  5102. struct category64_t c;
  5103. const char *r;
  5104. uint32_t offset, xoffset, left;
  5105. SectionRef S, xS;
  5106. const char *name, *sym_name;
  5107. uint64_t n_value;
  5108. r = get_pointer_64(p, offset, left, S, info);
  5109. if (r == nullptr)
  5110. return;
  5111. memset(&c, '\0', sizeof(struct category64_t));
  5112. if (left < sizeof(struct category64_t)) {
  5113. memcpy(&c, r, left);
  5114. outs() << " (category_t entends past the end of the section)\n";
  5115. } else
  5116. memcpy(&c, r, sizeof(struct category64_t));
  5117. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  5118. swapStruct(c);
  5119. outs() << " name ";
  5120. sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
  5121. info, n_value, c.name);
  5122. if (n_value != 0) {
  5123. if (info->verbose && sym_name != nullptr)
  5124. outs() << sym_name;
  5125. else
  5126. outs() << format("0x%" PRIx64, n_value);
  5127. if (c.name != 0)
  5128. outs() << " + " << format("0x%" PRIx64, c.name);
  5129. } else
  5130. outs() << format("0x%" PRIx64, c.name);
  5131. name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
  5132. if (name != nullptr)
  5133. outs() << format(" %.*s", left, name);
  5134. outs() << "\n";
  5135. outs() << " cls ";
  5136. sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
  5137. n_value, c.cls);
  5138. if (n_value != 0) {
  5139. if (info->verbose && sym_name != nullptr)
  5140. outs() << sym_name;
  5141. else
  5142. outs() << format("0x%" PRIx64, n_value);
  5143. if (c.cls != 0)
  5144. outs() << " + " << format("0x%" PRIx64, c.cls);
  5145. } else
  5146. outs() << format("0x%" PRIx64, c.cls);
  5147. outs() << "\n";
  5148. if (c.cls + n_value != 0)
  5149. print_class64_t(c.cls + n_value, info);
  5150. outs() << " instanceMethods ";
  5151. sym_name =
  5152. get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
  5153. info, n_value, c.instanceMethods);
  5154. if (n_value != 0) {
  5155. if (info->verbose && sym_name != nullptr)
  5156. outs() << sym_name;
  5157. else
  5158. outs() << format("0x%" PRIx64, n_value);
  5159. if (c.instanceMethods != 0)
  5160. outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
  5161. } else
  5162. outs() << format("0x%" PRIx64, c.instanceMethods);
  5163. outs() << "\n";
  5164. if (c.instanceMethods + n_value != 0)
  5165. print_method_list64_t(c.instanceMethods + n_value, info, "");
  5166. outs() << " classMethods ";
  5167. sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
  5168. S, info, n_value, c.classMethods);
  5169. if (n_value != 0) {
  5170. if (info->verbose && sym_name != nullptr)
  5171. outs() << sym_name;
  5172. else
  5173. outs() << format("0x%" PRIx64, n_value);
  5174. if (c.classMethods != 0)
  5175. outs() << " + " << format("0x%" PRIx64, c.classMethods);
  5176. } else
  5177. outs() << format("0x%" PRIx64, c.classMethods);
  5178. outs() << "\n";
  5179. if (c.classMethods + n_value != 0)
  5180. print_method_list64_t(c.classMethods + n_value, info, "");
  5181. outs() << " protocols ";
  5182. sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
  5183. info, n_value, c.protocols);
  5184. if (n_value != 0) {
  5185. if (info->verbose && sym_name != nullptr)
  5186. outs() << sym_name;
  5187. else
  5188. outs() << format("0x%" PRIx64, n_value);
  5189. if (c.protocols != 0)
  5190. outs() << " + " << format("0x%" PRIx64, c.protocols);
  5191. } else
  5192. outs() << format("0x%" PRIx64, c.protocols);
  5193. outs() << "\n";
  5194. if (c.protocols + n_value != 0)
  5195. print_protocol_list64_t(c.protocols + n_value, info);
  5196. outs() << "instanceProperties ";
  5197. sym_name =
  5198. get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
  5199. S, info, n_value, c.instanceProperties);
  5200. if (n_value != 0) {
  5201. if (info->verbose && sym_name != nullptr)
  5202. outs() << sym_name;
  5203. else
  5204. outs() << format("0x%" PRIx64, n_value);
  5205. if (c.instanceProperties != 0)
  5206. outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
  5207. } else
  5208. outs() << format("0x%" PRIx64, c.instanceProperties);
  5209. outs() << "\n";
  5210. if (c.instanceProperties + n_value != 0)
  5211. print_objc_property_list64(c.instanceProperties + n_value, info);
  5212. }
  5213. static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
  5214. struct category32_t c;
  5215. const char *r;
  5216. uint32_t offset, left;
  5217. SectionRef S, xS;
  5218. const char *name;
  5219. r = get_pointer_32(p, offset, left, S, info);
  5220. if (r == nullptr)
  5221. return;
  5222. memset(&c, '\0', sizeof(struct category32_t));
  5223. if (left < sizeof(struct category32_t)) {
  5224. memcpy(&c, r, left);
  5225. outs() << " (category_t entends past the end of the section)\n";
  5226. } else
  5227. memcpy(&c, r, sizeof(struct category32_t));
  5228. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  5229. swapStruct(c);
  5230. outs() << " name " << format("0x%" PRIx32, c.name);
  5231. name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
  5232. c.name);
  5233. if (name)
  5234. outs() << " " << name;
  5235. outs() << "\n";
  5236. outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
  5237. if (c.cls != 0)
  5238. print_class32_t(c.cls, info);
  5239. outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
  5240. << "\n";
  5241. if (c.instanceMethods != 0)
  5242. print_method_list32_t(c.instanceMethods, info, "");
  5243. outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
  5244. << "\n";
  5245. if (c.classMethods != 0)
  5246. print_method_list32_t(c.classMethods, info, "");
  5247. outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
  5248. if (c.protocols != 0)
  5249. print_protocol_list32_t(c.protocols, info);
  5250. outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
  5251. << "\n";
  5252. if (c.instanceProperties != 0)
  5253. print_objc_property_list32(c.instanceProperties, info);
  5254. }
  5255. static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
  5256. uint32_t i, left, offset, xoffset;
  5257. uint64_t p, n_value;
  5258. struct message_ref64 mr;
  5259. const char *name, *sym_name;
  5260. const char *r;
  5261. SectionRef xS;
  5262. if (S == SectionRef())
  5263. return;
  5264. StringRef SectName;
  5265. S.getName(SectName);
  5266. DataRefImpl Ref = S.getRawDataRefImpl();
  5267. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  5268. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  5269. offset = 0;
  5270. for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
  5271. p = S.getAddress() + i;
  5272. r = get_pointer_64(p, offset, left, S, info);
  5273. if (r == nullptr)
  5274. return;
  5275. memset(&mr, '\0', sizeof(struct message_ref64));
  5276. if (left < sizeof(struct message_ref64)) {
  5277. memcpy(&mr, r, left);
  5278. outs() << " (message_ref entends past the end of the section)\n";
  5279. } else
  5280. memcpy(&mr, r, sizeof(struct message_ref64));
  5281. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  5282. swapStruct(mr);
  5283. outs() << " imp ";
  5284. name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
  5285. n_value, mr.imp);
  5286. if (n_value != 0) {
  5287. outs() << format("0x%" PRIx64, n_value) << " ";
  5288. if (mr.imp != 0)
  5289. outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
  5290. } else
  5291. outs() << format("0x%" PRIx64, mr.imp) << " ";
  5292. if (name != nullptr)
  5293. outs() << " " << name;
  5294. outs() << "\n";
  5295. outs() << " sel ";
  5296. sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
  5297. info, n_value, mr.sel);
  5298. if (n_value != 0) {
  5299. if (info->verbose && sym_name != nullptr)
  5300. outs() << sym_name;
  5301. else
  5302. outs() << format("0x%" PRIx64, n_value);
  5303. if (mr.sel != 0)
  5304. outs() << " + " << format("0x%" PRIx64, mr.sel);
  5305. } else
  5306. outs() << format("0x%" PRIx64, mr.sel);
  5307. name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
  5308. if (name != nullptr)
  5309. outs() << format(" %.*s", left, name);
  5310. outs() << "\n";
  5311. offset += sizeof(struct message_ref64);
  5312. }
  5313. }
  5314. static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
  5315. uint32_t i, left, offset, xoffset, p;
  5316. struct message_ref32 mr;
  5317. const char *name, *r;
  5318. SectionRef xS;
  5319. if (S == SectionRef())
  5320. return;
  5321. StringRef SectName;
  5322. S.getName(SectName);
  5323. DataRefImpl Ref = S.getRawDataRefImpl();
  5324. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  5325. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  5326. offset = 0;
  5327. for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
  5328. p = S.getAddress() + i;
  5329. r = get_pointer_32(p, offset, left, S, info);
  5330. if (r == nullptr)
  5331. return;
  5332. memset(&mr, '\0', sizeof(struct message_ref32));
  5333. if (left < sizeof(struct message_ref32)) {
  5334. memcpy(&mr, r, left);
  5335. outs() << " (message_ref entends past the end of the section)\n";
  5336. } else
  5337. memcpy(&mr, r, sizeof(struct message_ref32));
  5338. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  5339. swapStruct(mr);
  5340. outs() << " imp " << format("0x%" PRIx32, mr.imp);
  5341. name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
  5342. mr.imp);
  5343. if (name != nullptr)
  5344. outs() << " " << name;
  5345. outs() << "\n";
  5346. outs() << " sel " << format("0x%" PRIx32, mr.sel);
  5347. name = get_pointer_32(mr.sel, xoffset, left, xS, info);
  5348. if (name != nullptr)
  5349. outs() << " " << name;
  5350. outs() << "\n";
  5351. offset += sizeof(struct message_ref32);
  5352. }
  5353. }
  5354. static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
  5355. uint32_t left, offset, swift_version;
  5356. uint64_t p;
  5357. struct objc_image_info64 o;
  5358. const char *r;
  5359. if (S == SectionRef())
  5360. return;
  5361. StringRef SectName;
  5362. S.getName(SectName);
  5363. DataRefImpl Ref = S.getRawDataRefImpl();
  5364. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  5365. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  5366. p = S.getAddress();
  5367. r = get_pointer_64(p, offset, left, S, info);
  5368. if (r == nullptr)
  5369. return;
  5370. memset(&o, '\0', sizeof(struct objc_image_info64));
  5371. if (left < sizeof(struct objc_image_info64)) {
  5372. memcpy(&o, r, left);
  5373. outs() << " (objc_image_info entends past the end of the section)\n";
  5374. } else
  5375. memcpy(&o, r, sizeof(struct objc_image_info64));
  5376. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  5377. swapStruct(o);
  5378. outs() << " version " << o.version << "\n";
  5379. outs() << " flags " << format("0x%" PRIx32, o.flags);
  5380. if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
  5381. outs() << " OBJC_IMAGE_IS_REPLACEMENT";
  5382. if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
  5383. outs() << " OBJC_IMAGE_SUPPORTS_GC";
  5384. if (o.flags & OBJC_IMAGE_IS_SIMULATED)
  5385. outs() << " OBJC_IMAGE_IS_SIMULATED";
  5386. if (o.flags & OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES)
  5387. outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES";
  5388. swift_version = (o.flags >> 8) & 0xff;
  5389. if (swift_version != 0) {
  5390. if (swift_version == 1)
  5391. outs() << " Swift 1.0";
  5392. else if (swift_version == 2)
  5393. outs() << " Swift 1.1";
  5394. else if(swift_version == 3)
  5395. outs() << " Swift 2.0";
  5396. else if(swift_version == 4)
  5397. outs() << " Swift 3.0";
  5398. else if(swift_version == 5)
  5399. outs() << " Swift 4.0";
  5400. else if(swift_version == 6)
  5401. outs() << " Swift 4.1/Swift 4.2";
  5402. else if(swift_version == 7)
  5403. outs() << " Swift 5 or later";
  5404. else
  5405. outs() << " unknown future Swift version (" << swift_version << ")";
  5406. }
  5407. outs() << "\n";
  5408. }
  5409. static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
  5410. uint32_t left, offset, swift_version, p;
  5411. struct objc_image_info32 o;
  5412. const char *r;
  5413. if (S == SectionRef())
  5414. return;
  5415. StringRef SectName;
  5416. S.getName(SectName);
  5417. DataRefImpl Ref = S.getRawDataRefImpl();
  5418. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  5419. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  5420. p = S.getAddress();
  5421. r = get_pointer_32(p, offset, left, S, info);
  5422. if (r == nullptr)
  5423. return;
  5424. memset(&o, '\0', sizeof(struct objc_image_info32));
  5425. if (left < sizeof(struct objc_image_info32)) {
  5426. memcpy(&o, r, left);
  5427. outs() << " (objc_image_info entends past the end of the section)\n";
  5428. } else
  5429. memcpy(&o, r, sizeof(struct objc_image_info32));
  5430. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  5431. swapStruct(o);
  5432. outs() << " version " << o.version << "\n";
  5433. outs() << " flags " << format("0x%" PRIx32, o.flags);
  5434. if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
  5435. outs() << " OBJC_IMAGE_IS_REPLACEMENT";
  5436. if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
  5437. outs() << " OBJC_IMAGE_SUPPORTS_GC";
  5438. swift_version = (o.flags >> 8) & 0xff;
  5439. if (swift_version != 0) {
  5440. if (swift_version == 1)
  5441. outs() << " Swift 1.0";
  5442. else if (swift_version == 2)
  5443. outs() << " Swift 1.1";
  5444. else if(swift_version == 3)
  5445. outs() << " Swift 2.0";
  5446. else if(swift_version == 4)
  5447. outs() << " Swift 3.0";
  5448. else if(swift_version == 5)
  5449. outs() << " Swift 4.0";
  5450. else if(swift_version == 6)
  5451. outs() << " Swift 4.1/Swift 4.2";
  5452. else if(swift_version == 7)
  5453. outs() << " Swift 5 or later";
  5454. else
  5455. outs() << " unknown future Swift version (" << swift_version << ")";
  5456. }
  5457. outs() << "\n";
  5458. }
  5459. static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
  5460. uint32_t left, offset, p;
  5461. struct imageInfo_t o;
  5462. const char *r;
  5463. StringRef SectName;
  5464. S.getName(SectName);
  5465. DataRefImpl Ref = S.getRawDataRefImpl();
  5466. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  5467. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  5468. p = S.getAddress();
  5469. r = get_pointer_32(p, offset, left, S, info);
  5470. if (r == nullptr)
  5471. return;
  5472. memset(&o, '\0', sizeof(struct imageInfo_t));
  5473. if (left < sizeof(struct imageInfo_t)) {
  5474. memcpy(&o, r, left);
  5475. outs() << " (imageInfo entends past the end of the section)\n";
  5476. } else
  5477. memcpy(&o, r, sizeof(struct imageInfo_t));
  5478. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  5479. swapStruct(o);
  5480. outs() << " version " << o.version << "\n";
  5481. outs() << " flags " << format("0x%" PRIx32, o.flags);
  5482. if (o.flags & 0x1)
  5483. outs() << " F&C";
  5484. if (o.flags & 0x2)
  5485. outs() << " GC";
  5486. if (o.flags & 0x4)
  5487. outs() << " GC-only";
  5488. else
  5489. outs() << " RR";
  5490. outs() << "\n";
  5491. }
  5492. static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
  5493. SymbolAddressMap AddrMap;
  5494. if (verbose)
  5495. CreateSymbolAddressMap(O, &AddrMap);
  5496. std::vector<SectionRef> Sections;
  5497. for (const SectionRef &Section : O->sections()) {
  5498. StringRef SectName;
  5499. Section.getName(SectName);
  5500. Sections.push_back(Section);
  5501. }
  5502. struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
  5503. SectionRef CL = get_section(O, "__OBJC2", "__class_list");
  5504. if (CL == SectionRef())
  5505. CL = get_section(O, "__DATA", "__objc_classlist");
  5506. if (CL == SectionRef())
  5507. CL = get_section(O, "__DATA_CONST", "__objc_classlist");
  5508. if (CL == SectionRef())
  5509. CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
  5510. info.S = CL;
  5511. walk_pointer_list_64("class", CL, O, &info, print_class64_t);
  5512. SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
  5513. if (CR == SectionRef())
  5514. CR = get_section(O, "__DATA", "__objc_classrefs");
  5515. if (CR == SectionRef())
  5516. CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
  5517. if (CR == SectionRef())
  5518. CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
  5519. info.S = CR;
  5520. walk_pointer_list_64("class refs", CR, O, &info, nullptr);
  5521. SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
  5522. if (SR == SectionRef())
  5523. SR = get_section(O, "__DATA", "__objc_superrefs");
  5524. if (SR == SectionRef())
  5525. SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
  5526. if (SR == SectionRef())
  5527. SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
  5528. info.S = SR;
  5529. walk_pointer_list_64("super refs", SR, O, &info, nullptr);
  5530. SectionRef CA = get_section(O, "__OBJC2", "__category_list");
  5531. if (CA == SectionRef())
  5532. CA = get_section(O, "__DATA", "__objc_catlist");
  5533. if (CA == SectionRef())
  5534. CA = get_section(O, "__DATA_CONST", "__objc_catlist");
  5535. if (CA == SectionRef())
  5536. CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
  5537. info.S = CA;
  5538. walk_pointer_list_64("category", CA, O, &info, print_category64_t);
  5539. SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
  5540. if (PL == SectionRef())
  5541. PL = get_section(O, "__DATA", "__objc_protolist");
  5542. if (PL == SectionRef())
  5543. PL = get_section(O, "__DATA_CONST", "__objc_protolist");
  5544. if (PL == SectionRef())
  5545. PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
  5546. info.S = PL;
  5547. walk_pointer_list_64("protocol", PL, O, &info, nullptr);
  5548. SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
  5549. if (MR == SectionRef())
  5550. MR = get_section(O, "__DATA", "__objc_msgrefs");
  5551. if (MR == SectionRef())
  5552. MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
  5553. if (MR == SectionRef())
  5554. MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
  5555. info.S = MR;
  5556. print_message_refs64(MR, &info);
  5557. SectionRef II = get_section(O, "__OBJC2", "__image_info");
  5558. if (II == SectionRef())
  5559. II = get_section(O, "__DATA", "__objc_imageinfo");
  5560. if (II == SectionRef())
  5561. II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
  5562. if (II == SectionRef())
  5563. II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
  5564. info.S = II;
  5565. print_image_info64(II, &info);
  5566. }
  5567. static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
  5568. SymbolAddressMap AddrMap;
  5569. if (verbose)
  5570. CreateSymbolAddressMap(O, &AddrMap);
  5571. std::vector<SectionRef> Sections;
  5572. for (const SectionRef &Section : O->sections()) {
  5573. StringRef SectName;
  5574. Section.getName(SectName);
  5575. Sections.push_back(Section);
  5576. }
  5577. struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
  5578. SectionRef CL = get_section(O, "__OBJC2", "__class_list");
  5579. if (CL == SectionRef())
  5580. CL = get_section(O, "__DATA", "__objc_classlist");
  5581. if (CL == SectionRef())
  5582. CL = get_section(O, "__DATA_CONST", "__objc_classlist");
  5583. if (CL == SectionRef())
  5584. CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
  5585. info.S = CL;
  5586. walk_pointer_list_32("class", CL, O, &info, print_class32_t);
  5587. SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
  5588. if (CR == SectionRef())
  5589. CR = get_section(O, "__DATA", "__objc_classrefs");
  5590. if (CR == SectionRef())
  5591. CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
  5592. if (CR == SectionRef())
  5593. CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
  5594. info.S = CR;
  5595. walk_pointer_list_32("class refs", CR, O, &info, nullptr);
  5596. SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
  5597. if (SR == SectionRef())
  5598. SR = get_section(O, "__DATA", "__objc_superrefs");
  5599. if (SR == SectionRef())
  5600. SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
  5601. if (SR == SectionRef())
  5602. SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
  5603. info.S = SR;
  5604. walk_pointer_list_32("super refs", SR, O, &info, nullptr);
  5605. SectionRef CA = get_section(O, "__OBJC2", "__category_list");
  5606. if (CA == SectionRef())
  5607. CA = get_section(O, "__DATA", "__objc_catlist");
  5608. if (CA == SectionRef())
  5609. CA = get_section(O, "__DATA_CONST", "__objc_catlist");
  5610. if (CA == SectionRef())
  5611. CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
  5612. info.S = CA;
  5613. walk_pointer_list_32("category", CA, O, &info, print_category32_t);
  5614. SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
  5615. if (PL == SectionRef())
  5616. PL = get_section(O, "__DATA", "__objc_protolist");
  5617. if (PL == SectionRef())
  5618. PL = get_section(O, "__DATA_CONST", "__objc_protolist");
  5619. if (PL == SectionRef())
  5620. PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
  5621. info.S = PL;
  5622. walk_pointer_list_32("protocol", PL, O, &info, nullptr);
  5623. SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
  5624. if (MR == SectionRef())
  5625. MR = get_section(O, "__DATA", "__objc_msgrefs");
  5626. if (MR == SectionRef())
  5627. MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
  5628. if (MR == SectionRef())
  5629. MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
  5630. info.S = MR;
  5631. print_message_refs32(MR, &info);
  5632. SectionRef II = get_section(O, "__OBJC2", "__image_info");
  5633. if (II == SectionRef())
  5634. II = get_section(O, "__DATA", "__objc_imageinfo");
  5635. if (II == SectionRef())
  5636. II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
  5637. if (II == SectionRef())
  5638. II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
  5639. info.S = II;
  5640. print_image_info32(II, &info);
  5641. }
  5642. static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
  5643. uint32_t i, j, p, offset, xoffset, left, defs_left, def;
  5644. const char *r, *name, *defs;
  5645. struct objc_module_t module;
  5646. SectionRef S, xS;
  5647. struct objc_symtab_t symtab;
  5648. struct objc_class_t objc_class;
  5649. struct objc_category_t objc_category;
  5650. outs() << "Objective-C segment\n";
  5651. S = get_section(O, "__OBJC", "__module_info");
  5652. if (S == SectionRef())
  5653. return false;
  5654. SymbolAddressMap AddrMap;
  5655. if (verbose)
  5656. CreateSymbolAddressMap(O, &AddrMap);
  5657. std::vector<SectionRef> Sections;
  5658. for (const SectionRef &Section : O->sections()) {
  5659. StringRef SectName;
  5660. Section.getName(SectName);
  5661. Sections.push_back(Section);
  5662. }
  5663. struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
  5664. for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
  5665. p = S.getAddress() + i;
  5666. r = get_pointer_32(p, offset, left, S, &info, true);
  5667. if (r == nullptr)
  5668. return true;
  5669. memset(&module, '\0', sizeof(struct objc_module_t));
  5670. if (left < sizeof(struct objc_module_t)) {
  5671. memcpy(&module, r, left);
  5672. outs() << " (module extends past end of __module_info section)\n";
  5673. } else
  5674. memcpy(&module, r, sizeof(struct objc_module_t));
  5675. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5676. swapStruct(module);
  5677. outs() << "Module " << format("0x%" PRIx32, p) << "\n";
  5678. outs() << " version " << module.version << "\n";
  5679. outs() << " size " << module.size << "\n";
  5680. outs() << " name ";
  5681. name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
  5682. if (name != nullptr)
  5683. outs() << format("%.*s", left, name);
  5684. else
  5685. outs() << format("0x%08" PRIx32, module.name)
  5686. << "(not in an __OBJC section)";
  5687. outs() << "\n";
  5688. r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
  5689. if (module.symtab == 0 || r == nullptr) {
  5690. outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
  5691. << " (not in an __OBJC section)\n";
  5692. continue;
  5693. }
  5694. outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
  5695. memset(&symtab, '\0', sizeof(struct objc_symtab_t));
  5696. defs_left = 0;
  5697. defs = nullptr;
  5698. if (left < sizeof(struct objc_symtab_t)) {
  5699. memcpy(&symtab, r, left);
  5700. outs() << "\tsymtab extends past end of an __OBJC section)\n";
  5701. } else {
  5702. memcpy(&symtab, r, sizeof(struct objc_symtab_t));
  5703. if (left > sizeof(struct objc_symtab_t)) {
  5704. defs_left = left - sizeof(struct objc_symtab_t);
  5705. defs = r + sizeof(struct objc_symtab_t);
  5706. }
  5707. }
  5708. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5709. swapStruct(symtab);
  5710. outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
  5711. r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
  5712. outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
  5713. if (r == nullptr)
  5714. outs() << " (not in an __OBJC section)";
  5715. outs() << "\n";
  5716. outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
  5717. outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
  5718. if (symtab.cls_def_cnt > 0)
  5719. outs() << "\tClass Definitions\n";
  5720. for (j = 0; j < symtab.cls_def_cnt; j++) {
  5721. if ((j + 1) * sizeof(uint32_t) > defs_left) {
  5722. outs() << "\t(remaining class defs entries entends past the end of the "
  5723. << "section)\n";
  5724. break;
  5725. }
  5726. memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
  5727. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5728. sys::swapByteOrder(def);
  5729. r = get_pointer_32(def, xoffset, left, xS, &info, true);
  5730. outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
  5731. if (r != nullptr) {
  5732. if (left > sizeof(struct objc_class_t)) {
  5733. outs() << "\n";
  5734. memcpy(&objc_class, r, sizeof(struct objc_class_t));
  5735. } else {
  5736. outs() << " (entends past the end of the section)\n";
  5737. memset(&objc_class, '\0', sizeof(struct objc_class_t));
  5738. memcpy(&objc_class, r, left);
  5739. }
  5740. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5741. swapStruct(objc_class);
  5742. print_objc_class_t(&objc_class, &info);
  5743. } else {
  5744. outs() << "(not in an __OBJC section)\n";
  5745. }
  5746. if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
  5747. outs() << "\tMeta Class";
  5748. r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
  5749. if (r != nullptr) {
  5750. if (left > sizeof(struct objc_class_t)) {
  5751. outs() << "\n";
  5752. memcpy(&objc_class, r, sizeof(struct objc_class_t));
  5753. } else {
  5754. outs() << " (entends past the end of the section)\n";
  5755. memset(&objc_class, '\0', sizeof(struct objc_class_t));
  5756. memcpy(&objc_class, r, left);
  5757. }
  5758. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5759. swapStruct(objc_class);
  5760. print_objc_class_t(&objc_class, &info);
  5761. } else {
  5762. outs() << "(not in an __OBJC section)\n";
  5763. }
  5764. }
  5765. }
  5766. if (symtab.cat_def_cnt > 0)
  5767. outs() << "\tCategory Definitions\n";
  5768. for (j = 0; j < symtab.cat_def_cnt; j++) {
  5769. if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
  5770. outs() << "\t(remaining category defs entries entends past the end of "
  5771. << "the section)\n";
  5772. break;
  5773. }
  5774. memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
  5775. sizeof(uint32_t));
  5776. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5777. sys::swapByteOrder(def);
  5778. r = get_pointer_32(def, xoffset, left, xS, &info, true);
  5779. outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
  5780. << format("0x%08" PRIx32, def);
  5781. if (r != nullptr) {
  5782. if (left > sizeof(struct objc_category_t)) {
  5783. outs() << "\n";
  5784. memcpy(&objc_category, r, sizeof(struct objc_category_t));
  5785. } else {
  5786. outs() << " (entends past the end of the section)\n";
  5787. memset(&objc_category, '\0', sizeof(struct objc_category_t));
  5788. memcpy(&objc_category, r, left);
  5789. }
  5790. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5791. swapStruct(objc_category);
  5792. print_objc_objc_category_t(&objc_category, &info);
  5793. } else {
  5794. outs() << "(not in an __OBJC section)\n";
  5795. }
  5796. }
  5797. }
  5798. const SectionRef II = get_section(O, "__OBJC", "__image_info");
  5799. if (II != SectionRef())
  5800. print_image_info(II, &info);
  5801. return true;
  5802. }
  5803. static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
  5804. uint32_t size, uint32_t addr) {
  5805. SymbolAddressMap AddrMap;
  5806. CreateSymbolAddressMap(O, &AddrMap);
  5807. std::vector<SectionRef> Sections;
  5808. for (const SectionRef &Section : O->sections()) {
  5809. StringRef SectName;
  5810. Section.getName(SectName);
  5811. Sections.push_back(Section);
  5812. }
  5813. struct DisassembleInfo info(O, &AddrMap, &Sections, true);
  5814. const char *p;
  5815. struct objc_protocol_t protocol;
  5816. uint32_t left, paddr;
  5817. for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
  5818. memset(&protocol, '\0', sizeof(struct objc_protocol_t));
  5819. left = size - (p - sect);
  5820. if (left < sizeof(struct objc_protocol_t)) {
  5821. outs() << "Protocol extends past end of __protocol section\n";
  5822. memcpy(&protocol, p, left);
  5823. } else
  5824. memcpy(&protocol, p, sizeof(struct objc_protocol_t));
  5825. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5826. swapStruct(protocol);
  5827. paddr = addr + (p - sect);
  5828. outs() << "Protocol " << format("0x%" PRIx32, paddr);
  5829. if (print_protocol(paddr, 0, &info))
  5830. outs() << "(not in an __OBJC section)\n";
  5831. }
  5832. }
  5833. #ifdef HAVE_LIBXAR
  5834. inline void swapStruct(struct xar_header &xar) {
  5835. sys::swapByteOrder(xar.magic);
  5836. sys::swapByteOrder(xar.size);
  5837. sys::swapByteOrder(xar.version);
  5838. sys::swapByteOrder(xar.toc_length_compressed);
  5839. sys::swapByteOrder(xar.toc_length_uncompressed);
  5840. sys::swapByteOrder(xar.cksum_alg);
  5841. }
  5842. static void PrintModeVerbose(uint32_t mode) {
  5843. switch(mode & S_IFMT){
  5844. case S_IFDIR:
  5845. outs() << "d";
  5846. break;
  5847. case S_IFCHR:
  5848. outs() << "c";
  5849. break;
  5850. case S_IFBLK:
  5851. outs() << "b";
  5852. break;
  5853. case S_IFREG:
  5854. outs() << "-";
  5855. break;
  5856. case S_IFLNK:
  5857. outs() << "l";
  5858. break;
  5859. case S_IFSOCK:
  5860. outs() << "s";
  5861. break;
  5862. default:
  5863. outs() << "?";
  5864. break;
  5865. }
  5866. /* owner permissions */
  5867. if(mode & S_IREAD)
  5868. outs() << "r";
  5869. else
  5870. outs() << "-";
  5871. if(mode & S_IWRITE)
  5872. outs() << "w";
  5873. else
  5874. outs() << "-";
  5875. if(mode & S_ISUID)
  5876. outs() << "s";
  5877. else if(mode & S_IEXEC)
  5878. outs() << "x";
  5879. else
  5880. outs() << "-";
  5881. /* group permissions */
  5882. if(mode & (S_IREAD >> 3))
  5883. outs() << "r";
  5884. else
  5885. outs() << "-";
  5886. if(mode & (S_IWRITE >> 3))
  5887. outs() << "w";
  5888. else
  5889. outs() << "-";
  5890. if(mode & S_ISGID)
  5891. outs() << "s";
  5892. else if(mode & (S_IEXEC >> 3))
  5893. outs() << "x";
  5894. else
  5895. outs() << "-";
  5896. /* other permissions */
  5897. if(mode & (S_IREAD >> 6))
  5898. outs() << "r";
  5899. else
  5900. outs() << "-";
  5901. if(mode & (S_IWRITE >> 6))
  5902. outs() << "w";
  5903. else
  5904. outs() << "-";
  5905. if(mode & S_ISVTX)
  5906. outs() << "t";
  5907. else if(mode & (S_IEXEC >> 6))
  5908. outs() << "x";
  5909. else
  5910. outs() << "-";
  5911. }
  5912. static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
  5913. xar_file_t xf;
  5914. const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
  5915. char *endp;
  5916. uint32_t mode_value;
  5917. ScopedXarIter xi;
  5918. if (!xi) {
  5919. WithColor::error(errs(), "llvm-objdump")
  5920. << "can't obtain an xar iterator for xar archive " << XarFilename
  5921. << "\n";
  5922. return;
  5923. }
  5924. // Go through the xar's files.
  5925. for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
  5926. ScopedXarIter xp;
  5927. if(!xp){
  5928. WithColor::error(errs(), "llvm-objdump")
  5929. << "can't obtain an xar iterator for xar archive " << XarFilename
  5930. << "\n";
  5931. return;
  5932. }
  5933. type = nullptr;
  5934. mode = nullptr;
  5935. user = nullptr;
  5936. group = nullptr;
  5937. size = nullptr;
  5938. mtime = nullptr;
  5939. name = nullptr;
  5940. for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
  5941. const char *val = nullptr;
  5942. xar_prop_get(xf, key, &val);
  5943. #if 0 // Useful for debugging.
  5944. outs() << "key: " << key << " value: " << val << "\n";
  5945. #endif
  5946. if(strcmp(key, "type") == 0)
  5947. type = val;
  5948. if(strcmp(key, "mode") == 0)
  5949. mode = val;
  5950. if(strcmp(key, "user") == 0)
  5951. user = val;
  5952. if(strcmp(key, "group") == 0)
  5953. group = val;
  5954. if(strcmp(key, "data/size") == 0)
  5955. size = val;
  5956. if(strcmp(key, "mtime") == 0)
  5957. mtime = val;
  5958. if(strcmp(key, "name") == 0)
  5959. name = val;
  5960. }
  5961. if(mode != nullptr){
  5962. mode_value = strtoul(mode, &endp, 8);
  5963. if(*endp != '\0')
  5964. outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
  5965. if(strcmp(type, "file") == 0)
  5966. mode_value |= S_IFREG;
  5967. PrintModeVerbose(mode_value);
  5968. outs() << " ";
  5969. }
  5970. if(user != nullptr)
  5971. outs() << format("%10s/", user);
  5972. if(group != nullptr)
  5973. outs() << format("%-10s ", group);
  5974. if(size != nullptr)
  5975. outs() << format("%7s ", size);
  5976. if(mtime != nullptr){
  5977. for(m = mtime; *m != 'T' && *m != '\0'; m++)
  5978. outs() << *m;
  5979. if(*m == 'T')
  5980. m++;
  5981. outs() << " ";
  5982. for( ; *m != 'Z' && *m != '\0'; m++)
  5983. outs() << *m;
  5984. outs() << " ";
  5985. }
  5986. if(name != nullptr)
  5987. outs() << name;
  5988. outs() << "\n";
  5989. }
  5990. }
  5991. static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
  5992. uint32_t size, bool verbose,
  5993. bool PrintXarHeader, bool PrintXarFileHeaders,
  5994. std::string XarMemberName) {
  5995. if(size < sizeof(struct xar_header)) {
  5996. outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
  5997. "of struct xar_header)\n";
  5998. return;
  5999. }
  6000. struct xar_header XarHeader;
  6001. memcpy(&XarHeader, sect, sizeof(struct xar_header));
  6002. if (sys::IsLittleEndianHost)
  6003. swapStruct(XarHeader);
  6004. if (PrintXarHeader) {
  6005. if (!XarMemberName.empty())
  6006. outs() << "In xar member " << XarMemberName << ": ";
  6007. else
  6008. outs() << "For (__LLVM,__bundle) section: ";
  6009. outs() << "xar header\n";
  6010. if (XarHeader.magic == XAR_HEADER_MAGIC)
  6011. outs() << " magic XAR_HEADER_MAGIC\n";
  6012. else
  6013. outs() << " magic "
  6014. << format_hex(XarHeader.magic, 10, true)
  6015. << " (not XAR_HEADER_MAGIC)\n";
  6016. outs() << " size " << XarHeader.size << "\n";
  6017. outs() << " version " << XarHeader.version << "\n";
  6018. outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
  6019. << "\n";
  6020. outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
  6021. << "\n";
  6022. outs() << " cksum_alg ";
  6023. switch (XarHeader.cksum_alg) {
  6024. case XAR_CKSUM_NONE:
  6025. outs() << "XAR_CKSUM_NONE\n";
  6026. break;
  6027. case XAR_CKSUM_SHA1:
  6028. outs() << "XAR_CKSUM_SHA1\n";
  6029. break;
  6030. case XAR_CKSUM_MD5:
  6031. outs() << "XAR_CKSUM_MD5\n";
  6032. break;
  6033. #ifdef XAR_CKSUM_SHA256
  6034. case XAR_CKSUM_SHA256:
  6035. outs() << "XAR_CKSUM_SHA256\n";
  6036. break;
  6037. #endif
  6038. #ifdef XAR_CKSUM_SHA512
  6039. case XAR_CKSUM_SHA512:
  6040. outs() << "XAR_CKSUM_SHA512\n";
  6041. break;
  6042. #endif
  6043. default:
  6044. outs() << XarHeader.cksum_alg << "\n";
  6045. }
  6046. }
  6047. SmallString<128> XarFilename;
  6048. int FD;
  6049. std::error_code XarEC =
  6050. sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
  6051. if (XarEC) {
  6052. WithColor::error(errs(), "llvm-objdump") << XarEC.message() << "\n";
  6053. return;
  6054. }
  6055. ToolOutputFile XarFile(XarFilename, FD);
  6056. raw_fd_ostream &XarOut = XarFile.os();
  6057. StringRef XarContents(sect, size);
  6058. XarOut << XarContents;
  6059. XarOut.close();
  6060. if (XarOut.has_error())
  6061. return;
  6062. ScopedXarFile xar(XarFilename.c_str(), READ);
  6063. if (!xar) {
  6064. WithColor::error(errs(), "llvm-objdump")
  6065. << "can't create temporary xar archive " << XarFilename << "\n";
  6066. return;
  6067. }
  6068. SmallString<128> TocFilename;
  6069. std::error_code TocEC =
  6070. sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
  6071. if (TocEC) {
  6072. WithColor::error(errs(), "llvm-objdump") << TocEC.message() << "\n";
  6073. return;
  6074. }
  6075. xar_serialize(xar, TocFilename.c_str());
  6076. if (PrintXarFileHeaders) {
  6077. if (!XarMemberName.empty())
  6078. outs() << "In xar member " << XarMemberName << ": ";
  6079. else
  6080. outs() << "For (__LLVM,__bundle) section: ";
  6081. outs() << "xar archive files:\n";
  6082. PrintXarFilesSummary(XarFilename.c_str(), xar);
  6083. }
  6084. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  6085. MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
  6086. if (std::error_code EC = FileOrErr.getError()) {
  6087. WithColor::error(errs(), "llvm-objdump") << EC.message() << "\n";
  6088. return;
  6089. }
  6090. std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
  6091. if (!XarMemberName.empty())
  6092. outs() << "In xar member " << XarMemberName << ": ";
  6093. else
  6094. outs() << "For (__LLVM,__bundle) section: ";
  6095. outs() << "xar table of contents:\n";
  6096. outs() << Buffer->getBuffer() << "\n";
  6097. // TODO: Go through the xar's files.
  6098. ScopedXarIter xi;
  6099. if(!xi){
  6100. WithColor::error(errs(), "llvm-objdump")
  6101. << "can't obtain an xar iterator for xar archive "
  6102. << XarFilename.c_str() << "\n";
  6103. return;
  6104. }
  6105. for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
  6106. const char *key;
  6107. const char *member_name, *member_type, *member_size_string;
  6108. size_t member_size;
  6109. ScopedXarIter xp;
  6110. if(!xp){
  6111. WithColor::error(errs(), "llvm-objdump")
  6112. << "can't obtain an xar iterator for xar archive "
  6113. << XarFilename.c_str() << "\n";
  6114. return;
  6115. }
  6116. member_name = NULL;
  6117. member_type = NULL;
  6118. member_size_string = NULL;
  6119. for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
  6120. const char *val = nullptr;
  6121. xar_prop_get(xf, key, &val);
  6122. #if 0 // Useful for debugging.
  6123. outs() << "key: " << key << " value: " << val << "\n";
  6124. #endif
  6125. if (strcmp(key, "name") == 0)
  6126. member_name = val;
  6127. if (strcmp(key, "type") == 0)
  6128. member_type = val;
  6129. if (strcmp(key, "data/size") == 0)
  6130. member_size_string = val;
  6131. }
  6132. /*
  6133. * If we find a file with a name, date/size and type properties
  6134. * and with the type being "file" see if that is a xar file.
  6135. */
  6136. if (member_name != NULL && member_type != NULL &&
  6137. strcmp(member_type, "file") == 0 &&
  6138. member_size_string != NULL){
  6139. // Extract the file into a buffer.
  6140. char *endptr;
  6141. member_size = strtoul(member_size_string, &endptr, 10);
  6142. if (*endptr == '\0' && member_size != 0) {
  6143. char *buffer;
  6144. if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
  6145. #if 0 // Useful for debugging.
  6146. outs() << "xar member: " << member_name << " extracted\n";
  6147. #endif
  6148. // Set the XarMemberName we want to see printed in the header.
  6149. std::string OldXarMemberName;
  6150. // If XarMemberName is already set this is nested. So
  6151. // save the old name and create the nested name.
  6152. if (!XarMemberName.empty()) {
  6153. OldXarMemberName = XarMemberName;
  6154. XarMemberName =
  6155. (Twine("[") + XarMemberName + "]" + member_name).str();
  6156. } else {
  6157. OldXarMemberName = "";
  6158. XarMemberName = member_name;
  6159. }
  6160. // See if this is could be a xar file (nested).
  6161. if (member_size >= sizeof(struct xar_header)) {
  6162. #if 0 // Useful for debugging.
  6163. outs() << "could be a xar file: " << member_name << "\n";
  6164. #endif
  6165. memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
  6166. if (sys::IsLittleEndianHost)
  6167. swapStruct(XarHeader);
  6168. if (XarHeader.magic == XAR_HEADER_MAGIC)
  6169. DumpBitcodeSection(O, buffer, member_size, verbose,
  6170. PrintXarHeader, PrintXarFileHeaders,
  6171. XarMemberName);
  6172. }
  6173. XarMemberName = OldXarMemberName;
  6174. delete buffer;
  6175. }
  6176. }
  6177. }
  6178. }
  6179. }
  6180. #endif // defined(HAVE_LIBXAR)
  6181. static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
  6182. if (O->is64Bit())
  6183. printObjc2_64bit_MetaData(O, verbose);
  6184. else {
  6185. MachO::mach_header H;
  6186. H = O->getHeader();
  6187. if (H.cputype == MachO::CPU_TYPE_ARM)
  6188. printObjc2_32bit_MetaData(O, verbose);
  6189. else {
  6190. // This is the 32-bit non-arm cputype case. Which is normally
  6191. // the first Objective-C ABI. But it may be the case of a
  6192. // binary for the iOS simulator which is the second Objective-C
  6193. // ABI. In that case printObjc1_32bit_MetaData() will determine that
  6194. // and return false.
  6195. if (!printObjc1_32bit_MetaData(O, verbose))
  6196. printObjc2_32bit_MetaData(O, verbose);
  6197. }
  6198. }
  6199. }
  6200. // GuessLiteralPointer returns a string which for the item in the Mach-O file
  6201. // for the address passed in as ReferenceValue for printing as a comment with
  6202. // the instruction and also returns the corresponding type of that item
  6203. // indirectly through ReferenceType.
  6204. //
  6205. // If ReferenceValue is an address of literal cstring then a pointer to the
  6206. // cstring is returned and ReferenceType is set to
  6207. // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
  6208. //
  6209. // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
  6210. // Class ref that name is returned and the ReferenceType is set accordingly.
  6211. //
  6212. // Lastly, literals which are Symbol address in a literal pool are looked for
  6213. // and if found the symbol name is returned and ReferenceType is set to
  6214. // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
  6215. //
  6216. // If there is no item in the Mach-O file for the address passed in as
  6217. // ReferenceValue nullptr is returned and ReferenceType is unchanged.
  6218. static const char *GuessLiteralPointer(uint64_t ReferenceValue,
  6219. uint64_t ReferencePC,
  6220. uint64_t *ReferenceType,
  6221. struct DisassembleInfo *info) {
  6222. // First see if there is an external relocation entry at the ReferencePC.
  6223. if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
  6224. uint64_t sect_addr = info->S.getAddress();
  6225. uint64_t sect_offset = ReferencePC - sect_addr;
  6226. bool reloc_found = false;
  6227. DataRefImpl Rel;
  6228. MachO::any_relocation_info RE;
  6229. bool isExtern = false;
  6230. SymbolRef Symbol;
  6231. for (const RelocationRef &Reloc : info->S.relocations()) {
  6232. uint64_t RelocOffset = Reloc.getOffset();
  6233. if (RelocOffset == sect_offset) {
  6234. Rel = Reloc.getRawDataRefImpl();
  6235. RE = info->O->getRelocation(Rel);
  6236. if (info->O->isRelocationScattered(RE))
  6237. continue;
  6238. isExtern = info->O->getPlainRelocationExternal(RE);
  6239. if (isExtern) {
  6240. symbol_iterator RelocSym = Reloc.getSymbol();
  6241. Symbol = *RelocSym;
  6242. }
  6243. reloc_found = true;
  6244. break;
  6245. }
  6246. }
  6247. // If there is an external relocation entry for a symbol in a section
  6248. // then used that symbol's value for the value of the reference.
  6249. if (reloc_found && isExtern) {
  6250. if (info->O->getAnyRelocationPCRel(RE)) {
  6251. unsigned Type = info->O->getAnyRelocationType(RE);
  6252. if (Type == MachO::X86_64_RELOC_SIGNED) {
  6253. ReferenceValue = Symbol.getValue();
  6254. }
  6255. }
  6256. }
  6257. }
  6258. // Look for literals such as Objective-C CFStrings refs, Selector refs,
  6259. // Message refs and Class refs.
  6260. bool classref, selref, msgref, cfstring;
  6261. uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
  6262. selref, msgref, cfstring);
  6263. if (classref && pointer_value == 0) {
  6264. // Note the ReferenceValue is a pointer into the __objc_classrefs section.
  6265. // And the pointer_value in that section is typically zero as it will be
  6266. // set by dyld as part of the "bind information".
  6267. const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
  6268. if (name != nullptr) {
  6269. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
  6270. const char *class_name = strrchr(name, '$');
  6271. if (class_name != nullptr && class_name[1] == '_' &&
  6272. class_name[2] != '\0') {
  6273. info->class_name = class_name + 2;
  6274. return name;
  6275. }
  6276. }
  6277. }
  6278. if (classref) {
  6279. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
  6280. const char *name =
  6281. get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
  6282. if (name != nullptr)
  6283. info->class_name = name;
  6284. else
  6285. name = "bad class ref";
  6286. return name;
  6287. }
  6288. if (cfstring) {
  6289. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
  6290. const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
  6291. return name;
  6292. }
  6293. if (selref && pointer_value == 0)
  6294. pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
  6295. if (pointer_value != 0)
  6296. ReferenceValue = pointer_value;
  6297. const char *name = GuessCstringPointer(ReferenceValue, info);
  6298. if (name) {
  6299. if (pointer_value != 0 && selref) {
  6300. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
  6301. info->selector_name = name;
  6302. } else if (pointer_value != 0 && msgref) {
  6303. info->class_name = nullptr;
  6304. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
  6305. info->selector_name = name;
  6306. } else
  6307. *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
  6308. return name;
  6309. }
  6310. // Lastly look for an indirect symbol with this ReferenceValue which is in
  6311. // a literal pool. If found return that symbol name.
  6312. name = GuessIndirectSymbol(ReferenceValue, info);
  6313. if (name) {
  6314. *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
  6315. return name;
  6316. }
  6317. return nullptr;
  6318. }
  6319. // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
  6320. // the Symbolizer. It looks up the ReferenceValue using the info passed via the
  6321. // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
  6322. // is created and returns the symbol name that matches the ReferenceValue or
  6323. // nullptr if none. The ReferenceType is passed in for the IN type of
  6324. // reference the instruction is making from the values in defined in the header
  6325. // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
  6326. // Out type and the ReferenceName will also be set which is added as a comment
  6327. // to the disassembled instruction.
  6328. //
  6329. // If the symbol name is a C++ mangled name then the demangled name is
  6330. // returned through ReferenceName and ReferenceType is set to
  6331. // LLVMDisassembler_ReferenceType_DeMangled_Name .
  6332. //
  6333. // When this is called to get a symbol name for a branch target then the
  6334. // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
  6335. // SymbolValue will be looked for in the indirect symbol table to determine if
  6336. // it is an address for a symbol stub. If so then the symbol name for that
  6337. // stub is returned indirectly through ReferenceName and then ReferenceType is
  6338. // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
  6339. //
  6340. // When this is called with an value loaded via a PC relative load then
  6341. // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
  6342. // SymbolValue is checked to be an address of literal pointer, symbol pointer,
  6343. // or an Objective-C meta data reference. If so the output ReferenceType is
  6344. // set to correspond to that as well as setting the ReferenceName.
  6345. static const char *SymbolizerSymbolLookUp(void *DisInfo,
  6346. uint64_t ReferenceValue,
  6347. uint64_t *ReferenceType,
  6348. uint64_t ReferencePC,
  6349. const char **ReferenceName) {
  6350. struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
  6351. // If no verbose symbolic information is wanted then just return nullptr.
  6352. if (!info->verbose) {
  6353. *ReferenceName = nullptr;
  6354. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  6355. return nullptr;
  6356. }
  6357. const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
  6358. if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
  6359. *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
  6360. if (*ReferenceName != nullptr) {
  6361. method_reference(info, ReferenceType, ReferenceName);
  6362. if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
  6363. *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
  6364. } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
  6365. if (info->demangled_name != nullptr)
  6366. free(info->demangled_name);
  6367. int status;
  6368. info->demangled_name =
  6369. itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
  6370. if (info->demangled_name != nullptr) {
  6371. *ReferenceName = info->demangled_name;
  6372. *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
  6373. } else
  6374. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  6375. } else
  6376. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  6377. } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
  6378. *ReferenceName =
  6379. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  6380. if (*ReferenceName)
  6381. method_reference(info, ReferenceType, ReferenceName);
  6382. else
  6383. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  6384. // If this is arm64 and the reference is an adrp instruction save the
  6385. // instruction, passed in ReferenceValue and the address of the instruction
  6386. // for use later if we see and add immediate instruction.
  6387. } else if (info->O->getArch() == Triple::aarch64 &&
  6388. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
  6389. info->adrp_inst = ReferenceValue;
  6390. info->adrp_addr = ReferencePC;
  6391. SymbolName = nullptr;
  6392. *ReferenceName = nullptr;
  6393. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  6394. // If this is arm64 and reference is an add immediate instruction and we
  6395. // have
  6396. // seen an adrp instruction just before it and the adrp's Xd register
  6397. // matches
  6398. // this add's Xn register reconstruct the value being referenced and look to
  6399. // see if it is a literal pointer. Note the add immediate instruction is
  6400. // passed in ReferenceValue.
  6401. } else if (info->O->getArch() == Triple::aarch64 &&
  6402. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
  6403. ReferencePC - 4 == info->adrp_addr &&
  6404. (info->adrp_inst & 0x9f000000) == 0x90000000 &&
  6405. (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
  6406. uint32_t addxri_inst;
  6407. uint64_t adrp_imm, addxri_imm;
  6408. adrp_imm =
  6409. ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
  6410. if (info->adrp_inst & 0x0200000)
  6411. adrp_imm |= 0xfffffffffc000000LL;
  6412. addxri_inst = ReferenceValue;
  6413. addxri_imm = (addxri_inst >> 10) & 0xfff;
  6414. if (((addxri_inst >> 22) & 0x3) == 1)
  6415. addxri_imm <<= 12;
  6416. ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
  6417. (adrp_imm << 12) + addxri_imm;
  6418. *ReferenceName =
  6419. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  6420. if (*ReferenceName == nullptr)
  6421. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  6422. // If this is arm64 and the reference is a load register instruction and we
  6423. // have seen an adrp instruction just before it and the adrp's Xd register
  6424. // matches this add's Xn register reconstruct the value being referenced and
  6425. // look to see if it is a literal pointer. Note the load register
  6426. // instruction is passed in ReferenceValue.
  6427. } else if (info->O->getArch() == Triple::aarch64 &&
  6428. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
  6429. ReferencePC - 4 == info->adrp_addr &&
  6430. (info->adrp_inst & 0x9f000000) == 0x90000000 &&
  6431. (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
  6432. uint32_t ldrxui_inst;
  6433. uint64_t adrp_imm, ldrxui_imm;
  6434. adrp_imm =
  6435. ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
  6436. if (info->adrp_inst & 0x0200000)
  6437. adrp_imm |= 0xfffffffffc000000LL;
  6438. ldrxui_inst = ReferenceValue;
  6439. ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
  6440. ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
  6441. (adrp_imm << 12) + (ldrxui_imm << 3);
  6442. *ReferenceName =
  6443. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  6444. if (*ReferenceName == nullptr)
  6445. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  6446. }
  6447. // If this arm64 and is an load register (PC-relative) instruction the
  6448. // ReferenceValue is the PC plus the immediate value.
  6449. else if (info->O->getArch() == Triple::aarch64 &&
  6450. (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
  6451. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
  6452. *ReferenceName =
  6453. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  6454. if (*ReferenceName == nullptr)
  6455. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  6456. } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
  6457. if (info->demangled_name != nullptr)
  6458. free(info->demangled_name);
  6459. int status;
  6460. info->demangled_name =
  6461. itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
  6462. if (info->demangled_name != nullptr) {
  6463. *ReferenceName = info->demangled_name;
  6464. *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
  6465. }
  6466. }
  6467. else {
  6468. *ReferenceName = nullptr;
  6469. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  6470. }
  6471. return SymbolName;
  6472. }
  6473. /// Emits the comments that are stored in the CommentStream.
  6474. /// Each comment in the CommentStream must end with a newline.
  6475. static void emitComments(raw_svector_ostream &CommentStream,
  6476. SmallString<128> &CommentsToEmit,
  6477. formatted_raw_ostream &FormattedOS,
  6478. const MCAsmInfo &MAI) {
  6479. // Flush the stream before taking its content.
  6480. StringRef Comments = CommentsToEmit.str();
  6481. // Get the default information for printing a comment.
  6482. StringRef CommentBegin = MAI.getCommentString();
  6483. unsigned CommentColumn = MAI.getCommentColumn();
  6484. bool IsFirst = true;
  6485. while (!Comments.empty()) {
  6486. if (!IsFirst)
  6487. FormattedOS << '\n';
  6488. // Emit a line of comments.
  6489. FormattedOS.PadToColumn(CommentColumn);
  6490. size_t Position = Comments.find('\n');
  6491. FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
  6492. // Move after the newline character.
  6493. Comments = Comments.substr(Position + 1);
  6494. IsFirst = false;
  6495. }
  6496. FormattedOS.flush();
  6497. // Tell the comment stream that the vector changed underneath it.
  6498. CommentsToEmit.clear();
  6499. }
  6500. static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
  6501. StringRef DisSegName, StringRef DisSectName) {
  6502. const char *McpuDefault = nullptr;
  6503. const Target *ThumbTarget = nullptr;
  6504. const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
  6505. if (!TheTarget) {
  6506. // GetTarget prints out stuff.
  6507. return;
  6508. }
  6509. std::string MachOMCPU;
  6510. if (MCPU.empty() && McpuDefault)
  6511. MachOMCPU = McpuDefault;
  6512. else
  6513. MachOMCPU = MCPU;
  6514. std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
  6515. std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
  6516. if (ThumbTarget)
  6517. ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
  6518. // Package up features to be passed to target/subtarget
  6519. std::string FeaturesStr;
  6520. if (!MAttrs.empty()) {
  6521. SubtargetFeatures Features;
  6522. for (unsigned i = 0; i != MAttrs.size(); ++i)
  6523. Features.AddFeature(MAttrs[i]);
  6524. FeaturesStr = Features.getString();
  6525. }
  6526. // Set up disassembler.
  6527. std::unique_ptr<const MCRegisterInfo> MRI(
  6528. TheTarget->createMCRegInfo(TripleName));
  6529. std::unique_ptr<const MCAsmInfo> AsmInfo(
  6530. TheTarget->createMCAsmInfo(*MRI, TripleName));
  6531. std::unique_ptr<const MCSubtargetInfo> STI(
  6532. TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr));
  6533. MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
  6534. std::unique_ptr<MCDisassembler> DisAsm(
  6535. TheTarget->createMCDisassembler(*STI, Ctx));
  6536. std::unique_ptr<MCSymbolizer> Symbolizer;
  6537. struct DisassembleInfo SymbolizerInfo(nullptr, nullptr, nullptr, false);
  6538. std::unique_ptr<MCRelocationInfo> RelInfo(
  6539. TheTarget->createMCRelocationInfo(TripleName, Ctx));
  6540. if (RelInfo) {
  6541. Symbolizer.reset(TheTarget->createMCSymbolizer(
  6542. TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
  6543. &SymbolizerInfo, &Ctx, std::move(RelInfo)));
  6544. DisAsm->setSymbolizer(std::move(Symbolizer));
  6545. }
  6546. int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
  6547. std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
  6548. Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
  6549. // Set the display preference for hex vs. decimal immediates.
  6550. IP->setPrintImmHex(PrintImmHex);
  6551. // Comment stream and backing vector.
  6552. SmallString<128> CommentsToEmit;
  6553. raw_svector_ostream CommentStream(CommentsToEmit);
  6554. // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
  6555. // if it is done then arm64 comments for string literals don't get printed
  6556. // and some constant get printed instead and not setting it causes intel
  6557. // (32-bit and 64-bit) comments printed with different spacing before the
  6558. // comment causing different diffs with the 'C' disassembler library API.
  6559. // IP->setCommentStream(CommentStream);
  6560. if (!AsmInfo || !STI || !DisAsm || !IP) {
  6561. WithColor::error(errs(), "llvm-objdump")
  6562. << "couldn't initialize disassembler for target " << TripleName << '\n';
  6563. return;
  6564. }
  6565. // Set up separate thumb disassembler if needed.
  6566. std::unique_ptr<const MCRegisterInfo> ThumbMRI;
  6567. std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
  6568. std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
  6569. std::unique_ptr<MCDisassembler> ThumbDisAsm;
  6570. std::unique_ptr<MCInstPrinter> ThumbIP;
  6571. std::unique_ptr<MCContext> ThumbCtx;
  6572. std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
  6573. struct DisassembleInfo ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false);
  6574. std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
  6575. if (ThumbTarget) {
  6576. ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
  6577. ThumbAsmInfo.reset(
  6578. ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
  6579. ThumbSTI.reset(
  6580. ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU,
  6581. FeaturesStr));
  6582. ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
  6583. ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
  6584. MCContext *PtrThumbCtx = ThumbCtx.get();
  6585. ThumbRelInfo.reset(
  6586. ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
  6587. if (ThumbRelInfo) {
  6588. ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
  6589. ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
  6590. &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
  6591. ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
  6592. }
  6593. int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
  6594. ThumbIP.reset(ThumbTarget->createMCInstPrinter(
  6595. Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
  6596. *ThumbInstrInfo, *ThumbMRI));
  6597. // Set the display preference for hex vs. decimal immediates.
  6598. ThumbIP->setPrintImmHex(PrintImmHex);
  6599. }
  6600. if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
  6601. WithColor::error(errs(), "llvm-objdump")
  6602. << "couldn't initialize disassembler for target " << ThumbTripleName
  6603. << '\n';
  6604. return;
  6605. }
  6606. MachO::mach_header Header = MachOOF->getHeader();
  6607. // FIXME: Using the -cfg command line option, this code used to be able to
  6608. // annotate relocations with the referenced symbol's name, and if this was
  6609. // inside a __[cf]string section, the data it points to. This is now replaced
  6610. // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
  6611. std::vector<SectionRef> Sections;
  6612. std::vector<SymbolRef> Symbols;
  6613. SmallVector<uint64_t, 8> FoundFns;
  6614. uint64_t BaseSegmentAddress;
  6615. getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
  6616. BaseSegmentAddress);
  6617. // Sort the symbols by address, just in case they didn't come in that way.
  6618. llvm::sort(Symbols, SymbolSorter());
  6619. // Build a data in code table that is sorted on by the address of each entry.
  6620. uint64_t BaseAddress = 0;
  6621. if (Header.filetype == MachO::MH_OBJECT)
  6622. BaseAddress = Sections[0].getAddress();
  6623. else
  6624. BaseAddress = BaseSegmentAddress;
  6625. DiceTable Dices;
  6626. for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
  6627. DI != DE; ++DI) {
  6628. uint32_t Offset;
  6629. DI->getOffset(Offset);
  6630. Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
  6631. }
  6632. array_pod_sort(Dices.begin(), Dices.end());
  6633. #ifndef NDEBUG
  6634. raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
  6635. #else
  6636. raw_ostream &DebugOut = nulls();
  6637. #endif
  6638. std::unique_ptr<DIContext> diContext;
  6639. ObjectFile *DbgObj = MachOOF;
  6640. std::unique_ptr<MemoryBuffer> DSYMBuf;
  6641. // Try to find debug info and set up the DIContext for it.
  6642. if (UseDbg) {
  6643. // A separate DSym file path was specified, parse it as a macho file,
  6644. // get the sections and supply it to the section name parsing machinery.
  6645. if (!DSYMFile.empty()) {
  6646. ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
  6647. MemoryBuffer::getFileOrSTDIN(DSYMFile);
  6648. if (std::error_code EC = BufOrErr.getError()) {
  6649. report_error(errorCodeToError(EC), DSYMFile);
  6650. return;
  6651. }
  6652. std::unique_ptr<MachOObjectFile> DbgObjCheck = unwrapOrError(
  6653. ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef()),
  6654. DSYMFile.getValue());
  6655. DbgObj = DbgObjCheck.release();
  6656. // We need to keep the file alive, because we're replacing DbgObj with it.
  6657. DSYMBuf = std::move(BufOrErr.get());
  6658. }
  6659. // Setup the DIContext
  6660. diContext = DWARFContext::create(*DbgObj);
  6661. }
  6662. if (FilterSections.empty())
  6663. outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
  6664. for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
  6665. StringRef SectName;
  6666. if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
  6667. continue;
  6668. DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
  6669. StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
  6670. if (SegmentName != DisSegName)
  6671. continue;
  6672. StringRef BytesStr =
  6673. unwrapOrError(Sections[SectIdx].getContents(), Filename);
  6674. ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
  6675. uint64_t SectAddress = Sections[SectIdx].getAddress();
  6676. bool symbolTableWorked = false;
  6677. // Create a map of symbol addresses to symbol names for use by
  6678. // the SymbolizerSymbolLookUp() routine.
  6679. SymbolAddressMap AddrMap;
  6680. bool DisSymNameFound = false;
  6681. for (const SymbolRef &Symbol : MachOOF->symbols()) {
  6682. SymbolRef::Type ST =
  6683. unwrapOrError(Symbol.getType(), MachOOF->getFileName());
  6684. if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
  6685. ST == SymbolRef::ST_Other) {
  6686. uint64_t Address = Symbol.getValue();
  6687. StringRef SymName =
  6688. unwrapOrError(Symbol.getName(), MachOOF->getFileName());
  6689. AddrMap[Address] = SymName;
  6690. if (!DisSymName.empty() && DisSymName == SymName)
  6691. DisSymNameFound = true;
  6692. }
  6693. }
  6694. if (!DisSymName.empty() && !DisSymNameFound) {
  6695. outs() << "Can't find -dis-symname: " << DisSymName << "\n";
  6696. return;
  6697. }
  6698. // Set up the block of info used by the Symbolizer call backs.
  6699. SymbolizerInfo.verbose = !NoSymbolicOperands;
  6700. SymbolizerInfo.O = MachOOF;
  6701. SymbolizerInfo.S = Sections[SectIdx];
  6702. SymbolizerInfo.AddrMap = &AddrMap;
  6703. SymbolizerInfo.Sections = &Sections;
  6704. // Same for the ThumbSymbolizer
  6705. ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
  6706. ThumbSymbolizerInfo.O = MachOOF;
  6707. ThumbSymbolizerInfo.S = Sections[SectIdx];
  6708. ThumbSymbolizerInfo.AddrMap = &AddrMap;
  6709. ThumbSymbolizerInfo.Sections = &Sections;
  6710. unsigned int Arch = MachOOF->getArch();
  6711. // Skip all symbols if this is a stubs file.
  6712. if (Bytes.empty())
  6713. return;
  6714. // If the section has symbols but no symbol at the start of the section
  6715. // these are used to make sure the bytes before the first symbol are
  6716. // disassembled.
  6717. bool FirstSymbol = true;
  6718. bool FirstSymbolAtSectionStart = true;
  6719. // Disassemble symbol by symbol.
  6720. for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
  6721. StringRef SymName =
  6722. unwrapOrError(Symbols[SymIdx].getName(), MachOOF->getFileName());
  6723. SymbolRef::Type ST =
  6724. unwrapOrError(Symbols[SymIdx].getType(), MachOOF->getFileName());
  6725. if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
  6726. continue;
  6727. // Make sure the symbol is defined in this section.
  6728. bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
  6729. if (!containsSym) {
  6730. if (!DisSymName.empty() && DisSymName == SymName) {
  6731. outs() << "-dis-symname: " << DisSymName << " not in the section\n";
  6732. return;
  6733. }
  6734. continue;
  6735. }
  6736. // The __mh_execute_header is special and we need to deal with that fact
  6737. // this symbol is before the start of the (__TEXT,__text) section and at the
  6738. // address of the start of the __TEXT segment. This is because this symbol
  6739. // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
  6740. // start of the section in a standard MH_EXECUTE filetype.
  6741. if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
  6742. outs() << "-dis-symname: __mh_execute_header not in any section\n";
  6743. return;
  6744. }
  6745. // When this code is trying to disassemble a symbol at a time and in the
  6746. // case there is only the __mh_execute_header symbol left as in a stripped
  6747. // executable, we need to deal with this by ignoring this symbol so the
  6748. // whole section is disassembled and this symbol is then not displayed.
  6749. if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
  6750. SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
  6751. SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
  6752. continue;
  6753. // If we are only disassembling one symbol see if this is that symbol.
  6754. if (!DisSymName.empty() && DisSymName != SymName)
  6755. continue;
  6756. // Start at the address of the symbol relative to the section's address.
  6757. uint64_t SectSize = Sections[SectIdx].getSize();
  6758. uint64_t Start = Symbols[SymIdx].getValue();
  6759. uint64_t SectionAddress = Sections[SectIdx].getAddress();
  6760. Start -= SectionAddress;
  6761. if (Start > SectSize) {
  6762. outs() << "section data ends, " << SymName
  6763. << " lies outside valid range\n";
  6764. return;
  6765. }
  6766. // Stop disassembling either at the beginning of the next symbol or at
  6767. // the end of the section.
  6768. bool containsNextSym = false;
  6769. uint64_t NextSym = 0;
  6770. uint64_t NextSymIdx = SymIdx + 1;
  6771. while (Symbols.size() > NextSymIdx) {
  6772. SymbolRef::Type NextSymType = unwrapOrError(
  6773. Symbols[NextSymIdx].getType(), MachOOF->getFileName());
  6774. if (NextSymType == SymbolRef::ST_Function) {
  6775. containsNextSym =
  6776. Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
  6777. NextSym = Symbols[NextSymIdx].getValue();
  6778. NextSym -= SectionAddress;
  6779. break;
  6780. }
  6781. ++NextSymIdx;
  6782. }
  6783. uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
  6784. uint64_t Size;
  6785. symbolTableWorked = true;
  6786. DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
  6787. bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
  6788. // We only need the dedicated Thumb target if there's a real choice
  6789. // (i.e. we're not targeting M-class) and the function is Thumb.
  6790. bool UseThumbTarget = IsThumb && ThumbTarget;
  6791. // If we are not specifying a symbol to start disassembly with and this
  6792. // is the first symbol in the section but not at the start of the section
  6793. // then move the disassembly index to the start of the section and
  6794. // don't print the symbol name just yet. This is so the bytes before the
  6795. // first symbol are disassembled.
  6796. uint64_t SymbolStart = Start;
  6797. if (DisSymName.empty() && FirstSymbol && Start != 0) {
  6798. FirstSymbolAtSectionStart = false;
  6799. Start = 0;
  6800. }
  6801. else
  6802. outs() << SymName << ":\n";
  6803. DILineInfo lastLine;
  6804. for (uint64_t Index = Start; Index < End; Index += Size) {
  6805. MCInst Inst;
  6806. // If this is the first symbol in the section and it was not at the
  6807. // start of the section, see if we are at its Index now and if so print
  6808. // the symbol name.
  6809. if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
  6810. outs() << SymName << ":\n";
  6811. uint64_t PC = SectAddress + Index;
  6812. if (!NoLeadingAddr) {
  6813. if (FullLeadingAddr) {
  6814. if (MachOOF->is64Bit())
  6815. outs() << format("%016" PRIx64, PC);
  6816. else
  6817. outs() << format("%08" PRIx64, PC);
  6818. } else {
  6819. outs() << format("%8" PRIx64 ":", PC);
  6820. }
  6821. }
  6822. if (!NoShowRawInsn || Arch == Triple::arm)
  6823. outs() << "\t";
  6824. // Check the data in code table here to see if this is data not an
  6825. // instruction to be disassembled.
  6826. DiceTable Dice;
  6827. Dice.push_back(std::make_pair(PC, DiceRef()));
  6828. dice_table_iterator DTI =
  6829. std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
  6830. compareDiceTableEntries);
  6831. if (DTI != Dices.end()) {
  6832. uint16_t Length;
  6833. DTI->second.getLength(Length);
  6834. uint16_t Kind;
  6835. DTI->second.getKind(Kind);
  6836. Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
  6837. if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
  6838. (PC == (DTI->first + Length - 1)) && (Length & 1))
  6839. Size++;
  6840. continue;
  6841. }
  6842. SmallVector<char, 64> AnnotationsBytes;
  6843. raw_svector_ostream Annotations(AnnotationsBytes);
  6844. bool gotInst;
  6845. if (UseThumbTarget)
  6846. gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
  6847. PC, DebugOut, Annotations);
  6848. else
  6849. gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
  6850. DebugOut, Annotations);
  6851. if (gotInst) {
  6852. if (!NoShowRawInsn || Arch == Triple::arm) {
  6853. dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
  6854. }
  6855. formatted_raw_ostream FormattedOS(outs());
  6856. StringRef AnnotationsStr = Annotations.str();
  6857. if (UseThumbTarget)
  6858. ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
  6859. else
  6860. IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
  6861. emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
  6862. // Print debug info.
  6863. if (diContext) {
  6864. DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx});
  6865. // Print valid line info if it changed.
  6866. if (dli != lastLine && dli.Line != 0)
  6867. outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
  6868. << dli.Column;
  6869. lastLine = dli;
  6870. }
  6871. outs() << "\n";
  6872. } else {
  6873. unsigned int Arch = MachOOF->getArch();
  6874. if (Arch == Triple::x86_64 || Arch == Triple::x86) {
  6875. outs() << format("\t.byte 0x%02x #bad opcode\n",
  6876. *(Bytes.data() + Index) & 0xff);
  6877. Size = 1; // skip exactly one illegible byte and move on.
  6878. } else if (Arch == Triple::aarch64 ||
  6879. (Arch == Triple::arm && !IsThumb)) {
  6880. uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
  6881. (*(Bytes.data() + Index + 1) & 0xff) << 8 |
  6882. (*(Bytes.data() + Index + 2) & 0xff) << 16 |
  6883. (*(Bytes.data() + Index + 3) & 0xff) << 24;
  6884. outs() << format("\t.long\t0x%08x\n", opcode);
  6885. Size = 4;
  6886. } else if (Arch == Triple::arm) {
  6887. assert(IsThumb && "ARM mode should have been dealt with above");
  6888. uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
  6889. (*(Bytes.data() + Index + 1) & 0xff) << 8;
  6890. outs() << format("\t.short\t0x%04x\n", opcode);
  6891. Size = 2;
  6892. } else{
  6893. WithColor::warning(errs(), "llvm-objdump")
  6894. << "invalid instruction encoding\n";
  6895. if (Size == 0)
  6896. Size = 1; // skip illegible bytes
  6897. }
  6898. }
  6899. }
  6900. // Now that we are done disassembled the first symbol set the bool that
  6901. // were doing this to false.
  6902. FirstSymbol = false;
  6903. }
  6904. if (!symbolTableWorked) {
  6905. // Reading the symbol table didn't work, disassemble the whole section.
  6906. uint64_t SectAddress = Sections[SectIdx].getAddress();
  6907. uint64_t SectSize = Sections[SectIdx].getSize();
  6908. uint64_t InstSize;
  6909. for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
  6910. MCInst Inst;
  6911. uint64_t PC = SectAddress + Index;
  6912. SmallVector<char, 64> AnnotationsBytes;
  6913. raw_svector_ostream Annotations(AnnotationsBytes);
  6914. if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
  6915. DebugOut, Annotations)) {
  6916. if (!NoLeadingAddr) {
  6917. if (FullLeadingAddr) {
  6918. if (MachOOF->is64Bit())
  6919. outs() << format("%016" PRIx64, PC);
  6920. else
  6921. outs() << format("%08" PRIx64, PC);
  6922. } else {
  6923. outs() << format("%8" PRIx64 ":", PC);
  6924. }
  6925. }
  6926. if (!NoShowRawInsn || Arch == Triple::arm) {
  6927. outs() << "\t";
  6928. dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
  6929. }
  6930. StringRef AnnotationsStr = Annotations.str();
  6931. IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
  6932. outs() << "\n";
  6933. } else {
  6934. unsigned int Arch = MachOOF->getArch();
  6935. if (Arch == Triple::x86_64 || Arch == Triple::x86) {
  6936. outs() << format("\t.byte 0x%02x #bad opcode\n",
  6937. *(Bytes.data() + Index) & 0xff);
  6938. InstSize = 1; // skip exactly one illegible byte and move on.
  6939. } else {
  6940. WithColor::warning(errs(), "llvm-objdump")
  6941. << "invalid instruction encoding\n";
  6942. if (InstSize == 0)
  6943. InstSize = 1; // skip illegible bytes
  6944. }
  6945. }
  6946. }
  6947. }
  6948. // The TripleName's need to be reset if we are called again for a different
  6949. // archtecture.
  6950. TripleName = "";
  6951. ThumbTripleName = "";
  6952. if (SymbolizerInfo.demangled_name != nullptr)
  6953. free(SymbolizerInfo.demangled_name);
  6954. if (ThumbSymbolizerInfo.demangled_name != nullptr)
  6955. free(ThumbSymbolizerInfo.demangled_name);
  6956. }
  6957. }
  6958. //===----------------------------------------------------------------------===//
  6959. // __compact_unwind section dumping
  6960. //===----------------------------------------------------------------------===//
  6961. namespace {
  6962. template <typename T>
  6963. static uint64_t read(StringRef Contents, ptrdiff_t Offset) {
  6964. using llvm::support::little;
  6965. using llvm::support::unaligned;
  6966. if (Offset + sizeof(T) > Contents.size()) {
  6967. outs() << "warning: attempt to read past end of buffer\n";
  6968. return T();
  6969. }
  6970. uint64_t Val =
  6971. support::endian::read<T, little, unaligned>(Contents.data() + Offset);
  6972. return Val;
  6973. }
  6974. template <typename T>
  6975. static uint64_t readNext(StringRef Contents, ptrdiff_t &Offset) {
  6976. T Val = read<T>(Contents, Offset);
  6977. Offset += sizeof(T);
  6978. return Val;
  6979. }
  6980. struct CompactUnwindEntry {
  6981. uint32_t OffsetInSection;
  6982. uint64_t FunctionAddr;
  6983. uint32_t Length;
  6984. uint32_t CompactEncoding;
  6985. uint64_t PersonalityAddr;
  6986. uint64_t LSDAAddr;
  6987. RelocationRef FunctionReloc;
  6988. RelocationRef PersonalityReloc;
  6989. RelocationRef LSDAReloc;
  6990. CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
  6991. : OffsetInSection(Offset) {
  6992. if (Is64)
  6993. read<uint64_t>(Contents, Offset);
  6994. else
  6995. read<uint32_t>(Contents, Offset);
  6996. }
  6997. private:
  6998. template <typename UIntPtr> void read(StringRef Contents, ptrdiff_t Offset) {
  6999. FunctionAddr = readNext<UIntPtr>(Contents, Offset);
  7000. Length = readNext<uint32_t>(Contents, Offset);
  7001. CompactEncoding = readNext<uint32_t>(Contents, Offset);
  7002. PersonalityAddr = readNext<UIntPtr>(Contents, Offset);
  7003. LSDAAddr = readNext<UIntPtr>(Contents, Offset);
  7004. }
  7005. };
  7006. }
  7007. /// Given a relocation from __compact_unwind, consisting of the RelocationRef
  7008. /// and data being relocated, determine the best base Name and Addend to use for
  7009. /// display purposes.
  7010. ///
  7011. /// 1. An Extern relocation will directly reference a symbol (and the data is
  7012. /// then already an addend), so use that.
  7013. /// 2. Otherwise the data is an offset in the object file's layout; try to find
  7014. // a symbol before it in the same section, and use the offset from there.
  7015. /// 3. Finally, if all that fails, fall back to an offset from the start of the
  7016. /// referenced section.
  7017. static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
  7018. std::map<uint64_t, SymbolRef> &Symbols,
  7019. const RelocationRef &Reloc, uint64_t Addr,
  7020. StringRef &Name, uint64_t &Addend) {
  7021. if (Reloc.getSymbol() != Obj->symbol_end()) {
  7022. Name = unwrapOrError(Reloc.getSymbol()->getName(), Obj->getFileName());
  7023. Addend = Addr;
  7024. return;
  7025. }
  7026. auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
  7027. SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
  7028. uint64_t SectionAddr = RelocSection.getAddress();
  7029. auto Sym = Symbols.upper_bound(Addr);
  7030. if (Sym == Symbols.begin()) {
  7031. // The first symbol in the object is after this reference, the best we can
  7032. // do is section-relative notation.
  7033. RelocSection.getName(Name);
  7034. Addend = Addr - SectionAddr;
  7035. return;
  7036. }
  7037. // Go back one so that SymbolAddress <= Addr.
  7038. --Sym;
  7039. section_iterator SymSection =
  7040. unwrapOrError(Sym->second.getSection(), Obj->getFileName());
  7041. if (RelocSection == *SymSection) {
  7042. // There's a valid symbol in the same section before this reference.
  7043. Name = unwrapOrError(Sym->second.getName(), Obj->getFileName());
  7044. Addend = Addr - Sym->first;
  7045. return;
  7046. }
  7047. // There is a symbol before this reference, but it's in a different
  7048. // section. Probably not helpful to mention it, so use the section name.
  7049. RelocSection.getName(Name);
  7050. Addend = Addr - SectionAddr;
  7051. }
  7052. static void printUnwindRelocDest(const MachOObjectFile *Obj,
  7053. std::map<uint64_t, SymbolRef> &Symbols,
  7054. const RelocationRef &Reloc, uint64_t Addr) {
  7055. StringRef Name;
  7056. uint64_t Addend;
  7057. if (!Reloc.getObject())
  7058. return;
  7059. findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
  7060. outs() << Name;
  7061. if (Addend)
  7062. outs() << " + " << format("0x%" PRIx64, Addend);
  7063. }
  7064. static void
  7065. printMachOCompactUnwindSection(const MachOObjectFile *Obj,
  7066. std::map<uint64_t, SymbolRef> &Symbols,
  7067. const SectionRef &CompactUnwind) {
  7068. if (!Obj->isLittleEndian()) {
  7069. outs() << "Skipping big-endian __compact_unwind section\n";
  7070. return;
  7071. }
  7072. bool Is64 = Obj->is64Bit();
  7073. uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
  7074. uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
  7075. StringRef Contents =
  7076. unwrapOrError(CompactUnwind.getContents(), Obj->getFileName());
  7077. SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
  7078. // First populate the initial raw offsets, encodings and so on from the entry.
  7079. for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
  7080. CompactUnwindEntry Entry(Contents, Offset, Is64);
  7081. CompactUnwinds.push_back(Entry);
  7082. }
  7083. // Next we need to look at the relocations to find out what objects are
  7084. // actually being referred to.
  7085. for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
  7086. uint64_t RelocAddress = Reloc.getOffset();
  7087. uint32_t EntryIdx = RelocAddress / EntrySize;
  7088. uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
  7089. CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
  7090. if (OffsetInEntry == 0)
  7091. Entry.FunctionReloc = Reloc;
  7092. else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
  7093. Entry.PersonalityReloc = Reloc;
  7094. else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
  7095. Entry.LSDAReloc = Reloc;
  7096. else {
  7097. outs() << "Invalid relocation in __compact_unwind section\n";
  7098. return;
  7099. }
  7100. }
  7101. // Finally, we're ready to print the data we've gathered.
  7102. outs() << "Contents of __compact_unwind section:\n";
  7103. for (auto &Entry : CompactUnwinds) {
  7104. outs() << " Entry at offset "
  7105. << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
  7106. // 1. Start of the region this entry applies to.
  7107. outs() << " start: " << format("0x%" PRIx64,
  7108. Entry.FunctionAddr) << ' ';
  7109. printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
  7110. outs() << '\n';
  7111. // 2. Length of the region this entry applies to.
  7112. outs() << " length: " << format("0x%" PRIx32, Entry.Length)
  7113. << '\n';
  7114. // 3. The 32-bit compact encoding.
  7115. outs() << " compact encoding: "
  7116. << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
  7117. // 4. The personality function, if present.
  7118. if (Entry.PersonalityReloc.getObject()) {
  7119. outs() << " personality function: "
  7120. << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
  7121. printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
  7122. Entry.PersonalityAddr);
  7123. outs() << '\n';
  7124. }
  7125. // 5. This entry's language-specific data area.
  7126. if (Entry.LSDAReloc.getObject()) {
  7127. outs() << " LSDA: " << format("0x%" PRIx64,
  7128. Entry.LSDAAddr) << ' ';
  7129. printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
  7130. outs() << '\n';
  7131. }
  7132. }
  7133. }
  7134. //===----------------------------------------------------------------------===//
  7135. // __unwind_info section dumping
  7136. //===----------------------------------------------------------------------===//
  7137. static void printRegularSecondLevelUnwindPage(StringRef PageData) {
  7138. ptrdiff_t Pos = 0;
  7139. uint32_t Kind = readNext<uint32_t>(PageData, Pos);
  7140. (void)Kind;
  7141. assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
  7142. uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
  7143. uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
  7144. Pos = EntriesStart;
  7145. for (unsigned i = 0; i < NumEntries; ++i) {
  7146. uint32_t FunctionOffset = readNext<uint32_t>(PageData, Pos);
  7147. uint32_t Encoding = readNext<uint32_t>(PageData, Pos);
  7148. outs() << " [" << i << "]: "
  7149. << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
  7150. << ", "
  7151. << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
  7152. }
  7153. }
  7154. static void printCompressedSecondLevelUnwindPage(
  7155. StringRef PageData, uint32_t FunctionBase,
  7156. const SmallVectorImpl<uint32_t> &CommonEncodings) {
  7157. ptrdiff_t Pos = 0;
  7158. uint32_t Kind = readNext<uint32_t>(PageData, Pos);
  7159. (void)Kind;
  7160. assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
  7161. uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
  7162. uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
  7163. uint16_t EncodingsStart = readNext<uint16_t>(PageData, Pos);
  7164. readNext<uint16_t>(PageData, Pos);
  7165. StringRef PageEncodings = PageData.substr(EncodingsStart, StringRef::npos);
  7166. Pos = EntriesStart;
  7167. for (unsigned i = 0; i < NumEntries; ++i) {
  7168. uint32_t Entry = readNext<uint32_t>(PageData, Pos);
  7169. uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
  7170. uint32_t EncodingIdx = Entry >> 24;
  7171. uint32_t Encoding;
  7172. if (EncodingIdx < CommonEncodings.size())
  7173. Encoding = CommonEncodings[EncodingIdx];
  7174. else
  7175. Encoding = read<uint32_t>(PageEncodings,
  7176. sizeof(uint32_t) *
  7177. (EncodingIdx - CommonEncodings.size()));
  7178. outs() << " [" << i << "]: "
  7179. << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
  7180. << ", "
  7181. << "encoding[" << EncodingIdx
  7182. << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
  7183. }
  7184. }
  7185. static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
  7186. std::map<uint64_t, SymbolRef> &Symbols,
  7187. const SectionRef &UnwindInfo) {
  7188. if (!Obj->isLittleEndian()) {
  7189. outs() << "Skipping big-endian __unwind_info section\n";
  7190. return;
  7191. }
  7192. outs() << "Contents of __unwind_info section:\n";
  7193. StringRef Contents =
  7194. unwrapOrError(UnwindInfo.getContents(), Obj->getFileName());
  7195. ptrdiff_t Pos = 0;
  7196. //===----------------------------------
  7197. // Section header
  7198. //===----------------------------------
  7199. uint32_t Version = readNext<uint32_t>(Contents, Pos);
  7200. outs() << " Version: "
  7201. << format("0x%" PRIx32, Version) << '\n';
  7202. if (Version != 1) {
  7203. outs() << " Skipping section with unknown version\n";
  7204. return;
  7205. }
  7206. uint32_t CommonEncodingsStart = readNext<uint32_t>(Contents, Pos);
  7207. outs() << " Common encodings array section offset: "
  7208. << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
  7209. uint32_t NumCommonEncodings = readNext<uint32_t>(Contents, Pos);
  7210. outs() << " Number of common encodings in array: "
  7211. << format("0x%" PRIx32, NumCommonEncodings) << '\n';
  7212. uint32_t PersonalitiesStart = readNext<uint32_t>(Contents, Pos);
  7213. outs() << " Personality function array section offset: "
  7214. << format("0x%" PRIx32, PersonalitiesStart) << '\n';
  7215. uint32_t NumPersonalities = readNext<uint32_t>(Contents, Pos);
  7216. outs() << " Number of personality functions in array: "
  7217. << format("0x%" PRIx32, NumPersonalities) << '\n';
  7218. uint32_t IndicesStart = readNext<uint32_t>(Contents, Pos);
  7219. outs() << " Index array section offset: "
  7220. << format("0x%" PRIx32, IndicesStart) << '\n';
  7221. uint32_t NumIndices = readNext<uint32_t>(Contents, Pos);
  7222. outs() << " Number of indices in array: "
  7223. << format("0x%" PRIx32, NumIndices) << '\n';
  7224. //===----------------------------------
  7225. // A shared list of common encodings
  7226. //===----------------------------------
  7227. // These occupy indices in the range [0, N] whenever an encoding is referenced
  7228. // from a compressed 2nd level index table. In practice the linker only
  7229. // creates ~128 of these, so that indices are available to embed encodings in
  7230. // the 2nd level index.
  7231. SmallVector<uint32_t, 64> CommonEncodings;
  7232. outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
  7233. Pos = CommonEncodingsStart;
  7234. for (unsigned i = 0; i < NumCommonEncodings; ++i) {
  7235. uint32_t Encoding = readNext<uint32_t>(Contents, Pos);
  7236. CommonEncodings.push_back(Encoding);
  7237. outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
  7238. << '\n';
  7239. }
  7240. //===----------------------------------
  7241. // Personality functions used in this executable
  7242. //===----------------------------------
  7243. // There should be only a handful of these (one per source language,
  7244. // roughly). Particularly since they only get 2 bits in the compact encoding.
  7245. outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
  7246. Pos = PersonalitiesStart;
  7247. for (unsigned i = 0; i < NumPersonalities; ++i) {
  7248. uint32_t PersonalityFn = readNext<uint32_t>(Contents, Pos);
  7249. outs() << " personality[" << i + 1
  7250. << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
  7251. }
  7252. //===----------------------------------
  7253. // The level 1 index entries
  7254. //===----------------------------------
  7255. // These specify an approximate place to start searching for the more detailed
  7256. // information, sorted by PC.
  7257. struct IndexEntry {
  7258. uint32_t FunctionOffset;
  7259. uint32_t SecondLevelPageStart;
  7260. uint32_t LSDAStart;
  7261. };
  7262. SmallVector<IndexEntry, 4> IndexEntries;
  7263. outs() << " Top level indices: (count = " << NumIndices << ")\n";
  7264. Pos = IndicesStart;
  7265. for (unsigned i = 0; i < NumIndices; ++i) {
  7266. IndexEntry Entry;
  7267. Entry.FunctionOffset = readNext<uint32_t>(Contents, Pos);
  7268. Entry.SecondLevelPageStart = readNext<uint32_t>(Contents, Pos);
  7269. Entry.LSDAStart = readNext<uint32_t>(Contents, Pos);
  7270. IndexEntries.push_back(Entry);
  7271. outs() << " [" << i << "]: "
  7272. << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
  7273. << ", "
  7274. << "2nd level page offset="
  7275. << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
  7276. << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
  7277. }
  7278. //===----------------------------------
  7279. // Next come the LSDA tables
  7280. //===----------------------------------
  7281. // The LSDA layout is rather implicit: it's a contiguous array of entries from
  7282. // the first top-level index's LSDAOffset to the last (sentinel).
  7283. outs() << " LSDA descriptors:\n";
  7284. Pos = IndexEntries[0].LSDAStart;
  7285. const uint32_t LSDASize = 2 * sizeof(uint32_t);
  7286. int NumLSDAs =
  7287. (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / LSDASize;
  7288. for (int i = 0; i < NumLSDAs; ++i) {
  7289. uint32_t FunctionOffset = readNext<uint32_t>(Contents, Pos);
  7290. uint32_t LSDAOffset = readNext<uint32_t>(Contents, Pos);
  7291. outs() << " [" << i << "]: "
  7292. << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
  7293. << ", "
  7294. << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
  7295. }
  7296. //===----------------------------------
  7297. // Finally, the 2nd level indices
  7298. //===----------------------------------
  7299. // Generally these are 4K in size, and have 2 possible forms:
  7300. // + Regular stores up to 511 entries with disparate encodings
  7301. // + Compressed stores up to 1021 entries if few enough compact encoding
  7302. // values are used.
  7303. outs() << " Second level indices:\n";
  7304. for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
  7305. // The final sentinel top-level index has no associated 2nd level page
  7306. if (IndexEntries[i].SecondLevelPageStart == 0)
  7307. break;
  7308. outs() << " Second level index[" << i << "]: "
  7309. << "offset in section="
  7310. << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
  7311. << ", "
  7312. << "base function offset="
  7313. << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
  7314. Pos = IndexEntries[i].SecondLevelPageStart;
  7315. if (Pos + sizeof(uint32_t) > Contents.size()) {
  7316. outs() << "warning: invalid offset for second level page: " << Pos << '\n';
  7317. continue;
  7318. }
  7319. uint32_t Kind =
  7320. *reinterpret_cast<const support::ulittle32_t *>(Contents.data() + Pos);
  7321. if (Kind == 2)
  7322. printRegularSecondLevelUnwindPage(Contents.substr(Pos, 4096));
  7323. else if (Kind == 3)
  7324. printCompressedSecondLevelUnwindPage(Contents.substr(Pos, 4096),
  7325. IndexEntries[i].FunctionOffset,
  7326. CommonEncodings);
  7327. else
  7328. outs() << " Skipping 2nd level page with unknown kind " << Kind
  7329. << '\n';
  7330. }
  7331. }
  7332. void printMachOUnwindInfo(const MachOObjectFile *Obj) {
  7333. std::map<uint64_t, SymbolRef> Symbols;
  7334. for (const SymbolRef &SymRef : Obj->symbols()) {
  7335. // Discard any undefined or absolute symbols. They're not going to take part
  7336. // in the convenience lookup for unwind info and just take up resources.
  7337. auto SectOrErr = SymRef.getSection();
  7338. if (!SectOrErr) {
  7339. // TODO: Actually report errors helpfully.
  7340. consumeError(SectOrErr.takeError());
  7341. continue;
  7342. }
  7343. section_iterator Section = *SectOrErr;
  7344. if (Section == Obj->section_end())
  7345. continue;
  7346. uint64_t Addr = SymRef.getValue();
  7347. Symbols.insert(std::make_pair(Addr, SymRef));
  7348. }
  7349. for (const SectionRef &Section : Obj->sections()) {
  7350. StringRef SectName;
  7351. Section.getName(SectName);
  7352. if (SectName == "__compact_unwind")
  7353. printMachOCompactUnwindSection(Obj, Symbols, Section);
  7354. else if (SectName == "__unwind_info")
  7355. printMachOUnwindInfoSection(Obj, Symbols, Section);
  7356. }
  7357. }
  7358. static void PrintMachHeader(uint32_t magic, uint32_t cputype,
  7359. uint32_t cpusubtype, uint32_t filetype,
  7360. uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
  7361. bool verbose) {
  7362. outs() << "Mach header\n";
  7363. outs() << " magic cputype cpusubtype caps filetype ncmds "
  7364. "sizeofcmds flags\n";
  7365. if (verbose) {
  7366. if (magic == MachO::MH_MAGIC)
  7367. outs() << " MH_MAGIC";
  7368. else if (magic == MachO::MH_MAGIC_64)
  7369. outs() << "MH_MAGIC_64";
  7370. else
  7371. outs() << format(" 0x%08" PRIx32, magic);
  7372. switch (cputype) {
  7373. case MachO::CPU_TYPE_I386:
  7374. outs() << " I386";
  7375. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  7376. case MachO::CPU_SUBTYPE_I386_ALL:
  7377. outs() << " ALL";
  7378. break;
  7379. default:
  7380. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  7381. break;
  7382. }
  7383. break;
  7384. case MachO::CPU_TYPE_X86_64:
  7385. outs() << " X86_64";
  7386. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  7387. case MachO::CPU_SUBTYPE_X86_64_ALL:
  7388. outs() << " ALL";
  7389. break;
  7390. case MachO::CPU_SUBTYPE_X86_64_H:
  7391. outs() << " Haswell";
  7392. break;
  7393. default:
  7394. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  7395. break;
  7396. }
  7397. break;
  7398. case MachO::CPU_TYPE_ARM:
  7399. outs() << " ARM";
  7400. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  7401. case MachO::CPU_SUBTYPE_ARM_ALL:
  7402. outs() << " ALL";
  7403. break;
  7404. case MachO::CPU_SUBTYPE_ARM_V4T:
  7405. outs() << " V4T";
  7406. break;
  7407. case MachO::CPU_SUBTYPE_ARM_V5TEJ:
  7408. outs() << " V5TEJ";
  7409. break;
  7410. case MachO::CPU_SUBTYPE_ARM_XSCALE:
  7411. outs() << " XSCALE";
  7412. break;
  7413. case MachO::CPU_SUBTYPE_ARM_V6:
  7414. outs() << " V6";
  7415. break;
  7416. case MachO::CPU_SUBTYPE_ARM_V6M:
  7417. outs() << " V6M";
  7418. break;
  7419. case MachO::CPU_SUBTYPE_ARM_V7:
  7420. outs() << " V7";
  7421. break;
  7422. case MachO::CPU_SUBTYPE_ARM_V7EM:
  7423. outs() << " V7EM";
  7424. break;
  7425. case MachO::CPU_SUBTYPE_ARM_V7K:
  7426. outs() << " V7K";
  7427. break;
  7428. case MachO::CPU_SUBTYPE_ARM_V7M:
  7429. outs() << " V7M";
  7430. break;
  7431. case MachO::CPU_SUBTYPE_ARM_V7S:
  7432. outs() << " V7S";
  7433. break;
  7434. default:
  7435. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  7436. break;
  7437. }
  7438. break;
  7439. case MachO::CPU_TYPE_ARM64:
  7440. outs() << " ARM64";
  7441. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  7442. case MachO::CPU_SUBTYPE_ARM64_ALL:
  7443. outs() << " ALL";
  7444. break;
  7445. case MachO::CPU_SUBTYPE_ARM64E:
  7446. outs() << " E";
  7447. break;
  7448. default:
  7449. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  7450. break;
  7451. }
  7452. break;
  7453. case MachO::CPU_TYPE_ARM64_32:
  7454. outs() << " ARM64_32";
  7455. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  7456. case MachO::CPU_SUBTYPE_ARM64_32_V8:
  7457. outs() << " V8";
  7458. break;
  7459. default:
  7460. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  7461. break;
  7462. }
  7463. break;
  7464. case MachO::CPU_TYPE_POWERPC:
  7465. outs() << " PPC";
  7466. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  7467. case MachO::CPU_SUBTYPE_POWERPC_ALL:
  7468. outs() << " ALL";
  7469. break;
  7470. default:
  7471. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  7472. break;
  7473. }
  7474. break;
  7475. case MachO::CPU_TYPE_POWERPC64:
  7476. outs() << " PPC64";
  7477. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  7478. case MachO::CPU_SUBTYPE_POWERPC_ALL:
  7479. outs() << " ALL";
  7480. break;
  7481. default:
  7482. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  7483. break;
  7484. }
  7485. break;
  7486. default:
  7487. outs() << format(" %7d", cputype);
  7488. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  7489. break;
  7490. }
  7491. if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
  7492. outs() << " LIB64";
  7493. } else {
  7494. outs() << format(" 0x%02" PRIx32,
  7495. (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
  7496. }
  7497. switch (filetype) {
  7498. case MachO::MH_OBJECT:
  7499. outs() << " OBJECT";
  7500. break;
  7501. case MachO::MH_EXECUTE:
  7502. outs() << " EXECUTE";
  7503. break;
  7504. case MachO::MH_FVMLIB:
  7505. outs() << " FVMLIB";
  7506. break;
  7507. case MachO::MH_CORE:
  7508. outs() << " CORE";
  7509. break;
  7510. case MachO::MH_PRELOAD:
  7511. outs() << " PRELOAD";
  7512. break;
  7513. case MachO::MH_DYLIB:
  7514. outs() << " DYLIB";
  7515. break;
  7516. case MachO::MH_DYLIB_STUB:
  7517. outs() << " DYLIB_STUB";
  7518. break;
  7519. case MachO::MH_DYLINKER:
  7520. outs() << " DYLINKER";
  7521. break;
  7522. case MachO::MH_BUNDLE:
  7523. outs() << " BUNDLE";
  7524. break;
  7525. case MachO::MH_DSYM:
  7526. outs() << " DSYM";
  7527. break;
  7528. case MachO::MH_KEXT_BUNDLE:
  7529. outs() << " KEXTBUNDLE";
  7530. break;
  7531. default:
  7532. outs() << format(" %10u", filetype);
  7533. break;
  7534. }
  7535. outs() << format(" %5u", ncmds);
  7536. outs() << format(" %10u", sizeofcmds);
  7537. uint32_t f = flags;
  7538. if (f & MachO::MH_NOUNDEFS) {
  7539. outs() << " NOUNDEFS";
  7540. f &= ~MachO::MH_NOUNDEFS;
  7541. }
  7542. if (f & MachO::MH_INCRLINK) {
  7543. outs() << " INCRLINK";
  7544. f &= ~MachO::MH_INCRLINK;
  7545. }
  7546. if (f & MachO::MH_DYLDLINK) {
  7547. outs() << " DYLDLINK";
  7548. f &= ~MachO::MH_DYLDLINK;
  7549. }
  7550. if (f & MachO::MH_BINDATLOAD) {
  7551. outs() << " BINDATLOAD";
  7552. f &= ~MachO::MH_BINDATLOAD;
  7553. }
  7554. if (f & MachO::MH_PREBOUND) {
  7555. outs() << " PREBOUND";
  7556. f &= ~MachO::MH_PREBOUND;
  7557. }
  7558. if (f & MachO::MH_SPLIT_SEGS) {
  7559. outs() << " SPLIT_SEGS";
  7560. f &= ~MachO::MH_SPLIT_SEGS;
  7561. }
  7562. if (f & MachO::MH_LAZY_INIT) {
  7563. outs() << " LAZY_INIT";
  7564. f &= ~MachO::MH_LAZY_INIT;
  7565. }
  7566. if (f & MachO::MH_TWOLEVEL) {
  7567. outs() << " TWOLEVEL";
  7568. f &= ~MachO::MH_TWOLEVEL;
  7569. }
  7570. if (f & MachO::MH_FORCE_FLAT) {
  7571. outs() << " FORCE_FLAT";
  7572. f &= ~MachO::MH_FORCE_FLAT;
  7573. }
  7574. if (f & MachO::MH_NOMULTIDEFS) {
  7575. outs() << " NOMULTIDEFS";
  7576. f &= ~MachO::MH_NOMULTIDEFS;
  7577. }
  7578. if (f & MachO::MH_NOFIXPREBINDING) {
  7579. outs() << " NOFIXPREBINDING";
  7580. f &= ~MachO::MH_NOFIXPREBINDING;
  7581. }
  7582. if (f & MachO::MH_PREBINDABLE) {
  7583. outs() << " PREBINDABLE";
  7584. f &= ~MachO::MH_PREBINDABLE;
  7585. }
  7586. if (f & MachO::MH_ALLMODSBOUND) {
  7587. outs() << " ALLMODSBOUND";
  7588. f &= ~MachO::MH_ALLMODSBOUND;
  7589. }
  7590. if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
  7591. outs() << " SUBSECTIONS_VIA_SYMBOLS";
  7592. f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
  7593. }
  7594. if (f & MachO::MH_CANONICAL) {
  7595. outs() << " CANONICAL";
  7596. f &= ~MachO::MH_CANONICAL;
  7597. }
  7598. if (f & MachO::MH_WEAK_DEFINES) {
  7599. outs() << " WEAK_DEFINES";
  7600. f &= ~MachO::MH_WEAK_DEFINES;
  7601. }
  7602. if (f & MachO::MH_BINDS_TO_WEAK) {
  7603. outs() << " BINDS_TO_WEAK";
  7604. f &= ~MachO::MH_BINDS_TO_WEAK;
  7605. }
  7606. if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
  7607. outs() << " ALLOW_STACK_EXECUTION";
  7608. f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
  7609. }
  7610. if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
  7611. outs() << " DEAD_STRIPPABLE_DYLIB";
  7612. f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
  7613. }
  7614. if (f & MachO::MH_PIE) {
  7615. outs() << " PIE";
  7616. f &= ~MachO::MH_PIE;
  7617. }
  7618. if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
  7619. outs() << " NO_REEXPORTED_DYLIBS";
  7620. f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
  7621. }
  7622. if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
  7623. outs() << " MH_HAS_TLV_DESCRIPTORS";
  7624. f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
  7625. }
  7626. if (f & MachO::MH_NO_HEAP_EXECUTION) {
  7627. outs() << " MH_NO_HEAP_EXECUTION";
  7628. f &= ~MachO::MH_NO_HEAP_EXECUTION;
  7629. }
  7630. if (f & MachO::MH_APP_EXTENSION_SAFE) {
  7631. outs() << " APP_EXTENSION_SAFE";
  7632. f &= ~MachO::MH_APP_EXTENSION_SAFE;
  7633. }
  7634. if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
  7635. outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
  7636. f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
  7637. }
  7638. if (f != 0 || flags == 0)
  7639. outs() << format(" 0x%08" PRIx32, f);
  7640. } else {
  7641. outs() << format(" 0x%08" PRIx32, magic);
  7642. outs() << format(" %7d", cputype);
  7643. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  7644. outs() << format(" 0x%02" PRIx32,
  7645. (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
  7646. outs() << format(" %10u", filetype);
  7647. outs() << format(" %5u", ncmds);
  7648. outs() << format(" %10u", sizeofcmds);
  7649. outs() << format(" 0x%08" PRIx32, flags);
  7650. }
  7651. outs() << "\n";
  7652. }
  7653. static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
  7654. StringRef SegName, uint64_t vmaddr,
  7655. uint64_t vmsize, uint64_t fileoff,
  7656. uint64_t filesize, uint32_t maxprot,
  7657. uint32_t initprot, uint32_t nsects,
  7658. uint32_t flags, uint32_t object_size,
  7659. bool verbose) {
  7660. uint64_t expected_cmdsize;
  7661. if (cmd == MachO::LC_SEGMENT) {
  7662. outs() << " cmd LC_SEGMENT\n";
  7663. expected_cmdsize = nsects;
  7664. expected_cmdsize *= sizeof(struct MachO::section);
  7665. expected_cmdsize += sizeof(struct MachO::segment_command);
  7666. } else {
  7667. outs() << " cmd LC_SEGMENT_64\n";
  7668. expected_cmdsize = nsects;
  7669. expected_cmdsize *= sizeof(struct MachO::section_64);
  7670. expected_cmdsize += sizeof(struct MachO::segment_command_64);
  7671. }
  7672. outs() << " cmdsize " << cmdsize;
  7673. if (cmdsize != expected_cmdsize)
  7674. outs() << " Inconsistent size\n";
  7675. else
  7676. outs() << "\n";
  7677. outs() << " segname " << SegName << "\n";
  7678. if (cmd == MachO::LC_SEGMENT_64) {
  7679. outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
  7680. outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
  7681. } else {
  7682. outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
  7683. outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
  7684. }
  7685. outs() << " fileoff " << fileoff;
  7686. if (fileoff > object_size)
  7687. outs() << " (past end of file)\n";
  7688. else
  7689. outs() << "\n";
  7690. outs() << " filesize " << filesize;
  7691. if (fileoff + filesize > object_size)
  7692. outs() << " (past end of file)\n";
  7693. else
  7694. outs() << "\n";
  7695. if (verbose) {
  7696. if ((maxprot &
  7697. ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
  7698. MachO::VM_PROT_EXECUTE)) != 0)
  7699. outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
  7700. else {
  7701. outs() << " maxprot ";
  7702. outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
  7703. outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
  7704. outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
  7705. }
  7706. if ((initprot &
  7707. ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
  7708. MachO::VM_PROT_EXECUTE)) != 0)
  7709. outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
  7710. else {
  7711. outs() << " initprot ";
  7712. outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
  7713. outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
  7714. outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
  7715. }
  7716. } else {
  7717. outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
  7718. outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
  7719. }
  7720. outs() << " nsects " << nsects << "\n";
  7721. if (verbose) {
  7722. outs() << " flags";
  7723. if (flags == 0)
  7724. outs() << " (none)\n";
  7725. else {
  7726. if (flags & MachO::SG_HIGHVM) {
  7727. outs() << " HIGHVM";
  7728. flags &= ~MachO::SG_HIGHVM;
  7729. }
  7730. if (flags & MachO::SG_FVMLIB) {
  7731. outs() << " FVMLIB";
  7732. flags &= ~MachO::SG_FVMLIB;
  7733. }
  7734. if (flags & MachO::SG_NORELOC) {
  7735. outs() << " NORELOC";
  7736. flags &= ~MachO::SG_NORELOC;
  7737. }
  7738. if (flags & MachO::SG_PROTECTED_VERSION_1) {
  7739. outs() << " PROTECTED_VERSION_1";
  7740. flags &= ~MachO::SG_PROTECTED_VERSION_1;
  7741. }
  7742. if (flags)
  7743. outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
  7744. else
  7745. outs() << "\n";
  7746. }
  7747. } else {
  7748. outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
  7749. }
  7750. }
  7751. static void PrintSection(const char *sectname, const char *segname,
  7752. uint64_t addr, uint64_t size, uint32_t offset,
  7753. uint32_t align, uint32_t reloff, uint32_t nreloc,
  7754. uint32_t flags, uint32_t reserved1, uint32_t reserved2,
  7755. uint32_t cmd, const char *sg_segname,
  7756. uint32_t filetype, uint32_t object_size,
  7757. bool verbose) {
  7758. outs() << "Section\n";
  7759. outs() << " sectname " << format("%.16s\n", sectname);
  7760. outs() << " segname " << format("%.16s", segname);
  7761. if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
  7762. outs() << " (does not match segment)\n";
  7763. else
  7764. outs() << "\n";
  7765. if (cmd == MachO::LC_SEGMENT_64) {
  7766. outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
  7767. outs() << " size " << format("0x%016" PRIx64, size);
  7768. } else {
  7769. outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
  7770. outs() << " size " << format("0x%08" PRIx64, size);
  7771. }
  7772. if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
  7773. outs() << " (past end of file)\n";
  7774. else
  7775. outs() << "\n";
  7776. outs() << " offset " << offset;
  7777. if (offset > object_size)
  7778. outs() << " (past end of file)\n";
  7779. else
  7780. outs() << "\n";
  7781. uint32_t align_shifted = 1 << align;
  7782. outs() << " align 2^" << align << " (" << align_shifted << ")\n";
  7783. outs() << " reloff " << reloff;
  7784. if (reloff > object_size)
  7785. outs() << " (past end of file)\n";
  7786. else
  7787. outs() << "\n";
  7788. outs() << " nreloc " << nreloc;
  7789. if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
  7790. outs() << " (past end of file)\n";
  7791. else
  7792. outs() << "\n";
  7793. uint32_t section_type = flags & MachO::SECTION_TYPE;
  7794. if (verbose) {
  7795. outs() << " type";
  7796. if (section_type == MachO::S_REGULAR)
  7797. outs() << " S_REGULAR\n";
  7798. else if (section_type == MachO::S_ZEROFILL)
  7799. outs() << " S_ZEROFILL\n";
  7800. else if (section_type == MachO::S_CSTRING_LITERALS)
  7801. outs() << " S_CSTRING_LITERALS\n";
  7802. else if (section_type == MachO::S_4BYTE_LITERALS)
  7803. outs() << " S_4BYTE_LITERALS\n";
  7804. else if (section_type == MachO::S_8BYTE_LITERALS)
  7805. outs() << " S_8BYTE_LITERALS\n";
  7806. else if (section_type == MachO::S_16BYTE_LITERALS)
  7807. outs() << " S_16BYTE_LITERALS\n";
  7808. else if (section_type == MachO::S_LITERAL_POINTERS)
  7809. outs() << " S_LITERAL_POINTERS\n";
  7810. else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
  7811. outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
  7812. else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
  7813. outs() << " S_LAZY_SYMBOL_POINTERS\n";
  7814. else if (section_type == MachO::S_SYMBOL_STUBS)
  7815. outs() << " S_SYMBOL_STUBS\n";
  7816. else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
  7817. outs() << " S_MOD_INIT_FUNC_POINTERS\n";
  7818. else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
  7819. outs() << " S_MOD_TERM_FUNC_POINTERS\n";
  7820. else if (section_type == MachO::S_COALESCED)
  7821. outs() << " S_COALESCED\n";
  7822. else if (section_type == MachO::S_INTERPOSING)
  7823. outs() << " S_INTERPOSING\n";
  7824. else if (section_type == MachO::S_DTRACE_DOF)
  7825. outs() << " S_DTRACE_DOF\n";
  7826. else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
  7827. outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
  7828. else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
  7829. outs() << " S_THREAD_LOCAL_REGULAR\n";
  7830. else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
  7831. outs() << " S_THREAD_LOCAL_ZEROFILL\n";
  7832. else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
  7833. outs() << " S_THREAD_LOCAL_VARIABLES\n";
  7834. else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
  7835. outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
  7836. else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
  7837. outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
  7838. else
  7839. outs() << format("0x%08" PRIx32, section_type) << "\n";
  7840. outs() << "attributes";
  7841. uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
  7842. if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
  7843. outs() << " PURE_INSTRUCTIONS";
  7844. if (section_attributes & MachO::S_ATTR_NO_TOC)
  7845. outs() << " NO_TOC";
  7846. if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
  7847. outs() << " STRIP_STATIC_SYMS";
  7848. if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
  7849. outs() << " NO_DEAD_STRIP";
  7850. if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
  7851. outs() << " LIVE_SUPPORT";
  7852. if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
  7853. outs() << " SELF_MODIFYING_CODE";
  7854. if (section_attributes & MachO::S_ATTR_DEBUG)
  7855. outs() << " DEBUG";
  7856. if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
  7857. outs() << " SOME_INSTRUCTIONS";
  7858. if (section_attributes & MachO::S_ATTR_EXT_RELOC)
  7859. outs() << " EXT_RELOC";
  7860. if (section_attributes & MachO::S_ATTR_LOC_RELOC)
  7861. outs() << " LOC_RELOC";
  7862. if (section_attributes == 0)
  7863. outs() << " (none)";
  7864. outs() << "\n";
  7865. } else
  7866. outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
  7867. outs() << " reserved1 " << reserved1;
  7868. if (section_type == MachO::S_SYMBOL_STUBS ||
  7869. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  7870. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  7871. section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  7872. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
  7873. outs() << " (index into indirect symbol table)\n";
  7874. else
  7875. outs() << "\n";
  7876. outs() << " reserved2 " << reserved2;
  7877. if (section_type == MachO::S_SYMBOL_STUBS)
  7878. outs() << " (size of stubs)\n";
  7879. else
  7880. outs() << "\n";
  7881. }
  7882. static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
  7883. uint32_t object_size) {
  7884. outs() << " cmd LC_SYMTAB\n";
  7885. outs() << " cmdsize " << st.cmdsize;
  7886. if (st.cmdsize != sizeof(struct MachO::symtab_command))
  7887. outs() << " Incorrect size\n";
  7888. else
  7889. outs() << "\n";
  7890. outs() << " symoff " << st.symoff;
  7891. if (st.symoff > object_size)
  7892. outs() << " (past end of file)\n";
  7893. else
  7894. outs() << "\n";
  7895. outs() << " nsyms " << st.nsyms;
  7896. uint64_t big_size;
  7897. if (Is64Bit) {
  7898. big_size = st.nsyms;
  7899. big_size *= sizeof(struct MachO::nlist_64);
  7900. big_size += st.symoff;
  7901. if (big_size > object_size)
  7902. outs() << " (past end of file)\n";
  7903. else
  7904. outs() << "\n";
  7905. } else {
  7906. big_size = st.nsyms;
  7907. big_size *= sizeof(struct MachO::nlist);
  7908. big_size += st.symoff;
  7909. if (big_size > object_size)
  7910. outs() << " (past end of file)\n";
  7911. else
  7912. outs() << "\n";
  7913. }
  7914. outs() << " stroff " << st.stroff;
  7915. if (st.stroff > object_size)
  7916. outs() << " (past end of file)\n";
  7917. else
  7918. outs() << "\n";
  7919. outs() << " strsize " << st.strsize;
  7920. big_size = st.stroff;
  7921. big_size += st.strsize;
  7922. if (big_size > object_size)
  7923. outs() << " (past end of file)\n";
  7924. else
  7925. outs() << "\n";
  7926. }
  7927. static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
  7928. uint32_t nsyms, uint32_t object_size,
  7929. bool Is64Bit) {
  7930. outs() << " cmd LC_DYSYMTAB\n";
  7931. outs() << " cmdsize " << dyst.cmdsize;
  7932. if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
  7933. outs() << " Incorrect size\n";
  7934. else
  7935. outs() << "\n";
  7936. outs() << " ilocalsym " << dyst.ilocalsym;
  7937. if (dyst.ilocalsym > nsyms)
  7938. outs() << " (greater than the number of symbols)\n";
  7939. else
  7940. outs() << "\n";
  7941. outs() << " nlocalsym " << dyst.nlocalsym;
  7942. uint64_t big_size;
  7943. big_size = dyst.ilocalsym;
  7944. big_size += dyst.nlocalsym;
  7945. if (big_size > nsyms)
  7946. outs() << " (past the end of the symbol table)\n";
  7947. else
  7948. outs() << "\n";
  7949. outs() << " iextdefsym " << dyst.iextdefsym;
  7950. if (dyst.iextdefsym > nsyms)
  7951. outs() << " (greater than the number of symbols)\n";
  7952. else
  7953. outs() << "\n";
  7954. outs() << " nextdefsym " << dyst.nextdefsym;
  7955. big_size = dyst.iextdefsym;
  7956. big_size += dyst.nextdefsym;
  7957. if (big_size > nsyms)
  7958. outs() << " (past the end of the symbol table)\n";
  7959. else
  7960. outs() << "\n";
  7961. outs() << " iundefsym " << dyst.iundefsym;
  7962. if (dyst.iundefsym > nsyms)
  7963. outs() << " (greater than the number of symbols)\n";
  7964. else
  7965. outs() << "\n";
  7966. outs() << " nundefsym " << dyst.nundefsym;
  7967. big_size = dyst.iundefsym;
  7968. big_size += dyst.nundefsym;
  7969. if (big_size > nsyms)
  7970. outs() << " (past the end of the symbol table)\n";
  7971. else
  7972. outs() << "\n";
  7973. outs() << " tocoff " << dyst.tocoff;
  7974. if (dyst.tocoff > object_size)
  7975. outs() << " (past end of file)\n";
  7976. else
  7977. outs() << "\n";
  7978. outs() << " ntoc " << dyst.ntoc;
  7979. big_size = dyst.ntoc;
  7980. big_size *= sizeof(struct MachO::dylib_table_of_contents);
  7981. big_size += dyst.tocoff;
  7982. if (big_size > object_size)
  7983. outs() << " (past end of file)\n";
  7984. else
  7985. outs() << "\n";
  7986. outs() << " modtaboff " << dyst.modtaboff;
  7987. if (dyst.modtaboff > object_size)
  7988. outs() << " (past end of file)\n";
  7989. else
  7990. outs() << "\n";
  7991. outs() << " nmodtab " << dyst.nmodtab;
  7992. uint64_t modtabend;
  7993. if (Is64Bit) {
  7994. modtabend = dyst.nmodtab;
  7995. modtabend *= sizeof(struct MachO::dylib_module_64);
  7996. modtabend += dyst.modtaboff;
  7997. } else {
  7998. modtabend = dyst.nmodtab;
  7999. modtabend *= sizeof(struct MachO::dylib_module);
  8000. modtabend += dyst.modtaboff;
  8001. }
  8002. if (modtabend > object_size)
  8003. outs() << " (past end of file)\n";
  8004. else
  8005. outs() << "\n";
  8006. outs() << " extrefsymoff " << dyst.extrefsymoff;
  8007. if (dyst.extrefsymoff > object_size)
  8008. outs() << " (past end of file)\n";
  8009. else
  8010. outs() << "\n";
  8011. outs() << " nextrefsyms " << dyst.nextrefsyms;
  8012. big_size = dyst.nextrefsyms;
  8013. big_size *= sizeof(struct MachO::dylib_reference);
  8014. big_size += dyst.extrefsymoff;
  8015. if (big_size > object_size)
  8016. outs() << " (past end of file)\n";
  8017. else
  8018. outs() << "\n";
  8019. outs() << " indirectsymoff " << dyst.indirectsymoff;
  8020. if (dyst.indirectsymoff > object_size)
  8021. outs() << " (past end of file)\n";
  8022. else
  8023. outs() << "\n";
  8024. outs() << " nindirectsyms " << dyst.nindirectsyms;
  8025. big_size = dyst.nindirectsyms;
  8026. big_size *= sizeof(uint32_t);
  8027. big_size += dyst.indirectsymoff;
  8028. if (big_size > object_size)
  8029. outs() << " (past end of file)\n";
  8030. else
  8031. outs() << "\n";
  8032. outs() << " extreloff " << dyst.extreloff;
  8033. if (dyst.extreloff > object_size)
  8034. outs() << " (past end of file)\n";
  8035. else
  8036. outs() << "\n";
  8037. outs() << " nextrel " << dyst.nextrel;
  8038. big_size = dyst.nextrel;
  8039. big_size *= sizeof(struct MachO::relocation_info);
  8040. big_size += dyst.extreloff;
  8041. if (big_size > object_size)
  8042. outs() << " (past end of file)\n";
  8043. else
  8044. outs() << "\n";
  8045. outs() << " locreloff " << dyst.locreloff;
  8046. if (dyst.locreloff > object_size)
  8047. outs() << " (past end of file)\n";
  8048. else
  8049. outs() << "\n";
  8050. outs() << " nlocrel " << dyst.nlocrel;
  8051. big_size = dyst.nlocrel;
  8052. big_size *= sizeof(struct MachO::relocation_info);
  8053. big_size += dyst.locreloff;
  8054. if (big_size > object_size)
  8055. outs() << " (past end of file)\n";
  8056. else
  8057. outs() << "\n";
  8058. }
  8059. static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
  8060. uint32_t object_size) {
  8061. if (dc.cmd == MachO::LC_DYLD_INFO)
  8062. outs() << " cmd LC_DYLD_INFO\n";
  8063. else
  8064. outs() << " cmd LC_DYLD_INFO_ONLY\n";
  8065. outs() << " cmdsize " << dc.cmdsize;
  8066. if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
  8067. outs() << " Incorrect size\n";
  8068. else
  8069. outs() << "\n";
  8070. outs() << " rebase_off " << dc.rebase_off;
  8071. if (dc.rebase_off > object_size)
  8072. outs() << " (past end of file)\n";
  8073. else
  8074. outs() << "\n";
  8075. outs() << " rebase_size " << dc.rebase_size;
  8076. uint64_t big_size;
  8077. big_size = dc.rebase_off;
  8078. big_size += dc.rebase_size;
  8079. if (big_size > object_size)
  8080. outs() << " (past end of file)\n";
  8081. else
  8082. outs() << "\n";
  8083. outs() << " bind_off " << dc.bind_off;
  8084. if (dc.bind_off > object_size)
  8085. outs() << " (past end of file)\n";
  8086. else
  8087. outs() << "\n";
  8088. outs() << " bind_size " << dc.bind_size;
  8089. big_size = dc.bind_off;
  8090. big_size += dc.bind_size;
  8091. if (big_size > object_size)
  8092. outs() << " (past end of file)\n";
  8093. else
  8094. outs() << "\n";
  8095. outs() << " weak_bind_off " << dc.weak_bind_off;
  8096. if (dc.weak_bind_off > object_size)
  8097. outs() << " (past end of file)\n";
  8098. else
  8099. outs() << "\n";
  8100. outs() << " weak_bind_size " << dc.weak_bind_size;
  8101. big_size = dc.weak_bind_off;
  8102. big_size += dc.weak_bind_size;
  8103. if (big_size > object_size)
  8104. outs() << " (past end of file)\n";
  8105. else
  8106. outs() << "\n";
  8107. outs() << " lazy_bind_off " << dc.lazy_bind_off;
  8108. if (dc.lazy_bind_off > object_size)
  8109. outs() << " (past end of file)\n";
  8110. else
  8111. outs() << "\n";
  8112. outs() << " lazy_bind_size " << dc.lazy_bind_size;
  8113. big_size = dc.lazy_bind_off;
  8114. big_size += dc.lazy_bind_size;
  8115. if (big_size > object_size)
  8116. outs() << " (past end of file)\n";
  8117. else
  8118. outs() << "\n";
  8119. outs() << " export_off " << dc.export_off;
  8120. if (dc.export_off > object_size)
  8121. outs() << " (past end of file)\n";
  8122. else
  8123. outs() << "\n";
  8124. outs() << " export_size " << dc.export_size;
  8125. big_size = dc.export_off;
  8126. big_size += dc.export_size;
  8127. if (big_size > object_size)
  8128. outs() << " (past end of file)\n";
  8129. else
  8130. outs() << "\n";
  8131. }
  8132. static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
  8133. const char *Ptr) {
  8134. if (dyld.cmd == MachO::LC_ID_DYLINKER)
  8135. outs() << " cmd LC_ID_DYLINKER\n";
  8136. else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
  8137. outs() << " cmd LC_LOAD_DYLINKER\n";
  8138. else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
  8139. outs() << " cmd LC_DYLD_ENVIRONMENT\n";
  8140. else
  8141. outs() << " cmd ?(" << dyld.cmd << ")\n";
  8142. outs() << " cmdsize " << dyld.cmdsize;
  8143. if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
  8144. outs() << " Incorrect size\n";
  8145. else
  8146. outs() << "\n";
  8147. if (dyld.name >= dyld.cmdsize)
  8148. outs() << " name ?(bad offset " << dyld.name << ")\n";
  8149. else {
  8150. const char *P = (const char *)(Ptr) + dyld.name;
  8151. outs() << " name " << P << " (offset " << dyld.name << ")\n";
  8152. }
  8153. }
  8154. static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
  8155. outs() << " cmd LC_UUID\n";
  8156. outs() << " cmdsize " << uuid.cmdsize;
  8157. if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
  8158. outs() << " Incorrect size\n";
  8159. else
  8160. outs() << "\n";
  8161. outs() << " uuid ";
  8162. for (int i = 0; i < 16; ++i) {
  8163. outs() << format("%02" PRIX32, uuid.uuid[i]);
  8164. if (i == 3 || i == 5 || i == 7 || i == 9)
  8165. outs() << "-";
  8166. }
  8167. outs() << "\n";
  8168. }
  8169. static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
  8170. outs() << " cmd LC_RPATH\n";
  8171. outs() << " cmdsize " << rpath.cmdsize;
  8172. if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
  8173. outs() << " Incorrect size\n";
  8174. else
  8175. outs() << "\n";
  8176. if (rpath.path >= rpath.cmdsize)
  8177. outs() << " path ?(bad offset " << rpath.path << ")\n";
  8178. else {
  8179. const char *P = (const char *)(Ptr) + rpath.path;
  8180. outs() << " path " << P << " (offset " << rpath.path << ")\n";
  8181. }
  8182. }
  8183. static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
  8184. StringRef LoadCmdName;
  8185. switch (vd.cmd) {
  8186. case MachO::LC_VERSION_MIN_MACOSX:
  8187. LoadCmdName = "LC_VERSION_MIN_MACOSX";
  8188. break;
  8189. case MachO::LC_VERSION_MIN_IPHONEOS:
  8190. LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
  8191. break;
  8192. case MachO::LC_VERSION_MIN_TVOS:
  8193. LoadCmdName = "LC_VERSION_MIN_TVOS";
  8194. break;
  8195. case MachO::LC_VERSION_MIN_WATCHOS:
  8196. LoadCmdName = "LC_VERSION_MIN_WATCHOS";
  8197. break;
  8198. default:
  8199. llvm_unreachable("Unknown version min load command");
  8200. }
  8201. outs() << " cmd " << LoadCmdName << '\n';
  8202. outs() << " cmdsize " << vd.cmdsize;
  8203. if (vd.cmdsize != sizeof(struct MachO::version_min_command))
  8204. outs() << " Incorrect size\n";
  8205. else
  8206. outs() << "\n";
  8207. outs() << " version "
  8208. << MachOObjectFile::getVersionMinMajor(vd, false) << "."
  8209. << MachOObjectFile::getVersionMinMinor(vd, false);
  8210. uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
  8211. if (Update != 0)
  8212. outs() << "." << Update;
  8213. outs() << "\n";
  8214. if (vd.sdk == 0)
  8215. outs() << " sdk n/a";
  8216. else {
  8217. outs() << " sdk "
  8218. << MachOObjectFile::getVersionMinMajor(vd, true) << "."
  8219. << MachOObjectFile::getVersionMinMinor(vd, true);
  8220. }
  8221. Update = MachOObjectFile::getVersionMinUpdate(vd, true);
  8222. if (Update != 0)
  8223. outs() << "." << Update;
  8224. outs() << "\n";
  8225. }
  8226. static void PrintNoteLoadCommand(MachO::note_command Nt) {
  8227. outs() << " cmd LC_NOTE\n";
  8228. outs() << " cmdsize " << Nt.cmdsize;
  8229. if (Nt.cmdsize != sizeof(struct MachO::note_command))
  8230. outs() << " Incorrect size\n";
  8231. else
  8232. outs() << "\n";
  8233. const char *d = Nt.data_owner;
  8234. outs() << "data_owner " << format("%.16s\n", d);
  8235. outs() << " offset " << Nt.offset << "\n";
  8236. outs() << " size " << Nt.size << "\n";
  8237. }
  8238. static void PrintBuildToolVersion(MachO::build_tool_version bv) {
  8239. outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n";
  8240. outs() << " version " << MachOObjectFile::getVersionString(bv.version)
  8241. << "\n";
  8242. }
  8243. static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
  8244. MachO::build_version_command bd) {
  8245. outs() << " cmd LC_BUILD_VERSION\n";
  8246. outs() << " cmdsize " << bd.cmdsize;
  8247. if (bd.cmdsize !=
  8248. sizeof(struct MachO::build_version_command) +
  8249. bd.ntools * sizeof(struct MachO::build_tool_version))
  8250. outs() << " Incorrect size\n";
  8251. else
  8252. outs() << "\n";
  8253. outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform)
  8254. << "\n";
  8255. if (bd.sdk)
  8256. outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk)
  8257. << "\n";
  8258. else
  8259. outs() << " sdk n/a\n";
  8260. outs() << " minos " << MachOObjectFile::getVersionString(bd.minos)
  8261. << "\n";
  8262. outs() << " ntools " << bd.ntools << "\n";
  8263. for (unsigned i = 0; i < bd.ntools; ++i) {
  8264. MachO::build_tool_version bv = obj->getBuildToolVersion(i);
  8265. PrintBuildToolVersion(bv);
  8266. }
  8267. }
  8268. static void PrintSourceVersionCommand(MachO::source_version_command sd) {
  8269. outs() << " cmd LC_SOURCE_VERSION\n";
  8270. outs() << " cmdsize " << sd.cmdsize;
  8271. if (sd.cmdsize != sizeof(struct MachO::source_version_command))
  8272. outs() << " Incorrect size\n";
  8273. else
  8274. outs() << "\n";
  8275. uint64_t a = (sd.version >> 40) & 0xffffff;
  8276. uint64_t b = (sd.version >> 30) & 0x3ff;
  8277. uint64_t c = (sd.version >> 20) & 0x3ff;
  8278. uint64_t d = (sd.version >> 10) & 0x3ff;
  8279. uint64_t e = sd.version & 0x3ff;
  8280. outs() << " version " << a << "." << b;
  8281. if (e != 0)
  8282. outs() << "." << c << "." << d << "." << e;
  8283. else if (d != 0)
  8284. outs() << "." << c << "." << d;
  8285. else if (c != 0)
  8286. outs() << "." << c;
  8287. outs() << "\n";
  8288. }
  8289. static void PrintEntryPointCommand(MachO::entry_point_command ep) {
  8290. outs() << " cmd LC_MAIN\n";
  8291. outs() << " cmdsize " << ep.cmdsize;
  8292. if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
  8293. outs() << " Incorrect size\n";
  8294. else
  8295. outs() << "\n";
  8296. outs() << " entryoff " << ep.entryoff << "\n";
  8297. outs() << " stacksize " << ep.stacksize << "\n";
  8298. }
  8299. static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
  8300. uint32_t object_size) {
  8301. outs() << " cmd LC_ENCRYPTION_INFO\n";
  8302. outs() << " cmdsize " << ec.cmdsize;
  8303. if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
  8304. outs() << " Incorrect size\n";
  8305. else
  8306. outs() << "\n";
  8307. outs() << " cryptoff " << ec.cryptoff;
  8308. if (ec.cryptoff > object_size)
  8309. outs() << " (past end of file)\n";
  8310. else
  8311. outs() << "\n";
  8312. outs() << " cryptsize " << ec.cryptsize;
  8313. if (ec.cryptsize > object_size)
  8314. outs() << " (past end of file)\n";
  8315. else
  8316. outs() << "\n";
  8317. outs() << " cryptid " << ec.cryptid << "\n";
  8318. }
  8319. static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
  8320. uint32_t object_size) {
  8321. outs() << " cmd LC_ENCRYPTION_INFO_64\n";
  8322. outs() << " cmdsize " << ec.cmdsize;
  8323. if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
  8324. outs() << " Incorrect size\n";
  8325. else
  8326. outs() << "\n";
  8327. outs() << " cryptoff " << ec.cryptoff;
  8328. if (ec.cryptoff > object_size)
  8329. outs() << " (past end of file)\n";
  8330. else
  8331. outs() << "\n";
  8332. outs() << " cryptsize " << ec.cryptsize;
  8333. if (ec.cryptsize > object_size)
  8334. outs() << " (past end of file)\n";
  8335. else
  8336. outs() << "\n";
  8337. outs() << " cryptid " << ec.cryptid << "\n";
  8338. outs() << " pad " << ec.pad << "\n";
  8339. }
  8340. static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
  8341. const char *Ptr) {
  8342. outs() << " cmd LC_LINKER_OPTION\n";
  8343. outs() << " cmdsize " << lo.cmdsize;
  8344. if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
  8345. outs() << " Incorrect size\n";
  8346. else
  8347. outs() << "\n";
  8348. outs() << " count " << lo.count << "\n";
  8349. const char *string = Ptr + sizeof(struct MachO::linker_option_command);
  8350. uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
  8351. uint32_t i = 0;
  8352. while (left > 0) {
  8353. while (*string == '\0' && left > 0) {
  8354. string++;
  8355. left--;
  8356. }
  8357. if (left > 0) {
  8358. i++;
  8359. outs() << " string #" << i << " " << format("%.*s\n", left, string);
  8360. uint32_t NullPos = StringRef(string, left).find('\0');
  8361. uint32_t len = std::min(NullPos, left) + 1;
  8362. string += len;
  8363. left -= len;
  8364. }
  8365. }
  8366. if (lo.count != i)
  8367. outs() << " count " << lo.count << " does not match number of strings "
  8368. << i << "\n";
  8369. }
  8370. static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
  8371. const char *Ptr) {
  8372. outs() << " cmd LC_SUB_FRAMEWORK\n";
  8373. outs() << " cmdsize " << sub.cmdsize;
  8374. if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
  8375. outs() << " Incorrect size\n";
  8376. else
  8377. outs() << "\n";
  8378. if (sub.umbrella < sub.cmdsize) {
  8379. const char *P = Ptr + sub.umbrella;
  8380. outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
  8381. } else {
  8382. outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
  8383. }
  8384. }
  8385. static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
  8386. const char *Ptr) {
  8387. outs() << " cmd LC_SUB_UMBRELLA\n";
  8388. outs() << " cmdsize " << sub.cmdsize;
  8389. if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
  8390. outs() << " Incorrect size\n";
  8391. else
  8392. outs() << "\n";
  8393. if (sub.sub_umbrella < sub.cmdsize) {
  8394. const char *P = Ptr + sub.sub_umbrella;
  8395. outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
  8396. } else {
  8397. outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
  8398. }
  8399. }
  8400. static void PrintSubLibraryCommand(MachO::sub_library_command sub,
  8401. const char *Ptr) {
  8402. outs() << " cmd LC_SUB_LIBRARY\n";
  8403. outs() << " cmdsize " << sub.cmdsize;
  8404. if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
  8405. outs() << " Incorrect size\n";
  8406. else
  8407. outs() << "\n";
  8408. if (sub.sub_library < sub.cmdsize) {
  8409. const char *P = Ptr + sub.sub_library;
  8410. outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
  8411. } else {
  8412. outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
  8413. }
  8414. }
  8415. static void PrintSubClientCommand(MachO::sub_client_command sub,
  8416. const char *Ptr) {
  8417. outs() << " cmd LC_SUB_CLIENT\n";
  8418. outs() << " cmdsize " << sub.cmdsize;
  8419. if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
  8420. outs() << " Incorrect size\n";
  8421. else
  8422. outs() << "\n";
  8423. if (sub.client < sub.cmdsize) {
  8424. const char *P = Ptr + sub.client;
  8425. outs() << " client " << P << " (offset " << sub.client << ")\n";
  8426. } else {
  8427. outs() << " client ?(bad offset " << sub.client << ")\n";
  8428. }
  8429. }
  8430. static void PrintRoutinesCommand(MachO::routines_command r) {
  8431. outs() << " cmd LC_ROUTINES\n";
  8432. outs() << " cmdsize " << r.cmdsize;
  8433. if (r.cmdsize != sizeof(struct MachO::routines_command))
  8434. outs() << " Incorrect size\n";
  8435. else
  8436. outs() << "\n";
  8437. outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
  8438. outs() << " init_module " << r.init_module << "\n";
  8439. outs() << " reserved1 " << r.reserved1 << "\n";
  8440. outs() << " reserved2 " << r.reserved2 << "\n";
  8441. outs() << " reserved3 " << r.reserved3 << "\n";
  8442. outs() << " reserved4 " << r.reserved4 << "\n";
  8443. outs() << " reserved5 " << r.reserved5 << "\n";
  8444. outs() << " reserved6 " << r.reserved6 << "\n";
  8445. }
  8446. static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
  8447. outs() << " cmd LC_ROUTINES_64\n";
  8448. outs() << " cmdsize " << r.cmdsize;
  8449. if (r.cmdsize != sizeof(struct MachO::routines_command_64))
  8450. outs() << " Incorrect size\n";
  8451. else
  8452. outs() << "\n";
  8453. outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
  8454. outs() << " init_module " << r.init_module << "\n";
  8455. outs() << " reserved1 " << r.reserved1 << "\n";
  8456. outs() << " reserved2 " << r.reserved2 << "\n";
  8457. outs() << " reserved3 " << r.reserved3 << "\n";
  8458. outs() << " reserved4 " << r.reserved4 << "\n";
  8459. outs() << " reserved5 " << r.reserved5 << "\n";
  8460. outs() << " reserved6 " << r.reserved6 << "\n";
  8461. }
  8462. static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
  8463. outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax);
  8464. outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx);
  8465. outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
  8466. outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
  8467. outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi);
  8468. outs() << " esi " << format("0x%08" PRIx32, cpu32.esi);
  8469. outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
  8470. outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
  8471. outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss);
  8472. outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
  8473. outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
  8474. outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n";
  8475. outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds);
  8476. outs() << " es " << format("0x%08" PRIx32, cpu32.es);
  8477. outs() << " fs " << format("0x%08" PRIx32, cpu32.fs);
  8478. outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n";
  8479. }
  8480. static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
  8481. outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
  8482. outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
  8483. outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
  8484. outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
  8485. outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
  8486. outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
  8487. outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
  8488. outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
  8489. outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
  8490. outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
  8491. outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
  8492. outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
  8493. outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
  8494. outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
  8495. outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
  8496. outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
  8497. outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
  8498. outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
  8499. outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
  8500. outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
  8501. outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
  8502. }
  8503. static void Print_mmst_reg(MachO::mmst_reg_t &r) {
  8504. uint32_t f;
  8505. outs() << "\t mmst_reg ";
  8506. for (f = 0; f < 10; f++)
  8507. outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
  8508. outs() << "\n";
  8509. outs() << "\t mmst_rsrv ";
  8510. for (f = 0; f < 6; f++)
  8511. outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
  8512. outs() << "\n";
  8513. }
  8514. static void Print_xmm_reg(MachO::xmm_reg_t &r) {
  8515. uint32_t f;
  8516. outs() << "\t xmm_reg ";
  8517. for (f = 0; f < 16; f++)
  8518. outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
  8519. outs() << "\n";
  8520. }
  8521. static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
  8522. outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
  8523. outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
  8524. outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
  8525. outs() << " denorm " << fpu.fpu_fcw.denorm;
  8526. outs() << " zdiv " << fpu.fpu_fcw.zdiv;
  8527. outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
  8528. outs() << " undfl " << fpu.fpu_fcw.undfl;
  8529. outs() << " precis " << fpu.fpu_fcw.precis << "\n";
  8530. outs() << "\t\t pc ";
  8531. if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
  8532. outs() << "FP_PREC_24B ";
  8533. else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
  8534. outs() << "FP_PREC_53B ";
  8535. else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
  8536. outs() << "FP_PREC_64B ";
  8537. else
  8538. outs() << fpu.fpu_fcw.pc << " ";
  8539. outs() << "rc ";
  8540. if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
  8541. outs() << "FP_RND_NEAR ";
  8542. else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
  8543. outs() << "FP_RND_DOWN ";
  8544. else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
  8545. outs() << "FP_RND_UP ";
  8546. else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
  8547. outs() << "FP_CHOP ";
  8548. outs() << "\n";
  8549. outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
  8550. outs() << " denorm " << fpu.fpu_fsw.denorm;
  8551. outs() << " zdiv " << fpu.fpu_fsw.zdiv;
  8552. outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
  8553. outs() << " undfl " << fpu.fpu_fsw.undfl;
  8554. outs() << " precis " << fpu.fpu_fsw.precis;
  8555. outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
  8556. outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
  8557. outs() << " c0 " << fpu.fpu_fsw.c0;
  8558. outs() << " c1 " << fpu.fpu_fsw.c1;
  8559. outs() << " c2 " << fpu.fpu_fsw.c2;
  8560. outs() << " tos " << fpu.fpu_fsw.tos;
  8561. outs() << " c3 " << fpu.fpu_fsw.c3;
  8562. outs() << " busy " << fpu.fpu_fsw.busy << "\n";
  8563. outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
  8564. outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
  8565. outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
  8566. outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
  8567. outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
  8568. outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
  8569. outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
  8570. outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
  8571. outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
  8572. outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
  8573. outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
  8574. outs() << "\n";
  8575. outs() << "\t fpu_stmm0:\n";
  8576. Print_mmst_reg(fpu.fpu_stmm0);
  8577. outs() << "\t fpu_stmm1:\n";
  8578. Print_mmst_reg(fpu.fpu_stmm1);
  8579. outs() << "\t fpu_stmm2:\n";
  8580. Print_mmst_reg(fpu.fpu_stmm2);
  8581. outs() << "\t fpu_stmm3:\n";
  8582. Print_mmst_reg(fpu.fpu_stmm3);
  8583. outs() << "\t fpu_stmm4:\n";
  8584. Print_mmst_reg(fpu.fpu_stmm4);
  8585. outs() << "\t fpu_stmm5:\n";
  8586. Print_mmst_reg(fpu.fpu_stmm5);
  8587. outs() << "\t fpu_stmm6:\n";
  8588. Print_mmst_reg(fpu.fpu_stmm6);
  8589. outs() << "\t fpu_stmm7:\n";
  8590. Print_mmst_reg(fpu.fpu_stmm7);
  8591. outs() << "\t fpu_xmm0:\n";
  8592. Print_xmm_reg(fpu.fpu_xmm0);
  8593. outs() << "\t fpu_xmm1:\n";
  8594. Print_xmm_reg(fpu.fpu_xmm1);
  8595. outs() << "\t fpu_xmm2:\n";
  8596. Print_xmm_reg(fpu.fpu_xmm2);
  8597. outs() << "\t fpu_xmm3:\n";
  8598. Print_xmm_reg(fpu.fpu_xmm3);
  8599. outs() << "\t fpu_xmm4:\n";
  8600. Print_xmm_reg(fpu.fpu_xmm4);
  8601. outs() << "\t fpu_xmm5:\n";
  8602. Print_xmm_reg(fpu.fpu_xmm5);
  8603. outs() << "\t fpu_xmm6:\n";
  8604. Print_xmm_reg(fpu.fpu_xmm6);
  8605. outs() << "\t fpu_xmm7:\n";
  8606. Print_xmm_reg(fpu.fpu_xmm7);
  8607. outs() << "\t fpu_xmm8:\n";
  8608. Print_xmm_reg(fpu.fpu_xmm8);
  8609. outs() << "\t fpu_xmm9:\n";
  8610. Print_xmm_reg(fpu.fpu_xmm9);
  8611. outs() << "\t fpu_xmm10:\n";
  8612. Print_xmm_reg(fpu.fpu_xmm10);
  8613. outs() << "\t fpu_xmm11:\n";
  8614. Print_xmm_reg(fpu.fpu_xmm11);
  8615. outs() << "\t fpu_xmm12:\n";
  8616. Print_xmm_reg(fpu.fpu_xmm12);
  8617. outs() << "\t fpu_xmm13:\n";
  8618. Print_xmm_reg(fpu.fpu_xmm13);
  8619. outs() << "\t fpu_xmm14:\n";
  8620. Print_xmm_reg(fpu.fpu_xmm14);
  8621. outs() << "\t fpu_xmm15:\n";
  8622. Print_xmm_reg(fpu.fpu_xmm15);
  8623. outs() << "\t fpu_rsrv4:\n";
  8624. for (uint32_t f = 0; f < 6; f++) {
  8625. outs() << "\t ";
  8626. for (uint32_t g = 0; g < 16; g++)
  8627. outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
  8628. outs() << "\n";
  8629. }
  8630. outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
  8631. outs() << "\n";
  8632. }
  8633. static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
  8634. outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
  8635. outs() << " err " << format("0x%08" PRIx32, exc64.err);
  8636. outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
  8637. }
  8638. static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
  8639. outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]);
  8640. outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]);
  8641. outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]);
  8642. outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
  8643. outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]);
  8644. outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]);
  8645. outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]);
  8646. outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
  8647. outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]);
  8648. outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]);
  8649. outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]);
  8650. outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
  8651. outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]);
  8652. outs() << " sp " << format("0x%08" PRIx32, cpu32.sp);
  8653. outs() << " lr " << format("0x%08" PRIx32, cpu32.lr);
  8654. outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n";
  8655. outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
  8656. }
  8657. static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
  8658. outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]);
  8659. outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]);
  8660. outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
  8661. outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]);
  8662. outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]);
  8663. outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
  8664. outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]);
  8665. outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]);
  8666. outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
  8667. outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]);
  8668. outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]);
  8669. outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
  8670. outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]);
  8671. outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]);
  8672. outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
  8673. outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]);
  8674. outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]);
  8675. outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
  8676. outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]);
  8677. outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]);
  8678. outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
  8679. outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]);
  8680. outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]);
  8681. outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
  8682. outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]);
  8683. outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]);
  8684. outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
  8685. outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]);
  8686. outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]);
  8687. outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n";
  8688. outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr);
  8689. outs() << " sp " << format("0x%016" PRIx64, cpu64.sp);
  8690. outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n";
  8691. outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n";
  8692. }
  8693. static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
  8694. bool isLittleEndian, uint32_t cputype) {
  8695. if (t.cmd == MachO::LC_THREAD)
  8696. outs() << " cmd LC_THREAD\n";
  8697. else if (t.cmd == MachO::LC_UNIXTHREAD)
  8698. outs() << " cmd LC_UNIXTHREAD\n";
  8699. else
  8700. outs() << " cmd " << t.cmd << " (unknown)\n";
  8701. outs() << " cmdsize " << t.cmdsize;
  8702. if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
  8703. outs() << " Incorrect size\n";
  8704. else
  8705. outs() << "\n";
  8706. const char *begin = Ptr + sizeof(struct MachO::thread_command);
  8707. const char *end = Ptr + t.cmdsize;
  8708. uint32_t flavor, count, left;
  8709. if (cputype == MachO::CPU_TYPE_I386) {
  8710. while (begin < end) {
  8711. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8712. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  8713. begin += sizeof(uint32_t);
  8714. } else {
  8715. flavor = 0;
  8716. begin = end;
  8717. }
  8718. if (isLittleEndian != sys::IsLittleEndianHost)
  8719. sys::swapByteOrder(flavor);
  8720. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8721. memcpy((char *)&count, begin, sizeof(uint32_t));
  8722. begin += sizeof(uint32_t);
  8723. } else {
  8724. count = 0;
  8725. begin = end;
  8726. }
  8727. if (isLittleEndian != sys::IsLittleEndianHost)
  8728. sys::swapByteOrder(count);
  8729. if (flavor == MachO::x86_THREAD_STATE32) {
  8730. outs() << " flavor i386_THREAD_STATE\n";
  8731. if (count == MachO::x86_THREAD_STATE32_COUNT)
  8732. outs() << " count i386_THREAD_STATE_COUNT\n";
  8733. else
  8734. outs() << " count " << count
  8735. << " (not x86_THREAD_STATE32_COUNT)\n";
  8736. MachO::x86_thread_state32_t cpu32;
  8737. left = end - begin;
  8738. if (left >= sizeof(MachO::x86_thread_state32_t)) {
  8739. memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
  8740. begin += sizeof(MachO::x86_thread_state32_t);
  8741. } else {
  8742. memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
  8743. memcpy(&cpu32, begin, left);
  8744. begin += left;
  8745. }
  8746. if (isLittleEndian != sys::IsLittleEndianHost)
  8747. swapStruct(cpu32);
  8748. Print_x86_thread_state32_t(cpu32);
  8749. } else if (flavor == MachO::x86_THREAD_STATE) {
  8750. outs() << " flavor x86_THREAD_STATE\n";
  8751. if (count == MachO::x86_THREAD_STATE_COUNT)
  8752. outs() << " count x86_THREAD_STATE_COUNT\n";
  8753. else
  8754. outs() << " count " << count
  8755. << " (not x86_THREAD_STATE_COUNT)\n";
  8756. struct MachO::x86_thread_state_t ts;
  8757. left = end - begin;
  8758. if (left >= sizeof(MachO::x86_thread_state_t)) {
  8759. memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
  8760. begin += sizeof(MachO::x86_thread_state_t);
  8761. } else {
  8762. memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
  8763. memcpy(&ts, begin, left);
  8764. begin += left;
  8765. }
  8766. if (isLittleEndian != sys::IsLittleEndianHost)
  8767. swapStruct(ts);
  8768. if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
  8769. outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
  8770. if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
  8771. outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
  8772. else
  8773. outs() << "tsh.count " << ts.tsh.count
  8774. << " (not x86_THREAD_STATE32_COUNT\n";
  8775. Print_x86_thread_state32_t(ts.uts.ts32);
  8776. } else {
  8777. outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
  8778. << ts.tsh.count << "\n";
  8779. }
  8780. } else {
  8781. outs() << " flavor " << flavor << " (unknown)\n";
  8782. outs() << " count " << count << "\n";
  8783. outs() << " state (unknown)\n";
  8784. begin += count * sizeof(uint32_t);
  8785. }
  8786. }
  8787. } else if (cputype == MachO::CPU_TYPE_X86_64) {
  8788. while (begin < end) {
  8789. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8790. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  8791. begin += sizeof(uint32_t);
  8792. } else {
  8793. flavor = 0;
  8794. begin = end;
  8795. }
  8796. if (isLittleEndian != sys::IsLittleEndianHost)
  8797. sys::swapByteOrder(flavor);
  8798. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8799. memcpy((char *)&count, begin, sizeof(uint32_t));
  8800. begin += sizeof(uint32_t);
  8801. } else {
  8802. count = 0;
  8803. begin = end;
  8804. }
  8805. if (isLittleEndian != sys::IsLittleEndianHost)
  8806. sys::swapByteOrder(count);
  8807. if (flavor == MachO::x86_THREAD_STATE64) {
  8808. outs() << " flavor x86_THREAD_STATE64\n";
  8809. if (count == MachO::x86_THREAD_STATE64_COUNT)
  8810. outs() << " count x86_THREAD_STATE64_COUNT\n";
  8811. else
  8812. outs() << " count " << count
  8813. << " (not x86_THREAD_STATE64_COUNT)\n";
  8814. MachO::x86_thread_state64_t cpu64;
  8815. left = end - begin;
  8816. if (left >= sizeof(MachO::x86_thread_state64_t)) {
  8817. memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
  8818. begin += sizeof(MachO::x86_thread_state64_t);
  8819. } else {
  8820. memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
  8821. memcpy(&cpu64, begin, left);
  8822. begin += left;
  8823. }
  8824. if (isLittleEndian != sys::IsLittleEndianHost)
  8825. swapStruct(cpu64);
  8826. Print_x86_thread_state64_t(cpu64);
  8827. } else if (flavor == MachO::x86_THREAD_STATE) {
  8828. outs() << " flavor x86_THREAD_STATE\n";
  8829. if (count == MachO::x86_THREAD_STATE_COUNT)
  8830. outs() << " count x86_THREAD_STATE_COUNT\n";
  8831. else
  8832. outs() << " count " << count
  8833. << " (not x86_THREAD_STATE_COUNT)\n";
  8834. struct MachO::x86_thread_state_t ts;
  8835. left = end - begin;
  8836. if (left >= sizeof(MachO::x86_thread_state_t)) {
  8837. memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
  8838. begin += sizeof(MachO::x86_thread_state_t);
  8839. } else {
  8840. memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
  8841. memcpy(&ts, begin, left);
  8842. begin += left;
  8843. }
  8844. if (isLittleEndian != sys::IsLittleEndianHost)
  8845. swapStruct(ts);
  8846. if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
  8847. outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
  8848. if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
  8849. outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
  8850. else
  8851. outs() << "tsh.count " << ts.tsh.count
  8852. << " (not x86_THREAD_STATE64_COUNT\n";
  8853. Print_x86_thread_state64_t(ts.uts.ts64);
  8854. } else {
  8855. outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
  8856. << ts.tsh.count << "\n";
  8857. }
  8858. } else if (flavor == MachO::x86_FLOAT_STATE) {
  8859. outs() << " flavor x86_FLOAT_STATE\n";
  8860. if (count == MachO::x86_FLOAT_STATE_COUNT)
  8861. outs() << " count x86_FLOAT_STATE_COUNT\n";
  8862. else
  8863. outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
  8864. struct MachO::x86_float_state_t fs;
  8865. left = end - begin;
  8866. if (left >= sizeof(MachO::x86_float_state_t)) {
  8867. memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
  8868. begin += sizeof(MachO::x86_float_state_t);
  8869. } else {
  8870. memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
  8871. memcpy(&fs, begin, left);
  8872. begin += left;
  8873. }
  8874. if (isLittleEndian != sys::IsLittleEndianHost)
  8875. swapStruct(fs);
  8876. if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
  8877. outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
  8878. if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
  8879. outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
  8880. else
  8881. outs() << "fsh.count " << fs.fsh.count
  8882. << " (not x86_FLOAT_STATE64_COUNT\n";
  8883. Print_x86_float_state_t(fs.ufs.fs64);
  8884. } else {
  8885. outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
  8886. << fs.fsh.count << "\n";
  8887. }
  8888. } else if (flavor == MachO::x86_EXCEPTION_STATE) {
  8889. outs() << " flavor x86_EXCEPTION_STATE\n";
  8890. if (count == MachO::x86_EXCEPTION_STATE_COUNT)
  8891. outs() << " count x86_EXCEPTION_STATE_COUNT\n";
  8892. else
  8893. outs() << " count " << count
  8894. << " (not x86_EXCEPTION_STATE_COUNT)\n";
  8895. struct MachO::x86_exception_state_t es;
  8896. left = end - begin;
  8897. if (left >= sizeof(MachO::x86_exception_state_t)) {
  8898. memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
  8899. begin += sizeof(MachO::x86_exception_state_t);
  8900. } else {
  8901. memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
  8902. memcpy(&es, begin, left);
  8903. begin += left;
  8904. }
  8905. if (isLittleEndian != sys::IsLittleEndianHost)
  8906. swapStruct(es);
  8907. if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
  8908. outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
  8909. if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
  8910. outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
  8911. else
  8912. outs() << "\t esh.count " << es.esh.count
  8913. << " (not x86_EXCEPTION_STATE64_COUNT\n";
  8914. Print_x86_exception_state_t(es.ues.es64);
  8915. } else {
  8916. outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
  8917. << es.esh.count << "\n";
  8918. }
  8919. } else if (flavor == MachO::x86_EXCEPTION_STATE64) {
  8920. outs() << " flavor x86_EXCEPTION_STATE64\n";
  8921. if (count == MachO::x86_EXCEPTION_STATE64_COUNT)
  8922. outs() << " count x86_EXCEPTION_STATE64_COUNT\n";
  8923. else
  8924. outs() << " count " << count
  8925. << " (not x86_EXCEPTION_STATE64_COUNT)\n";
  8926. struct MachO::x86_exception_state64_t es64;
  8927. left = end - begin;
  8928. if (left >= sizeof(MachO::x86_exception_state64_t)) {
  8929. memcpy(&es64, begin, sizeof(MachO::x86_exception_state64_t));
  8930. begin += sizeof(MachO::x86_exception_state64_t);
  8931. } else {
  8932. memset(&es64, '\0', sizeof(MachO::x86_exception_state64_t));
  8933. memcpy(&es64, begin, left);
  8934. begin += left;
  8935. }
  8936. if (isLittleEndian != sys::IsLittleEndianHost)
  8937. swapStruct(es64);
  8938. Print_x86_exception_state_t(es64);
  8939. } else {
  8940. outs() << " flavor " << flavor << " (unknown)\n";
  8941. outs() << " count " << count << "\n";
  8942. outs() << " state (unknown)\n";
  8943. begin += count * sizeof(uint32_t);
  8944. }
  8945. }
  8946. } else if (cputype == MachO::CPU_TYPE_ARM) {
  8947. while (begin < end) {
  8948. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8949. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  8950. begin += sizeof(uint32_t);
  8951. } else {
  8952. flavor = 0;
  8953. begin = end;
  8954. }
  8955. if (isLittleEndian != sys::IsLittleEndianHost)
  8956. sys::swapByteOrder(flavor);
  8957. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8958. memcpy((char *)&count, begin, sizeof(uint32_t));
  8959. begin += sizeof(uint32_t);
  8960. } else {
  8961. count = 0;
  8962. begin = end;
  8963. }
  8964. if (isLittleEndian != sys::IsLittleEndianHost)
  8965. sys::swapByteOrder(count);
  8966. if (flavor == MachO::ARM_THREAD_STATE) {
  8967. outs() << " flavor ARM_THREAD_STATE\n";
  8968. if (count == MachO::ARM_THREAD_STATE_COUNT)
  8969. outs() << " count ARM_THREAD_STATE_COUNT\n";
  8970. else
  8971. outs() << " count " << count
  8972. << " (not ARM_THREAD_STATE_COUNT)\n";
  8973. MachO::arm_thread_state32_t cpu32;
  8974. left = end - begin;
  8975. if (left >= sizeof(MachO::arm_thread_state32_t)) {
  8976. memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
  8977. begin += sizeof(MachO::arm_thread_state32_t);
  8978. } else {
  8979. memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
  8980. memcpy(&cpu32, begin, left);
  8981. begin += left;
  8982. }
  8983. if (isLittleEndian != sys::IsLittleEndianHost)
  8984. swapStruct(cpu32);
  8985. Print_arm_thread_state32_t(cpu32);
  8986. } else {
  8987. outs() << " flavor " << flavor << " (unknown)\n";
  8988. outs() << " count " << count << "\n";
  8989. outs() << " state (unknown)\n";
  8990. begin += count * sizeof(uint32_t);
  8991. }
  8992. }
  8993. } else if (cputype == MachO::CPU_TYPE_ARM64 ||
  8994. cputype == MachO::CPU_TYPE_ARM64_32) {
  8995. while (begin < end) {
  8996. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8997. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  8998. begin += sizeof(uint32_t);
  8999. } else {
  9000. flavor = 0;
  9001. begin = end;
  9002. }
  9003. if (isLittleEndian != sys::IsLittleEndianHost)
  9004. sys::swapByteOrder(flavor);
  9005. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  9006. memcpy((char *)&count, begin, sizeof(uint32_t));
  9007. begin += sizeof(uint32_t);
  9008. } else {
  9009. count = 0;
  9010. begin = end;
  9011. }
  9012. if (isLittleEndian != sys::IsLittleEndianHost)
  9013. sys::swapByteOrder(count);
  9014. if (flavor == MachO::ARM_THREAD_STATE64) {
  9015. outs() << " flavor ARM_THREAD_STATE64\n";
  9016. if (count == MachO::ARM_THREAD_STATE64_COUNT)
  9017. outs() << " count ARM_THREAD_STATE64_COUNT\n";
  9018. else
  9019. outs() << " count " << count
  9020. << " (not ARM_THREAD_STATE64_COUNT)\n";
  9021. MachO::arm_thread_state64_t cpu64;
  9022. left = end - begin;
  9023. if (left >= sizeof(MachO::arm_thread_state64_t)) {
  9024. memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
  9025. begin += sizeof(MachO::arm_thread_state64_t);
  9026. } else {
  9027. memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
  9028. memcpy(&cpu64, begin, left);
  9029. begin += left;
  9030. }
  9031. if (isLittleEndian != sys::IsLittleEndianHost)
  9032. swapStruct(cpu64);
  9033. Print_arm_thread_state64_t(cpu64);
  9034. } else {
  9035. outs() << " flavor " << flavor << " (unknown)\n";
  9036. outs() << " count " << count << "\n";
  9037. outs() << " state (unknown)\n";
  9038. begin += count * sizeof(uint32_t);
  9039. }
  9040. }
  9041. } else {
  9042. while (begin < end) {
  9043. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  9044. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  9045. begin += sizeof(uint32_t);
  9046. } else {
  9047. flavor = 0;
  9048. begin = end;
  9049. }
  9050. if (isLittleEndian != sys::IsLittleEndianHost)
  9051. sys::swapByteOrder(flavor);
  9052. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  9053. memcpy((char *)&count, begin, sizeof(uint32_t));
  9054. begin += sizeof(uint32_t);
  9055. } else {
  9056. count = 0;
  9057. begin = end;
  9058. }
  9059. if (isLittleEndian != sys::IsLittleEndianHost)
  9060. sys::swapByteOrder(count);
  9061. outs() << " flavor " << flavor << "\n";
  9062. outs() << " count " << count << "\n";
  9063. outs() << " state (Unknown cputype/cpusubtype)\n";
  9064. begin += count * sizeof(uint32_t);
  9065. }
  9066. }
  9067. }
  9068. static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
  9069. if (dl.cmd == MachO::LC_ID_DYLIB)
  9070. outs() << " cmd LC_ID_DYLIB\n";
  9071. else if (dl.cmd == MachO::LC_LOAD_DYLIB)
  9072. outs() << " cmd LC_LOAD_DYLIB\n";
  9073. else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
  9074. outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
  9075. else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
  9076. outs() << " cmd LC_REEXPORT_DYLIB\n";
  9077. else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
  9078. outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
  9079. else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
  9080. outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
  9081. else
  9082. outs() << " cmd " << dl.cmd << " (unknown)\n";
  9083. outs() << " cmdsize " << dl.cmdsize;
  9084. if (dl.cmdsize < sizeof(struct MachO::dylib_command))
  9085. outs() << " Incorrect size\n";
  9086. else
  9087. outs() << "\n";
  9088. if (dl.dylib.name < dl.cmdsize) {
  9089. const char *P = (const char *)(Ptr) + dl.dylib.name;
  9090. outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
  9091. } else {
  9092. outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
  9093. }
  9094. outs() << " time stamp " << dl.dylib.timestamp << " ";
  9095. time_t t = dl.dylib.timestamp;
  9096. outs() << ctime(&t);
  9097. outs() << " current version ";
  9098. if (dl.dylib.current_version == 0xffffffff)
  9099. outs() << "n/a\n";
  9100. else
  9101. outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
  9102. << ((dl.dylib.current_version >> 8) & 0xff) << "."
  9103. << (dl.dylib.current_version & 0xff) << "\n";
  9104. outs() << "compatibility version ";
  9105. if (dl.dylib.compatibility_version == 0xffffffff)
  9106. outs() << "n/a\n";
  9107. else
  9108. outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
  9109. << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
  9110. << (dl.dylib.compatibility_version & 0xff) << "\n";
  9111. }
  9112. static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
  9113. uint32_t object_size) {
  9114. if (ld.cmd == MachO::LC_CODE_SIGNATURE)
  9115. outs() << " cmd LC_CODE_SIGNATURE\n";
  9116. else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
  9117. outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
  9118. else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
  9119. outs() << " cmd LC_FUNCTION_STARTS\n";
  9120. else if (ld.cmd == MachO::LC_DATA_IN_CODE)
  9121. outs() << " cmd LC_DATA_IN_CODE\n";
  9122. else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
  9123. outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
  9124. else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
  9125. outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
  9126. else
  9127. outs() << " cmd " << ld.cmd << " (?)\n";
  9128. outs() << " cmdsize " << ld.cmdsize;
  9129. if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
  9130. outs() << " Incorrect size\n";
  9131. else
  9132. outs() << "\n";
  9133. outs() << " dataoff " << ld.dataoff;
  9134. if (ld.dataoff > object_size)
  9135. outs() << " (past end of file)\n";
  9136. else
  9137. outs() << "\n";
  9138. outs() << " datasize " << ld.datasize;
  9139. uint64_t big_size = ld.dataoff;
  9140. big_size += ld.datasize;
  9141. if (big_size > object_size)
  9142. outs() << " (past end of file)\n";
  9143. else
  9144. outs() << "\n";
  9145. }
  9146. static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
  9147. uint32_t cputype, bool verbose) {
  9148. StringRef Buf = Obj->getData();
  9149. unsigned Index = 0;
  9150. for (const auto &Command : Obj->load_commands()) {
  9151. outs() << "Load command " << Index++ << "\n";
  9152. if (Command.C.cmd == MachO::LC_SEGMENT) {
  9153. MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
  9154. const char *sg_segname = SLC.segname;
  9155. PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
  9156. SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
  9157. SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
  9158. verbose);
  9159. for (unsigned j = 0; j < SLC.nsects; j++) {
  9160. MachO::section S = Obj->getSection(Command, j);
  9161. PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
  9162. S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
  9163. SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
  9164. }
  9165. } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
  9166. MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
  9167. const char *sg_segname = SLC_64.segname;
  9168. PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
  9169. SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
  9170. SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
  9171. SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
  9172. for (unsigned j = 0; j < SLC_64.nsects; j++) {
  9173. MachO::section_64 S_64 = Obj->getSection64(Command, j);
  9174. PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
  9175. S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
  9176. S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
  9177. sg_segname, filetype, Buf.size(), verbose);
  9178. }
  9179. } else if (Command.C.cmd == MachO::LC_SYMTAB) {
  9180. MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
  9181. PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
  9182. } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
  9183. MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
  9184. MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
  9185. PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
  9186. Obj->is64Bit());
  9187. } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
  9188. Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
  9189. MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
  9190. PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
  9191. } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
  9192. Command.C.cmd == MachO::LC_ID_DYLINKER ||
  9193. Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
  9194. MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
  9195. PrintDyldLoadCommand(Dyld, Command.Ptr);
  9196. } else if (Command.C.cmd == MachO::LC_UUID) {
  9197. MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
  9198. PrintUuidLoadCommand(Uuid);
  9199. } else if (Command.C.cmd == MachO::LC_RPATH) {
  9200. MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
  9201. PrintRpathLoadCommand(Rpath, Command.Ptr);
  9202. } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
  9203. Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
  9204. Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
  9205. Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
  9206. MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
  9207. PrintVersionMinLoadCommand(Vd);
  9208. } else if (Command.C.cmd == MachO::LC_NOTE) {
  9209. MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
  9210. PrintNoteLoadCommand(Nt);
  9211. } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
  9212. MachO::build_version_command Bv =
  9213. Obj->getBuildVersionLoadCommand(Command);
  9214. PrintBuildVersionLoadCommand(Obj, Bv);
  9215. } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
  9216. MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
  9217. PrintSourceVersionCommand(Sd);
  9218. } else if (Command.C.cmd == MachO::LC_MAIN) {
  9219. MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
  9220. PrintEntryPointCommand(Ep);
  9221. } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
  9222. MachO::encryption_info_command Ei =
  9223. Obj->getEncryptionInfoCommand(Command);
  9224. PrintEncryptionInfoCommand(Ei, Buf.size());
  9225. } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
  9226. MachO::encryption_info_command_64 Ei =
  9227. Obj->getEncryptionInfoCommand64(Command);
  9228. PrintEncryptionInfoCommand64(Ei, Buf.size());
  9229. } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
  9230. MachO::linker_option_command Lo =
  9231. Obj->getLinkerOptionLoadCommand(Command);
  9232. PrintLinkerOptionCommand(Lo, Command.Ptr);
  9233. } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
  9234. MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
  9235. PrintSubFrameworkCommand(Sf, Command.Ptr);
  9236. } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
  9237. MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
  9238. PrintSubUmbrellaCommand(Sf, Command.Ptr);
  9239. } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
  9240. MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
  9241. PrintSubLibraryCommand(Sl, Command.Ptr);
  9242. } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
  9243. MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
  9244. PrintSubClientCommand(Sc, Command.Ptr);
  9245. } else if (Command.C.cmd == MachO::LC_ROUTINES) {
  9246. MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
  9247. PrintRoutinesCommand(Rc);
  9248. } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
  9249. MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
  9250. PrintRoutinesCommand64(Rc);
  9251. } else if (Command.C.cmd == MachO::LC_THREAD ||
  9252. Command.C.cmd == MachO::LC_UNIXTHREAD) {
  9253. MachO::thread_command Tc = Obj->getThreadCommand(Command);
  9254. PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
  9255. } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
  9256. Command.C.cmd == MachO::LC_ID_DYLIB ||
  9257. Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
  9258. Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
  9259. Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
  9260. Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
  9261. MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
  9262. PrintDylibCommand(Dl, Command.Ptr);
  9263. } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
  9264. Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
  9265. Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
  9266. Command.C.cmd == MachO::LC_DATA_IN_CODE ||
  9267. Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
  9268. Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
  9269. MachO::linkedit_data_command Ld =
  9270. Obj->getLinkeditDataLoadCommand(Command);
  9271. PrintLinkEditDataCommand(Ld, Buf.size());
  9272. } else {
  9273. outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
  9274. << ")\n";
  9275. outs() << " cmdsize " << Command.C.cmdsize << "\n";
  9276. // TODO: get and print the raw bytes of the load command.
  9277. }
  9278. // TODO: print all the other kinds of load commands.
  9279. }
  9280. }
  9281. static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
  9282. if (Obj->is64Bit()) {
  9283. MachO::mach_header_64 H_64;
  9284. H_64 = Obj->getHeader64();
  9285. PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
  9286. H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
  9287. } else {
  9288. MachO::mach_header H;
  9289. H = Obj->getHeader();
  9290. PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
  9291. H.sizeofcmds, H.flags, verbose);
  9292. }
  9293. }
  9294. void printMachOFileHeader(const object::ObjectFile *Obj) {
  9295. const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
  9296. PrintMachHeader(file, !NonVerbose);
  9297. }
  9298. void printMachOLoadCommands(const object::ObjectFile *Obj) {
  9299. const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
  9300. uint32_t filetype = 0;
  9301. uint32_t cputype = 0;
  9302. if (file->is64Bit()) {
  9303. MachO::mach_header_64 H_64;
  9304. H_64 = file->getHeader64();
  9305. filetype = H_64.filetype;
  9306. cputype = H_64.cputype;
  9307. } else {
  9308. MachO::mach_header H;
  9309. H = file->getHeader();
  9310. filetype = H.filetype;
  9311. cputype = H.cputype;
  9312. }
  9313. PrintLoadCommands(file, filetype, cputype, !NonVerbose);
  9314. }
  9315. //===----------------------------------------------------------------------===//
  9316. // export trie dumping
  9317. //===----------------------------------------------------------------------===//
  9318. void printMachOExportsTrie(const object::MachOObjectFile *Obj) {
  9319. uint64_t BaseSegmentAddress = 0;
  9320. for (const auto &Command : Obj->load_commands()) {
  9321. if (Command.C.cmd == MachO::LC_SEGMENT) {
  9322. MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
  9323. if (Seg.fileoff == 0 && Seg.filesize != 0) {
  9324. BaseSegmentAddress = Seg.vmaddr;
  9325. break;
  9326. }
  9327. } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
  9328. MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
  9329. if (Seg.fileoff == 0 && Seg.filesize != 0) {
  9330. BaseSegmentAddress = Seg.vmaddr;
  9331. break;
  9332. }
  9333. }
  9334. }
  9335. Error Err = Error::success();
  9336. for (const object::ExportEntry &Entry : Obj->exports(Err)) {
  9337. uint64_t Flags = Entry.flags();
  9338. bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
  9339. bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
  9340. bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
  9341. MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
  9342. bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
  9343. MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
  9344. bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
  9345. if (ReExport)
  9346. outs() << "[re-export] ";
  9347. else
  9348. outs() << format("0x%08llX ",
  9349. Entry.address() + BaseSegmentAddress);
  9350. outs() << Entry.name();
  9351. if (WeakDef || ThreadLocal || Resolver || Abs) {
  9352. bool NeedsComma = false;
  9353. outs() << " [";
  9354. if (WeakDef) {
  9355. outs() << "weak_def";
  9356. NeedsComma = true;
  9357. }
  9358. if (ThreadLocal) {
  9359. if (NeedsComma)
  9360. outs() << ", ";
  9361. outs() << "per-thread";
  9362. NeedsComma = true;
  9363. }
  9364. if (Abs) {
  9365. if (NeedsComma)
  9366. outs() << ", ";
  9367. outs() << "absolute";
  9368. NeedsComma = true;
  9369. }
  9370. if (Resolver) {
  9371. if (NeedsComma)
  9372. outs() << ", ";
  9373. outs() << format("resolver=0x%08llX", Entry.other());
  9374. NeedsComma = true;
  9375. }
  9376. outs() << "]";
  9377. }
  9378. if (ReExport) {
  9379. StringRef DylibName = "unknown";
  9380. int Ordinal = Entry.other() - 1;
  9381. Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
  9382. if (Entry.otherName().empty())
  9383. outs() << " (from " << DylibName << ")";
  9384. else
  9385. outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
  9386. }
  9387. outs() << "\n";
  9388. }
  9389. if (Err)
  9390. report_error(std::move(Err), Obj->getFileName());
  9391. }
  9392. //===----------------------------------------------------------------------===//
  9393. // rebase table dumping
  9394. //===----------------------------------------------------------------------===//
  9395. void printMachORebaseTable(object::MachOObjectFile *Obj) {
  9396. outs() << "segment section address type\n";
  9397. Error Err = Error::success();
  9398. for (const object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
  9399. StringRef SegmentName = Entry.segmentName();
  9400. StringRef SectionName = Entry.sectionName();
  9401. uint64_t Address = Entry.address();
  9402. // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
  9403. outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
  9404. SegmentName.str().c_str(), SectionName.str().c_str(),
  9405. Address, Entry.typeName().str().c_str());
  9406. }
  9407. if (Err)
  9408. report_error(std::move(Err), Obj->getFileName());
  9409. }
  9410. static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
  9411. StringRef DylibName;
  9412. switch (Ordinal) {
  9413. case MachO::BIND_SPECIAL_DYLIB_SELF:
  9414. return "this-image";
  9415. case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
  9416. return "main-executable";
  9417. case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
  9418. return "flat-namespace";
  9419. default:
  9420. if (Ordinal > 0) {
  9421. std::error_code EC =
  9422. Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
  9423. if (EC)
  9424. return "<<bad library ordinal>>";
  9425. return DylibName;
  9426. }
  9427. }
  9428. return "<<unknown special ordinal>>";
  9429. }
  9430. //===----------------------------------------------------------------------===//
  9431. // bind table dumping
  9432. //===----------------------------------------------------------------------===//
  9433. void printMachOBindTable(object::MachOObjectFile *Obj) {
  9434. // Build table of sections so names can used in final output.
  9435. outs() << "segment section address type "
  9436. "addend dylib symbol\n";
  9437. Error Err = Error::success();
  9438. for (const object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
  9439. StringRef SegmentName = Entry.segmentName();
  9440. StringRef SectionName = Entry.sectionName();
  9441. uint64_t Address = Entry.address();
  9442. // Table lines look like:
  9443. // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
  9444. StringRef Attr;
  9445. if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
  9446. Attr = " (weak_import)";
  9447. outs() << left_justify(SegmentName, 8) << " "
  9448. << left_justify(SectionName, 18) << " "
  9449. << format_hex(Address, 10, true) << " "
  9450. << left_justify(Entry.typeName(), 8) << " "
  9451. << format_decimal(Entry.addend(), 8) << " "
  9452. << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
  9453. << Entry.symbolName() << Attr << "\n";
  9454. }
  9455. if (Err)
  9456. report_error(std::move(Err), Obj->getFileName());
  9457. }
  9458. //===----------------------------------------------------------------------===//
  9459. // lazy bind table dumping
  9460. //===----------------------------------------------------------------------===//
  9461. void printMachOLazyBindTable(object::MachOObjectFile *Obj) {
  9462. outs() << "segment section address "
  9463. "dylib symbol\n";
  9464. Error Err = Error::success();
  9465. for (const object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
  9466. StringRef SegmentName = Entry.segmentName();
  9467. StringRef SectionName = Entry.sectionName();
  9468. uint64_t Address = Entry.address();
  9469. // Table lines look like:
  9470. // __DATA __got 0x00012010 libSystem ___stack_chk_guard
  9471. outs() << left_justify(SegmentName, 8) << " "
  9472. << left_justify(SectionName, 18) << " "
  9473. << format_hex(Address, 10, true) << " "
  9474. << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
  9475. << Entry.symbolName() << "\n";
  9476. }
  9477. if (Err)
  9478. report_error(std::move(Err), Obj->getFileName());
  9479. }
  9480. //===----------------------------------------------------------------------===//
  9481. // weak bind table dumping
  9482. //===----------------------------------------------------------------------===//
  9483. void printMachOWeakBindTable(object::MachOObjectFile *Obj) {
  9484. outs() << "segment section address "
  9485. "type addend symbol\n";
  9486. Error Err = Error::success();
  9487. for (const object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
  9488. // Strong symbols don't have a location to update.
  9489. if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
  9490. outs() << " strong "
  9491. << Entry.symbolName() << "\n";
  9492. continue;
  9493. }
  9494. StringRef SegmentName = Entry.segmentName();
  9495. StringRef SectionName = Entry.sectionName();
  9496. uint64_t Address = Entry.address();
  9497. // Table lines look like:
  9498. // __DATA __data 0x00001000 pointer 0 _foo
  9499. outs() << left_justify(SegmentName, 8) << " "
  9500. << left_justify(SectionName, 18) << " "
  9501. << format_hex(Address, 10, true) << " "
  9502. << left_justify(Entry.typeName(), 8) << " "
  9503. << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
  9504. << "\n";
  9505. }
  9506. if (Err)
  9507. report_error(std::move(Err), Obj->getFileName());
  9508. }
  9509. // get_dyld_bind_info_symbolname() is used for disassembly and passed an
  9510. // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
  9511. // information for that address. If the address is found its binding symbol
  9512. // name is returned. If not nullptr is returned.
  9513. static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
  9514. struct DisassembleInfo *info) {
  9515. if (info->bindtable == nullptr) {
  9516. info->bindtable = llvm::make_unique<SymbolAddressMap>();
  9517. Error Err = Error::success();
  9518. for (const object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
  9519. uint64_t Address = Entry.address();
  9520. StringRef name = Entry.symbolName();
  9521. if (!name.empty())
  9522. (*info->bindtable)[Address] = name;
  9523. }
  9524. if (Err)
  9525. report_error(std::move(Err), info->O->getFileName());
  9526. }
  9527. auto name = info->bindtable->lookup(ReferenceValue);
  9528. return !name.empty() ? name.data() : nullptr;
  9529. }
  9530. void printLazyBindTable(ObjectFile *o) {
  9531. outs() << "Lazy bind table:\n";
  9532. if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  9533. printMachOLazyBindTable(MachO);
  9534. else
  9535. WithColor::error()
  9536. << "This operation is only currently supported "
  9537. "for Mach-O executable files.\n";
  9538. }
  9539. void printWeakBindTable(ObjectFile *o) {
  9540. outs() << "Weak bind table:\n";
  9541. if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  9542. printMachOWeakBindTable(MachO);
  9543. else
  9544. WithColor::error()
  9545. << "This operation is only currently supported "
  9546. "for Mach-O executable files.\n";
  9547. }
  9548. void printExportsTrie(const ObjectFile *o) {
  9549. outs() << "Exports trie:\n";
  9550. if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  9551. printMachOExportsTrie(MachO);
  9552. else
  9553. WithColor::error()
  9554. << "This operation is only currently supported "
  9555. "for Mach-O executable files.\n";
  9556. }
  9557. void printRebaseTable(ObjectFile *o) {
  9558. outs() << "Rebase table:\n";
  9559. if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  9560. printMachORebaseTable(MachO);
  9561. else
  9562. WithColor::error()
  9563. << "This operation is only currently supported "
  9564. "for Mach-O executable files.\n";
  9565. }
  9566. void printBindTable(ObjectFile *o) {
  9567. outs() << "Bind table:\n";
  9568. if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
  9569. printMachOBindTable(MachO);
  9570. else
  9571. WithColor::error()
  9572. << "This operation is only currently supported "
  9573. "for Mach-O executable files.\n";
  9574. }
  9575. } // namespace llvm