MachODump.cpp 316 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774
  1. //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the MachO-specific dumper for llvm-objdump.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm-objdump.h"
  14. #include "llvm-c/Disassembler.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/Config/config.h"
  19. #include "llvm/DebugInfo/DIContext.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  21. #include "llvm/MC/MCAsmInfo.h"
  22. #include "llvm/MC/MCContext.h"
  23. #include "llvm/MC/MCDisassembler.h"
  24. #include "llvm/MC/MCInst.h"
  25. #include "llvm/MC/MCInstPrinter.h"
  26. #include "llvm/MC/MCInstrDesc.h"
  27. #include "llvm/MC/MCInstrInfo.h"
  28. #include "llvm/MC/MCRegisterInfo.h"
  29. #include "llvm/MC/MCSubtargetInfo.h"
  30. #include "llvm/Object/MachO.h"
  31. #include "llvm/Object/MachOUniversal.h"
  32. #include "llvm/Support/Casting.h"
  33. #include "llvm/Support/CommandLine.h"
  34. #include "llvm/Support/Debug.h"
  35. #include "llvm/Support/Endian.h"
  36. #include "llvm/Support/Format.h"
  37. #include "llvm/Support/FormattedStream.h"
  38. #include "llvm/Support/GraphWriter.h"
  39. #include "llvm/Support/LEB128.h"
  40. #include "llvm/Support/MachO.h"
  41. #include "llvm/Support/MemoryBuffer.h"
  42. #include "llvm/Support/TargetRegistry.h"
  43. #include "llvm/Support/TargetSelect.h"
  44. #include "llvm/Support/raw_ostream.h"
  45. #include <algorithm>
  46. #include <cstring>
  47. #include <system_error>
  48. #if HAVE_CXXABI_H
  49. #include <cxxabi.h>
  50. #endif
  51. using namespace llvm;
  52. using namespace object;
  53. static cl::opt<bool>
  54. UseDbg("g",
  55. cl::desc("Print line information from debug info if available"));
  56. static cl::opt<std::string> DSYMFile("dsym",
  57. cl::desc("Use .dSYM file for debug info"));
  58. static cl::opt<bool> FullLeadingAddr("full-leading-addr",
  59. cl::desc("Print full leading address"));
  60. static cl::opt<bool> NoLeadingAddr("no-leading-addr",
  61. cl::desc("Print no leading address"));
  62. cl::opt<bool> llvm::UniversalHeaders("universal-headers",
  63. cl::desc("Print Mach-O universal headers "
  64. "(requires -macho)"));
  65. cl::opt<bool>
  66. llvm::ArchiveHeaders("archive-headers",
  67. cl::desc("Print archive headers for Mach-O archives "
  68. "(requires -macho)"));
  69. cl::opt<bool>
  70. ArchiveMemberOffsets("archive-member-offsets",
  71. cl::desc("Print the offset to each archive member for "
  72. "Mach-O archives (requires -macho and "
  73. "-archive-headers)"));
  74. cl::opt<bool>
  75. llvm::IndirectSymbols("indirect-symbols",
  76. cl::desc("Print indirect symbol table for Mach-O "
  77. "objects (requires -macho)"));
  78. cl::opt<bool>
  79. llvm::DataInCode("data-in-code",
  80. cl::desc("Print the data in code table for Mach-O objects "
  81. "(requires -macho)"));
  82. cl::opt<bool>
  83. llvm::LinkOptHints("link-opt-hints",
  84. cl::desc("Print the linker optimization hints for "
  85. "Mach-O objects (requires -macho)"));
  86. cl::list<std::string>
  87. llvm::DumpSections("section",
  88. cl::desc("Prints the specified segment,section for "
  89. "Mach-O objects (requires -macho)"));
  90. cl::opt<bool>
  91. llvm::InfoPlist("info-plist",
  92. cl::desc("Print the info plist section as strings for "
  93. "Mach-O objects (requires -macho)"));
  94. cl::opt<bool>
  95. llvm::DylibsUsed("dylibs-used",
  96. cl::desc("Print the shared libraries used for linked "
  97. "Mach-O files (requires -macho)"));
  98. cl::opt<bool>
  99. llvm::DylibId("dylib-id",
  100. cl::desc("Print the shared library's id for the dylib Mach-O "
  101. "file (requires -macho)"));
  102. cl::opt<bool>
  103. llvm::NonVerbose("non-verbose",
  104. cl::desc("Print the info for Mach-O objects in "
  105. "non-verbose or numeric form (requires -macho)"));
  106. cl::opt<bool>
  107. llvm::ObjcMetaData("objc-meta-data",
  108. cl::desc("Print the Objective-C runtime meta data for "
  109. "Mach-O files (requires -macho)"));
  110. cl::opt<std::string> llvm::DisSymName(
  111. "dis-symname",
  112. cl::desc("disassemble just this symbol's instructions (requires -macho"));
  113. static cl::opt<bool> NoSymbolicOperands(
  114. "no-symbolic-operands",
  115. cl::desc("do not symbolic operands when disassembling (requires -macho)"));
  116. static cl::list<std::string>
  117. ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
  118. cl::ZeroOrMore);
  119. bool ArchAll = false;
  120. static std::string ThumbTripleName;
  121. static const Target *GetTarget(const MachOObjectFile *MachOObj,
  122. const char **McpuDefault,
  123. const Target **ThumbTarget) {
  124. // Figure out the target triple.
  125. if (TripleName.empty()) {
  126. llvm::Triple TT("unknown-unknown-unknown");
  127. llvm::Triple ThumbTriple = Triple();
  128. TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
  129. TripleName = TT.str();
  130. ThumbTripleName = ThumbTriple.str();
  131. }
  132. // Get the target specific parser.
  133. std::string Error;
  134. const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
  135. if (TheTarget && ThumbTripleName.empty())
  136. return TheTarget;
  137. *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
  138. if (*ThumbTarget)
  139. return TheTarget;
  140. errs() << "llvm-objdump: error: unable to get target for '";
  141. if (!TheTarget)
  142. errs() << TripleName;
  143. else
  144. errs() << ThumbTripleName;
  145. errs() << "', see --version and --triple.\n";
  146. return nullptr;
  147. }
  148. struct SymbolSorter {
  149. bool operator()(const SymbolRef &A, const SymbolRef &B) {
  150. uint64_t AAddr = (A.getType() != SymbolRef::ST_Function) ? 0 : A.getValue();
  151. uint64_t BAddr = (B.getType() != SymbolRef::ST_Function) ? 0 : B.getValue();
  152. return AAddr < BAddr;
  153. }
  154. };
  155. // Types for the storted data in code table that is built before disassembly
  156. // and the predicate function to sort them.
  157. typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
  158. typedef std::vector<DiceTableEntry> DiceTable;
  159. typedef DiceTable::iterator dice_table_iterator;
  160. // This is used to search for a data in code table entry for the PC being
  161. // disassembled. The j parameter has the PC in j.first. A single data in code
  162. // table entry can cover many bytes for each of its Kind's. So if the offset,
  163. // aka the i.first value, of the data in code table entry plus its Length
  164. // covers the PC being searched for this will return true. If not it will
  165. // return false.
  166. static bool compareDiceTableEntries(const DiceTableEntry &i,
  167. const DiceTableEntry &j) {
  168. uint16_t Length;
  169. i.second.getLength(Length);
  170. return j.first >= i.first && j.first < i.first + Length;
  171. }
  172. static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
  173. unsigned short Kind) {
  174. uint32_t Value, Size = 1;
  175. switch (Kind) {
  176. default:
  177. case MachO::DICE_KIND_DATA:
  178. if (Length >= 4) {
  179. if (!NoShowRawInsn)
  180. dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
  181. Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
  182. outs() << "\t.long " << Value;
  183. Size = 4;
  184. } else if (Length >= 2) {
  185. if (!NoShowRawInsn)
  186. dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
  187. Value = bytes[1] << 8 | bytes[0];
  188. outs() << "\t.short " << Value;
  189. Size = 2;
  190. } else {
  191. if (!NoShowRawInsn)
  192. dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
  193. Value = bytes[0];
  194. outs() << "\t.byte " << Value;
  195. Size = 1;
  196. }
  197. if (Kind == MachO::DICE_KIND_DATA)
  198. outs() << "\t@ KIND_DATA\n";
  199. else
  200. outs() << "\t@ data in code kind = " << Kind << "\n";
  201. break;
  202. case MachO::DICE_KIND_JUMP_TABLE8:
  203. if (!NoShowRawInsn)
  204. dumpBytes(ArrayRef<uint8_t>(bytes, 1), outs());
  205. Value = bytes[0];
  206. outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
  207. Size = 1;
  208. break;
  209. case MachO::DICE_KIND_JUMP_TABLE16:
  210. if (!NoShowRawInsn)
  211. dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
  212. Value = bytes[1] << 8 | bytes[0];
  213. outs() << "\t.short " << format("%5u", Value & 0xffff)
  214. << "\t@ KIND_JUMP_TABLE16\n";
  215. Size = 2;
  216. break;
  217. case MachO::DICE_KIND_JUMP_TABLE32:
  218. case MachO::DICE_KIND_ABS_JUMP_TABLE32:
  219. if (!NoShowRawInsn)
  220. dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
  221. Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
  222. outs() << "\t.long " << Value;
  223. if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
  224. outs() << "\t@ KIND_JUMP_TABLE32\n";
  225. else
  226. outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
  227. Size = 4;
  228. break;
  229. }
  230. return Size;
  231. }
  232. static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
  233. std::vector<SectionRef> &Sections,
  234. std::vector<SymbolRef> &Symbols,
  235. SmallVectorImpl<uint64_t> &FoundFns,
  236. uint64_t &BaseSegmentAddress) {
  237. for (const SymbolRef &Symbol : MachOObj->symbols()) {
  238. ErrorOr<StringRef> SymName = Symbol.getName();
  239. if (std::error_code EC = SymName.getError())
  240. report_fatal_error(EC.message());
  241. if (!SymName->startswith("ltmp"))
  242. Symbols.push_back(Symbol);
  243. }
  244. for (const SectionRef &Section : MachOObj->sections()) {
  245. StringRef SectName;
  246. Section.getName(SectName);
  247. Sections.push_back(Section);
  248. }
  249. bool BaseSegmentAddressSet = false;
  250. for (const auto &Command : MachOObj->load_commands()) {
  251. if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
  252. // We found a function starts segment, parse the addresses for later
  253. // consumption.
  254. MachO::linkedit_data_command LLC =
  255. MachOObj->getLinkeditDataLoadCommand(Command);
  256. MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
  257. } else if (Command.C.cmd == MachO::LC_SEGMENT) {
  258. MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
  259. StringRef SegName = SLC.segname;
  260. if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
  261. BaseSegmentAddressSet = true;
  262. BaseSegmentAddress = SLC.vmaddr;
  263. }
  264. }
  265. }
  266. }
  267. static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
  268. uint32_t n, uint32_t count,
  269. uint32_t stride, uint64_t addr) {
  270. MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
  271. uint32_t nindirectsyms = Dysymtab.nindirectsyms;
  272. if (n > nindirectsyms)
  273. outs() << " (entries start past the end of the indirect symbol "
  274. "table) (reserved1 field greater than the table size)";
  275. else if (n + count > nindirectsyms)
  276. outs() << " (entries extends past the end of the indirect symbol "
  277. "table)";
  278. outs() << "\n";
  279. uint32_t cputype = O->getHeader().cputype;
  280. if (cputype & MachO::CPU_ARCH_ABI64)
  281. outs() << "address index";
  282. else
  283. outs() << "address index";
  284. if (verbose)
  285. outs() << " name\n";
  286. else
  287. outs() << "\n";
  288. for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
  289. if (cputype & MachO::CPU_ARCH_ABI64)
  290. outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
  291. else
  292. outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
  293. MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
  294. uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
  295. if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
  296. outs() << "LOCAL\n";
  297. continue;
  298. }
  299. if (indirect_symbol ==
  300. (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
  301. outs() << "LOCAL ABSOLUTE\n";
  302. continue;
  303. }
  304. if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
  305. outs() << "ABSOLUTE\n";
  306. continue;
  307. }
  308. outs() << format("%5u ", indirect_symbol);
  309. if (verbose) {
  310. MachO::symtab_command Symtab = O->getSymtabLoadCommand();
  311. if (indirect_symbol < Symtab.nsyms) {
  312. symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
  313. SymbolRef Symbol = *Sym;
  314. ErrorOr<StringRef> SymName = Symbol.getName();
  315. if (std::error_code EC = SymName.getError())
  316. report_fatal_error(EC.message());
  317. outs() << *SymName;
  318. } else {
  319. outs() << "?";
  320. }
  321. }
  322. outs() << "\n";
  323. }
  324. }
  325. static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
  326. for (const auto &Load : O->load_commands()) {
  327. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  328. MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
  329. for (unsigned J = 0; J < Seg.nsects; ++J) {
  330. MachO::section_64 Sec = O->getSection64(Load, J);
  331. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  332. if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  333. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  334. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  335. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  336. section_type == MachO::S_SYMBOL_STUBS) {
  337. uint32_t stride;
  338. if (section_type == MachO::S_SYMBOL_STUBS)
  339. stride = Sec.reserved2;
  340. else
  341. stride = 8;
  342. if (stride == 0) {
  343. outs() << "Can't print indirect symbols for (" << Sec.segname << ","
  344. << Sec.sectname << ") "
  345. << "(size of stubs in reserved2 field is zero)\n";
  346. continue;
  347. }
  348. uint32_t count = Sec.size / stride;
  349. outs() << "Indirect symbols for (" << Sec.segname << ","
  350. << Sec.sectname << ") " << count << " entries";
  351. uint32_t n = Sec.reserved1;
  352. PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
  353. }
  354. }
  355. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  356. MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
  357. for (unsigned J = 0; J < Seg.nsects; ++J) {
  358. MachO::section Sec = O->getSection(Load, J);
  359. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  360. if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  361. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  362. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  363. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  364. section_type == MachO::S_SYMBOL_STUBS) {
  365. uint32_t stride;
  366. if (section_type == MachO::S_SYMBOL_STUBS)
  367. stride = Sec.reserved2;
  368. else
  369. stride = 4;
  370. if (stride == 0) {
  371. outs() << "Can't print indirect symbols for (" << Sec.segname << ","
  372. << Sec.sectname << ") "
  373. << "(size of stubs in reserved2 field is zero)\n";
  374. continue;
  375. }
  376. uint32_t count = Sec.size / stride;
  377. outs() << "Indirect symbols for (" << Sec.segname << ","
  378. << Sec.sectname << ") " << count << " entries";
  379. uint32_t n = Sec.reserved1;
  380. PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
  381. }
  382. }
  383. }
  384. }
  385. }
  386. static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
  387. MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
  388. uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
  389. outs() << "Data in code table (" << nentries << " entries)\n";
  390. outs() << "offset length kind\n";
  391. for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
  392. ++DI) {
  393. uint32_t Offset;
  394. DI->getOffset(Offset);
  395. outs() << format("0x%08" PRIx32, Offset) << " ";
  396. uint16_t Length;
  397. DI->getLength(Length);
  398. outs() << format("%6u", Length) << " ";
  399. uint16_t Kind;
  400. DI->getKind(Kind);
  401. if (verbose) {
  402. switch (Kind) {
  403. case MachO::DICE_KIND_DATA:
  404. outs() << "DATA";
  405. break;
  406. case MachO::DICE_KIND_JUMP_TABLE8:
  407. outs() << "JUMP_TABLE8";
  408. break;
  409. case MachO::DICE_KIND_JUMP_TABLE16:
  410. outs() << "JUMP_TABLE16";
  411. break;
  412. case MachO::DICE_KIND_JUMP_TABLE32:
  413. outs() << "JUMP_TABLE32";
  414. break;
  415. case MachO::DICE_KIND_ABS_JUMP_TABLE32:
  416. outs() << "ABS_JUMP_TABLE32";
  417. break;
  418. default:
  419. outs() << format("0x%04" PRIx32, Kind);
  420. break;
  421. }
  422. } else
  423. outs() << format("0x%04" PRIx32, Kind);
  424. outs() << "\n";
  425. }
  426. }
  427. static void PrintLinkOptHints(MachOObjectFile *O) {
  428. MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
  429. const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
  430. uint32_t nloh = LohLC.datasize;
  431. outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
  432. for (uint32_t i = 0; i < nloh;) {
  433. unsigned n;
  434. uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
  435. i += n;
  436. outs() << " identifier " << identifier << " ";
  437. if (i >= nloh)
  438. return;
  439. switch (identifier) {
  440. case 1:
  441. outs() << "AdrpAdrp\n";
  442. break;
  443. case 2:
  444. outs() << "AdrpLdr\n";
  445. break;
  446. case 3:
  447. outs() << "AdrpAddLdr\n";
  448. break;
  449. case 4:
  450. outs() << "AdrpLdrGotLdr\n";
  451. break;
  452. case 5:
  453. outs() << "AdrpAddStr\n";
  454. break;
  455. case 6:
  456. outs() << "AdrpLdrGotStr\n";
  457. break;
  458. case 7:
  459. outs() << "AdrpAdd\n";
  460. break;
  461. case 8:
  462. outs() << "AdrpLdrGot\n";
  463. break;
  464. default:
  465. outs() << "Unknown identifier value\n";
  466. break;
  467. }
  468. uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
  469. i += n;
  470. outs() << " narguments " << narguments << "\n";
  471. if (i >= nloh)
  472. return;
  473. for (uint32_t j = 0; j < narguments; j++) {
  474. uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
  475. i += n;
  476. outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
  477. if (i >= nloh)
  478. return;
  479. }
  480. }
  481. }
  482. static void PrintDylibs(MachOObjectFile *O, bool JustId) {
  483. unsigned Index = 0;
  484. for (const auto &Load : O->load_commands()) {
  485. if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
  486. (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
  487. Load.C.cmd == MachO::LC_LOAD_DYLIB ||
  488. Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
  489. Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
  490. Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
  491. Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
  492. MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
  493. if (dl.dylib.name < dl.cmdsize) {
  494. const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
  495. if (JustId)
  496. outs() << p << "\n";
  497. else {
  498. outs() << "\t" << p;
  499. outs() << " (compatibility version "
  500. << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
  501. << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
  502. << (dl.dylib.compatibility_version & 0xff) << ",";
  503. outs() << " current version "
  504. << ((dl.dylib.current_version >> 16) & 0xffff) << "."
  505. << ((dl.dylib.current_version >> 8) & 0xff) << "."
  506. << (dl.dylib.current_version & 0xff) << ")\n";
  507. }
  508. } else {
  509. outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
  510. if (Load.C.cmd == MachO::LC_ID_DYLIB)
  511. outs() << "LC_ID_DYLIB ";
  512. else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
  513. outs() << "LC_LOAD_DYLIB ";
  514. else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
  515. outs() << "LC_LOAD_WEAK_DYLIB ";
  516. else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
  517. outs() << "LC_LAZY_LOAD_DYLIB ";
  518. else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
  519. outs() << "LC_REEXPORT_DYLIB ";
  520. else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
  521. outs() << "LC_LOAD_UPWARD_DYLIB ";
  522. else
  523. outs() << "LC_??? ";
  524. outs() << "command " << Index++ << "\n";
  525. }
  526. }
  527. }
  528. }
  529. typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
  530. static void CreateSymbolAddressMap(MachOObjectFile *O,
  531. SymbolAddressMap *AddrMap) {
  532. // Create a map of symbol addresses to symbol names.
  533. for (const SymbolRef &Symbol : O->symbols()) {
  534. SymbolRef::Type ST = Symbol.getType();
  535. if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
  536. ST == SymbolRef::ST_Other) {
  537. uint64_t Address = Symbol.getValue();
  538. ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
  539. if (std::error_code EC = SymNameOrErr.getError())
  540. report_fatal_error(EC.message());
  541. StringRef SymName = *SymNameOrErr;
  542. if (!SymName.startswith(".objc"))
  543. (*AddrMap)[Address] = SymName;
  544. }
  545. }
  546. }
  547. // GuessSymbolName is passed the address of what might be a symbol and a
  548. // pointer to the SymbolAddressMap. It returns the name of a symbol
  549. // with that address or nullptr if no symbol is found with that address.
  550. static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
  551. const char *SymbolName = nullptr;
  552. // A DenseMap can't lookup up some values.
  553. if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
  554. StringRef name = AddrMap->lookup(value);
  555. if (!name.empty())
  556. SymbolName = name.data();
  557. }
  558. return SymbolName;
  559. }
  560. static void DumpCstringChar(const char c) {
  561. char p[2];
  562. p[0] = c;
  563. p[1] = '\0';
  564. outs().write_escaped(p);
  565. }
  566. static void DumpCstringSection(MachOObjectFile *O, const char *sect,
  567. uint32_t sect_size, uint64_t sect_addr,
  568. bool print_addresses) {
  569. for (uint32_t i = 0; i < sect_size; i++) {
  570. if (print_addresses) {
  571. if (O->is64Bit())
  572. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  573. else
  574. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  575. }
  576. for (; i < sect_size && sect[i] != '\0'; i++)
  577. DumpCstringChar(sect[i]);
  578. if (i < sect_size && sect[i] == '\0')
  579. outs() << "\n";
  580. }
  581. }
  582. static void DumpLiteral4(uint32_t l, float f) {
  583. outs() << format("0x%08" PRIx32, l);
  584. if ((l & 0x7f800000) != 0x7f800000)
  585. outs() << format(" (%.16e)\n", f);
  586. else {
  587. if (l == 0x7f800000)
  588. outs() << " (+Infinity)\n";
  589. else if (l == 0xff800000)
  590. outs() << " (-Infinity)\n";
  591. else if ((l & 0x00400000) == 0x00400000)
  592. outs() << " (non-signaling Not-a-Number)\n";
  593. else
  594. outs() << " (signaling Not-a-Number)\n";
  595. }
  596. }
  597. static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
  598. uint32_t sect_size, uint64_t sect_addr,
  599. bool print_addresses) {
  600. for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
  601. if (print_addresses) {
  602. if (O->is64Bit())
  603. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  604. else
  605. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  606. }
  607. float f;
  608. memcpy(&f, sect + i, sizeof(float));
  609. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  610. sys::swapByteOrder(f);
  611. uint32_t l;
  612. memcpy(&l, sect + i, sizeof(uint32_t));
  613. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  614. sys::swapByteOrder(l);
  615. DumpLiteral4(l, f);
  616. }
  617. }
  618. static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
  619. double d) {
  620. outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
  621. uint32_t Hi, Lo;
  622. if (O->isLittleEndian()) {
  623. Hi = l1;
  624. Lo = l0;
  625. } else {
  626. Hi = l0;
  627. Lo = l1;
  628. }
  629. // Hi is the high word, so this is equivalent to if(isfinite(d))
  630. if ((Hi & 0x7ff00000) != 0x7ff00000)
  631. outs() << format(" (%.16e)\n", d);
  632. else {
  633. if (Hi == 0x7ff00000 && Lo == 0)
  634. outs() << " (+Infinity)\n";
  635. else if (Hi == 0xfff00000 && Lo == 0)
  636. outs() << " (-Infinity)\n";
  637. else if ((Hi & 0x00080000) == 0x00080000)
  638. outs() << " (non-signaling Not-a-Number)\n";
  639. else
  640. outs() << " (signaling Not-a-Number)\n";
  641. }
  642. }
  643. static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
  644. uint32_t sect_size, uint64_t sect_addr,
  645. bool print_addresses) {
  646. for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
  647. if (print_addresses) {
  648. if (O->is64Bit())
  649. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  650. else
  651. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  652. }
  653. double d;
  654. memcpy(&d, sect + i, sizeof(double));
  655. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  656. sys::swapByteOrder(d);
  657. uint32_t l0, l1;
  658. memcpy(&l0, sect + i, sizeof(uint32_t));
  659. memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
  660. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  661. sys::swapByteOrder(l0);
  662. sys::swapByteOrder(l1);
  663. }
  664. DumpLiteral8(O, l0, l1, d);
  665. }
  666. }
  667. static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
  668. outs() << format("0x%08" PRIx32, l0) << " ";
  669. outs() << format("0x%08" PRIx32, l1) << " ";
  670. outs() << format("0x%08" PRIx32, l2) << " ";
  671. outs() << format("0x%08" PRIx32, l3) << "\n";
  672. }
  673. static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
  674. uint32_t sect_size, uint64_t sect_addr,
  675. bool print_addresses) {
  676. for (uint32_t i = 0; i < sect_size; i += 16) {
  677. if (print_addresses) {
  678. if (O->is64Bit())
  679. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  680. else
  681. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  682. }
  683. uint32_t l0, l1, l2, l3;
  684. memcpy(&l0, sect + i, sizeof(uint32_t));
  685. memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
  686. memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
  687. memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
  688. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  689. sys::swapByteOrder(l0);
  690. sys::swapByteOrder(l1);
  691. sys::swapByteOrder(l2);
  692. sys::swapByteOrder(l3);
  693. }
  694. DumpLiteral16(l0, l1, l2, l3);
  695. }
  696. }
  697. static void DumpLiteralPointerSection(MachOObjectFile *O,
  698. const SectionRef &Section,
  699. const char *sect, uint32_t sect_size,
  700. uint64_t sect_addr,
  701. bool print_addresses) {
  702. // Collect the literal sections in this Mach-O file.
  703. std::vector<SectionRef> LiteralSections;
  704. for (const SectionRef &Section : O->sections()) {
  705. DataRefImpl Ref = Section.getRawDataRefImpl();
  706. uint32_t section_type;
  707. if (O->is64Bit()) {
  708. const MachO::section_64 Sec = O->getSection64(Ref);
  709. section_type = Sec.flags & MachO::SECTION_TYPE;
  710. } else {
  711. const MachO::section Sec = O->getSection(Ref);
  712. section_type = Sec.flags & MachO::SECTION_TYPE;
  713. }
  714. if (section_type == MachO::S_CSTRING_LITERALS ||
  715. section_type == MachO::S_4BYTE_LITERALS ||
  716. section_type == MachO::S_8BYTE_LITERALS ||
  717. section_type == MachO::S_16BYTE_LITERALS)
  718. LiteralSections.push_back(Section);
  719. }
  720. // Set the size of the literal pointer.
  721. uint32_t lp_size = O->is64Bit() ? 8 : 4;
  722. // Collect the external relocation symbols for the literal pointers.
  723. std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
  724. for (const RelocationRef &Reloc : Section.relocations()) {
  725. DataRefImpl Rel;
  726. MachO::any_relocation_info RE;
  727. bool isExtern = false;
  728. Rel = Reloc.getRawDataRefImpl();
  729. RE = O->getRelocation(Rel);
  730. isExtern = O->getPlainRelocationExternal(RE);
  731. if (isExtern) {
  732. uint64_t RelocOffset = Reloc.getOffset();
  733. symbol_iterator RelocSym = Reloc.getSymbol();
  734. Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
  735. }
  736. }
  737. array_pod_sort(Relocs.begin(), Relocs.end());
  738. // Dump each literal pointer.
  739. for (uint32_t i = 0; i < sect_size; i += lp_size) {
  740. if (print_addresses) {
  741. if (O->is64Bit())
  742. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  743. else
  744. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  745. }
  746. uint64_t lp;
  747. if (O->is64Bit()) {
  748. memcpy(&lp, sect + i, sizeof(uint64_t));
  749. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  750. sys::swapByteOrder(lp);
  751. } else {
  752. uint32_t li;
  753. memcpy(&li, sect + i, sizeof(uint32_t));
  754. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  755. sys::swapByteOrder(li);
  756. lp = li;
  757. }
  758. // First look for an external relocation entry for this literal pointer.
  759. auto Reloc = std::find_if(
  760. Relocs.begin(), Relocs.end(),
  761. [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
  762. if (Reloc != Relocs.end()) {
  763. symbol_iterator RelocSym = Reloc->second;
  764. ErrorOr<StringRef> SymName = RelocSym->getName();
  765. if (std::error_code EC = SymName.getError())
  766. report_fatal_error(EC.message());
  767. outs() << "external relocation entry for symbol:" << *SymName << "\n";
  768. continue;
  769. }
  770. // For local references see what the section the literal pointer points to.
  771. auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
  772. [&](const SectionRef &R) {
  773. return lp >= R.getAddress() &&
  774. lp < R.getAddress() + R.getSize();
  775. });
  776. if (Sect == LiteralSections.end()) {
  777. outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
  778. continue;
  779. }
  780. uint64_t SectAddress = Sect->getAddress();
  781. uint64_t SectSize = Sect->getSize();
  782. StringRef SectName;
  783. Sect->getName(SectName);
  784. DataRefImpl Ref = Sect->getRawDataRefImpl();
  785. StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
  786. outs() << SegmentName << ":" << SectName << ":";
  787. uint32_t section_type;
  788. if (O->is64Bit()) {
  789. const MachO::section_64 Sec = O->getSection64(Ref);
  790. section_type = Sec.flags & MachO::SECTION_TYPE;
  791. } else {
  792. const MachO::section Sec = O->getSection(Ref);
  793. section_type = Sec.flags & MachO::SECTION_TYPE;
  794. }
  795. StringRef BytesStr;
  796. Sect->getContents(BytesStr);
  797. const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
  798. switch (section_type) {
  799. case MachO::S_CSTRING_LITERALS:
  800. for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
  801. i++) {
  802. DumpCstringChar(Contents[i]);
  803. }
  804. outs() << "\n";
  805. break;
  806. case MachO::S_4BYTE_LITERALS:
  807. float f;
  808. memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
  809. uint32_t l;
  810. memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
  811. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  812. sys::swapByteOrder(f);
  813. sys::swapByteOrder(l);
  814. }
  815. DumpLiteral4(l, f);
  816. break;
  817. case MachO::S_8BYTE_LITERALS: {
  818. double d;
  819. memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
  820. uint32_t l0, l1;
  821. memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
  822. memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
  823. sizeof(uint32_t));
  824. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  825. sys::swapByteOrder(f);
  826. sys::swapByteOrder(l0);
  827. sys::swapByteOrder(l1);
  828. }
  829. DumpLiteral8(O, l0, l1, d);
  830. break;
  831. }
  832. case MachO::S_16BYTE_LITERALS: {
  833. uint32_t l0, l1, l2, l3;
  834. memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
  835. memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
  836. sizeof(uint32_t));
  837. memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
  838. sizeof(uint32_t));
  839. memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
  840. sizeof(uint32_t));
  841. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  842. sys::swapByteOrder(l0);
  843. sys::swapByteOrder(l1);
  844. sys::swapByteOrder(l2);
  845. sys::swapByteOrder(l3);
  846. }
  847. DumpLiteral16(l0, l1, l2, l3);
  848. break;
  849. }
  850. }
  851. }
  852. }
  853. static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
  854. uint32_t sect_size, uint64_t sect_addr,
  855. SymbolAddressMap *AddrMap,
  856. bool verbose) {
  857. uint32_t stride;
  858. if (O->is64Bit())
  859. stride = sizeof(uint64_t);
  860. else
  861. stride = sizeof(uint32_t);
  862. for (uint32_t i = 0; i < sect_size; i += stride) {
  863. const char *SymbolName = nullptr;
  864. if (O->is64Bit()) {
  865. outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
  866. uint64_t pointer_value;
  867. memcpy(&pointer_value, sect + i, stride);
  868. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  869. sys::swapByteOrder(pointer_value);
  870. outs() << format("0x%016" PRIx64, pointer_value);
  871. if (verbose)
  872. SymbolName = GuessSymbolName(pointer_value, AddrMap);
  873. } else {
  874. outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
  875. uint32_t pointer_value;
  876. memcpy(&pointer_value, sect + i, stride);
  877. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  878. sys::swapByteOrder(pointer_value);
  879. outs() << format("0x%08" PRIx32, pointer_value);
  880. if (verbose)
  881. SymbolName = GuessSymbolName(pointer_value, AddrMap);
  882. }
  883. if (SymbolName)
  884. outs() << " " << SymbolName;
  885. outs() << "\n";
  886. }
  887. }
  888. static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
  889. uint32_t size, uint64_t addr) {
  890. uint32_t cputype = O->getHeader().cputype;
  891. if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
  892. uint32_t j;
  893. for (uint32_t i = 0; i < size; i += j, addr += j) {
  894. if (O->is64Bit())
  895. outs() << format("%016" PRIx64, addr) << "\t";
  896. else
  897. outs() << format("%08" PRIx64, addr) << "\t";
  898. for (j = 0; j < 16 && i + j < size; j++) {
  899. uint8_t byte_word = *(sect + i + j);
  900. outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
  901. }
  902. outs() << "\n";
  903. }
  904. } else {
  905. uint32_t j;
  906. for (uint32_t i = 0; i < size; i += j, addr += j) {
  907. if (O->is64Bit())
  908. outs() << format("%016" PRIx64, addr) << "\t";
  909. else
  910. outs() << format("%08" PRIx64, sect) << "\t";
  911. for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
  912. j += sizeof(int32_t)) {
  913. if (i + j + sizeof(int32_t) < size) {
  914. uint32_t long_word;
  915. memcpy(&long_word, sect + i + j, sizeof(int32_t));
  916. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  917. sys::swapByteOrder(long_word);
  918. outs() << format("%08" PRIx32, long_word) << " ";
  919. } else {
  920. for (uint32_t k = 0; i + j + k < size; k++) {
  921. uint8_t byte_word = *(sect + i + j);
  922. outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
  923. }
  924. }
  925. }
  926. outs() << "\n";
  927. }
  928. }
  929. }
  930. static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
  931. StringRef DisSegName, StringRef DisSectName);
  932. static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
  933. uint32_t size, uint32_t addr);
  934. static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
  935. bool verbose) {
  936. SymbolAddressMap AddrMap;
  937. if (verbose)
  938. CreateSymbolAddressMap(O, &AddrMap);
  939. for (unsigned i = 0; i < DumpSections.size(); ++i) {
  940. StringRef DumpSection = DumpSections[i];
  941. std::pair<StringRef, StringRef> DumpSegSectName;
  942. DumpSegSectName = DumpSection.split(',');
  943. StringRef DumpSegName, DumpSectName;
  944. if (DumpSegSectName.second.size()) {
  945. DumpSegName = DumpSegSectName.first;
  946. DumpSectName = DumpSegSectName.second;
  947. } else {
  948. DumpSegName = "";
  949. DumpSectName = DumpSegSectName.first;
  950. }
  951. for (const SectionRef &Section : O->sections()) {
  952. StringRef SectName;
  953. Section.getName(SectName);
  954. DataRefImpl Ref = Section.getRawDataRefImpl();
  955. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  956. if ((DumpSegName.empty() || SegName == DumpSegName) &&
  957. (SectName == DumpSectName)) {
  958. uint32_t section_flags;
  959. if (O->is64Bit()) {
  960. const MachO::section_64 Sec = O->getSection64(Ref);
  961. section_flags = Sec.flags;
  962. } else {
  963. const MachO::section Sec = O->getSection(Ref);
  964. section_flags = Sec.flags;
  965. }
  966. uint32_t section_type = section_flags & MachO::SECTION_TYPE;
  967. StringRef BytesStr;
  968. Section.getContents(BytesStr);
  969. const char *sect = reinterpret_cast<const char *>(BytesStr.data());
  970. uint32_t sect_size = BytesStr.size();
  971. uint64_t sect_addr = Section.getAddress();
  972. outs() << "Contents of (" << SegName << "," << SectName
  973. << ") section\n";
  974. if (verbose) {
  975. if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
  976. (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
  977. DisassembleMachO(Filename, O, SegName, SectName);
  978. continue;
  979. }
  980. if (SegName == "__TEXT" && SectName == "__info_plist") {
  981. outs() << sect;
  982. continue;
  983. }
  984. if (SegName == "__OBJC" && SectName == "__protocol") {
  985. DumpProtocolSection(O, sect, sect_size, sect_addr);
  986. continue;
  987. }
  988. switch (section_type) {
  989. case MachO::S_REGULAR:
  990. DumpRawSectionContents(O, sect, sect_size, sect_addr);
  991. break;
  992. case MachO::S_ZEROFILL:
  993. outs() << "zerofill section and has no contents in the file\n";
  994. break;
  995. case MachO::S_CSTRING_LITERALS:
  996. DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  997. break;
  998. case MachO::S_4BYTE_LITERALS:
  999. DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1000. break;
  1001. case MachO::S_8BYTE_LITERALS:
  1002. DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1003. break;
  1004. case MachO::S_16BYTE_LITERALS:
  1005. DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1006. break;
  1007. case MachO::S_LITERAL_POINTERS:
  1008. DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
  1009. !NoLeadingAddr);
  1010. break;
  1011. case MachO::S_MOD_INIT_FUNC_POINTERS:
  1012. case MachO::S_MOD_TERM_FUNC_POINTERS:
  1013. DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
  1014. verbose);
  1015. break;
  1016. default:
  1017. outs() << "Unknown section type ("
  1018. << format("0x%08" PRIx32, section_type) << ")\n";
  1019. DumpRawSectionContents(O, sect, sect_size, sect_addr);
  1020. break;
  1021. }
  1022. } else {
  1023. if (section_type == MachO::S_ZEROFILL)
  1024. outs() << "zerofill section and has no contents in the file\n";
  1025. else
  1026. DumpRawSectionContents(O, sect, sect_size, sect_addr);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. }
  1032. static void DumpInfoPlistSectionContents(StringRef Filename,
  1033. MachOObjectFile *O) {
  1034. for (const SectionRef &Section : O->sections()) {
  1035. StringRef SectName;
  1036. Section.getName(SectName);
  1037. DataRefImpl Ref = Section.getRawDataRefImpl();
  1038. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  1039. if (SegName == "__TEXT" && SectName == "__info_plist") {
  1040. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  1041. StringRef BytesStr;
  1042. Section.getContents(BytesStr);
  1043. const char *sect = reinterpret_cast<const char *>(BytesStr.data());
  1044. outs() << sect;
  1045. return;
  1046. }
  1047. }
  1048. }
  1049. // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
  1050. // and if it is and there is a list of architecture flags is specified then
  1051. // check to make sure this Mach-O file is one of those architectures or all
  1052. // architectures were specified. If not then an error is generated and this
  1053. // routine returns false. Else it returns true.
  1054. static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
  1055. if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
  1056. MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
  1057. bool ArchFound = false;
  1058. MachO::mach_header H;
  1059. MachO::mach_header_64 H_64;
  1060. Triple T;
  1061. if (MachO->is64Bit()) {
  1062. H_64 = MachO->MachOObjectFile::getHeader64();
  1063. T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
  1064. } else {
  1065. H = MachO->MachOObjectFile::getHeader();
  1066. T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
  1067. }
  1068. unsigned i;
  1069. for (i = 0; i < ArchFlags.size(); ++i) {
  1070. if (ArchFlags[i] == T.getArchName())
  1071. ArchFound = true;
  1072. break;
  1073. }
  1074. if (!ArchFound) {
  1075. errs() << "llvm-objdump: file: " + Filename + " does not contain "
  1076. << "architecture: " + ArchFlags[i] + "\n";
  1077. return false;
  1078. }
  1079. }
  1080. return true;
  1081. }
  1082. static void printObjcMetaData(MachOObjectFile *O, bool verbose);
  1083. // ProcessMachO() is passed a single opened Mach-O file, which may be an
  1084. // archive member and or in a slice of a universal file. It prints the
  1085. // the file name and header info and then processes it according to the
  1086. // command line options.
  1087. static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
  1088. StringRef ArchiveMemberName = StringRef(),
  1089. StringRef ArchitectureName = StringRef()) {
  1090. // If we are doing some processing here on the Mach-O file print the header
  1091. // info. And don't print it otherwise like in the case of printing the
  1092. // UniversalHeaders or ArchiveHeaders.
  1093. if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
  1094. LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
  1095. DylibsUsed || DylibId || ObjcMetaData || (DumpSections.size() != 0)) {
  1096. outs() << Filename;
  1097. if (!ArchiveMemberName.empty())
  1098. outs() << '(' << ArchiveMemberName << ')';
  1099. if (!ArchitectureName.empty())
  1100. outs() << " (architecture " << ArchitectureName << ")";
  1101. outs() << ":\n";
  1102. }
  1103. if (Disassemble)
  1104. DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
  1105. if (IndirectSymbols)
  1106. PrintIndirectSymbols(MachOOF, !NonVerbose);
  1107. if (DataInCode)
  1108. PrintDataInCodeTable(MachOOF, !NonVerbose);
  1109. if (LinkOptHints)
  1110. PrintLinkOptHints(MachOOF);
  1111. if (Relocations)
  1112. PrintRelocations(MachOOF);
  1113. if (SectionHeaders)
  1114. PrintSectionHeaders(MachOOF);
  1115. if (SectionContents)
  1116. PrintSectionContents(MachOOF);
  1117. if (DumpSections.size() != 0)
  1118. DumpSectionContents(Filename, MachOOF, !NonVerbose);
  1119. if (InfoPlist)
  1120. DumpInfoPlistSectionContents(Filename, MachOOF);
  1121. if (DylibsUsed)
  1122. PrintDylibs(MachOOF, false);
  1123. if (DylibId)
  1124. PrintDylibs(MachOOF, true);
  1125. if (SymbolTable)
  1126. PrintSymbolTable(MachOOF);
  1127. if (UnwindInfo)
  1128. printMachOUnwindInfo(MachOOF);
  1129. if (PrivateHeaders)
  1130. printMachOFileHeader(MachOOF);
  1131. if (ObjcMetaData)
  1132. printObjcMetaData(MachOOF, !NonVerbose);
  1133. if (ExportsTrie)
  1134. printExportsTrie(MachOOF);
  1135. if (Rebase)
  1136. printRebaseTable(MachOOF);
  1137. if (Bind)
  1138. printBindTable(MachOOF);
  1139. if (LazyBind)
  1140. printLazyBindTable(MachOOF);
  1141. if (WeakBind)
  1142. printWeakBindTable(MachOOF);
  1143. }
  1144. // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
  1145. static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
  1146. outs() << " cputype (" << cputype << ")\n";
  1147. outs() << " cpusubtype (" << cpusubtype << ")\n";
  1148. }
  1149. // printCPUType() helps print_fat_headers by printing the cputype and
  1150. // pusubtype (symbolically for the one's it knows about).
  1151. static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
  1152. switch (cputype) {
  1153. case MachO::CPU_TYPE_I386:
  1154. switch (cpusubtype) {
  1155. case MachO::CPU_SUBTYPE_I386_ALL:
  1156. outs() << " cputype CPU_TYPE_I386\n";
  1157. outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
  1158. break;
  1159. default:
  1160. printUnknownCPUType(cputype, cpusubtype);
  1161. break;
  1162. }
  1163. break;
  1164. case MachO::CPU_TYPE_X86_64:
  1165. switch (cpusubtype) {
  1166. case MachO::CPU_SUBTYPE_X86_64_ALL:
  1167. outs() << " cputype CPU_TYPE_X86_64\n";
  1168. outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
  1169. break;
  1170. case MachO::CPU_SUBTYPE_X86_64_H:
  1171. outs() << " cputype CPU_TYPE_X86_64\n";
  1172. outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
  1173. break;
  1174. default:
  1175. printUnknownCPUType(cputype, cpusubtype);
  1176. break;
  1177. }
  1178. break;
  1179. case MachO::CPU_TYPE_ARM:
  1180. switch (cpusubtype) {
  1181. case MachO::CPU_SUBTYPE_ARM_ALL:
  1182. outs() << " cputype CPU_TYPE_ARM\n";
  1183. outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
  1184. break;
  1185. case MachO::CPU_SUBTYPE_ARM_V4T:
  1186. outs() << " cputype CPU_TYPE_ARM\n";
  1187. outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
  1188. break;
  1189. case MachO::CPU_SUBTYPE_ARM_V5TEJ:
  1190. outs() << " cputype CPU_TYPE_ARM\n";
  1191. outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
  1192. break;
  1193. case MachO::CPU_SUBTYPE_ARM_XSCALE:
  1194. outs() << " cputype CPU_TYPE_ARM\n";
  1195. outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
  1196. break;
  1197. case MachO::CPU_SUBTYPE_ARM_V6:
  1198. outs() << " cputype CPU_TYPE_ARM\n";
  1199. outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
  1200. break;
  1201. case MachO::CPU_SUBTYPE_ARM_V6M:
  1202. outs() << " cputype CPU_TYPE_ARM\n";
  1203. outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
  1204. break;
  1205. case MachO::CPU_SUBTYPE_ARM_V7:
  1206. outs() << " cputype CPU_TYPE_ARM\n";
  1207. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
  1208. break;
  1209. case MachO::CPU_SUBTYPE_ARM_V7EM:
  1210. outs() << " cputype CPU_TYPE_ARM\n";
  1211. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
  1212. break;
  1213. case MachO::CPU_SUBTYPE_ARM_V7K:
  1214. outs() << " cputype CPU_TYPE_ARM\n";
  1215. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
  1216. break;
  1217. case MachO::CPU_SUBTYPE_ARM_V7M:
  1218. outs() << " cputype CPU_TYPE_ARM\n";
  1219. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
  1220. break;
  1221. case MachO::CPU_SUBTYPE_ARM_V7S:
  1222. outs() << " cputype CPU_TYPE_ARM\n";
  1223. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
  1224. break;
  1225. default:
  1226. printUnknownCPUType(cputype, cpusubtype);
  1227. break;
  1228. }
  1229. break;
  1230. case MachO::CPU_TYPE_ARM64:
  1231. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  1232. case MachO::CPU_SUBTYPE_ARM64_ALL:
  1233. outs() << " cputype CPU_TYPE_ARM64\n";
  1234. outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
  1235. break;
  1236. default:
  1237. printUnknownCPUType(cputype, cpusubtype);
  1238. break;
  1239. }
  1240. break;
  1241. default:
  1242. printUnknownCPUType(cputype, cpusubtype);
  1243. break;
  1244. }
  1245. }
  1246. static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
  1247. bool verbose) {
  1248. outs() << "Fat headers\n";
  1249. if (verbose)
  1250. outs() << "fat_magic FAT_MAGIC\n";
  1251. else
  1252. outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
  1253. uint32_t nfat_arch = UB->getNumberOfObjects();
  1254. StringRef Buf = UB->getData();
  1255. uint64_t size = Buf.size();
  1256. uint64_t big_size = sizeof(struct MachO::fat_header) +
  1257. nfat_arch * sizeof(struct MachO::fat_arch);
  1258. outs() << "nfat_arch " << UB->getNumberOfObjects();
  1259. if (nfat_arch == 0)
  1260. outs() << " (malformed, contains zero architecture types)\n";
  1261. else if (big_size > size)
  1262. outs() << " (malformed, architectures past end of file)\n";
  1263. else
  1264. outs() << "\n";
  1265. for (uint32_t i = 0; i < nfat_arch; ++i) {
  1266. MachOUniversalBinary::ObjectForArch OFA(UB, i);
  1267. uint32_t cputype = OFA.getCPUType();
  1268. uint32_t cpusubtype = OFA.getCPUSubType();
  1269. outs() << "architecture ";
  1270. for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
  1271. MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
  1272. uint32_t other_cputype = other_OFA.getCPUType();
  1273. uint32_t other_cpusubtype = other_OFA.getCPUSubType();
  1274. if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
  1275. (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
  1276. (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
  1277. outs() << "(illegal duplicate architecture) ";
  1278. break;
  1279. }
  1280. }
  1281. if (verbose) {
  1282. outs() << OFA.getArchTypeName() << "\n";
  1283. printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  1284. } else {
  1285. outs() << i << "\n";
  1286. outs() << " cputype " << cputype << "\n";
  1287. outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
  1288. << "\n";
  1289. }
  1290. if (verbose &&
  1291. (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
  1292. outs() << " capabilities CPU_SUBTYPE_LIB64\n";
  1293. else
  1294. outs() << " capabilities "
  1295. << format("0x%" PRIx32,
  1296. (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
  1297. outs() << " offset " << OFA.getOffset();
  1298. if (OFA.getOffset() > size)
  1299. outs() << " (past end of file)";
  1300. if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
  1301. outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
  1302. outs() << "\n";
  1303. outs() << " size " << OFA.getSize();
  1304. big_size = OFA.getOffset() + OFA.getSize();
  1305. if (big_size > size)
  1306. outs() << " (past end of file)";
  1307. outs() << "\n";
  1308. outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
  1309. << ")\n";
  1310. }
  1311. }
  1312. static void printArchiveChild(Archive::Child &C, bool verbose,
  1313. bool print_offset) {
  1314. if (print_offset)
  1315. outs() << C.getChildOffset() << "\t";
  1316. sys::fs::perms Mode = C.getAccessMode();
  1317. if (verbose) {
  1318. // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
  1319. // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
  1320. outs() << "-";
  1321. if (Mode & sys::fs::owner_read)
  1322. outs() << "r";
  1323. else
  1324. outs() << "-";
  1325. if (Mode & sys::fs::owner_write)
  1326. outs() << "w";
  1327. else
  1328. outs() << "-";
  1329. if (Mode & sys::fs::owner_exe)
  1330. outs() << "x";
  1331. else
  1332. outs() << "-";
  1333. if (Mode & sys::fs::group_read)
  1334. outs() << "r";
  1335. else
  1336. outs() << "-";
  1337. if (Mode & sys::fs::group_write)
  1338. outs() << "w";
  1339. else
  1340. outs() << "-";
  1341. if (Mode & sys::fs::group_exe)
  1342. outs() << "x";
  1343. else
  1344. outs() << "-";
  1345. if (Mode & sys::fs::others_read)
  1346. outs() << "r";
  1347. else
  1348. outs() << "-";
  1349. if (Mode & sys::fs::others_write)
  1350. outs() << "w";
  1351. else
  1352. outs() << "-";
  1353. if (Mode & sys::fs::others_exe)
  1354. outs() << "x";
  1355. else
  1356. outs() << "-";
  1357. } else {
  1358. outs() << format("0%o ", Mode);
  1359. }
  1360. unsigned UID = C.getUID();
  1361. outs() << format("%3d/", UID);
  1362. unsigned GID = C.getGID();
  1363. outs() << format("%-3d ", GID);
  1364. uint64_t Size = C.getRawSize();
  1365. outs() << format("%5" PRId64, Size) << " ";
  1366. StringRef RawLastModified = C.getRawLastModified();
  1367. if (verbose) {
  1368. unsigned Seconds;
  1369. if (RawLastModified.getAsInteger(10, Seconds))
  1370. outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
  1371. else {
  1372. // Since cime(3) returns a 26 character string of the form:
  1373. // "Sun Sep 16 01:03:52 1973\n\0"
  1374. // just print 24 characters.
  1375. time_t t = Seconds;
  1376. outs() << format("%.24s ", ctime(&t));
  1377. }
  1378. } else {
  1379. outs() << RawLastModified << " ";
  1380. }
  1381. if (verbose) {
  1382. ErrorOr<StringRef> NameOrErr = C.getName();
  1383. if (NameOrErr.getError()) {
  1384. StringRef RawName = C.getRawName();
  1385. outs() << RawName << "\n";
  1386. } else {
  1387. StringRef Name = NameOrErr.get();
  1388. outs() << Name << "\n";
  1389. }
  1390. } else {
  1391. StringRef RawName = C.getRawName();
  1392. outs() << RawName << "\n";
  1393. }
  1394. }
  1395. static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
  1396. if (A->hasSymbolTable()) {
  1397. Archive::child_iterator S = A->getSymbolTableChild();
  1398. Archive::Child C = *S;
  1399. printArchiveChild(C, verbose, print_offset);
  1400. }
  1401. for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
  1402. ++I) {
  1403. Archive::Child C = *I;
  1404. printArchiveChild(C, verbose, print_offset);
  1405. }
  1406. }
  1407. // ParseInputMachO() parses the named Mach-O file in Filename and handles the
  1408. // -arch flags selecting just those slices as specified by them and also parses
  1409. // archive files. Then for each individual Mach-O file ProcessMachO() is
  1410. // called to process the file based on the command line options.
  1411. void llvm::ParseInputMachO(StringRef Filename) {
  1412. // Check for -arch all and verifiy the -arch flags are valid.
  1413. for (unsigned i = 0; i < ArchFlags.size(); ++i) {
  1414. if (ArchFlags[i] == "all") {
  1415. ArchAll = true;
  1416. } else {
  1417. if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
  1418. errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
  1419. "'for the -arch option\n";
  1420. return;
  1421. }
  1422. }
  1423. }
  1424. // Attempt to open the binary.
  1425. ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
  1426. if (std::error_code EC = BinaryOrErr.getError()) {
  1427. errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
  1428. return;
  1429. }
  1430. Binary &Bin = *BinaryOrErr.get().getBinary();
  1431. if (Archive *A = dyn_cast<Archive>(&Bin)) {
  1432. outs() << "Archive : " << Filename << "\n";
  1433. if (ArchiveHeaders)
  1434. printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
  1435. for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
  1436. I != E; ++I) {
  1437. ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
  1438. if (ChildOrErr.getError())
  1439. continue;
  1440. if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
  1441. if (!checkMachOAndArchFlags(O, Filename))
  1442. return;
  1443. ProcessMachO(Filename, O, O->getFileName());
  1444. }
  1445. }
  1446. return;
  1447. }
  1448. if (UniversalHeaders) {
  1449. if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
  1450. printMachOUniversalHeaders(UB, !NonVerbose);
  1451. }
  1452. if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
  1453. // If we have a list of architecture flags specified dump only those.
  1454. if (!ArchAll && ArchFlags.size() != 0) {
  1455. // Look for a slice in the universal binary that matches each ArchFlag.
  1456. bool ArchFound;
  1457. for (unsigned i = 0; i < ArchFlags.size(); ++i) {
  1458. ArchFound = false;
  1459. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  1460. E = UB->end_objects();
  1461. I != E; ++I) {
  1462. if (ArchFlags[i] == I->getArchTypeName()) {
  1463. ArchFound = true;
  1464. ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
  1465. I->getAsObjectFile();
  1466. std::string ArchitectureName = "";
  1467. if (ArchFlags.size() > 1)
  1468. ArchitectureName = I->getArchTypeName();
  1469. if (ObjOrErr) {
  1470. ObjectFile &O = *ObjOrErr.get();
  1471. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
  1472. ProcessMachO(Filename, MachOOF, "", ArchitectureName);
  1473. } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
  1474. I->getAsArchive()) {
  1475. std::unique_ptr<Archive> &A = *AOrErr;
  1476. outs() << "Archive : " << Filename;
  1477. if (!ArchitectureName.empty())
  1478. outs() << " (architecture " << ArchitectureName << ")";
  1479. outs() << "\n";
  1480. if (ArchiveHeaders)
  1481. printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
  1482. for (Archive::child_iterator AI = A->child_begin(),
  1483. AE = A->child_end();
  1484. AI != AE; ++AI) {
  1485. ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
  1486. if (ChildOrErr.getError())
  1487. continue;
  1488. if (MachOObjectFile *O =
  1489. dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
  1490. ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
  1491. }
  1492. }
  1493. }
  1494. }
  1495. if (!ArchFound) {
  1496. errs() << "llvm-objdump: file: " + Filename + " does not contain "
  1497. << "architecture: " + ArchFlags[i] + "\n";
  1498. return;
  1499. }
  1500. }
  1501. return;
  1502. }
  1503. // No architecture flags were specified so if this contains a slice that
  1504. // matches the host architecture dump only that.
  1505. if (!ArchAll) {
  1506. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  1507. E = UB->end_objects();
  1508. I != E; ++I) {
  1509. if (MachOObjectFile::getHostArch().getArchName() ==
  1510. I->getArchTypeName()) {
  1511. ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
  1512. std::string ArchiveName;
  1513. ArchiveName.clear();
  1514. if (ObjOrErr) {
  1515. ObjectFile &O = *ObjOrErr.get();
  1516. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
  1517. ProcessMachO(Filename, MachOOF);
  1518. } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
  1519. I->getAsArchive()) {
  1520. std::unique_ptr<Archive> &A = *AOrErr;
  1521. outs() << "Archive : " << Filename << "\n";
  1522. if (ArchiveHeaders)
  1523. printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
  1524. for (Archive::child_iterator AI = A->child_begin(),
  1525. AE = A->child_end();
  1526. AI != AE; ++AI) {
  1527. ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
  1528. if (ChildOrErr.getError())
  1529. continue;
  1530. if (MachOObjectFile *O =
  1531. dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
  1532. ProcessMachO(Filename, O, O->getFileName());
  1533. }
  1534. }
  1535. return;
  1536. }
  1537. }
  1538. }
  1539. // Either all architectures have been specified or none have been specified
  1540. // and this does not contain the host architecture so dump all the slices.
  1541. bool moreThanOneArch = UB->getNumberOfObjects() > 1;
  1542. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  1543. E = UB->end_objects();
  1544. I != E; ++I) {
  1545. ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
  1546. std::string ArchitectureName = "";
  1547. if (moreThanOneArch)
  1548. ArchitectureName = I->getArchTypeName();
  1549. if (ObjOrErr) {
  1550. ObjectFile &Obj = *ObjOrErr.get();
  1551. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
  1552. ProcessMachO(Filename, MachOOF, "", ArchitectureName);
  1553. } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
  1554. std::unique_ptr<Archive> &A = *AOrErr;
  1555. outs() << "Archive : " << Filename;
  1556. if (!ArchitectureName.empty())
  1557. outs() << " (architecture " << ArchitectureName << ")";
  1558. outs() << "\n";
  1559. if (ArchiveHeaders)
  1560. printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
  1561. for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
  1562. AI != AE; ++AI) {
  1563. ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
  1564. if (ChildOrErr.getError())
  1565. continue;
  1566. if (MachOObjectFile *O =
  1567. dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
  1568. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
  1569. ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
  1570. ArchitectureName);
  1571. }
  1572. }
  1573. }
  1574. }
  1575. return;
  1576. }
  1577. if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
  1578. if (!checkMachOAndArchFlags(O, Filename))
  1579. return;
  1580. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
  1581. ProcessMachO(Filename, MachOOF);
  1582. } else
  1583. errs() << "llvm-objdump: '" << Filename << "': "
  1584. << "Object is not a Mach-O file type.\n";
  1585. } else
  1586. errs() << "llvm-objdump: '" << Filename << "': "
  1587. << "Unrecognized file type.\n";
  1588. }
  1589. typedef std::pair<uint64_t, const char *> BindInfoEntry;
  1590. typedef std::vector<BindInfoEntry> BindTable;
  1591. typedef BindTable::iterator bind_table_iterator;
  1592. // The block of info used by the Symbolizer call backs.
  1593. struct DisassembleInfo {
  1594. bool verbose;
  1595. MachOObjectFile *O;
  1596. SectionRef S;
  1597. SymbolAddressMap *AddrMap;
  1598. std::vector<SectionRef> *Sections;
  1599. const char *class_name;
  1600. const char *selector_name;
  1601. char *method;
  1602. char *demangled_name;
  1603. uint64_t adrp_addr;
  1604. uint32_t adrp_inst;
  1605. BindTable *bindtable;
  1606. };
  1607. // SymbolizerGetOpInfo() is the operand information call back function.
  1608. // This is called to get the symbolic information for operand(s) of an
  1609. // instruction when it is being done. This routine does this from
  1610. // the relocation information, symbol table, etc. That block of information
  1611. // is a pointer to the struct DisassembleInfo that was passed when the
  1612. // disassembler context was created and passed to back to here when
  1613. // called back by the disassembler for instruction operands that could have
  1614. // relocation information. The address of the instruction containing operand is
  1615. // at the Pc parameter. The immediate value the operand has is passed in
  1616. // op_info->Value and is at Offset past the start of the instruction and has a
  1617. // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
  1618. // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
  1619. // names and addends of the symbolic expression to add for the operand. The
  1620. // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
  1621. // information is returned then this function returns 1 else it returns 0.
  1622. static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
  1623. uint64_t Size, int TagType, void *TagBuf) {
  1624. struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
  1625. struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
  1626. uint64_t value = op_info->Value;
  1627. // Make sure all fields returned are zero if we don't set them.
  1628. memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
  1629. op_info->Value = value;
  1630. // If the TagType is not the value 1 which it code knows about or if no
  1631. // verbose symbolic information is wanted then just return 0, indicating no
  1632. // information is being returned.
  1633. if (TagType != 1 || !info->verbose)
  1634. return 0;
  1635. unsigned int Arch = info->O->getArch();
  1636. if (Arch == Triple::x86) {
  1637. if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
  1638. return 0;
  1639. // First search the section's relocation entries (if any) for an entry
  1640. // for this section offset.
  1641. uint32_t sect_addr = info->S.getAddress();
  1642. uint32_t sect_offset = (Pc + Offset) - sect_addr;
  1643. bool reloc_found = false;
  1644. DataRefImpl Rel;
  1645. MachO::any_relocation_info RE;
  1646. bool isExtern = false;
  1647. SymbolRef Symbol;
  1648. bool r_scattered = false;
  1649. uint32_t r_value, pair_r_value, r_type;
  1650. for (const RelocationRef &Reloc : info->S.relocations()) {
  1651. uint64_t RelocOffset = Reloc.getOffset();
  1652. if (RelocOffset == sect_offset) {
  1653. Rel = Reloc.getRawDataRefImpl();
  1654. RE = info->O->getRelocation(Rel);
  1655. r_type = info->O->getAnyRelocationType(RE);
  1656. r_scattered = info->O->isRelocationScattered(RE);
  1657. if (r_scattered) {
  1658. r_value = info->O->getScatteredRelocationValue(RE);
  1659. if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
  1660. r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
  1661. DataRefImpl RelNext = Rel;
  1662. info->O->moveRelocationNext(RelNext);
  1663. MachO::any_relocation_info RENext;
  1664. RENext = info->O->getRelocation(RelNext);
  1665. if (info->O->isRelocationScattered(RENext))
  1666. pair_r_value = info->O->getScatteredRelocationValue(RENext);
  1667. else
  1668. return 0;
  1669. }
  1670. } else {
  1671. isExtern = info->O->getPlainRelocationExternal(RE);
  1672. if (isExtern) {
  1673. symbol_iterator RelocSym = Reloc.getSymbol();
  1674. Symbol = *RelocSym;
  1675. }
  1676. }
  1677. reloc_found = true;
  1678. break;
  1679. }
  1680. }
  1681. if (reloc_found && isExtern) {
  1682. ErrorOr<StringRef> SymName = Symbol.getName();
  1683. if (std::error_code EC = SymName.getError())
  1684. report_fatal_error(EC.message());
  1685. const char *name = SymName->data();
  1686. op_info->AddSymbol.Present = 1;
  1687. op_info->AddSymbol.Name = name;
  1688. // For i386 extern relocation entries the value in the instruction is
  1689. // the offset from the symbol, and value is already set in op_info->Value.
  1690. return 1;
  1691. }
  1692. if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
  1693. r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
  1694. const char *add = GuessSymbolName(r_value, info->AddrMap);
  1695. const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
  1696. uint32_t offset = value - (r_value - pair_r_value);
  1697. op_info->AddSymbol.Present = 1;
  1698. if (add != nullptr)
  1699. op_info->AddSymbol.Name = add;
  1700. else
  1701. op_info->AddSymbol.Value = r_value;
  1702. op_info->SubtractSymbol.Present = 1;
  1703. if (sub != nullptr)
  1704. op_info->SubtractSymbol.Name = sub;
  1705. else
  1706. op_info->SubtractSymbol.Value = pair_r_value;
  1707. op_info->Value = offset;
  1708. return 1;
  1709. }
  1710. // TODO:
  1711. // Second search the external relocation entries of a fully linked image
  1712. // (if any) for an entry that matches this segment offset.
  1713. // uint32_t seg_offset = (Pc + Offset);
  1714. return 0;
  1715. }
  1716. if (Arch == Triple::x86_64) {
  1717. if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
  1718. return 0;
  1719. // First search the section's relocation entries (if any) for an entry
  1720. // for this section offset.
  1721. uint64_t sect_addr = info->S.getAddress();
  1722. uint64_t sect_offset = (Pc + Offset) - sect_addr;
  1723. bool reloc_found = false;
  1724. DataRefImpl Rel;
  1725. MachO::any_relocation_info RE;
  1726. bool isExtern = false;
  1727. SymbolRef Symbol;
  1728. for (const RelocationRef &Reloc : info->S.relocations()) {
  1729. uint64_t RelocOffset = Reloc.getOffset();
  1730. if (RelocOffset == sect_offset) {
  1731. Rel = Reloc.getRawDataRefImpl();
  1732. RE = info->O->getRelocation(Rel);
  1733. // NOTE: Scattered relocations don't exist on x86_64.
  1734. isExtern = info->O->getPlainRelocationExternal(RE);
  1735. if (isExtern) {
  1736. symbol_iterator RelocSym = Reloc.getSymbol();
  1737. Symbol = *RelocSym;
  1738. }
  1739. reloc_found = true;
  1740. break;
  1741. }
  1742. }
  1743. if (reloc_found && isExtern) {
  1744. // The Value passed in will be adjusted by the Pc if the instruction
  1745. // adds the Pc. But for x86_64 external relocation entries the Value
  1746. // is the offset from the external symbol.
  1747. if (info->O->getAnyRelocationPCRel(RE))
  1748. op_info->Value -= Pc + Offset + Size;
  1749. ErrorOr<StringRef> SymName = Symbol.getName();
  1750. if (std::error_code EC = SymName.getError())
  1751. report_fatal_error(EC.message());
  1752. const char *name = SymName->data();
  1753. unsigned Type = info->O->getAnyRelocationType(RE);
  1754. if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
  1755. DataRefImpl RelNext = Rel;
  1756. info->O->moveRelocationNext(RelNext);
  1757. MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
  1758. unsigned TypeNext = info->O->getAnyRelocationType(RENext);
  1759. bool isExternNext = info->O->getPlainRelocationExternal(RENext);
  1760. unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
  1761. if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
  1762. op_info->SubtractSymbol.Present = 1;
  1763. op_info->SubtractSymbol.Name = name;
  1764. symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
  1765. Symbol = *RelocSymNext;
  1766. ErrorOr<StringRef> SymNameNext = Symbol.getName();
  1767. if (std::error_code EC = SymNameNext.getError())
  1768. report_fatal_error(EC.message());
  1769. name = SymNameNext->data();
  1770. }
  1771. }
  1772. // TODO: add the VariantKinds to op_info->VariantKind for relocation types
  1773. // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
  1774. op_info->AddSymbol.Present = 1;
  1775. op_info->AddSymbol.Name = name;
  1776. return 1;
  1777. }
  1778. // TODO:
  1779. // Second search the external relocation entries of a fully linked image
  1780. // (if any) for an entry that matches this segment offset.
  1781. // uint64_t seg_offset = (Pc + Offset);
  1782. return 0;
  1783. }
  1784. if (Arch == Triple::arm) {
  1785. if (Offset != 0 || (Size != 4 && Size != 2))
  1786. return 0;
  1787. // First search the section's relocation entries (if any) for an entry
  1788. // for this section offset.
  1789. uint32_t sect_addr = info->S.getAddress();
  1790. uint32_t sect_offset = (Pc + Offset) - sect_addr;
  1791. DataRefImpl Rel;
  1792. MachO::any_relocation_info RE;
  1793. bool isExtern = false;
  1794. SymbolRef Symbol;
  1795. bool r_scattered = false;
  1796. uint32_t r_value, pair_r_value, r_type, r_length, other_half;
  1797. auto Reloc =
  1798. std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
  1799. [&](const RelocationRef &Reloc) {
  1800. uint64_t RelocOffset = Reloc.getOffset();
  1801. return RelocOffset == sect_offset;
  1802. });
  1803. if (Reloc == info->S.relocations().end())
  1804. return 0;
  1805. Rel = Reloc->getRawDataRefImpl();
  1806. RE = info->O->getRelocation(Rel);
  1807. r_length = info->O->getAnyRelocationLength(RE);
  1808. r_scattered = info->O->isRelocationScattered(RE);
  1809. if (r_scattered) {
  1810. r_value = info->O->getScatteredRelocationValue(RE);
  1811. r_type = info->O->getScatteredRelocationType(RE);
  1812. } else {
  1813. r_type = info->O->getAnyRelocationType(RE);
  1814. isExtern = info->O->getPlainRelocationExternal(RE);
  1815. if (isExtern) {
  1816. symbol_iterator RelocSym = Reloc->getSymbol();
  1817. Symbol = *RelocSym;
  1818. }
  1819. }
  1820. if (r_type == MachO::ARM_RELOC_HALF ||
  1821. r_type == MachO::ARM_RELOC_SECTDIFF ||
  1822. r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
  1823. r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  1824. DataRefImpl RelNext = Rel;
  1825. info->O->moveRelocationNext(RelNext);
  1826. MachO::any_relocation_info RENext;
  1827. RENext = info->O->getRelocation(RelNext);
  1828. other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
  1829. if (info->O->isRelocationScattered(RENext))
  1830. pair_r_value = info->O->getScatteredRelocationValue(RENext);
  1831. }
  1832. if (isExtern) {
  1833. ErrorOr<StringRef> SymName = Symbol.getName();
  1834. if (std::error_code EC = SymName.getError())
  1835. report_fatal_error(EC.message());
  1836. const char *name = SymName->data();
  1837. op_info->AddSymbol.Present = 1;
  1838. op_info->AddSymbol.Name = name;
  1839. switch (r_type) {
  1840. case MachO::ARM_RELOC_HALF:
  1841. if ((r_length & 0x1) == 1) {
  1842. op_info->Value = value << 16 | other_half;
  1843. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
  1844. } else {
  1845. op_info->Value = other_half << 16 | value;
  1846. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
  1847. }
  1848. break;
  1849. default:
  1850. break;
  1851. }
  1852. return 1;
  1853. }
  1854. // If we have a branch that is not an external relocation entry then
  1855. // return 0 so the code in tryAddingSymbolicOperand() can use the
  1856. // SymbolLookUp call back with the branch target address to look up the
  1857. // symbol and possiblity add an annotation for a symbol stub.
  1858. if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
  1859. r_type == MachO::ARM_THUMB_RELOC_BR22))
  1860. return 0;
  1861. uint32_t offset = 0;
  1862. if (r_type == MachO::ARM_RELOC_HALF ||
  1863. r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  1864. if ((r_length & 0x1) == 1)
  1865. value = value << 16 | other_half;
  1866. else
  1867. value = other_half << 16 | value;
  1868. }
  1869. if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
  1870. r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
  1871. offset = value - r_value;
  1872. value = r_value;
  1873. }
  1874. if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  1875. if ((r_length & 0x1) == 1)
  1876. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
  1877. else
  1878. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
  1879. const char *add = GuessSymbolName(r_value, info->AddrMap);
  1880. const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
  1881. int32_t offset = value - (r_value - pair_r_value);
  1882. op_info->AddSymbol.Present = 1;
  1883. if (add != nullptr)
  1884. op_info->AddSymbol.Name = add;
  1885. else
  1886. op_info->AddSymbol.Value = r_value;
  1887. op_info->SubtractSymbol.Present = 1;
  1888. if (sub != nullptr)
  1889. op_info->SubtractSymbol.Name = sub;
  1890. else
  1891. op_info->SubtractSymbol.Value = pair_r_value;
  1892. op_info->Value = offset;
  1893. return 1;
  1894. }
  1895. op_info->AddSymbol.Present = 1;
  1896. op_info->Value = offset;
  1897. if (r_type == MachO::ARM_RELOC_HALF) {
  1898. if ((r_length & 0x1) == 1)
  1899. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
  1900. else
  1901. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
  1902. }
  1903. const char *add = GuessSymbolName(value, info->AddrMap);
  1904. if (add != nullptr) {
  1905. op_info->AddSymbol.Name = add;
  1906. return 1;
  1907. }
  1908. op_info->AddSymbol.Value = value;
  1909. return 1;
  1910. }
  1911. if (Arch == Triple::aarch64) {
  1912. if (Offset != 0 || Size != 4)
  1913. return 0;
  1914. // First search the section's relocation entries (if any) for an entry
  1915. // for this section offset.
  1916. uint64_t sect_addr = info->S.getAddress();
  1917. uint64_t sect_offset = (Pc + Offset) - sect_addr;
  1918. auto Reloc =
  1919. std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
  1920. [&](const RelocationRef &Reloc) {
  1921. uint64_t RelocOffset = Reloc.getOffset();
  1922. return RelocOffset == sect_offset;
  1923. });
  1924. if (Reloc == info->S.relocations().end())
  1925. return 0;
  1926. DataRefImpl Rel = Reloc->getRawDataRefImpl();
  1927. MachO::any_relocation_info RE = info->O->getRelocation(Rel);
  1928. uint32_t r_type = info->O->getAnyRelocationType(RE);
  1929. if (r_type == MachO::ARM64_RELOC_ADDEND) {
  1930. DataRefImpl RelNext = Rel;
  1931. info->O->moveRelocationNext(RelNext);
  1932. MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
  1933. if (value == 0) {
  1934. value = info->O->getPlainRelocationSymbolNum(RENext);
  1935. op_info->Value = value;
  1936. }
  1937. }
  1938. // NOTE: Scattered relocations don't exist on arm64.
  1939. if (!info->O->getPlainRelocationExternal(RE))
  1940. return 0;
  1941. ErrorOr<StringRef> SymName = Reloc->getSymbol()->getName();
  1942. if (std::error_code EC = SymName.getError())
  1943. report_fatal_error(EC.message());
  1944. const char *name = SymName->data();
  1945. op_info->AddSymbol.Present = 1;
  1946. op_info->AddSymbol.Name = name;
  1947. switch (r_type) {
  1948. case MachO::ARM64_RELOC_PAGE21:
  1949. /* @page */
  1950. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
  1951. break;
  1952. case MachO::ARM64_RELOC_PAGEOFF12:
  1953. /* @pageoff */
  1954. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
  1955. break;
  1956. case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
  1957. /* @gotpage */
  1958. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
  1959. break;
  1960. case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
  1961. /* @gotpageoff */
  1962. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
  1963. break;
  1964. case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
  1965. /* @tvlppage is not implemented in llvm-mc */
  1966. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
  1967. break;
  1968. case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
  1969. /* @tvlppageoff is not implemented in llvm-mc */
  1970. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
  1971. break;
  1972. default:
  1973. case MachO::ARM64_RELOC_BRANCH26:
  1974. op_info->VariantKind = LLVMDisassembler_VariantKind_None;
  1975. break;
  1976. }
  1977. return 1;
  1978. }
  1979. return 0;
  1980. }
  1981. // GuessCstringPointer is passed the address of what might be a pointer to a
  1982. // literal string in a cstring section. If that address is in a cstring section
  1983. // it returns a pointer to that string. Else it returns nullptr.
  1984. static const char *GuessCstringPointer(uint64_t ReferenceValue,
  1985. struct DisassembleInfo *info) {
  1986. for (const auto &Load : info->O->load_commands()) {
  1987. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  1988. MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
  1989. for (unsigned J = 0; J < Seg.nsects; ++J) {
  1990. MachO::section_64 Sec = info->O->getSection64(Load, J);
  1991. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  1992. if (section_type == MachO::S_CSTRING_LITERALS &&
  1993. ReferenceValue >= Sec.addr &&
  1994. ReferenceValue < Sec.addr + Sec.size) {
  1995. uint64_t sect_offset = ReferenceValue - Sec.addr;
  1996. uint64_t object_offset = Sec.offset + sect_offset;
  1997. StringRef MachOContents = info->O->getData();
  1998. uint64_t object_size = MachOContents.size();
  1999. const char *object_addr = (const char *)MachOContents.data();
  2000. if (object_offset < object_size) {
  2001. const char *name = object_addr + object_offset;
  2002. return name;
  2003. } else {
  2004. return nullptr;
  2005. }
  2006. }
  2007. }
  2008. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  2009. MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
  2010. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2011. MachO::section Sec = info->O->getSection(Load, J);
  2012. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2013. if (section_type == MachO::S_CSTRING_LITERALS &&
  2014. ReferenceValue >= Sec.addr &&
  2015. ReferenceValue < Sec.addr + Sec.size) {
  2016. uint64_t sect_offset = ReferenceValue - Sec.addr;
  2017. uint64_t object_offset = Sec.offset + sect_offset;
  2018. StringRef MachOContents = info->O->getData();
  2019. uint64_t object_size = MachOContents.size();
  2020. const char *object_addr = (const char *)MachOContents.data();
  2021. if (object_offset < object_size) {
  2022. const char *name = object_addr + object_offset;
  2023. return name;
  2024. } else {
  2025. return nullptr;
  2026. }
  2027. }
  2028. }
  2029. }
  2030. }
  2031. return nullptr;
  2032. }
  2033. // GuessIndirectSymbol returns the name of the indirect symbol for the
  2034. // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
  2035. // an address of a symbol stub or a lazy or non-lazy pointer to associate the
  2036. // symbol name being referenced by the stub or pointer.
  2037. static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
  2038. struct DisassembleInfo *info) {
  2039. MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
  2040. MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
  2041. for (const auto &Load : info->O->load_commands()) {
  2042. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  2043. MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
  2044. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2045. MachO::section_64 Sec = info->O->getSection64(Load, J);
  2046. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2047. if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  2048. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  2049. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  2050. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  2051. section_type == MachO::S_SYMBOL_STUBS) &&
  2052. ReferenceValue >= Sec.addr &&
  2053. ReferenceValue < Sec.addr + Sec.size) {
  2054. uint32_t stride;
  2055. if (section_type == MachO::S_SYMBOL_STUBS)
  2056. stride = Sec.reserved2;
  2057. else
  2058. stride = 8;
  2059. if (stride == 0)
  2060. return nullptr;
  2061. uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
  2062. if (index < Dysymtab.nindirectsyms) {
  2063. uint32_t indirect_symbol =
  2064. info->O->getIndirectSymbolTableEntry(Dysymtab, index);
  2065. if (indirect_symbol < Symtab.nsyms) {
  2066. symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
  2067. SymbolRef Symbol = *Sym;
  2068. ErrorOr<StringRef> SymName = Symbol.getName();
  2069. if (std::error_code EC = SymName.getError())
  2070. report_fatal_error(EC.message());
  2071. const char *name = SymName->data();
  2072. return name;
  2073. }
  2074. }
  2075. }
  2076. }
  2077. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  2078. MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
  2079. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2080. MachO::section Sec = info->O->getSection(Load, J);
  2081. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2082. if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  2083. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  2084. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  2085. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  2086. section_type == MachO::S_SYMBOL_STUBS) &&
  2087. ReferenceValue >= Sec.addr &&
  2088. ReferenceValue < Sec.addr + Sec.size) {
  2089. uint32_t stride;
  2090. if (section_type == MachO::S_SYMBOL_STUBS)
  2091. stride = Sec.reserved2;
  2092. else
  2093. stride = 4;
  2094. if (stride == 0)
  2095. return nullptr;
  2096. uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
  2097. if (index < Dysymtab.nindirectsyms) {
  2098. uint32_t indirect_symbol =
  2099. info->O->getIndirectSymbolTableEntry(Dysymtab, index);
  2100. if (indirect_symbol < Symtab.nsyms) {
  2101. symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
  2102. SymbolRef Symbol = *Sym;
  2103. ErrorOr<StringRef> SymName = Symbol.getName();
  2104. if (std::error_code EC = SymName.getError())
  2105. report_fatal_error(EC.message());
  2106. const char *name = SymName->data();
  2107. return name;
  2108. }
  2109. }
  2110. }
  2111. }
  2112. }
  2113. }
  2114. return nullptr;
  2115. }
  2116. // method_reference() is called passing it the ReferenceName that might be
  2117. // a reference it to an Objective-C method call. If so then it allocates and
  2118. // assembles a method call string with the values last seen and saved in
  2119. // the DisassembleInfo's class_name and selector_name fields. This is saved
  2120. // into the method field of the info and any previous string is free'ed.
  2121. // Then the class_name field in the info is set to nullptr. The method call
  2122. // string is set into ReferenceName and ReferenceType is set to
  2123. // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
  2124. // then both ReferenceType and ReferenceName are left unchanged.
  2125. static void method_reference(struct DisassembleInfo *info,
  2126. uint64_t *ReferenceType,
  2127. const char **ReferenceName) {
  2128. unsigned int Arch = info->O->getArch();
  2129. if (*ReferenceName != nullptr) {
  2130. if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
  2131. if (info->selector_name != nullptr) {
  2132. if (info->method != nullptr)
  2133. free(info->method);
  2134. if (info->class_name != nullptr) {
  2135. info->method = (char *)malloc(5 + strlen(info->class_name) +
  2136. strlen(info->selector_name));
  2137. if (info->method != nullptr) {
  2138. strcpy(info->method, "+[");
  2139. strcat(info->method, info->class_name);
  2140. strcat(info->method, " ");
  2141. strcat(info->method, info->selector_name);
  2142. strcat(info->method, "]");
  2143. *ReferenceName = info->method;
  2144. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
  2145. }
  2146. } else {
  2147. info->method = (char *)malloc(9 + strlen(info->selector_name));
  2148. if (info->method != nullptr) {
  2149. if (Arch == Triple::x86_64)
  2150. strcpy(info->method, "-[%rdi ");
  2151. else if (Arch == Triple::aarch64)
  2152. strcpy(info->method, "-[x0 ");
  2153. else
  2154. strcpy(info->method, "-[r? ");
  2155. strcat(info->method, info->selector_name);
  2156. strcat(info->method, "]");
  2157. *ReferenceName = info->method;
  2158. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
  2159. }
  2160. }
  2161. info->class_name = nullptr;
  2162. }
  2163. } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
  2164. if (info->selector_name != nullptr) {
  2165. if (info->method != nullptr)
  2166. free(info->method);
  2167. info->method = (char *)malloc(17 + strlen(info->selector_name));
  2168. if (info->method != nullptr) {
  2169. if (Arch == Triple::x86_64)
  2170. strcpy(info->method, "-[[%rdi super] ");
  2171. else if (Arch == Triple::aarch64)
  2172. strcpy(info->method, "-[[x0 super] ");
  2173. else
  2174. strcpy(info->method, "-[[r? super] ");
  2175. strcat(info->method, info->selector_name);
  2176. strcat(info->method, "]");
  2177. *ReferenceName = info->method;
  2178. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
  2179. }
  2180. info->class_name = nullptr;
  2181. }
  2182. }
  2183. }
  2184. }
  2185. // GuessPointerPointer() is passed the address of what might be a pointer to
  2186. // a reference to an Objective-C class, selector, message ref or cfstring.
  2187. // If so the value of the pointer is returned and one of the booleans are set
  2188. // to true. If not zero is returned and all the booleans are set to false.
  2189. static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
  2190. struct DisassembleInfo *info,
  2191. bool &classref, bool &selref, bool &msgref,
  2192. bool &cfstring) {
  2193. classref = false;
  2194. selref = false;
  2195. msgref = false;
  2196. cfstring = false;
  2197. for (const auto &Load : info->O->load_commands()) {
  2198. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  2199. MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
  2200. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2201. MachO::section_64 Sec = info->O->getSection64(Load, J);
  2202. if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
  2203. strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
  2204. strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
  2205. strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
  2206. strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
  2207. ReferenceValue >= Sec.addr &&
  2208. ReferenceValue < Sec.addr + Sec.size) {
  2209. uint64_t sect_offset = ReferenceValue - Sec.addr;
  2210. uint64_t object_offset = Sec.offset + sect_offset;
  2211. StringRef MachOContents = info->O->getData();
  2212. uint64_t object_size = MachOContents.size();
  2213. const char *object_addr = (const char *)MachOContents.data();
  2214. if (object_offset < object_size) {
  2215. uint64_t pointer_value;
  2216. memcpy(&pointer_value, object_addr + object_offset,
  2217. sizeof(uint64_t));
  2218. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  2219. sys::swapByteOrder(pointer_value);
  2220. if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
  2221. selref = true;
  2222. else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
  2223. strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
  2224. classref = true;
  2225. else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
  2226. ReferenceValue + 8 < Sec.addr + Sec.size) {
  2227. msgref = true;
  2228. memcpy(&pointer_value, object_addr + object_offset + 8,
  2229. sizeof(uint64_t));
  2230. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  2231. sys::swapByteOrder(pointer_value);
  2232. } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
  2233. cfstring = true;
  2234. return pointer_value;
  2235. } else {
  2236. return 0;
  2237. }
  2238. }
  2239. }
  2240. }
  2241. // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
  2242. }
  2243. return 0;
  2244. }
  2245. // get_pointer_64 returns a pointer to the bytes in the object file at the
  2246. // Address from a section in the Mach-O file. And indirectly returns the
  2247. // offset into the section, number of bytes left in the section past the offset
  2248. // and which section is was being referenced. If the Address is not in a
  2249. // section nullptr is returned.
  2250. static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
  2251. uint32_t &left, SectionRef &S,
  2252. DisassembleInfo *info,
  2253. bool objc_only = false) {
  2254. offset = 0;
  2255. left = 0;
  2256. S = SectionRef();
  2257. for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
  2258. uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
  2259. uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
  2260. if (objc_only) {
  2261. StringRef SectName;
  2262. ((*(info->Sections))[SectIdx]).getName(SectName);
  2263. DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
  2264. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  2265. if (SegName != "__OBJC" && SectName != "__cstring")
  2266. continue;
  2267. }
  2268. if (Address >= SectAddress && Address < SectAddress + SectSize) {
  2269. S = (*(info->Sections))[SectIdx];
  2270. offset = Address - SectAddress;
  2271. left = SectSize - offset;
  2272. StringRef SectContents;
  2273. ((*(info->Sections))[SectIdx]).getContents(SectContents);
  2274. return SectContents.data() + offset;
  2275. }
  2276. }
  2277. return nullptr;
  2278. }
  2279. static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
  2280. uint32_t &left, SectionRef &S,
  2281. DisassembleInfo *info,
  2282. bool objc_only = false) {
  2283. return get_pointer_64(Address, offset, left, S, info, objc_only);
  2284. }
  2285. // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
  2286. // the symbol indirectly through n_value. Based on the relocation information
  2287. // for the specified section offset in the specified section reference.
  2288. // If no relocation information is found and a non-zero ReferenceValue for the
  2289. // symbol is passed, look up that address in the info's AddrMap.
  2290. static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
  2291. DisassembleInfo *info, uint64_t &n_value,
  2292. uint64_t ReferenceValue = 0) {
  2293. n_value = 0;
  2294. if (!info->verbose)
  2295. return nullptr;
  2296. // See if there is an external relocation entry at the sect_offset.
  2297. bool reloc_found = false;
  2298. DataRefImpl Rel;
  2299. MachO::any_relocation_info RE;
  2300. bool isExtern = false;
  2301. SymbolRef Symbol;
  2302. for (const RelocationRef &Reloc : S.relocations()) {
  2303. uint64_t RelocOffset = Reloc.getOffset();
  2304. if (RelocOffset == sect_offset) {
  2305. Rel = Reloc.getRawDataRefImpl();
  2306. RE = info->O->getRelocation(Rel);
  2307. if (info->O->isRelocationScattered(RE))
  2308. continue;
  2309. isExtern = info->O->getPlainRelocationExternal(RE);
  2310. if (isExtern) {
  2311. symbol_iterator RelocSym = Reloc.getSymbol();
  2312. Symbol = *RelocSym;
  2313. }
  2314. reloc_found = true;
  2315. break;
  2316. }
  2317. }
  2318. // If there is an external relocation entry for a symbol in this section
  2319. // at this section_offset then use that symbol's value for the n_value
  2320. // and return its name.
  2321. const char *SymbolName = nullptr;
  2322. if (reloc_found && isExtern) {
  2323. n_value = Symbol.getValue();
  2324. ErrorOr<StringRef> NameOrError = Symbol.getName();
  2325. if (std::error_code EC = NameOrError.getError())
  2326. report_fatal_error(EC.message());
  2327. StringRef Name = *NameOrError;
  2328. if (!Name.empty()) {
  2329. SymbolName = Name.data();
  2330. return SymbolName;
  2331. }
  2332. }
  2333. // TODO: For fully linked images, look through the external relocation
  2334. // entries off the dynamic symtab command. For these the r_offset is from the
  2335. // start of the first writeable segment in the Mach-O file. So the offset
  2336. // to this section from that segment is passed to this routine by the caller,
  2337. // as the database_offset. Which is the difference of the section's starting
  2338. // address and the first writable segment.
  2339. //
  2340. // NOTE: need add passing the database_offset to this routine.
  2341. // We did not find an external relocation entry so look up the ReferenceValue
  2342. // as an address of a symbol and if found return that symbol's name.
  2343. SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
  2344. return SymbolName;
  2345. }
  2346. static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
  2347. DisassembleInfo *info,
  2348. uint32_t ReferenceValue) {
  2349. uint64_t n_value64;
  2350. return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
  2351. }
  2352. // These are structs in the Objective-C meta data and read to produce the
  2353. // comments for disassembly. While these are part of the ABI they are no
  2354. // public defintions. So the are here not in include/llvm/Support/MachO.h .
  2355. // The cfstring object in a 64-bit Mach-O file.
  2356. struct cfstring64_t {
  2357. uint64_t isa; // class64_t * (64-bit pointer)
  2358. uint64_t flags; // flag bits
  2359. uint64_t characters; // char * (64-bit pointer)
  2360. uint64_t length; // number of non-NULL characters in above
  2361. };
  2362. // The class object in a 64-bit Mach-O file.
  2363. struct class64_t {
  2364. uint64_t isa; // class64_t * (64-bit pointer)
  2365. uint64_t superclass; // class64_t * (64-bit pointer)
  2366. uint64_t cache; // Cache (64-bit pointer)
  2367. uint64_t vtable; // IMP * (64-bit pointer)
  2368. uint64_t data; // class_ro64_t * (64-bit pointer)
  2369. };
  2370. struct class32_t {
  2371. uint32_t isa; /* class32_t * (32-bit pointer) */
  2372. uint32_t superclass; /* class32_t * (32-bit pointer) */
  2373. uint32_t cache; /* Cache (32-bit pointer) */
  2374. uint32_t vtable; /* IMP * (32-bit pointer) */
  2375. uint32_t data; /* class_ro32_t * (32-bit pointer) */
  2376. };
  2377. struct class_ro64_t {
  2378. uint32_t flags;
  2379. uint32_t instanceStart;
  2380. uint32_t instanceSize;
  2381. uint32_t reserved;
  2382. uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
  2383. uint64_t name; // const char * (64-bit pointer)
  2384. uint64_t baseMethods; // const method_list_t * (64-bit pointer)
  2385. uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
  2386. uint64_t ivars; // const ivar_list_t * (64-bit pointer)
  2387. uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
  2388. uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
  2389. };
  2390. struct class_ro32_t {
  2391. uint32_t flags;
  2392. uint32_t instanceStart;
  2393. uint32_t instanceSize;
  2394. uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
  2395. uint32_t name; /* const char * (32-bit pointer) */
  2396. uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
  2397. uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
  2398. uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
  2399. uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
  2400. uint32_t baseProperties; /* const struct objc_property_list *
  2401. (32-bit pointer) */
  2402. };
  2403. /* Values for class_ro{64,32}_t->flags */
  2404. #define RO_META (1 << 0)
  2405. #define RO_ROOT (1 << 1)
  2406. #define RO_HAS_CXX_STRUCTORS (1 << 2)
  2407. struct method_list64_t {
  2408. uint32_t entsize;
  2409. uint32_t count;
  2410. /* struct method64_t first; These structures follow inline */
  2411. };
  2412. struct method_list32_t {
  2413. uint32_t entsize;
  2414. uint32_t count;
  2415. /* struct method32_t first; These structures follow inline */
  2416. };
  2417. struct method64_t {
  2418. uint64_t name; /* SEL (64-bit pointer) */
  2419. uint64_t types; /* const char * (64-bit pointer) */
  2420. uint64_t imp; /* IMP (64-bit pointer) */
  2421. };
  2422. struct method32_t {
  2423. uint32_t name; /* SEL (32-bit pointer) */
  2424. uint32_t types; /* const char * (32-bit pointer) */
  2425. uint32_t imp; /* IMP (32-bit pointer) */
  2426. };
  2427. struct protocol_list64_t {
  2428. uint64_t count; /* uintptr_t (a 64-bit value) */
  2429. /* struct protocol64_t * list[0]; These pointers follow inline */
  2430. };
  2431. struct protocol_list32_t {
  2432. uint32_t count; /* uintptr_t (a 32-bit value) */
  2433. /* struct protocol32_t * list[0]; These pointers follow inline */
  2434. };
  2435. struct protocol64_t {
  2436. uint64_t isa; /* id * (64-bit pointer) */
  2437. uint64_t name; /* const char * (64-bit pointer) */
  2438. uint64_t protocols; /* struct protocol_list64_t *
  2439. (64-bit pointer) */
  2440. uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
  2441. uint64_t classMethods; /* method_list_t * (64-bit pointer) */
  2442. uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
  2443. uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
  2444. uint64_t instanceProperties; /* struct objc_property_list *
  2445. (64-bit pointer) */
  2446. };
  2447. struct protocol32_t {
  2448. uint32_t isa; /* id * (32-bit pointer) */
  2449. uint32_t name; /* const char * (32-bit pointer) */
  2450. uint32_t protocols; /* struct protocol_list_t *
  2451. (32-bit pointer) */
  2452. uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
  2453. uint32_t classMethods; /* method_list_t * (32-bit pointer) */
  2454. uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
  2455. uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
  2456. uint32_t instanceProperties; /* struct objc_property_list *
  2457. (32-bit pointer) */
  2458. };
  2459. struct ivar_list64_t {
  2460. uint32_t entsize;
  2461. uint32_t count;
  2462. /* struct ivar64_t first; These structures follow inline */
  2463. };
  2464. struct ivar_list32_t {
  2465. uint32_t entsize;
  2466. uint32_t count;
  2467. /* struct ivar32_t first; These structures follow inline */
  2468. };
  2469. struct ivar64_t {
  2470. uint64_t offset; /* uintptr_t * (64-bit pointer) */
  2471. uint64_t name; /* const char * (64-bit pointer) */
  2472. uint64_t type; /* const char * (64-bit pointer) */
  2473. uint32_t alignment;
  2474. uint32_t size;
  2475. };
  2476. struct ivar32_t {
  2477. uint32_t offset; /* uintptr_t * (32-bit pointer) */
  2478. uint32_t name; /* const char * (32-bit pointer) */
  2479. uint32_t type; /* const char * (32-bit pointer) */
  2480. uint32_t alignment;
  2481. uint32_t size;
  2482. };
  2483. struct objc_property_list64 {
  2484. uint32_t entsize;
  2485. uint32_t count;
  2486. /* struct objc_property64 first; These structures follow inline */
  2487. };
  2488. struct objc_property_list32 {
  2489. uint32_t entsize;
  2490. uint32_t count;
  2491. /* struct objc_property32 first; These structures follow inline */
  2492. };
  2493. struct objc_property64 {
  2494. uint64_t name; /* const char * (64-bit pointer) */
  2495. uint64_t attributes; /* const char * (64-bit pointer) */
  2496. };
  2497. struct objc_property32 {
  2498. uint32_t name; /* const char * (32-bit pointer) */
  2499. uint32_t attributes; /* const char * (32-bit pointer) */
  2500. };
  2501. struct category64_t {
  2502. uint64_t name; /* const char * (64-bit pointer) */
  2503. uint64_t cls; /* struct class_t * (64-bit pointer) */
  2504. uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
  2505. uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
  2506. uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
  2507. uint64_t instanceProperties; /* struct objc_property_list *
  2508. (64-bit pointer) */
  2509. };
  2510. struct category32_t {
  2511. uint32_t name; /* const char * (32-bit pointer) */
  2512. uint32_t cls; /* struct class_t * (32-bit pointer) */
  2513. uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
  2514. uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
  2515. uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
  2516. uint32_t instanceProperties; /* struct objc_property_list *
  2517. (32-bit pointer) */
  2518. };
  2519. struct objc_image_info64 {
  2520. uint32_t version;
  2521. uint32_t flags;
  2522. };
  2523. struct objc_image_info32 {
  2524. uint32_t version;
  2525. uint32_t flags;
  2526. };
  2527. struct imageInfo_t {
  2528. uint32_t version;
  2529. uint32_t flags;
  2530. };
  2531. /* masks for objc_image_info.flags */
  2532. #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
  2533. #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
  2534. struct message_ref64 {
  2535. uint64_t imp; /* IMP (64-bit pointer) */
  2536. uint64_t sel; /* SEL (64-bit pointer) */
  2537. };
  2538. struct message_ref32 {
  2539. uint32_t imp; /* IMP (32-bit pointer) */
  2540. uint32_t sel; /* SEL (32-bit pointer) */
  2541. };
  2542. // Objective-C 1 (32-bit only) meta data structs.
  2543. struct objc_module_t {
  2544. uint32_t version;
  2545. uint32_t size;
  2546. uint32_t name; /* char * (32-bit pointer) */
  2547. uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
  2548. };
  2549. struct objc_symtab_t {
  2550. uint32_t sel_ref_cnt;
  2551. uint32_t refs; /* SEL * (32-bit pointer) */
  2552. uint16_t cls_def_cnt;
  2553. uint16_t cat_def_cnt;
  2554. // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
  2555. };
  2556. struct objc_class_t {
  2557. uint32_t isa; /* struct objc_class * (32-bit pointer) */
  2558. uint32_t super_class; /* struct objc_class * (32-bit pointer) */
  2559. uint32_t name; /* const char * (32-bit pointer) */
  2560. int32_t version;
  2561. int32_t info;
  2562. int32_t instance_size;
  2563. uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
  2564. uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
  2565. uint32_t cache; /* struct objc_cache * (32-bit pointer) */
  2566. uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
  2567. };
  2568. #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
  2569. // class is not a metaclass
  2570. #define CLS_CLASS 0x1
  2571. // class is a metaclass
  2572. #define CLS_META 0x2
  2573. struct objc_category_t {
  2574. uint32_t category_name; /* char * (32-bit pointer) */
  2575. uint32_t class_name; /* char * (32-bit pointer) */
  2576. uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
  2577. uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
  2578. uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
  2579. };
  2580. struct objc_ivar_t {
  2581. uint32_t ivar_name; /* char * (32-bit pointer) */
  2582. uint32_t ivar_type; /* char * (32-bit pointer) */
  2583. int32_t ivar_offset;
  2584. };
  2585. struct objc_ivar_list_t {
  2586. int32_t ivar_count;
  2587. // struct objc_ivar_t ivar_list[1]; /* variable length structure */
  2588. };
  2589. struct objc_method_list_t {
  2590. uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
  2591. int32_t method_count;
  2592. // struct objc_method_t method_list[1]; /* variable length structure */
  2593. };
  2594. struct objc_method_t {
  2595. uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
  2596. uint32_t method_types; /* char * (32-bit pointer) */
  2597. uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
  2598. (32-bit pointer) */
  2599. };
  2600. struct objc_protocol_list_t {
  2601. uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
  2602. int32_t count;
  2603. // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
  2604. // (32-bit pointer) */
  2605. };
  2606. struct objc_protocol_t {
  2607. uint32_t isa; /* struct objc_class * (32-bit pointer) */
  2608. uint32_t protocol_name; /* char * (32-bit pointer) */
  2609. uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
  2610. uint32_t instance_methods; /* struct objc_method_description_list *
  2611. (32-bit pointer) */
  2612. uint32_t class_methods; /* struct objc_method_description_list *
  2613. (32-bit pointer) */
  2614. };
  2615. struct objc_method_description_list_t {
  2616. int32_t count;
  2617. // struct objc_method_description_t list[1];
  2618. };
  2619. struct objc_method_description_t {
  2620. uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
  2621. uint32_t types; /* char * (32-bit pointer) */
  2622. };
  2623. inline void swapStruct(struct cfstring64_t &cfs) {
  2624. sys::swapByteOrder(cfs.isa);
  2625. sys::swapByteOrder(cfs.flags);
  2626. sys::swapByteOrder(cfs.characters);
  2627. sys::swapByteOrder(cfs.length);
  2628. }
  2629. inline void swapStruct(struct class64_t &c) {
  2630. sys::swapByteOrder(c.isa);
  2631. sys::swapByteOrder(c.superclass);
  2632. sys::swapByteOrder(c.cache);
  2633. sys::swapByteOrder(c.vtable);
  2634. sys::swapByteOrder(c.data);
  2635. }
  2636. inline void swapStruct(struct class32_t &c) {
  2637. sys::swapByteOrder(c.isa);
  2638. sys::swapByteOrder(c.superclass);
  2639. sys::swapByteOrder(c.cache);
  2640. sys::swapByteOrder(c.vtable);
  2641. sys::swapByteOrder(c.data);
  2642. }
  2643. inline void swapStruct(struct class_ro64_t &cro) {
  2644. sys::swapByteOrder(cro.flags);
  2645. sys::swapByteOrder(cro.instanceStart);
  2646. sys::swapByteOrder(cro.instanceSize);
  2647. sys::swapByteOrder(cro.reserved);
  2648. sys::swapByteOrder(cro.ivarLayout);
  2649. sys::swapByteOrder(cro.name);
  2650. sys::swapByteOrder(cro.baseMethods);
  2651. sys::swapByteOrder(cro.baseProtocols);
  2652. sys::swapByteOrder(cro.ivars);
  2653. sys::swapByteOrder(cro.weakIvarLayout);
  2654. sys::swapByteOrder(cro.baseProperties);
  2655. }
  2656. inline void swapStruct(struct class_ro32_t &cro) {
  2657. sys::swapByteOrder(cro.flags);
  2658. sys::swapByteOrder(cro.instanceStart);
  2659. sys::swapByteOrder(cro.instanceSize);
  2660. sys::swapByteOrder(cro.ivarLayout);
  2661. sys::swapByteOrder(cro.name);
  2662. sys::swapByteOrder(cro.baseMethods);
  2663. sys::swapByteOrder(cro.baseProtocols);
  2664. sys::swapByteOrder(cro.ivars);
  2665. sys::swapByteOrder(cro.weakIvarLayout);
  2666. sys::swapByteOrder(cro.baseProperties);
  2667. }
  2668. inline void swapStruct(struct method_list64_t &ml) {
  2669. sys::swapByteOrder(ml.entsize);
  2670. sys::swapByteOrder(ml.count);
  2671. }
  2672. inline void swapStruct(struct method_list32_t &ml) {
  2673. sys::swapByteOrder(ml.entsize);
  2674. sys::swapByteOrder(ml.count);
  2675. }
  2676. inline void swapStruct(struct method64_t &m) {
  2677. sys::swapByteOrder(m.name);
  2678. sys::swapByteOrder(m.types);
  2679. sys::swapByteOrder(m.imp);
  2680. }
  2681. inline void swapStruct(struct method32_t &m) {
  2682. sys::swapByteOrder(m.name);
  2683. sys::swapByteOrder(m.types);
  2684. sys::swapByteOrder(m.imp);
  2685. }
  2686. inline void swapStruct(struct protocol_list64_t &pl) {
  2687. sys::swapByteOrder(pl.count);
  2688. }
  2689. inline void swapStruct(struct protocol_list32_t &pl) {
  2690. sys::swapByteOrder(pl.count);
  2691. }
  2692. inline void swapStruct(struct protocol64_t &p) {
  2693. sys::swapByteOrder(p.isa);
  2694. sys::swapByteOrder(p.name);
  2695. sys::swapByteOrder(p.protocols);
  2696. sys::swapByteOrder(p.instanceMethods);
  2697. sys::swapByteOrder(p.classMethods);
  2698. sys::swapByteOrder(p.optionalInstanceMethods);
  2699. sys::swapByteOrder(p.optionalClassMethods);
  2700. sys::swapByteOrder(p.instanceProperties);
  2701. }
  2702. inline void swapStruct(struct protocol32_t &p) {
  2703. sys::swapByteOrder(p.isa);
  2704. sys::swapByteOrder(p.name);
  2705. sys::swapByteOrder(p.protocols);
  2706. sys::swapByteOrder(p.instanceMethods);
  2707. sys::swapByteOrder(p.classMethods);
  2708. sys::swapByteOrder(p.optionalInstanceMethods);
  2709. sys::swapByteOrder(p.optionalClassMethods);
  2710. sys::swapByteOrder(p.instanceProperties);
  2711. }
  2712. inline void swapStruct(struct ivar_list64_t &il) {
  2713. sys::swapByteOrder(il.entsize);
  2714. sys::swapByteOrder(il.count);
  2715. }
  2716. inline void swapStruct(struct ivar_list32_t &il) {
  2717. sys::swapByteOrder(il.entsize);
  2718. sys::swapByteOrder(il.count);
  2719. }
  2720. inline void swapStruct(struct ivar64_t &i) {
  2721. sys::swapByteOrder(i.offset);
  2722. sys::swapByteOrder(i.name);
  2723. sys::swapByteOrder(i.type);
  2724. sys::swapByteOrder(i.alignment);
  2725. sys::swapByteOrder(i.size);
  2726. }
  2727. inline void swapStruct(struct ivar32_t &i) {
  2728. sys::swapByteOrder(i.offset);
  2729. sys::swapByteOrder(i.name);
  2730. sys::swapByteOrder(i.type);
  2731. sys::swapByteOrder(i.alignment);
  2732. sys::swapByteOrder(i.size);
  2733. }
  2734. inline void swapStruct(struct objc_property_list64 &pl) {
  2735. sys::swapByteOrder(pl.entsize);
  2736. sys::swapByteOrder(pl.count);
  2737. }
  2738. inline void swapStruct(struct objc_property_list32 &pl) {
  2739. sys::swapByteOrder(pl.entsize);
  2740. sys::swapByteOrder(pl.count);
  2741. }
  2742. inline void swapStruct(struct objc_property64 &op) {
  2743. sys::swapByteOrder(op.name);
  2744. sys::swapByteOrder(op.attributes);
  2745. }
  2746. inline void swapStruct(struct objc_property32 &op) {
  2747. sys::swapByteOrder(op.name);
  2748. sys::swapByteOrder(op.attributes);
  2749. }
  2750. inline void swapStruct(struct category64_t &c) {
  2751. sys::swapByteOrder(c.name);
  2752. sys::swapByteOrder(c.cls);
  2753. sys::swapByteOrder(c.instanceMethods);
  2754. sys::swapByteOrder(c.classMethods);
  2755. sys::swapByteOrder(c.protocols);
  2756. sys::swapByteOrder(c.instanceProperties);
  2757. }
  2758. inline void swapStruct(struct category32_t &c) {
  2759. sys::swapByteOrder(c.name);
  2760. sys::swapByteOrder(c.cls);
  2761. sys::swapByteOrder(c.instanceMethods);
  2762. sys::swapByteOrder(c.classMethods);
  2763. sys::swapByteOrder(c.protocols);
  2764. sys::swapByteOrder(c.instanceProperties);
  2765. }
  2766. inline void swapStruct(struct objc_image_info64 &o) {
  2767. sys::swapByteOrder(o.version);
  2768. sys::swapByteOrder(o.flags);
  2769. }
  2770. inline void swapStruct(struct objc_image_info32 &o) {
  2771. sys::swapByteOrder(o.version);
  2772. sys::swapByteOrder(o.flags);
  2773. }
  2774. inline void swapStruct(struct imageInfo_t &o) {
  2775. sys::swapByteOrder(o.version);
  2776. sys::swapByteOrder(o.flags);
  2777. }
  2778. inline void swapStruct(struct message_ref64 &mr) {
  2779. sys::swapByteOrder(mr.imp);
  2780. sys::swapByteOrder(mr.sel);
  2781. }
  2782. inline void swapStruct(struct message_ref32 &mr) {
  2783. sys::swapByteOrder(mr.imp);
  2784. sys::swapByteOrder(mr.sel);
  2785. }
  2786. inline void swapStruct(struct objc_module_t &mod) {
  2787. sys::swapByteOrder(mod.version);
  2788. sys::swapByteOrder(mod.size);
  2789. sys::swapByteOrder(mod.name);
  2790. sys::swapByteOrder(mod.symtab);
  2791. }
  2792. inline void swapStruct(struct objc_symtab_t &symtab) {
  2793. sys::swapByteOrder(symtab.sel_ref_cnt);
  2794. sys::swapByteOrder(symtab.refs);
  2795. sys::swapByteOrder(symtab.cls_def_cnt);
  2796. sys::swapByteOrder(symtab.cat_def_cnt);
  2797. }
  2798. inline void swapStruct(struct objc_class_t &objc_class) {
  2799. sys::swapByteOrder(objc_class.isa);
  2800. sys::swapByteOrder(objc_class.super_class);
  2801. sys::swapByteOrder(objc_class.name);
  2802. sys::swapByteOrder(objc_class.version);
  2803. sys::swapByteOrder(objc_class.info);
  2804. sys::swapByteOrder(objc_class.instance_size);
  2805. sys::swapByteOrder(objc_class.ivars);
  2806. sys::swapByteOrder(objc_class.methodLists);
  2807. sys::swapByteOrder(objc_class.cache);
  2808. sys::swapByteOrder(objc_class.protocols);
  2809. }
  2810. inline void swapStruct(struct objc_category_t &objc_category) {
  2811. sys::swapByteOrder(objc_category.category_name);
  2812. sys::swapByteOrder(objc_category.class_name);
  2813. sys::swapByteOrder(objc_category.instance_methods);
  2814. sys::swapByteOrder(objc_category.class_methods);
  2815. sys::swapByteOrder(objc_category.protocols);
  2816. }
  2817. inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
  2818. sys::swapByteOrder(objc_ivar_list.ivar_count);
  2819. }
  2820. inline void swapStruct(struct objc_ivar_t &objc_ivar) {
  2821. sys::swapByteOrder(objc_ivar.ivar_name);
  2822. sys::swapByteOrder(objc_ivar.ivar_type);
  2823. sys::swapByteOrder(objc_ivar.ivar_offset);
  2824. }
  2825. inline void swapStruct(struct objc_method_list_t &method_list) {
  2826. sys::swapByteOrder(method_list.obsolete);
  2827. sys::swapByteOrder(method_list.method_count);
  2828. }
  2829. inline void swapStruct(struct objc_method_t &method) {
  2830. sys::swapByteOrder(method.method_name);
  2831. sys::swapByteOrder(method.method_types);
  2832. sys::swapByteOrder(method.method_imp);
  2833. }
  2834. inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
  2835. sys::swapByteOrder(protocol_list.next);
  2836. sys::swapByteOrder(protocol_list.count);
  2837. }
  2838. inline void swapStruct(struct objc_protocol_t &protocol) {
  2839. sys::swapByteOrder(protocol.isa);
  2840. sys::swapByteOrder(protocol.protocol_name);
  2841. sys::swapByteOrder(protocol.protocol_list);
  2842. sys::swapByteOrder(protocol.instance_methods);
  2843. sys::swapByteOrder(protocol.class_methods);
  2844. }
  2845. inline void swapStruct(struct objc_method_description_list_t &mdl) {
  2846. sys::swapByteOrder(mdl.count);
  2847. }
  2848. inline void swapStruct(struct objc_method_description_t &md) {
  2849. sys::swapByteOrder(md.name);
  2850. sys::swapByteOrder(md.types);
  2851. }
  2852. static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
  2853. struct DisassembleInfo *info);
  2854. // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
  2855. // to an Objective-C class and returns the class name. It is also passed the
  2856. // address of the pointer, so when the pointer is zero as it can be in an .o
  2857. // file, that is used to look for an external relocation entry with a symbol
  2858. // name.
  2859. static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
  2860. uint64_t ReferenceValue,
  2861. struct DisassembleInfo *info) {
  2862. const char *r;
  2863. uint32_t offset, left;
  2864. SectionRef S;
  2865. // The pointer_value can be 0 in an object file and have a relocation
  2866. // entry for the class symbol at the ReferenceValue (the address of the
  2867. // pointer).
  2868. if (pointer_value == 0) {
  2869. r = get_pointer_64(ReferenceValue, offset, left, S, info);
  2870. if (r == nullptr || left < sizeof(uint64_t))
  2871. return nullptr;
  2872. uint64_t n_value;
  2873. const char *symbol_name = get_symbol_64(offset, S, info, n_value);
  2874. if (symbol_name == nullptr)
  2875. return nullptr;
  2876. const char *class_name = strrchr(symbol_name, '$');
  2877. if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
  2878. return class_name + 2;
  2879. else
  2880. return nullptr;
  2881. }
  2882. // The case were the pointer_value is non-zero and points to a class defined
  2883. // in this Mach-O file.
  2884. r = get_pointer_64(pointer_value, offset, left, S, info);
  2885. if (r == nullptr || left < sizeof(struct class64_t))
  2886. return nullptr;
  2887. struct class64_t c;
  2888. memcpy(&c, r, sizeof(struct class64_t));
  2889. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  2890. swapStruct(c);
  2891. if (c.data == 0)
  2892. return nullptr;
  2893. r = get_pointer_64(c.data, offset, left, S, info);
  2894. if (r == nullptr || left < sizeof(struct class_ro64_t))
  2895. return nullptr;
  2896. struct class_ro64_t cro;
  2897. memcpy(&cro, r, sizeof(struct class_ro64_t));
  2898. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  2899. swapStruct(cro);
  2900. if (cro.name == 0)
  2901. return nullptr;
  2902. const char *name = get_pointer_64(cro.name, offset, left, S, info);
  2903. return name;
  2904. }
  2905. // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
  2906. // pointer to a cfstring and returns its name or nullptr.
  2907. static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
  2908. struct DisassembleInfo *info) {
  2909. const char *r, *name;
  2910. uint32_t offset, left;
  2911. SectionRef S;
  2912. struct cfstring64_t cfs;
  2913. uint64_t cfs_characters;
  2914. r = get_pointer_64(ReferenceValue, offset, left, S, info);
  2915. if (r == nullptr || left < sizeof(struct cfstring64_t))
  2916. return nullptr;
  2917. memcpy(&cfs, r, sizeof(struct cfstring64_t));
  2918. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  2919. swapStruct(cfs);
  2920. if (cfs.characters == 0) {
  2921. uint64_t n_value;
  2922. const char *symbol_name = get_symbol_64(
  2923. offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
  2924. if (symbol_name == nullptr)
  2925. return nullptr;
  2926. cfs_characters = n_value;
  2927. } else
  2928. cfs_characters = cfs.characters;
  2929. name = get_pointer_64(cfs_characters, offset, left, S, info);
  2930. return name;
  2931. }
  2932. // get_objc2_64bit_selref() is used for disassembly and is passed a the address
  2933. // of a pointer to an Objective-C selector reference when the pointer value is
  2934. // zero as in a .o file and is likely to have a external relocation entry with
  2935. // who's symbol's n_value is the real pointer to the selector name. If that is
  2936. // the case the real pointer to the selector name is returned else 0 is
  2937. // returned
  2938. static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
  2939. struct DisassembleInfo *info) {
  2940. uint32_t offset, left;
  2941. SectionRef S;
  2942. const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
  2943. if (r == nullptr || left < sizeof(uint64_t))
  2944. return 0;
  2945. uint64_t n_value;
  2946. const char *symbol_name = get_symbol_64(offset, S, info, n_value);
  2947. if (symbol_name == nullptr)
  2948. return 0;
  2949. return n_value;
  2950. }
  2951. static const SectionRef get_section(MachOObjectFile *O, const char *segname,
  2952. const char *sectname) {
  2953. for (const SectionRef &Section : O->sections()) {
  2954. StringRef SectName;
  2955. Section.getName(SectName);
  2956. DataRefImpl Ref = Section.getRawDataRefImpl();
  2957. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  2958. if (SegName == segname && SectName == sectname)
  2959. return Section;
  2960. }
  2961. return SectionRef();
  2962. }
  2963. static void
  2964. walk_pointer_list_64(const char *listname, const SectionRef S,
  2965. MachOObjectFile *O, struct DisassembleInfo *info,
  2966. void (*func)(uint64_t, struct DisassembleInfo *info)) {
  2967. if (S == SectionRef())
  2968. return;
  2969. StringRef SectName;
  2970. S.getName(SectName);
  2971. DataRefImpl Ref = S.getRawDataRefImpl();
  2972. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  2973. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  2974. StringRef BytesStr;
  2975. S.getContents(BytesStr);
  2976. const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
  2977. for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
  2978. uint32_t left = S.getSize() - i;
  2979. uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
  2980. uint64_t p = 0;
  2981. memcpy(&p, Contents + i, size);
  2982. if (i + sizeof(uint64_t) > S.getSize())
  2983. outs() << listname << " list pointer extends past end of (" << SegName
  2984. << "," << SectName << ") section\n";
  2985. outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
  2986. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  2987. sys::swapByteOrder(p);
  2988. uint64_t n_value = 0;
  2989. const char *name = get_symbol_64(i, S, info, n_value, p);
  2990. if (name == nullptr)
  2991. name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
  2992. if (n_value != 0) {
  2993. outs() << format("0x%" PRIx64, n_value);
  2994. if (p != 0)
  2995. outs() << " + " << format("0x%" PRIx64, p);
  2996. } else
  2997. outs() << format("0x%" PRIx64, p);
  2998. if (name != nullptr)
  2999. outs() << " " << name;
  3000. outs() << "\n";
  3001. p += n_value;
  3002. if (func)
  3003. func(p, info);
  3004. }
  3005. }
  3006. static void
  3007. walk_pointer_list_32(const char *listname, const SectionRef S,
  3008. MachOObjectFile *O, struct DisassembleInfo *info,
  3009. void (*func)(uint32_t, struct DisassembleInfo *info)) {
  3010. if (S == SectionRef())
  3011. return;
  3012. StringRef SectName;
  3013. S.getName(SectName);
  3014. DataRefImpl Ref = S.getRawDataRefImpl();
  3015. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  3016. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  3017. StringRef BytesStr;
  3018. S.getContents(BytesStr);
  3019. const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
  3020. for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
  3021. uint32_t left = S.getSize() - i;
  3022. uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
  3023. uint32_t p = 0;
  3024. memcpy(&p, Contents + i, size);
  3025. if (i + sizeof(uint32_t) > S.getSize())
  3026. outs() << listname << " list pointer extends past end of (" << SegName
  3027. << "," << SectName << ") section\n";
  3028. uint32_t Address = S.getAddress() + i;
  3029. outs() << format("%08" PRIx32, Address) << " ";
  3030. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  3031. sys::swapByteOrder(p);
  3032. outs() << format("0x%" PRIx32, p);
  3033. const char *name = get_symbol_32(i, S, info, p);
  3034. if (name != nullptr)
  3035. outs() << " " << name;
  3036. outs() << "\n";
  3037. if (func)
  3038. func(p, info);
  3039. }
  3040. }
  3041. static void print_layout_map(const char *layout_map, uint32_t left) {
  3042. outs() << " layout map: ";
  3043. do {
  3044. outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
  3045. left--;
  3046. layout_map++;
  3047. } while (*layout_map != '\0' && left != 0);
  3048. outs() << "\n";
  3049. }
  3050. static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
  3051. uint32_t offset, left;
  3052. SectionRef S;
  3053. const char *layout_map;
  3054. if (p == 0)
  3055. return;
  3056. layout_map = get_pointer_64(p, offset, left, S, info);
  3057. print_layout_map(layout_map, left);
  3058. }
  3059. static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
  3060. uint32_t offset, left;
  3061. SectionRef S;
  3062. const char *layout_map;
  3063. if (p == 0)
  3064. return;
  3065. layout_map = get_pointer_32(p, offset, left, S, info);
  3066. print_layout_map(layout_map, left);
  3067. }
  3068. static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
  3069. const char *indent) {
  3070. struct method_list64_t ml;
  3071. struct method64_t m;
  3072. const char *r;
  3073. uint32_t offset, xoffset, left, i;
  3074. SectionRef S, xS;
  3075. const char *name, *sym_name;
  3076. uint64_t n_value;
  3077. r = get_pointer_64(p, offset, left, S, info);
  3078. if (r == nullptr)
  3079. return;
  3080. memset(&ml, '\0', sizeof(struct method_list64_t));
  3081. if (left < sizeof(struct method_list64_t)) {
  3082. memcpy(&ml, r, left);
  3083. outs() << " (method_list_t entends past the end of the section)\n";
  3084. } else
  3085. memcpy(&ml, r, sizeof(struct method_list64_t));
  3086. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3087. swapStruct(ml);
  3088. outs() << indent << "\t\t entsize " << ml.entsize << "\n";
  3089. outs() << indent << "\t\t count " << ml.count << "\n";
  3090. p += sizeof(struct method_list64_t);
  3091. offset += sizeof(struct method_list64_t);
  3092. for (i = 0; i < ml.count; i++) {
  3093. r = get_pointer_64(p, offset, left, S, info);
  3094. if (r == nullptr)
  3095. return;
  3096. memset(&m, '\0', sizeof(struct method64_t));
  3097. if (left < sizeof(struct method64_t)) {
  3098. memcpy(&ml, r, left);
  3099. outs() << indent << " (method_t entends past the end of the section)\n";
  3100. } else
  3101. memcpy(&m, r, sizeof(struct method64_t));
  3102. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3103. swapStruct(m);
  3104. outs() << indent << "\t\t name ";
  3105. sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
  3106. info, n_value, m.name);
  3107. if (n_value != 0) {
  3108. if (info->verbose && sym_name != nullptr)
  3109. outs() << sym_name;
  3110. else
  3111. outs() << format("0x%" PRIx64, n_value);
  3112. if (m.name != 0)
  3113. outs() << " + " << format("0x%" PRIx64, m.name);
  3114. } else
  3115. outs() << format("0x%" PRIx64, m.name);
  3116. name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
  3117. if (name != nullptr)
  3118. outs() << format(" %.*s", left, name);
  3119. outs() << "\n";
  3120. outs() << indent << "\t\t types ";
  3121. sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
  3122. info, n_value, m.types);
  3123. if (n_value != 0) {
  3124. if (info->verbose && sym_name != nullptr)
  3125. outs() << sym_name;
  3126. else
  3127. outs() << format("0x%" PRIx64, n_value);
  3128. if (m.types != 0)
  3129. outs() << " + " << format("0x%" PRIx64, m.types);
  3130. } else
  3131. outs() << format("0x%" PRIx64, m.types);
  3132. name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
  3133. if (name != nullptr)
  3134. outs() << format(" %.*s", left, name);
  3135. outs() << "\n";
  3136. outs() << indent << "\t\t imp ";
  3137. name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
  3138. n_value, m.imp);
  3139. if (info->verbose && name == nullptr) {
  3140. if (n_value != 0) {
  3141. outs() << format("0x%" PRIx64, n_value) << " ";
  3142. if (m.imp != 0)
  3143. outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
  3144. } else
  3145. outs() << format("0x%" PRIx64, m.imp) << " ";
  3146. }
  3147. if (name != nullptr)
  3148. outs() << name;
  3149. outs() << "\n";
  3150. p += sizeof(struct method64_t);
  3151. offset += sizeof(struct method64_t);
  3152. }
  3153. }
  3154. static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
  3155. const char *indent) {
  3156. struct method_list32_t ml;
  3157. struct method32_t m;
  3158. const char *r, *name;
  3159. uint32_t offset, xoffset, left, i;
  3160. SectionRef S, xS;
  3161. r = get_pointer_32(p, offset, left, S, info);
  3162. if (r == nullptr)
  3163. return;
  3164. memset(&ml, '\0', sizeof(struct method_list32_t));
  3165. if (left < sizeof(struct method_list32_t)) {
  3166. memcpy(&ml, r, left);
  3167. outs() << " (method_list_t entends past the end of the section)\n";
  3168. } else
  3169. memcpy(&ml, r, sizeof(struct method_list32_t));
  3170. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3171. swapStruct(ml);
  3172. outs() << indent << "\t\t entsize " << ml.entsize << "\n";
  3173. outs() << indent << "\t\t count " << ml.count << "\n";
  3174. p += sizeof(struct method_list32_t);
  3175. offset += sizeof(struct method_list32_t);
  3176. for (i = 0; i < ml.count; i++) {
  3177. r = get_pointer_32(p, offset, left, S, info);
  3178. if (r == nullptr)
  3179. return;
  3180. memset(&m, '\0', sizeof(struct method32_t));
  3181. if (left < sizeof(struct method32_t)) {
  3182. memcpy(&ml, r, left);
  3183. outs() << indent << " (method_t entends past the end of the section)\n";
  3184. } else
  3185. memcpy(&m, r, sizeof(struct method32_t));
  3186. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3187. swapStruct(m);
  3188. outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
  3189. name = get_pointer_32(m.name, xoffset, left, xS, info);
  3190. if (name != nullptr)
  3191. outs() << format(" %.*s", left, name);
  3192. outs() << "\n";
  3193. outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
  3194. name = get_pointer_32(m.types, xoffset, left, xS, info);
  3195. if (name != nullptr)
  3196. outs() << format(" %.*s", left, name);
  3197. outs() << "\n";
  3198. outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
  3199. name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
  3200. m.imp);
  3201. if (name != nullptr)
  3202. outs() << " " << name;
  3203. outs() << "\n";
  3204. p += sizeof(struct method32_t);
  3205. offset += sizeof(struct method32_t);
  3206. }
  3207. }
  3208. static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
  3209. uint32_t offset, left, xleft;
  3210. SectionRef S;
  3211. struct objc_method_list_t method_list;
  3212. struct objc_method_t method;
  3213. const char *r, *methods, *name, *SymbolName;
  3214. int32_t i;
  3215. r = get_pointer_32(p, offset, left, S, info, true);
  3216. if (r == nullptr)
  3217. return true;
  3218. outs() << "\n";
  3219. if (left > sizeof(struct objc_method_list_t)) {
  3220. memcpy(&method_list, r, sizeof(struct objc_method_list_t));
  3221. } else {
  3222. outs() << "\t\t objc_method_list extends past end of the section\n";
  3223. memset(&method_list, '\0', sizeof(struct objc_method_list_t));
  3224. memcpy(&method_list, r, left);
  3225. }
  3226. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3227. swapStruct(method_list);
  3228. outs() << "\t\t obsolete "
  3229. << format("0x%08" PRIx32, method_list.obsolete) << "\n";
  3230. outs() << "\t\t method_count " << method_list.method_count << "\n";
  3231. methods = r + sizeof(struct objc_method_list_t);
  3232. for (i = 0; i < method_list.method_count; i++) {
  3233. if ((i + 1) * sizeof(struct objc_method_t) > left) {
  3234. outs() << "\t\t remaining method's extend past the of the section\n";
  3235. break;
  3236. }
  3237. memcpy(&method, methods + i * sizeof(struct objc_method_t),
  3238. sizeof(struct objc_method_t));
  3239. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3240. swapStruct(method);
  3241. outs() << "\t\t method_name "
  3242. << format("0x%08" PRIx32, method.method_name);
  3243. if (info->verbose) {
  3244. name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
  3245. if (name != nullptr)
  3246. outs() << format(" %.*s", xleft, name);
  3247. else
  3248. outs() << " (not in an __OBJC section)";
  3249. }
  3250. outs() << "\n";
  3251. outs() << "\t\t method_types "
  3252. << format("0x%08" PRIx32, method.method_types);
  3253. if (info->verbose) {
  3254. name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
  3255. if (name != nullptr)
  3256. outs() << format(" %.*s", xleft, name);
  3257. else
  3258. outs() << " (not in an __OBJC section)";
  3259. }
  3260. outs() << "\n";
  3261. outs() << "\t\t method_imp "
  3262. << format("0x%08" PRIx32, method.method_imp) << " ";
  3263. if (info->verbose) {
  3264. SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
  3265. if (SymbolName != nullptr)
  3266. outs() << SymbolName;
  3267. }
  3268. outs() << "\n";
  3269. }
  3270. return false;
  3271. }
  3272. static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
  3273. struct protocol_list64_t pl;
  3274. uint64_t q, n_value;
  3275. struct protocol64_t pc;
  3276. const char *r;
  3277. uint32_t offset, xoffset, left, i;
  3278. SectionRef S, xS;
  3279. const char *name, *sym_name;
  3280. r = get_pointer_64(p, offset, left, S, info);
  3281. if (r == nullptr)
  3282. return;
  3283. memset(&pl, '\0', sizeof(struct protocol_list64_t));
  3284. if (left < sizeof(struct protocol_list64_t)) {
  3285. memcpy(&pl, r, left);
  3286. outs() << " (protocol_list_t entends past the end of the section)\n";
  3287. } else
  3288. memcpy(&pl, r, sizeof(struct protocol_list64_t));
  3289. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3290. swapStruct(pl);
  3291. outs() << " count " << pl.count << "\n";
  3292. p += sizeof(struct protocol_list64_t);
  3293. offset += sizeof(struct protocol_list64_t);
  3294. for (i = 0; i < pl.count; i++) {
  3295. r = get_pointer_64(p, offset, left, S, info);
  3296. if (r == nullptr)
  3297. return;
  3298. q = 0;
  3299. if (left < sizeof(uint64_t)) {
  3300. memcpy(&q, r, left);
  3301. outs() << " (protocol_t * entends past the end of the section)\n";
  3302. } else
  3303. memcpy(&q, r, sizeof(uint64_t));
  3304. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3305. sys::swapByteOrder(q);
  3306. outs() << "\t\t list[" << i << "] ";
  3307. sym_name = get_symbol_64(offset, S, info, n_value, q);
  3308. if (n_value != 0) {
  3309. if (info->verbose && sym_name != nullptr)
  3310. outs() << sym_name;
  3311. else
  3312. outs() << format("0x%" PRIx64, n_value);
  3313. if (q != 0)
  3314. outs() << " + " << format("0x%" PRIx64, q);
  3315. } else
  3316. outs() << format("0x%" PRIx64, q);
  3317. outs() << " (struct protocol_t *)\n";
  3318. r = get_pointer_64(q + n_value, offset, left, S, info);
  3319. if (r == nullptr)
  3320. return;
  3321. memset(&pc, '\0', sizeof(struct protocol64_t));
  3322. if (left < sizeof(struct protocol64_t)) {
  3323. memcpy(&pc, r, left);
  3324. outs() << " (protocol_t entends past the end of the section)\n";
  3325. } else
  3326. memcpy(&pc, r, sizeof(struct protocol64_t));
  3327. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3328. swapStruct(pc);
  3329. outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
  3330. outs() << "\t\t\t name ";
  3331. sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
  3332. info, n_value, pc.name);
  3333. if (n_value != 0) {
  3334. if (info->verbose && sym_name != nullptr)
  3335. outs() << sym_name;
  3336. else
  3337. outs() << format("0x%" PRIx64, n_value);
  3338. if (pc.name != 0)
  3339. outs() << " + " << format("0x%" PRIx64, pc.name);
  3340. } else
  3341. outs() << format("0x%" PRIx64, pc.name);
  3342. name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
  3343. if (name != nullptr)
  3344. outs() << format(" %.*s", left, name);
  3345. outs() << "\n";
  3346. outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
  3347. outs() << "\t\t instanceMethods ";
  3348. sym_name =
  3349. get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
  3350. S, info, n_value, pc.instanceMethods);
  3351. if (n_value != 0) {
  3352. if (info->verbose && sym_name != nullptr)
  3353. outs() << sym_name;
  3354. else
  3355. outs() << format("0x%" PRIx64, n_value);
  3356. if (pc.instanceMethods != 0)
  3357. outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
  3358. } else
  3359. outs() << format("0x%" PRIx64, pc.instanceMethods);
  3360. outs() << " (struct method_list_t *)\n";
  3361. if (pc.instanceMethods + n_value != 0)
  3362. print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
  3363. outs() << "\t\t classMethods ";
  3364. sym_name =
  3365. get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
  3366. info, n_value, pc.classMethods);
  3367. if (n_value != 0) {
  3368. if (info->verbose && sym_name != nullptr)
  3369. outs() << sym_name;
  3370. else
  3371. outs() << format("0x%" PRIx64, n_value);
  3372. if (pc.classMethods != 0)
  3373. outs() << " + " << format("0x%" PRIx64, pc.classMethods);
  3374. } else
  3375. outs() << format("0x%" PRIx64, pc.classMethods);
  3376. outs() << " (struct method_list_t *)\n";
  3377. if (pc.classMethods + n_value != 0)
  3378. print_method_list64_t(pc.classMethods + n_value, info, "\t");
  3379. outs() << "\t optionalInstanceMethods "
  3380. << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
  3381. outs() << "\t optionalClassMethods "
  3382. << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
  3383. outs() << "\t instanceProperties "
  3384. << format("0x%" PRIx64, pc.instanceProperties) << "\n";
  3385. p += sizeof(uint64_t);
  3386. offset += sizeof(uint64_t);
  3387. }
  3388. }
  3389. static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
  3390. struct protocol_list32_t pl;
  3391. uint32_t q;
  3392. struct protocol32_t pc;
  3393. const char *r;
  3394. uint32_t offset, xoffset, left, i;
  3395. SectionRef S, xS;
  3396. const char *name;
  3397. r = get_pointer_32(p, offset, left, S, info);
  3398. if (r == nullptr)
  3399. return;
  3400. memset(&pl, '\0', sizeof(struct protocol_list32_t));
  3401. if (left < sizeof(struct protocol_list32_t)) {
  3402. memcpy(&pl, r, left);
  3403. outs() << " (protocol_list_t entends past the end of the section)\n";
  3404. } else
  3405. memcpy(&pl, r, sizeof(struct protocol_list32_t));
  3406. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3407. swapStruct(pl);
  3408. outs() << " count " << pl.count << "\n";
  3409. p += sizeof(struct protocol_list32_t);
  3410. offset += sizeof(struct protocol_list32_t);
  3411. for (i = 0; i < pl.count; i++) {
  3412. r = get_pointer_32(p, offset, left, S, info);
  3413. if (r == nullptr)
  3414. return;
  3415. q = 0;
  3416. if (left < sizeof(uint32_t)) {
  3417. memcpy(&q, r, left);
  3418. outs() << " (protocol_t * entends past the end of the section)\n";
  3419. } else
  3420. memcpy(&q, r, sizeof(uint32_t));
  3421. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3422. sys::swapByteOrder(q);
  3423. outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
  3424. << " (struct protocol_t *)\n";
  3425. r = get_pointer_32(q, offset, left, S, info);
  3426. if (r == nullptr)
  3427. return;
  3428. memset(&pc, '\0', sizeof(struct protocol32_t));
  3429. if (left < sizeof(struct protocol32_t)) {
  3430. memcpy(&pc, r, left);
  3431. outs() << " (protocol_t entends past the end of the section)\n";
  3432. } else
  3433. memcpy(&pc, r, sizeof(struct protocol32_t));
  3434. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3435. swapStruct(pc);
  3436. outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
  3437. outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
  3438. name = get_pointer_32(pc.name, xoffset, left, xS, info);
  3439. if (name != nullptr)
  3440. outs() << format(" %.*s", left, name);
  3441. outs() << "\n";
  3442. outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
  3443. outs() << "\t\t instanceMethods "
  3444. << format("0x%" PRIx32, pc.instanceMethods)
  3445. << " (struct method_list_t *)\n";
  3446. if (pc.instanceMethods != 0)
  3447. print_method_list32_t(pc.instanceMethods, info, "\t");
  3448. outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
  3449. << " (struct method_list_t *)\n";
  3450. if (pc.classMethods != 0)
  3451. print_method_list32_t(pc.classMethods, info, "\t");
  3452. outs() << "\t optionalInstanceMethods "
  3453. << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
  3454. outs() << "\t optionalClassMethods "
  3455. << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
  3456. outs() << "\t instanceProperties "
  3457. << format("0x%" PRIx32, pc.instanceProperties) << "\n";
  3458. p += sizeof(uint32_t);
  3459. offset += sizeof(uint32_t);
  3460. }
  3461. }
  3462. static void print_indent(uint32_t indent) {
  3463. for (uint32_t i = 0; i < indent;) {
  3464. if (indent - i >= 8) {
  3465. outs() << "\t";
  3466. i += 8;
  3467. } else {
  3468. for (uint32_t j = i; j < indent; j++)
  3469. outs() << " ";
  3470. return;
  3471. }
  3472. }
  3473. }
  3474. static bool print_method_description_list(uint32_t p, uint32_t indent,
  3475. struct DisassembleInfo *info) {
  3476. uint32_t offset, left, xleft;
  3477. SectionRef S;
  3478. struct objc_method_description_list_t mdl;
  3479. struct objc_method_description_t md;
  3480. const char *r, *list, *name;
  3481. int32_t i;
  3482. r = get_pointer_32(p, offset, left, S, info, true);
  3483. if (r == nullptr)
  3484. return true;
  3485. outs() << "\n";
  3486. if (left > sizeof(struct objc_method_description_list_t)) {
  3487. memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
  3488. } else {
  3489. print_indent(indent);
  3490. outs() << " objc_method_description_list extends past end of the section\n";
  3491. memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
  3492. memcpy(&mdl, r, left);
  3493. }
  3494. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3495. swapStruct(mdl);
  3496. print_indent(indent);
  3497. outs() << " count " << mdl.count << "\n";
  3498. list = r + sizeof(struct objc_method_description_list_t);
  3499. for (i = 0; i < mdl.count; i++) {
  3500. if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
  3501. print_indent(indent);
  3502. outs() << " remaining list entries extend past the of the section\n";
  3503. break;
  3504. }
  3505. print_indent(indent);
  3506. outs() << " list[" << i << "]\n";
  3507. memcpy(&md, list + i * sizeof(struct objc_method_description_t),
  3508. sizeof(struct objc_method_description_t));
  3509. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3510. swapStruct(md);
  3511. print_indent(indent);
  3512. outs() << " name " << format("0x%08" PRIx32, md.name);
  3513. if (info->verbose) {
  3514. name = get_pointer_32(md.name, offset, xleft, S, info, true);
  3515. if (name != nullptr)
  3516. outs() << format(" %.*s", xleft, name);
  3517. else
  3518. outs() << " (not in an __OBJC section)";
  3519. }
  3520. outs() << "\n";
  3521. print_indent(indent);
  3522. outs() << " types " << format("0x%08" PRIx32, md.types);
  3523. if (info->verbose) {
  3524. name = get_pointer_32(md.types, offset, xleft, S, info, true);
  3525. if (name != nullptr)
  3526. outs() << format(" %.*s", xleft, name);
  3527. else
  3528. outs() << " (not in an __OBJC section)";
  3529. }
  3530. outs() << "\n";
  3531. }
  3532. return false;
  3533. }
  3534. static bool print_protocol_list(uint32_t p, uint32_t indent,
  3535. struct DisassembleInfo *info);
  3536. static bool print_protocol(uint32_t p, uint32_t indent,
  3537. struct DisassembleInfo *info) {
  3538. uint32_t offset, left;
  3539. SectionRef S;
  3540. struct objc_protocol_t protocol;
  3541. const char *r, *name;
  3542. r = get_pointer_32(p, offset, left, S, info, true);
  3543. if (r == nullptr)
  3544. return true;
  3545. outs() << "\n";
  3546. if (left >= sizeof(struct objc_protocol_t)) {
  3547. memcpy(&protocol, r, sizeof(struct objc_protocol_t));
  3548. } else {
  3549. print_indent(indent);
  3550. outs() << " Protocol extends past end of the section\n";
  3551. memset(&protocol, '\0', sizeof(struct objc_protocol_t));
  3552. memcpy(&protocol, r, left);
  3553. }
  3554. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3555. swapStruct(protocol);
  3556. print_indent(indent);
  3557. outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
  3558. << "\n";
  3559. print_indent(indent);
  3560. outs() << " protocol_name "
  3561. << format("0x%08" PRIx32, protocol.protocol_name);
  3562. if (info->verbose) {
  3563. name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
  3564. if (name != nullptr)
  3565. outs() << format(" %.*s", left, name);
  3566. else
  3567. outs() << " (not in an __OBJC section)";
  3568. }
  3569. outs() << "\n";
  3570. print_indent(indent);
  3571. outs() << " protocol_list "
  3572. << format("0x%08" PRIx32, protocol.protocol_list);
  3573. if (print_protocol_list(protocol.protocol_list, indent + 4, info))
  3574. outs() << " (not in an __OBJC section)\n";
  3575. print_indent(indent);
  3576. outs() << " instance_methods "
  3577. << format("0x%08" PRIx32, protocol.instance_methods);
  3578. if (print_method_description_list(protocol.instance_methods, indent, info))
  3579. outs() << " (not in an __OBJC section)\n";
  3580. print_indent(indent);
  3581. outs() << " class_methods "
  3582. << format("0x%08" PRIx32, protocol.class_methods);
  3583. if (print_method_description_list(protocol.class_methods, indent, info))
  3584. outs() << " (not in an __OBJC section)\n";
  3585. return false;
  3586. }
  3587. static bool print_protocol_list(uint32_t p, uint32_t indent,
  3588. struct DisassembleInfo *info) {
  3589. uint32_t offset, left, l;
  3590. SectionRef S;
  3591. struct objc_protocol_list_t protocol_list;
  3592. const char *r, *list;
  3593. int32_t i;
  3594. r = get_pointer_32(p, offset, left, S, info, true);
  3595. if (r == nullptr)
  3596. return true;
  3597. outs() << "\n";
  3598. if (left > sizeof(struct objc_protocol_list_t)) {
  3599. memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
  3600. } else {
  3601. outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
  3602. memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
  3603. memcpy(&protocol_list, r, left);
  3604. }
  3605. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3606. swapStruct(protocol_list);
  3607. print_indent(indent);
  3608. outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
  3609. << "\n";
  3610. print_indent(indent);
  3611. outs() << " count " << protocol_list.count << "\n";
  3612. list = r + sizeof(struct objc_protocol_list_t);
  3613. for (i = 0; i < protocol_list.count; i++) {
  3614. if ((i + 1) * sizeof(uint32_t) > left) {
  3615. outs() << "\t\t remaining list entries extend past the of the section\n";
  3616. break;
  3617. }
  3618. memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
  3619. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3620. sys::swapByteOrder(l);
  3621. print_indent(indent);
  3622. outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
  3623. if (print_protocol(l, indent, info))
  3624. outs() << "(not in an __OBJC section)\n";
  3625. }
  3626. return false;
  3627. }
  3628. static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
  3629. struct ivar_list64_t il;
  3630. struct ivar64_t i;
  3631. const char *r;
  3632. uint32_t offset, xoffset, left, j;
  3633. SectionRef S, xS;
  3634. const char *name, *sym_name, *ivar_offset_p;
  3635. uint64_t ivar_offset, n_value;
  3636. r = get_pointer_64(p, offset, left, S, info);
  3637. if (r == nullptr)
  3638. return;
  3639. memset(&il, '\0', sizeof(struct ivar_list64_t));
  3640. if (left < sizeof(struct ivar_list64_t)) {
  3641. memcpy(&il, r, left);
  3642. outs() << " (ivar_list_t entends past the end of the section)\n";
  3643. } else
  3644. memcpy(&il, r, sizeof(struct ivar_list64_t));
  3645. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3646. swapStruct(il);
  3647. outs() << " entsize " << il.entsize << "\n";
  3648. outs() << " count " << il.count << "\n";
  3649. p += sizeof(struct ivar_list64_t);
  3650. offset += sizeof(struct ivar_list64_t);
  3651. for (j = 0; j < il.count; j++) {
  3652. r = get_pointer_64(p, offset, left, S, info);
  3653. if (r == nullptr)
  3654. return;
  3655. memset(&i, '\0', sizeof(struct ivar64_t));
  3656. if (left < sizeof(struct ivar64_t)) {
  3657. memcpy(&i, r, left);
  3658. outs() << " (ivar_t entends past the end of the section)\n";
  3659. } else
  3660. memcpy(&i, r, sizeof(struct ivar64_t));
  3661. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3662. swapStruct(i);
  3663. outs() << "\t\t\t offset ";
  3664. sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
  3665. info, n_value, i.offset);
  3666. if (n_value != 0) {
  3667. if (info->verbose && sym_name != nullptr)
  3668. outs() << sym_name;
  3669. else
  3670. outs() << format("0x%" PRIx64, n_value);
  3671. if (i.offset != 0)
  3672. outs() << " + " << format("0x%" PRIx64, i.offset);
  3673. } else
  3674. outs() << format("0x%" PRIx64, i.offset);
  3675. ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
  3676. if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
  3677. memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
  3678. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3679. sys::swapByteOrder(ivar_offset);
  3680. outs() << " " << ivar_offset << "\n";
  3681. } else
  3682. outs() << "\n";
  3683. outs() << "\t\t\t name ";
  3684. sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
  3685. n_value, i.name);
  3686. if (n_value != 0) {
  3687. if (info->verbose && sym_name != nullptr)
  3688. outs() << sym_name;
  3689. else
  3690. outs() << format("0x%" PRIx64, n_value);
  3691. if (i.name != 0)
  3692. outs() << " + " << format("0x%" PRIx64, i.name);
  3693. } else
  3694. outs() << format("0x%" PRIx64, i.name);
  3695. name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
  3696. if (name != nullptr)
  3697. outs() << format(" %.*s", left, name);
  3698. outs() << "\n";
  3699. outs() << "\t\t\t type ";
  3700. sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
  3701. n_value, i.name);
  3702. name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
  3703. if (n_value != 0) {
  3704. if (info->verbose && sym_name != nullptr)
  3705. outs() << sym_name;
  3706. else
  3707. outs() << format("0x%" PRIx64, n_value);
  3708. if (i.type != 0)
  3709. outs() << " + " << format("0x%" PRIx64, i.type);
  3710. } else
  3711. outs() << format("0x%" PRIx64, i.type);
  3712. if (name != nullptr)
  3713. outs() << format(" %.*s", left, name);
  3714. outs() << "\n";
  3715. outs() << "\t\t\talignment " << i.alignment << "\n";
  3716. outs() << "\t\t\t size " << i.size << "\n";
  3717. p += sizeof(struct ivar64_t);
  3718. offset += sizeof(struct ivar64_t);
  3719. }
  3720. }
  3721. static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
  3722. struct ivar_list32_t il;
  3723. struct ivar32_t i;
  3724. const char *r;
  3725. uint32_t offset, xoffset, left, j;
  3726. SectionRef S, xS;
  3727. const char *name, *ivar_offset_p;
  3728. uint32_t ivar_offset;
  3729. r = get_pointer_32(p, offset, left, S, info);
  3730. if (r == nullptr)
  3731. return;
  3732. memset(&il, '\0', sizeof(struct ivar_list32_t));
  3733. if (left < sizeof(struct ivar_list32_t)) {
  3734. memcpy(&il, r, left);
  3735. outs() << " (ivar_list_t entends past the end of the section)\n";
  3736. } else
  3737. memcpy(&il, r, sizeof(struct ivar_list32_t));
  3738. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3739. swapStruct(il);
  3740. outs() << " entsize " << il.entsize << "\n";
  3741. outs() << " count " << il.count << "\n";
  3742. p += sizeof(struct ivar_list32_t);
  3743. offset += sizeof(struct ivar_list32_t);
  3744. for (j = 0; j < il.count; j++) {
  3745. r = get_pointer_32(p, offset, left, S, info);
  3746. if (r == nullptr)
  3747. return;
  3748. memset(&i, '\0', sizeof(struct ivar32_t));
  3749. if (left < sizeof(struct ivar32_t)) {
  3750. memcpy(&i, r, left);
  3751. outs() << " (ivar_t entends past the end of the section)\n";
  3752. } else
  3753. memcpy(&i, r, sizeof(struct ivar32_t));
  3754. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3755. swapStruct(i);
  3756. outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
  3757. ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
  3758. if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
  3759. memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
  3760. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3761. sys::swapByteOrder(ivar_offset);
  3762. outs() << " " << ivar_offset << "\n";
  3763. } else
  3764. outs() << "\n";
  3765. outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
  3766. name = get_pointer_32(i.name, xoffset, left, xS, info);
  3767. if (name != nullptr)
  3768. outs() << format(" %.*s", left, name);
  3769. outs() << "\n";
  3770. outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
  3771. name = get_pointer_32(i.type, xoffset, left, xS, info);
  3772. if (name != nullptr)
  3773. outs() << format(" %.*s", left, name);
  3774. outs() << "\n";
  3775. outs() << "\t\t\talignment " << i.alignment << "\n";
  3776. outs() << "\t\t\t size " << i.size << "\n";
  3777. p += sizeof(struct ivar32_t);
  3778. offset += sizeof(struct ivar32_t);
  3779. }
  3780. }
  3781. static void print_objc_property_list64(uint64_t p,
  3782. struct DisassembleInfo *info) {
  3783. struct objc_property_list64 opl;
  3784. struct objc_property64 op;
  3785. const char *r;
  3786. uint32_t offset, xoffset, left, j;
  3787. SectionRef S, xS;
  3788. const char *name, *sym_name;
  3789. uint64_t n_value;
  3790. r = get_pointer_64(p, offset, left, S, info);
  3791. if (r == nullptr)
  3792. return;
  3793. memset(&opl, '\0', sizeof(struct objc_property_list64));
  3794. if (left < sizeof(struct objc_property_list64)) {
  3795. memcpy(&opl, r, left);
  3796. outs() << " (objc_property_list entends past the end of the section)\n";
  3797. } else
  3798. memcpy(&opl, r, sizeof(struct objc_property_list64));
  3799. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3800. swapStruct(opl);
  3801. outs() << " entsize " << opl.entsize << "\n";
  3802. outs() << " count " << opl.count << "\n";
  3803. p += sizeof(struct objc_property_list64);
  3804. offset += sizeof(struct objc_property_list64);
  3805. for (j = 0; j < opl.count; j++) {
  3806. r = get_pointer_64(p, offset, left, S, info);
  3807. if (r == nullptr)
  3808. return;
  3809. memset(&op, '\0', sizeof(struct objc_property64));
  3810. if (left < sizeof(struct objc_property64)) {
  3811. memcpy(&op, r, left);
  3812. outs() << " (objc_property entends past the end of the section)\n";
  3813. } else
  3814. memcpy(&op, r, sizeof(struct objc_property64));
  3815. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3816. swapStruct(op);
  3817. outs() << "\t\t\t name ";
  3818. sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
  3819. info, n_value, op.name);
  3820. if (n_value != 0) {
  3821. if (info->verbose && sym_name != nullptr)
  3822. outs() << sym_name;
  3823. else
  3824. outs() << format("0x%" PRIx64, n_value);
  3825. if (op.name != 0)
  3826. outs() << " + " << format("0x%" PRIx64, op.name);
  3827. } else
  3828. outs() << format("0x%" PRIx64, op.name);
  3829. name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
  3830. if (name != nullptr)
  3831. outs() << format(" %.*s", left, name);
  3832. outs() << "\n";
  3833. outs() << "\t\t\tattributes ";
  3834. sym_name =
  3835. get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
  3836. info, n_value, op.attributes);
  3837. if (n_value != 0) {
  3838. if (info->verbose && sym_name != nullptr)
  3839. outs() << sym_name;
  3840. else
  3841. outs() << format("0x%" PRIx64, n_value);
  3842. if (op.attributes != 0)
  3843. outs() << " + " << format("0x%" PRIx64, op.attributes);
  3844. } else
  3845. outs() << format("0x%" PRIx64, op.attributes);
  3846. name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
  3847. if (name != nullptr)
  3848. outs() << format(" %.*s", left, name);
  3849. outs() << "\n";
  3850. p += sizeof(struct objc_property64);
  3851. offset += sizeof(struct objc_property64);
  3852. }
  3853. }
  3854. static void print_objc_property_list32(uint32_t p,
  3855. struct DisassembleInfo *info) {
  3856. struct objc_property_list32 opl;
  3857. struct objc_property32 op;
  3858. const char *r;
  3859. uint32_t offset, xoffset, left, j;
  3860. SectionRef S, xS;
  3861. const char *name;
  3862. r = get_pointer_32(p, offset, left, S, info);
  3863. if (r == nullptr)
  3864. return;
  3865. memset(&opl, '\0', sizeof(struct objc_property_list32));
  3866. if (left < sizeof(struct objc_property_list32)) {
  3867. memcpy(&opl, r, left);
  3868. outs() << " (objc_property_list entends past the end of the section)\n";
  3869. } else
  3870. memcpy(&opl, r, sizeof(struct objc_property_list32));
  3871. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3872. swapStruct(opl);
  3873. outs() << " entsize " << opl.entsize << "\n";
  3874. outs() << " count " << opl.count << "\n";
  3875. p += sizeof(struct objc_property_list32);
  3876. offset += sizeof(struct objc_property_list32);
  3877. for (j = 0; j < opl.count; j++) {
  3878. r = get_pointer_32(p, offset, left, S, info);
  3879. if (r == nullptr)
  3880. return;
  3881. memset(&op, '\0', sizeof(struct objc_property32));
  3882. if (left < sizeof(struct objc_property32)) {
  3883. memcpy(&op, r, left);
  3884. outs() << " (objc_property entends past the end of the section)\n";
  3885. } else
  3886. memcpy(&op, r, sizeof(struct objc_property32));
  3887. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3888. swapStruct(op);
  3889. outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
  3890. name = get_pointer_32(op.name, xoffset, left, xS, info);
  3891. if (name != nullptr)
  3892. outs() << format(" %.*s", left, name);
  3893. outs() << "\n";
  3894. outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
  3895. name = get_pointer_32(op.attributes, xoffset, left, xS, info);
  3896. if (name != nullptr)
  3897. outs() << format(" %.*s", left, name);
  3898. outs() << "\n";
  3899. p += sizeof(struct objc_property32);
  3900. offset += sizeof(struct objc_property32);
  3901. }
  3902. }
  3903. static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
  3904. bool &is_meta_class) {
  3905. struct class_ro64_t cro;
  3906. const char *r;
  3907. uint32_t offset, xoffset, left;
  3908. SectionRef S, xS;
  3909. const char *name, *sym_name;
  3910. uint64_t n_value;
  3911. r = get_pointer_64(p, offset, left, S, info);
  3912. if (r == nullptr || left < sizeof(struct class_ro64_t))
  3913. return;
  3914. memset(&cro, '\0', sizeof(struct class_ro64_t));
  3915. if (left < sizeof(struct class_ro64_t)) {
  3916. memcpy(&cro, r, left);
  3917. outs() << " (class_ro_t entends past the end of the section)\n";
  3918. } else
  3919. memcpy(&cro, r, sizeof(struct class_ro64_t));
  3920. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3921. swapStruct(cro);
  3922. outs() << " flags " << format("0x%" PRIx32, cro.flags);
  3923. if (cro.flags & RO_META)
  3924. outs() << " RO_META";
  3925. if (cro.flags & RO_ROOT)
  3926. outs() << " RO_ROOT";
  3927. if (cro.flags & RO_HAS_CXX_STRUCTORS)
  3928. outs() << " RO_HAS_CXX_STRUCTORS";
  3929. outs() << "\n";
  3930. outs() << " instanceStart " << cro.instanceStart << "\n";
  3931. outs() << " instanceSize " << cro.instanceSize << "\n";
  3932. outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
  3933. << "\n";
  3934. outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
  3935. << "\n";
  3936. print_layout_map64(cro.ivarLayout, info);
  3937. outs() << " name ";
  3938. sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
  3939. info, n_value, cro.name);
  3940. if (n_value != 0) {
  3941. if (info->verbose && sym_name != nullptr)
  3942. outs() << sym_name;
  3943. else
  3944. outs() << format("0x%" PRIx64, n_value);
  3945. if (cro.name != 0)
  3946. outs() << " + " << format("0x%" PRIx64, cro.name);
  3947. } else
  3948. outs() << format("0x%" PRIx64, cro.name);
  3949. name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
  3950. if (name != nullptr)
  3951. outs() << format(" %.*s", left, name);
  3952. outs() << "\n";
  3953. outs() << " baseMethods ";
  3954. sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
  3955. S, info, n_value, cro.baseMethods);
  3956. if (n_value != 0) {
  3957. if (info->verbose && sym_name != nullptr)
  3958. outs() << sym_name;
  3959. else
  3960. outs() << format("0x%" PRIx64, n_value);
  3961. if (cro.baseMethods != 0)
  3962. outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
  3963. } else
  3964. outs() << format("0x%" PRIx64, cro.baseMethods);
  3965. outs() << " (struct method_list_t *)\n";
  3966. if (cro.baseMethods + n_value != 0)
  3967. print_method_list64_t(cro.baseMethods + n_value, info, "");
  3968. outs() << " baseProtocols ";
  3969. sym_name =
  3970. get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
  3971. info, n_value, cro.baseProtocols);
  3972. if (n_value != 0) {
  3973. if (info->verbose && sym_name != nullptr)
  3974. outs() << sym_name;
  3975. else
  3976. outs() << format("0x%" PRIx64, n_value);
  3977. if (cro.baseProtocols != 0)
  3978. outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
  3979. } else
  3980. outs() << format("0x%" PRIx64, cro.baseProtocols);
  3981. outs() << "\n";
  3982. if (cro.baseProtocols + n_value != 0)
  3983. print_protocol_list64_t(cro.baseProtocols + n_value, info);
  3984. outs() << " ivars ";
  3985. sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
  3986. info, n_value, cro.ivars);
  3987. if (n_value != 0) {
  3988. if (info->verbose && sym_name != nullptr)
  3989. outs() << sym_name;
  3990. else
  3991. outs() << format("0x%" PRIx64, n_value);
  3992. if (cro.ivars != 0)
  3993. outs() << " + " << format("0x%" PRIx64, cro.ivars);
  3994. } else
  3995. outs() << format("0x%" PRIx64, cro.ivars);
  3996. outs() << "\n";
  3997. if (cro.ivars + n_value != 0)
  3998. print_ivar_list64_t(cro.ivars + n_value, info);
  3999. outs() << " weakIvarLayout ";
  4000. sym_name =
  4001. get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
  4002. info, n_value, cro.weakIvarLayout);
  4003. if (n_value != 0) {
  4004. if (info->verbose && sym_name != nullptr)
  4005. outs() << sym_name;
  4006. else
  4007. outs() << format("0x%" PRIx64, n_value);
  4008. if (cro.weakIvarLayout != 0)
  4009. outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
  4010. } else
  4011. outs() << format("0x%" PRIx64, cro.weakIvarLayout);
  4012. outs() << "\n";
  4013. print_layout_map64(cro.weakIvarLayout + n_value, info);
  4014. outs() << " baseProperties ";
  4015. sym_name =
  4016. get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
  4017. info, n_value, cro.baseProperties);
  4018. if (n_value != 0) {
  4019. if (info->verbose && sym_name != nullptr)
  4020. outs() << sym_name;
  4021. else
  4022. outs() << format("0x%" PRIx64, n_value);
  4023. if (cro.baseProperties != 0)
  4024. outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
  4025. } else
  4026. outs() << format("0x%" PRIx64, cro.baseProperties);
  4027. outs() << "\n";
  4028. if (cro.baseProperties + n_value != 0)
  4029. print_objc_property_list64(cro.baseProperties + n_value, info);
  4030. is_meta_class = (cro.flags & RO_META) ? true : false;
  4031. }
  4032. static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
  4033. bool &is_meta_class) {
  4034. struct class_ro32_t cro;
  4035. const char *r;
  4036. uint32_t offset, xoffset, left;
  4037. SectionRef S, xS;
  4038. const char *name;
  4039. r = get_pointer_32(p, offset, left, S, info);
  4040. if (r == nullptr)
  4041. return;
  4042. memset(&cro, '\0', sizeof(struct class_ro32_t));
  4043. if (left < sizeof(struct class_ro32_t)) {
  4044. memcpy(&cro, r, left);
  4045. outs() << " (class_ro_t entends past the end of the section)\n";
  4046. } else
  4047. memcpy(&cro, r, sizeof(struct class_ro32_t));
  4048. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4049. swapStruct(cro);
  4050. outs() << " flags " << format("0x%" PRIx32, cro.flags);
  4051. if (cro.flags & RO_META)
  4052. outs() << " RO_META";
  4053. if (cro.flags & RO_ROOT)
  4054. outs() << " RO_ROOT";
  4055. if (cro.flags & RO_HAS_CXX_STRUCTORS)
  4056. outs() << " RO_HAS_CXX_STRUCTORS";
  4057. outs() << "\n";
  4058. outs() << " instanceStart " << cro.instanceStart << "\n";
  4059. outs() << " instanceSize " << cro.instanceSize << "\n";
  4060. outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
  4061. << "\n";
  4062. print_layout_map32(cro.ivarLayout, info);
  4063. outs() << " name " << format("0x%" PRIx32, cro.name);
  4064. name = get_pointer_32(cro.name, xoffset, left, xS, info);
  4065. if (name != nullptr)
  4066. outs() << format(" %.*s", left, name);
  4067. outs() << "\n";
  4068. outs() << " baseMethods "
  4069. << format("0x%" PRIx32, cro.baseMethods)
  4070. << " (struct method_list_t *)\n";
  4071. if (cro.baseMethods != 0)
  4072. print_method_list32_t(cro.baseMethods, info, "");
  4073. outs() << " baseProtocols "
  4074. << format("0x%" PRIx32, cro.baseProtocols) << "\n";
  4075. if (cro.baseProtocols != 0)
  4076. print_protocol_list32_t(cro.baseProtocols, info);
  4077. outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
  4078. << "\n";
  4079. if (cro.ivars != 0)
  4080. print_ivar_list32_t(cro.ivars, info);
  4081. outs() << " weakIvarLayout "
  4082. << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
  4083. print_layout_map32(cro.weakIvarLayout, info);
  4084. outs() << " baseProperties "
  4085. << format("0x%" PRIx32, cro.baseProperties) << "\n";
  4086. if (cro.baseProperties != 0)
  4087. print_objc_property_list32(cro.baseProperties, info);
  4088. is_meta_class = (cro.flags & RO_META) ? true : false;
  4089. }
  4090. static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
  4091. struct class64_t c;
  4092. const char *r;
  4093. uint32_t offset, left;
  4094. SectionRef S;
  4095. const char *name;
  4096. uint64_t isa_n_value, n_value;
  4097. r = get_pointer_64(p, offset, left, S, info);
  4098. if (r == nullptr || left < sizeof(struct class64_t))
  4099. return;
  4100. memset(&c, '\0', sizeof(struct class64_t));
  4101. if (left < sizeof(struct class64_t)) {
  4102. memcpy(&c, r, left);
  4103. outs() << " (class_t entends past the end of the section)\n";
  4104. } else
  4105. memcpy(&c, r, sizeof(struct class64_t));
  4106. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4107. swapStruct(c);
  4108. outs() << " isa " << format("0x%" PRIx64, c.isa);
  4109. name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
  4110. isa_n_value, c.isa);
  4111. if (name != nullptr)
  4112. outs() << " " << name;
  4113. outs() << "\n";
  4114. outs() << " superclass " << format("0x%" PRIx64, c.superclass);
  4115. name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
  4116. n_value, c.superclass);
  4117. if (name != nullptr)
  4118. outs() << " " << name;
  4119. outs() << "\n";
  4120. outs() << " cache " << format("0x%" PRIx64, c.cache);
  4121. name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
  4122. n_value, c.cache);
  4123. if (name != nullptr)
  4124. outs() << " " << name;
  4125. outs() << "\n";
  4126. outs() << " vtable " << format("0x%" PRIx64, c.vtable);
  4127. name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
  4128. n_value, c.vtable);
  4129. if (name != nullptr)
  4130. outs() << " " << name;
  4131. outs() << "\n";
  4132. name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
  4133. n_value, c.data);
  4134. outs() << " data ";
  4135. if (n_value != 0) {
  4136. if (info->verbose && name != nullptr)
  4137. outs() << name;
  4138. else
  4139. outs() << format("0x%" PRIx64, n_value);
  4140. if (c.data != 0)
  4141. outs() << " + " << format("0x%" PRIx64, c.data);
  4142. } else
  4143. outs() << format("0x%" PRIx64, c.data);
  4144. outs() << " (struct class_ro_t *)";
  4145. // This is a Swift class if some of the low bits of the pointer are set.
  4146. if ((c.data + n_value) & 0x7)
  4147. outs() << " Swift class";
  4148. outs() << "\n";
  4149. bool is_meta_class;
  4150. print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
  4151. if (is_meta_class == false) {
  4152. outs() << "Meta Class\n";
  4153. print_class64_t(c.isa + isa_n_value, info);
  4154. }
  4155. }
  4156. static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
  4157. struct class32_t c;
  4158. const char *r;
  4159. uint32_t offset, left;
  4160. SectionRef S;
  4161. const char *name;
  4162. r = get_pointer_32(p, offset, left, S, info);
  4163. if (r == nullptr)
  4164. return;
  4165. memset(&c, '\0', sizeof(struct class32_t));
  4166. if (left < sizeof(struct class32_t)) {
  4167. memcpy(&c, r, left);
  4168. outs() << " (class_t entends past the end of the section)\n";
  4169. } else
  4170. memcpy(&c, r, sizeof(struct class32_t));
  4171. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4172. swapStruct(c);
  4173. outs() << " isa " << format("0x%" PRIx32, c.isa);
  4174. name =
  4175. get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
  4176. if (name != nullptr)
  4177. outs() << " " << name;
  4178. outs() << "\n";
  4179. outs() << " superclass " << format("0x%" PRIx32, c.superclass);
  4180. name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
  4181. c.superclass);
  4182. if (name != nullptr)
  4183. outs() << " " << name;
  4184. outs() << "\n";
  4185. outs() << " cache " << format("0x%" PRIx32, c.cache);
  4186. name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
  4187. c.cache);
  4188. if (name != nullptr)
  4189. outs() << " " << name;
  4190. outs() << "\n";
  4191. outs() << " vtable " << format("0x%" PRIx32, c.vtable);
  4192. name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
  4193. c.vtable);
  4194. if (name != nullptr)
  4195. outs() << " " << name;
  4196. outs() << "\n";
  4197. name =
  4198. get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
  4199. outs() << " data " << format("0x%" PRIx32, c.data)
  4200. << " (struct class_ro_t *)";
  4201. // This is a Swift class if some of the low bits of the pointer are set.
  4202. if (c.data & 0x3)
  4203. outs() << " Swift class";
  4204. outs() << "\n";
  4205. bool is_meta_class;
  4206. print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
  4207. if (is_meta_class == false) {
  4208. outs() << "Meta Class\n";
  4209. print_class32_t(c.isa, info);
  4210. }
  4211. }
  4212. static void print_objc_class_t(struct objc_class_t *objc_class,
  4213. struct DisassembleInfo *info) {
  4214. uint32_t offset, left, xleft;
  4215. const char *name, *p, *ivar_list;
  4216. SectionRef S;
  4217. int32_t i;
  4218. struct objc_ivar_list_t objc_ivar_list;
  4219. struct objc_ivar_t ivar;
  4220. outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
  4221. if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
  4222. name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
  4223. if (name != nullptr)
  4224. outs() << format(" %.*s", left, name);
  4225. else
  4226. outs() << " (not in an __OBJC section)";
  4227. }
  4228. outs() << "\n";
  4229. outs() << "\t super_class "
  4230. << format("0x%08" PRIx32, objc_class->super_class);
  4231. if (info->verbose) {
  4232. name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
  4233. if (name != nullptr)
  4234. outs() << format(" %.*s", left, name);
  4235. else
  4236. outs() << " (not in an __OBJC section)";
  4237. }
  4238. outs() << "\n";
  4239. outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
  4240. if (info->verbose) {
  4241. name = get_pointer_32(objc_class->name, offset, left, S, info, true);
  4242. if (name != nullptr)
  4243. outs() << format(" %.*s", left, name);
  4244. else
  4245. outs() << " (not in an __OBJC section)";
  4246. }
  4247. outs() << "\n";
  4248. outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
  4249. << "\n";
  4250. outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
  4251. if (info->verbose) {
  4252. if (CLS_GETINFO(objc_class, CLS_CLASS))
  4253. outs() << " CLS_CLASS";
  4254. else if (CLS_GETINFO(objc_class, CLS_META))
  4255. outs() << " CLS_META";
  4256. }
  4257. outs() << "\n";
  4258. outs() << "\t instance_size "
  4259. << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
  4260. p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
  4261. outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
  4262. if (p != nullptr) {
  4263. if (left > sizeof(struct objc_ivar_list_t)) {
  4264. outs() << "\n";
  4265. memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
  4266. } else {
  4267. outs() << " (entends past the end of the section)\n";
  4268. memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
  4269. memcpy(&objc_ivar_list, p, left);
  4270. }
  4271. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4272. swapStruct(objc_ivar_list);
  4273. outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
  4274. ivar_list = p + sizeof(struct objc_ivar_list_t);
  4275. for (i = 0; i < objc_ivar_list.ivar_count; i++) {
  4276. if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
  4277. outs() << "\t\t remaining ivar's extend past the of the section\n";
  4278. break;
  4279. }
  4280. memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
  4281. sizeof(struct objc_ivar_t));
  4282. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4283. swapStruct(ivar);
  4284. outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
  4285. if (info->verbose) {
  4286. name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
  4287. if (name != nullptr)
  4288. outs() << format(" %.*s", xleft, name);
  4289. else
  4290. outs() << " (not in an __OBJC section)";
  4291. }
  4292. outs() << "\n";
  4293. outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
  4294. if (info->verbose) {
  4295. name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
  4296. if (name != nullptr)
  4297. outs() << format(" %.*s", xleft, name);
  4298. else
  4299. outs() << " (not in an __OBJC section)";
  4300. }
  4301. outs() << "\n";
  4302. outs() << "\t\t ivar_offset "
  4303. << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
  4304. }
  4305. } else {
  4306. outs() << " (not in an __OBJC section)\n";
  4307. }
  4308. outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
  4309. if (print_method_list(objc_class->methodLists, info))
  4310. outs() << " (not in an __OBJC section)\n";
  4311. outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
  4312. << "\n";
  4313. outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
  4314. if (print_protocol_list(objc_class->protocols, 16, info))
  4315. outs() << " (not in an __OBJC section)\n";
  4316. }
  4317. static void print_objc_objc_category_t(struct objc_category_t *objc_category,
  4318. struct DisassembleInfo *info) {
  4319. uint32_t offset, left;
  4320. const char *name;
  4321. SectionRef S;
  4322. outs() << "\t category name "
  4323. << format("0x%08" PRIx32, objc_category->category_name);
  4324. if (info->verbose) {
  4325. name = get_pointer_32(objc_category->category_name, offset, left, S, info,
  4326. true);
  4327. if (name != nullptr)
  4328. outs() << format(" %.*s", left, name);
  4329. else
  4330. outs() << " (not in an __OBJC section)";
  4331. }
  4332. outs() << "\n";
  4333. outs() << "\t\t class name "
  4334. << format("0x%08" PRIx32, objc_category->class_name);
  4335. if (info->verbose) {
  4336. name =
  4337. get_pointer_32(objc_category->class_name, offset, left, S, info, true);
  4338. if (name != nullptr)
  4339. outs() << format(" %.*s", left, name);
  4340. else
  4341. outs() << " (not in an __OBJC section)";
  4342. }
  4343. outs() << "\n";
  4344. outs() << "\t instance methods "
  4345. << format("0x%08" PRIx32, objc_category->instance_methods);
  4346. if (print_method_list(objc_category->instance_methods, info))
  4347. outs() << " (not in an __OBJC section)\n";
  4348. outs() << "\t class methods "
  4349. << format("0x%08" PRIx32, objc_category->class_methods);
  4350. if (print_method_list(objc_category->class_methods, info))
  4351. outs() << " (not in an __OBJC section)\n";
  4352. }
  4353. static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
  4354. struct category64_t c;
  4355. const char *r;
  4356. uint32_t offset, xoffset, left;
  4357. SectionRef S, xS;
  4358. const char *name, *sym_name;
  4359. uint64_t n_value;
  4360. r = get_pointer_64(p, offset, left, S, info);
  4361. if (r == nullptr)
  4362. return;
  4363. memset(&c, '\0', sizeof(struct category64_t));
  4364. if (left < sizeof(struct category64_t)) {
  4365. memcpy(&c, r, left);
  4366. outs() << " (category_t entends past the end of the section)\n";
  4367. } else
  4368. memcpy(&c, r, sizeof(struct category64_t));
  4369. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4370. swapStruct(c);
  4371. outs() << " name ";
  4372. sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
  4373. info, n_value, c.name);
  4374. if (n_value != 0) {
  4375. if (info->verbose && sym_name != nullptr)
  4376. outs() << sym_name;
  4377. else
  4378. outs() << format("0x%" PRIx64, n_value);
  4379. if (c.name != 0)
  4380. outs() << " + " << format("0x%" PRIx64, c.name);
  4381. } else
  4382. outs() << format("0x%" PRIx64, c.name);
  4383. name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
  4384. if (name != nullptr)
  4385. outs() << format(" %.*s", left, name);
  4386. outs() << "\n";
  4387. outs() << " cls ";
  4388. sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
  4389. n_value, c.cls);
  4390. if (n_value != 0) {
  4391. if (info->verbose && sym_name != nullptr)
  4392. outs() << sym_name;
  4393. else
  4394. outs() << format("0x%" PRIx64, n_value);
  4395. if (c.cls != 0)
  4396. outs() << " + " << format("0x%" PRIx64, c.cls);
  4397. } else
  4398. outs() << format("0x%" PRIx64, c.cls);
  4399. outs() << "\n";
  4400. if (c.cls + n_value != 0)
  4401. print_class64_t(c.cls + n_value, info);
  4402. outs() << " instanceMethods ";
  4403. sym_name =
  4404. get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
  4405. info, n_value, c.instanceMethods);
  4406. if (n_value != 0) {
  4407. if (info->verbose && sym_name != nullptr)
  4408. outs() << sym_name;
  4409. else
  4410. outs() << format("0x%" PRIx64, n_value);
  4411. if (c.instanceMethods != 0)
  4412. outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
  4413. } else
  4414. outs() << format("0x%" PRIx64, c.instanceMethods);
  4415. outs() << "\n";
  4416. if (c.instanceMethods + n_value != 0)
  4417. print_method_list64_t(c.instanceMethods + n_value, info, "");
  4418. outs() << " classMethods ";
  4419. sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
  4420. S, info, n_value, c.classMethods);
  4421. if (n_value != 0) {
  4422. if (info->verbose && sym_name != nullptr)
  4423. outs() << sym_name;
  4424. else
  4425. outs() << format("0x%" PRIx64, n_value);
  4426. if (c.classMethods != 0)
  4427. outs() << " + " << format("0x%" PRIx64, c.classMethods);
  4428. } else
  4429. outs() << format("0x%" PRIx64, c.classMethods);
  4430. outs() << "\n";
  4431. if (c.classMethods + n_value != 0)
  4432. print_method_list64_t(c.classMethods + n_value, info, "");
  4433. outs() << " protocols ";
  4434. sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
  4435. info, n_value, c.protocols);
  4436. if (n_value != 0) {
  4437. if (info->verbose && sym_name != nullptr)
  4438. outs() << sym_name;
  4439. else
  4440. outs() << format("0x%" PRIx64, n_value);
  4441. if (c.protocols != 0)
  4442. outs() << " + " << format("0x%" PRIx64, c.protocols);
  4443. } else
  4444. outs() << format("0x%" PRIx64, c.protocols);
  4445. outs() << "\n";
  4446. if (c.protocols + n_value != 0)
  4447. print_protocol_list64_t(c.protocols + n_value, info);
  4448. outs() << "instanceProperties ";
  4449. sym_name =
  4450. get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
  4451. S, info, n_value, c.instanceProperties);
  4452. if (n_value != 0) {
  4453. if (info->verbose && sym_name != nullptr)
  4454. outs() << sym_name;
  4455. else
  4456. outs() << format("0x%" PRIx64, n_value);
  4457. if (c.instanceProperties != 0)
  4458. outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
  4459. } else
  4460. outs() << format("0x%" PRIx64, c.instanceProperties);
  4461. outs() << "\n";
  4462. if (c.instanceProperties + n_value != 0)
  4463. print_objc_property_list64(c.instanceProperties + n_value, info);
  4464. }
  4465. static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
  4466. struct category32_t c;
  4467. const char *r;
  4468. uint32_t offset, left;
  4469. SectionRef S, xS;
  4470. const char *name;
  4471. r = get_pointer_32(p, offset, left, S, info);
  4472. if (r == nullptr)
  4473. return;
  4474. memset(&c, '\0', sizeof(struct category32_t));
  4475. if (left < sizeof(struct category32_t)) {
  4476. memcpy(&c, r, left);
  4477. outs() << " (category_t entends past the end of the section)\n";
  4478. } else
  4479. memcpy(&c, r, sizeof(struct category32_t));
  4480. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4481. swapStruct(c);
  4482. outs() << " name " << format("0x%" PRIx32, c.name);
  4483. name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
  4484. c.name);
  4485. if (name != NULL)
  4486. outs() << " " << name;
  4487. outs() << "\n";
  4488. outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
  4489. if (c.cls != 0)
  4490. print_class32_t(c.cls, info);
  4491. outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
  4492. << "\n";
  4493. if (c.instanceMethods != 0)
  4494. print_method_list32_t(c.instanceMethods, info, "");
  4495. outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
  4496. << "\n";
  4497. if (c.classMethods != 0)
  4498. print_method_list32_t(c.classMethods, info, "");
  4499. outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
  4500. if (c.protocols != 0)
  4501. print_protocol_list32_t(c.protocols, info);
  4502. outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
  4503. << "\n";
  4504. if (c.instanceProperties != 0)
  4505. print_objc_property_list32(c.instanceProperties, info);
  4506. }
  4507. static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
  4508. uint32_t i, left, offset, xoffset;
  4509. uint64_t p, n_value;
  4510. struct message_ref64 mr;
  4511. const char *name, *sym_name;
  4512. const char *r;
  4513. SectionRef xS;
  4514. if (S == SectionRef())
  4515. return;
  4516. StringRef SectName;
  4517. S.getName(SectName);
  4518. DataRefImpl Ref = S.getRawDataRefImpl();
  4519. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  4520. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  4521. offset = 0;
  4522. for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
  4523. p = S.getAddress() + i;
  4524. r = get_pointer_64(p, offset, left, S, info);
  4525. if (r == nullptr)
  4526. return;
  4527. memset(&mr, '\0', sizeof(struct message_ref64));
  4528. if (left < sizeof(struct message_ref64)) {
  4529. memcpy(&mr, r, left);
  4530. outs() << " (message_ref entends past the end of the section)\n";
  4531. } else
  4532. memcpy(&mr, r, sizeof(struct message_ref64));
  4533. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4534. swapStruct(mr);
  4535. outs() << " imp ";
  4536. name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
  4537. n_value, mr.imp);
  4538. if (n_value != 0) {
  4539. outs() << format("0x%" PRIx64, n_value) << " ";
  4540. if (mr.imp != 0)
  4541. outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
  4542. } else
  4543. outs() << format("0x%" PRIx64, mr.imp) << " ";
  4544. if (name != nullptr)
  4545. outs() << " " << name;
  4546. outs() << "\n";
  4547. outs() << " sel ";
  4548. sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
  4549. info, n_value, mr.sel);
  4550. if (n_value != 0) {
  4551. if (info->verbose && sym_name != nullptr)
  4552. outs() << sym_name;
  4553. else
  4554. outs() << format("0x%" PRIx64, n_value);
  4555. if (mr.sel != 0)
  4556. outs() << " + " << format("0x%" PRIx64, mr.sel);
  4557. } else
  4558. outs() << format("0x%" PRIx64, mr.sel);
  4559. name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
  4560. if (name != nullptr)
  4561. outs() << format(" %.*s", left, name);
  4562. outs() << "\n";
  4563. offset += sizeof(struct message_ref64);
  4564. }
  4565. }
  4566. static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
  4567. uint32_t i, left, offset, xoffset, p;
  4568. struct message_ref32 mr;
  4569. const char *name, *r;
  4570. SectionRef xS;
  4571. if (S == SectionRef())
  4572. return;
  4573. StringRef SectName;
  4574. S.getName(SectName);
  4575. DataRefImpl Ref = S.getRawDataRefImpl();
  4576. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  4577. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  4578. offset = 0;
  4579. for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
  4580. p = S.getAddress() + i;
  4581. r = get_pointer_32(p, offset, left, S, info);
  4582. if (r == nullptr)
  4583. return;
  4584. memset(&mr, '\0', sizeof(struct message_ref32));
  4585. if (left < sizeof(struct message_ref32)) {
  4586. memcpy(&mr, r, left);
  4587. outs() << " (message_ref entends past the end of the section)\n";
  4588. } else
  4589. memcpy(&mr, r, sizeof(struct message_ref32));
  4590. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4591. swapStruct(mr);
  4592. outs() << " imp " << format("0x%" PRIx32, mr.imp);
  4593. name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
  4594. mr.imp);
  4595. if (name != nullptr)
  4596. outs() << " " << name;
  4597. outs() << "\n";
  4598. outs() << " sel " << format("0x%" PRIx32, mr.sel);
  4599. name = get_pointer_32(mr.sel, xoffset, left, xS, info);
  4600. if (name != nullptr)
  4601. outs() << " " << name;
  4602. outs() << "\n";
  4603. offset += sizeof(struct message_ref32);
  4604. }
  4605. }
  4606. static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
  4607. uint32_t left, offset, swift_version;
  4608. uint64_t p;
  4609. struct objc_image_info64 o;
  4610. const char *r;
  4611. StringRef SectName;
  4612. S.getName(SectName);
  4613. DataRefImpl Ref = S.getRawDataRefImpl();
  4614. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  4615. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  4616. p = S.getAddress();
  4617. r = get_pointer_64(p, offset, left, S, info);
  4618. if (r == nullptr)
  4619. return;
  4620. memset(&o, '\0', sizeof(struct objc_image_info64));
  4621. if (left < sizeof(struct objc_image_info64)) {
  4622. memcpy(&o, r, left);
  4623. outs() << " (objc_image_info entends past the end of the section)\n";
  4624. } else
  4625. memcpy(&o, r, sizeof(struct objc_image_info64));
  4626. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4627. swapStruct(o);
  4628. outs() << " version " << o.version << "\n";
  4629. outs() << " flags " << format("0x%" PRIx32, o.flags);
  4630. if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
  4631. outs() << " OBJC_IMAGE_IS_REPLACEMENT";
  4632. if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
  4633. outs() << " OBJC_IMAGE_SUPPORTS_GC";
  4634. swift_version = (o.flags >> 8) & 0xff;
  4635. if (swift_version != 0) {
  4636. if (swift_version == 1)
  4637. outs() << " Swift 1.0";
  4638. else if (swift_version == 2)
  4639. outs() << " Swift 1.1";
  4640. else
  4641. outs() << " unknown future Swift version (" << swift_version << ")";
  4642. }
  4643. outs() << "\n";
  4644. }
  4645. static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
  4646. uint32_t left, offset, swift_version, p;
  4647. struct objc_image_info32 o;
  4648. const char *r;
  4649. StringRef SectName;
  4650. S.getName(SectName);
  4651. DataRefImpl Ref = S.getRawDataRefImpl();
  4652. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  4653. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  4654. p = S.getAddress();
  4655. r = get_pointer_32(p, offset, left, S, info);
  4656. if (r == nullptr)
  4657. return;
  4658. memset(&o, '\0', sizeof(struct objc_image_info32));
  4659. if (left < sizeof(struct objc_image_info32)) {
  4660. memcpy(&o, r, left);
  4661. outs() << " (objc_image_info entends past the end of the section)\n";
  4662. } else
  4663. memcpy(&o, r, sizeof(struct objc_image_info32));
  4664. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4665. swapStruct(o);
  4666. outs() << " version " << o.version << "\n";
  4667. outs() << " flags " << format("0x%" PRIx32, o.flags);
  4668. if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
  4669. outs() << " OBJC_IMAGE_IS_REPLACEMENT";
  4670. if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
  4671. outs() << " OBJC_IMAGE_SUPPORTS_GC";
  4672. swift_version = (o.flags >> 8) & 0xff;
  4673. if (swift_version != 0) {
  4674. if (swift_version == 1)
  4675. outs() << " Swift 1.0";
  4676. else if (swift_version == 2)
  4677. outs() << " Swift 1.1";
  4678. else
  4679. outs() << " unknown future Swift version (" << swift_version << ")";
  4680. }
  4681. outs() << "\n";
  4682. }
  4683. static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
  4684. uint32_t left, offset, p;
  4685. struct imageInfo_t o;
  4686. const char *r;
  4687. StringRef SectName;
  4688. S.getName(SectName);
  4689. DataRefImpl Ref = S.getRawDataRefImpl();
  4690. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  4691. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  4692. p = S.getAddress();
  4693. r = get_pointer_32(p, offset, left, S, info);
  4694. if (r == nullptr)
  4695. return;
  4696. memset(&o, '\0', sizeof(struct imageInfo_t));
  4697. if (left < sizeof(struct imageInfo_t)) {
  4698. memcpy(&o, r, left);
  4699. outs() << " (imageInfo entends past the end of the section)\n";
  4700. } else
  4701. memcpy(&o, r, sizeof(struct imageInfo_t));
  4702. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4703. swapStruct(o);
  4704. outs() << " version " << o.version << "\n";
  4705. outs() << " flags " << format("0x%" PRIx32, o.flags);
  4706. if (o.flags & 0x1)
  4707. outs() << " F&C";
  4708. if (o.flags & 0x2)
  4709. outs() << " GC";
  4710. if (o.flags & 0x4)
  4711. outs() << " GC-only";
  4712. else
  4713. outs() << " RR";
  4714. outs() << "\n";
  4715. }
  4716. static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
  4717. SymbolAddressMap AddrMap;
  4718. if (verbose)
  4719. CreateSymbolAddressMap(O, &AddrMap);
  4720. std::vector<SectionRef> Sections;
  4721. for (const SectionRef &Section : O->sections()) {
  4722. StringRef SectName;
  4723. Section.getName(SectName);
  4724. Sections.push_back(Section);
  4725. }
  4726. struct DisassembleInfo info;
  4727. // Set up the block of info used by the Symbolizer call backs.
  4728. info.verbose = verbose;
  4729. info.O = O;
  4730. info.AddrMap = &AddrMap;
  4731. info.Sections = &Sections;
  4732. info.class_name = nullptr;
  4733. info.selector_name = nullptr;
  4734. info.method = nullptr;
  4735. info.demangled_name = nullptr;
  4736. info.bindtable = nullptr;
  4737. info.adrp_addr = 0;
  4738. info.adrp_inst = 0;
  4739. const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
  4740. if (CL != SectionRef()) {
  4741. info.S = CL;
  4742. walk_pointer_list_64("class", CL, O, &info, print_class64_t);
  4743. } else {
  4744. const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
  4745. info.S = CL;
  4746. walk_pointer_list_64("class", CL, O, &info, print_class64_t);
  4747. }
  4748. const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
  4749. if (CR != SectionRef()) {
  4750. info.S = CR;
  4751. walk_pointer_list_64("class refs", CR, O, &info, nullptr);
  4752. } else {
  4753. const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
  4754. info.S = CR;
  4755. walk_pointer_list_64("class refs", CR, O, &info, nullptr);
  4756. }
  4757. const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
  4758. if (SR != SectionRef()) {
  4759. info.S = SR;
  4760. walk_pointer_list_64("super refs", SR, O, &info, nullptr);
  4761. } else {
  4762. const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
  4763. info.S = SR;
  4764. walk_pointer_list_64("super refs", SR, O, &info, nullptr);
  4765. }
  4766. const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
  4767. if (CA != SectionRef()) {
  4768. info.S = CA;
  4769. walk_pointer_list_64("category", CA, O, &info, print_category64_t);
  4770. } else {
  4771. const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
  4772. info.S = CA;
  4773. walk_pointer_list_64("category", CA, O, &info, print_category64_t);
  4774. }
  4775. const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
  4776. if (PL != SectionRef()) {
  4777. info.S = PL;
  4778. walk_pointer_list_64("protocol", PL, O, &info, nullptr);
  4779. } else {
  4780. const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
  4781. info.S = PL;
  4782. walk_pointer_list_64("protocol", PL, O, &info, nullptr);
  4783. }
  4784. const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
  4785. if (MR != SectionRef()) {
  4786. info.S = MR;
  4787. print_message_refs64(MR, &info);
  4788. } else {
  4789. const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
  4790. info.S = MR;
  4791. print_message_refs64(MR, &info);
  4792. }
  4793. const SectionRef II = get_section(O, "__OBJC2", "__image_info");
  4794. if (II != SectionRef()) {
  4795. info.S = II;
  4796. print_image_info64(II, &info);
  4797. } else {
  4798. const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
  4799. info.S = II;
  4800. print_image_info64(II, &info);
  4801. }
  4802. if (info.bindtable != nullptr)
  4803. delete info.bindtable;
  4804. }
  4805. static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
  4806. SymbolAddressMap AddrMap;
  4807. if (verbose)
  4808. CreateSymbolAddressMap(O, &AddrMap);
  4809. std::vector<SectionRef> Sections;
  4810. for (const SectionRef &Section : O->sections()) {
  4811. StringRef SectName;
  4812. Section.getName(SectName);
  4813. Sections.push_back(Section);
  4814. }
  4815. struct DisassembleInfo info;
  4816. // Set up the block of info used by the Symbolizer call backs.
  4817. info.verbose = verbose;
  4818. info.O = O;
  4819. info.AddrMap = &AddrMap;
  4820. info.Sections = &Sections;
  4821. info.class_name = nullptr;
  4822. info.selector_name = nullptr;
  4823. info.method = nullptr;
  4824. info.demangled_name = nullptr;
  4825. info.bindtable = nullptr;
  4826. info.adrp_addr = 0;
  4827. info.adrp_inst = 0;
  4828. const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
  4829. if (CL != SectionRef()) {
  4830. info.S = CL;
  4831. walk_pointer_list_32("class", CL, O, &info, print_class32_t);
  4832. } else {
  4833. const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
  4834. info.S = CL;
  4835. walk_pointer_list_32("class", CL, O, &info, print_class32_t);
  4836. }
  4837. const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
  4838. if (CR != SectionRef()) {
  4839. info.S = CR;
  4840. walk_pointer_list_32("class refs", CR, O, &info, nullptr);
  4841. } else {
  4842. const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
  4843. info.S = CR;
  4844. walk_pointer_list_32("class refs", CR, O, &info, nullptr);
  4845. }
  4846. const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
  4847. if (SR != SectionRef()) {
  4848. info.S = SR;
  4849. walk_pointer_list_32("super refs", SR, O, &info, nullptr);
  4850. } else {
  4851. const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
  4852. info.S = SR;
  4853. walk_pointer_list_32("super refs", SR, O, &info, nullptr);
  4854. }
  4855. const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
  4856. if (CA != SectionRef()) {
  4857. info.S = CA;
  4858. walk_pointer_list_32("category", CA, O, &info, print_category32_t);
  4859. } else {
  4860. const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
  4861. info.S = CA;
  4862. walk_pointer_list_32("category", CA, O, &info, print_category32_t);
  4863. }
  4864. const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
  4865. if (PL != SectionRef()) {
  4866. info.S = PL;
  4867. walk_pointer_list_32("protocol", PL, O, &info, nullptr);
  4868. } else {
  4869. const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
  4870. info.S = PL;
  4871. walk_pointer_list_32("protocol", PL, O, &info, nullptr);
  4872. }
  4873. const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
  4874. if (MR != SectionRef()) {
  4875. info.S = MR;
  4876. print_message_refs32(MR, &info);
  4877. } else {
  4878. const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
  4879. info.S = MR;
  4880. print_message_refs32(MR, &info);
  4881. }
  4882. const SectionRef II = get_section(O, "__OBJC2", "__image_info");
  4883. if (II != SectionRef()) {
  4884. info.S = II;
  4885. print_image_info32(II, &info);
  4886. } else {
  4887. const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
  4888. info.S = II;
  4889. print_image_info32(II, &info);
  4890. }
  4891. }
  4892. static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
  4893. uint32_t i, j, p, offset, xoffset, left, defs_left, def;
  4894. const char *r, *name, *defs;
  4895. struct objc_module_t mod;
  4896. SectionRef S, xS;
  4897. struct objc_symtab_t symtab;
  4898. struct objc_class_t objc_class;
  4899. struct objc_category_t objc_category;
  4900. outs() << "Objective-C segment\n";
  4901. S = get_section(O, "__OBJC", "__module_info");
  4902. if (S == SectionRef())
  4903. return false;
  4904. SymbolAddressMap AddrMap;
  4905. if (verbose)
  4906. CreateSymbolAddressMap(O, &AddrMap);
  4907. std::vector<SectionRef> Sections;
  4908. for (const SectionRef &Section : O->sections()) {
  4909. StringRef SectName;
  4910. Section.getName(SectName);
  4911. Sections.push_back(Section);
  4912. }
  4913. struct DisassembleInfo info;
  4914. // Set up the block of info used by the Symbolizer call backs.
  4915. info.verbose = verbose;
  4916. info.O = O;
  4917. info.AddrMap = &AddrMap;
  4918. info.Sections = &Sections;
  4919. info.class_name = nullptr;
  4920. info.selector_name = nullptr;
  4921. info.method = nullptr;
  4922. info.demangled_name = nullptr;
  4923. info.bindtable = nullptr;
  4924. info.adrp_addr = 0;
  4925. info.adrp_inst = 0;
  4926. for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
  4927. p = S.getAddress() + i;
  4928. r = get_pointer_32(p, offset, left, S, &info, true);
  4929. if (r == nullptr)
  4930. return true;
  4931. memset(&mod, '\0', sizeof(struct objc_module_t));
  4932. if (left < sizeof(struct objc_module_t)) {
  4933. memcpy(&mod, r, left);
  4934. outs() << " (module extends past end of __module_info section)\n";
  4935. } else
  4936. memcpy(&mod, r, sizeof(struct objc_module_t));
  4937. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  4938. swapStruct(mod);
  4939. outs() << "Module " << format("0x%" PRIx32, p) << "\n";
  4940. outs() << " version " << mod.version << "\n";
  4941. outs() << " size " << mod.size << "\n";
  4942. outs() << " name ";
  4943. name = get_pointer_32(mod.name, xoffset, left, xS, &info, true);
  4944. if (name != nullptr)
  4945. outs() << format("%.*s", left, name);
  4946. else
  4947. outs() << format("0x%08" PRIx32, mod.name)
  4948. << "(not in an __OBJC section)";
  4949. outs() << "\n";
  4950. r = get_pointer_32(mod.symtab, xoffset, left, xS, &info, true);
  4951. if (mod.symtab == 0 || r == nullptr) {
  4952. outs() << " symtab " << format("0x%08" PRIx32, mod.symtab)
  4953. << " (not in an __OBJC section)\n";
  4954. continue;
  4955. }
  4956. outs() << " symtab " << format("0x%08" PRIx32, mod.symtab) << "\n";
  4957. memset(&symtab, '\0', sizeof(struct objc_symtab_t));
  4958. defs_left = 0;
  4959. defs = nullptr;
  4960. if (left < sizeof(struct objc_symtab_t)) {
  4961. memcpy(&symtab, r, left);
  4962. outs() << "\tsymtab extends past end of an __OBJC section)\n";
  4963. } else {
  4964. memcpy(&symtab, r, sizeof(struct objc_symtab_t));
  4965. if (left > sizeof(struct objc_symtab_t)) {
  4966. defs_left = left - sizeof(struct objc_symtab_t);
  4967. defs = r + sizeof(struct objc_symtab_t);
  4968. }
  4969. }
  4970. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  4971. swapStruct(symtab);
  4972. outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
  4973. r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
  4974. outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
  4975. if (r == nullptr)
  4976. outs() << " (not in an __OBJC section)";
  4977. outs() << "\n";
  4978. outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
  4979. outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
  4980. if (symtab.cls_def_cnt > 0)
  4981. outs() << "\tClass Definitions\n";
  4982. for (j = 0; j < symtab.cls_def_cnt; j++) {
  4983. if ((j + 1) * sizeof(uint32_t) > defs_left) {
  4984. outs() << "\t(remaining class defs entries entends past the end of the "
  4985. << "section)\n";
  4986. break;
  4987. }
  4988. memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
  4989. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  4990. sys::swapByteOrder(def);
  4991. r = get_pointer_32(def, xoffset, left, xS, &info, true);
  4992. outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
  4993. if (r != nullptr) {
  4994. if (left > sizeof(struct objc_class_t)) {
  4995. outs() << "\n";
  4996. memcpy(&objc_class, r, sizeof(struct objc_class_t));
  4997. } else {
  4998. outs() << " (entends past the end of the section)\n";
  4999. memset(&objc_class, '\0', sizeof(struct objc_class_t));
  5000. memcpy(&objc_class, r, left);
  5001. }
  5002. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5003. swapStruct(objc_class);
  5004. print_objc_class_t(&objc_class, &info);
  5005. } else {
  5006. outs() << "(not in an __OBJC section)\n";
  5007. }
  5008. if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
  5009. outs() << "\tMeta Class";
  5010. r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
  5011. if (r != nullptr) {
  5012. if (left > sizeof(struct objc_class_t)) {
  5013. outs() << "\n";
  5014. memcpy(&objc_class, r, sizeof(struct objc_class_t));
  5015. } else {
  5016. outs() << " (entends past the end of the section)\n";
  5017. memset(&objc_class, '\0', sizeof(struct objc_class_t));
  5018. memcpy(&objc_class, r, left);
  5019. }
  5020. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5021. swapStruct(objc_class);
  5022. print_objc_class_t(&objc_class, &info);
  5023. } else {
  5024. outs() << "(not in an __OBJC section)\n";
  5025. }
  5026. }
  5027. }
  5028. if (symtab.cat_def_cnt > 0)
  5029. outs() << "\tCategory Definitions\n";
  5030. for (j = 0; j < symtab.cat_def_cnt; j++) {
  5031. if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
  5032. outs() << "\t(remaining category defs entries entends past the end of "
  5033. << "the section)\n";
  5034. break;
  5035. }
  5036. memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
  5037. sizeof(uint32_t));
  5038. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5039. sys::swapByteOrder(def);
  5040. r = get_pointer_32(def, xoffset, left, xS, &info, true);
  5041. outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
  5042. << format("0x%08" PRIx32, def);
  5043. if (r != nullptr) {
  5044. if (left > sizeof(struct objc_category_t)) {
  5045. outs() << "\n";
  5046. memcpy(&objc_category, r, sizeof(struct objc_category_t));
  5047. } else {
  5048. outs() << " (entends past the end of the section)\n";
  5049. memset(&objc_category, '\0', sizeof(struct objc_category_t));
  5050. memcpy(&objc_category, r, left);
  5051. }
  5052. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5053. swapStruct(objc_category);
  5054. print_objc_objc_category_t(&objc_category, &info);
  5055. } else {
  5056. outs() << "(not in an __OBJC section)\n";
  5057. }
  5058. }
  5059. }
  5060. const SectionRef II = get_section(O, "__OBJC", "__image_info");
  5061. if (II != SectionRef())
  5062. print_image_info(II, &info);
  5063. return true;
  5064. }
  5065. static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
  5066. uint32_t size, uint32_t addr) {
  5067. SymbolAddressMap AddrMap;
  5068. CreateSymbolAddressMap(O, &AddrMap);
  5069. std::vector<SectionRef> Sections;
  5070. for (const SectionRef &Section : O->sections()) {
  5071. StringRef SectName;
  5072. Section.getName(SectName);
  5073. Sections.push_back(Section);
  5074. }
  5075. struct DisassembleInfo info;
  5076. // Set up the block of info used by the Symbolizer call backs.
  5077. info.verbose = true;
  5078. info.O = O;
  5079. info.AddrMap = &AddrMap;
  5080. info.Sections = &Sections;
  5081. info.class_name = nullptr;
  5082. info.selector_name = nullptr;
  5083. info.method = nullptr;
  5084. info.demangled_name = nullptr;
  5085. info.bindtable = nullptr;
  5086. info.adrp_addr = 0;
  5087. info.adrp_inst = 0;
  5088. const char *p;
  5089. struct objc_protocol_t protocol;
  5090. uint32_t left, paddr;
  5091. for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
  5092. memset(&protocol, '\0', sizeof(struct objc_protocol_t));
  5093. left = size - (p - sect);
  5094. if (left < sizeof(struct objc_protocol_t)) {
  5095. outs() << "Protocol extends past end of __protocol section\n";
  5096. memcpy(&protocol, p, left);
  5097. } else
  5098. memcpy(&protocol, p, sizeof(struct objc_protocol_t));
  5099. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5100. swapStruct(protocol);
  5101. paddr = addr + (p - sect);
  5102. outs() << "Protocol " << format("0x%" PRIx32, paddr);
  5103. if (print_protocol(paddr, 0, &info))
  5104. outs() << "(not in an __OBJC section)\n";
  5105. }
  5106. }
  5107. static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
  5108. if (O->is64Bit())
  5109. printObjc2_64bit_MetaData(O, verbose);
  5110. else {
  5111. MachO::mach_header H;
  5112. H = O->getHeader();
  5113. if (H.cputype == MachO::CPU_TYPE_ARM)
  5114. printObjc2_32bit_MetaData(O, verbose);
  5115. else {
  5116. // This is the 32-bit non-arm cputype case. Which is normally
  5117. // the first Objective-C ABI. But it may be the case of a
  5118. // binary for the iOS simulator which is the second Objective-C
  5119. // ABI. In that case printObjc1_32bit_MetaData() will determine that
  5120. // and return false.
  5121. if (printObjc1_32bit_MetaData(O, verbose) == false)
  5122. printObjc2_32bit_MetaData(O, verbose);
  5123. }
  5124. }
  5125. }
  5126. // GuessLiteralPointer returns a string which for the item in the Mach-O file
  5127. // for the address passed in as ReferenceValue for printing as a comment with
  5128. // the instruction and also returns the corresponding type of that item
  5129. // indirectly through ReferenceType.
  5130. //
  5131. // If ReferenceValue is an address of literal cstring then a pointer to the
  5132. // cstring is returned and ReferenceType is set to
  5133. // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
  5134. //
  5135. // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
  5136. // Class ref that name is returned and the ReferenceType is set accordingly.
  5137. //
  5138. // Lastly, literals which are Symbol address in a literal pool are looked for
  5139. // and if found the symbol name is returned and ReferenceType is set to
  5140. // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
  5141. //
  5142. // If there is no item in the Mach-O file for the address passed in as
  5143. // ReferenceValue nullptr is returned and ReferenceType is unchanged.
  5144. static const char *GuessLiteralPointer(uint64_t ReferenceValue,
  5145. uint64_t ReferencePC,
  5146. uint64_t *ReferenceType,
  5147. struct DisassembleInfo *info) {
  5148. // First see if there is an external relocation entry at the ReferencePC.
  5149. uint64_t sect_addr = info->S.getAddress();
  5150. uint64_t sect_offset = ReferencePC - sect_addr;
  5151. bool reloc_found = false;
  5152. DataRefImpl Rel;
  5153. MachO::any_relocation_info RE;
  5154. bool isExtern = false;
  5155. SymbolRef Symbol;
  5156. for (const RelocationRef &Reloc : info->S.relocations()) {
  5157. uint64_t RelocOffset = Reloc.getOffset();
  5158. if (RelocOffset == sect_offset) {
  5159. Rel = Reloc.getRawDataRefImpl();
  5160. RE = info->O->getRelocation(Rel);
  5161. if (info->O->isRelocationScattered(RE))
  5162. continue;
  5163. isExtern = info->O->getPlainRelocationExternal(RE);
  5164. if (isExtern) {
  5165. symbol_iterator RelocSym = Reloc.getSymbol();
  5166. Symbol = *RelocSym;
  5167. }
  5168. reloc_found = true;
  5169. break;
  5170. }
  5171. }
  5172. // If there is an external relocation entry for a symbol in a section
  5173. // then used that symbol's value for the value of the reference.
  5174. if (reloc_found && isExtern) {
  5175. if (info->O->getAnyRelocationPCRel(RE)) {
  5176. unsigned Type = info->O->getAnyRelocationType(RE);
  5177. if (Type == MachO::X86_64_RELOC_SIGNED) {
  5178. ReferenceValue = Symbol.getValue();
  5179. }
  5180. }
  5181. }
  5182. // Look for literals such as Objective-C CFStrings refs, Selector refs,
  5183. // Message refs and Class refs.
  5184. bool classref, selref, msgref, cfstring;
  5185. uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
  5186. selref, msgref, cfstring);
  5187. if (classref && pointer_value == 0) {
  5188. // Note the ReferenceValue is a pointer into the __objc_classrefs section.
  5189. // And the pointer_value in that section is typically zero as it will be
  5190. // set by dyld as part of the "bind information".
  5191. const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
  5192. if (name != nullptr) {
  5193. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
  5194. const char *class_name = strrchr(name, '$');
  5195. if (class_name != nullptr && class_name[1] == '_' &&
  5196. class_name[2] != '\0') {
  5197. info->class_name = class_name + 2;
  5198. return name;
  5199. }
  5200. }
  5201. }
  5202. if (classref) {
  5203. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
  5204. const char *name =
  5205. get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
  5206. if (name != nullptr)
  5207. info->class_name = name;
  5208. else
  5209. name = "bad class ref";
  5210. return name;
  5211. }
  5212. if (cfstring) {
  5213. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
  5214. const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
  5215. return name;
  5216. }
  5217. if (selref && pointer_value == 0)
  5218. pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
  5219. if (pointer_value != 0)
  5220. ReferenceValue = pointer_value;
  5221. const char *name = GuessCstringPointer(ReferenceValue, info);
  5222. if (name) {
  5223. if (pointer_value != 0 && selref) {
  5224. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
  5225. info->selector_name = name;
  5226. } else if (pointer_value != 0 && msgref) {
  5227. info->class_name = nullptr;
  5228. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
  5229. info->selector_name = name;
  5230. } else
  5231. *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
  5232. return name;
  5233. }
  5234. // Lastly look for an indirect symbol with this ReferenceValue which is in
  5235. // a literal pool. If found return that symbol name.
  5236. name = GuessIndirectSymbol(ReferenceValue, info);
  5237. if (name) {
  5238. *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
  5239. return name;
  5240. }
  5241. return nullptr;
  5242. }
  5243. // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
  5244. // the Symbolizer. It looks up the ReferenceValue using the info passed via the
  5245. // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
  5246. // is created and returns the symbol name that matches the ReferenceValue or
  5247. // nullptr if none. The ReferenceType is passed in for the IN type of
  5248. // reference the instruction is making from the values in defined in the header
  5249. // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
  5250. // Out type and the ReferenceName will also be set which is added as a comment
  5251. // to the disassembled instruction.
  5252. //
  5253. #if HAVE_CXXABI_H
  5254. // If the symbol name is a C++ mangled name then the demangled name is
  5255. // returned through ReferenceName and ReferenceType is set to
  5256. // LLVMDisassembler_ReferenceType_DeMangled_Name .
  5257. #endif
  5258. //
  5259. // When this is called to get a symbol name for a branch target then the
  5260. // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
  5261. // SymbolValue will be looked for in the indirect symbol table to determine if
  5262. // it is an address for a symbol stub. If so then the symbol name for that
  5263. // stub is returned indirectly through ReferenceName and then ReferenceType is
  5264. // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
  5265. //
  5266. // When this is called with an value loaded via a PC relative load then
  5267. // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
  5268. // SymbolValue is checked to be an address of literal pointer, symbol pointer,
  5269. // or an Objective-C meta data reference. If so the output ReferenceType is
  5270. // set to correspond to that as well as setting the ReferenceName.
  5271. static const char *SymbolizerSymbolLookUp(void *DisInfo,
  5272. uint64_t ReferenceValue,
  5273. uint64_t *ReferenceType,
  5274. uint64_t ReferencePC,
  5275. const char **ReferenceName) {
  5276. struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
  5277. // If no verbose symbolic information is wanted then just return nullptr.
  5278. if (!info->verbose) {
  5279. *ReferenceName = nullptr;
  5280. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5281. return nullptr;
  5282. }
  5283. const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
  5284. if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
  5285. *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
  5286. if (*ReferenceName != nullptr) {
  5287. method_reference(info, ReferenceType, ReferenceName);
  5288. if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
  5289. *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
  5290. } else
  5291. #if HAVE_CXXABI_H
  5292. if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
  5293. if (info->demangled_name != nullptr)
  5294. free(info->demangled_name);
  5295. int status;
  5296. info->demangled_name =
  5297. abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
  5298. if (info->demangled_name != nullptr) {
  5299. *ReferenceName = info->demangled_name;
  5300. *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
  5301. } else
  5302. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5303. } else
  5304. #endif
  5305. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5306. } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
  5307. *ReferenceName =
  5308. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  5309. if (*ReferenceName)
  5310. method_reference(info, ReferenceType, ReferenceName);
  5311. else
  5312. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5313. // If this is arm64 and the reference is an adrp instruction save the
  5314. // instruction, passed in ReferenceValue and the address of the instruction
  5315. // for use later if we see and add immediate instruction.
  5316. } else if (info->O->getArch() == Triple::aarch64 &&
  5317. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
  5318. info->adrp_inst = ReferenceValue;
  5319. info->adrp_addr = ReferencePC;
  5320. SymbolName = nullptr;
  5321. *ReferenceName = nullptr;
  5322. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5323. // If this is arm64 and reference is an add immediate instruction and we
  5324. // have
  5325. // seen an adrp instruction just before it and the adrp's Xd register
  5326. // matches
  5327. // this add's Xn register reconstruct the value being referenced and look to
  5328. // see if it is a literal pointer. Note the add immediate instruction is
  5329. // passed in ReferenceValue.
  5330. } else if (info->O->getArch() == Triple::aarch64 &&
  5331. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
  5332. ReferencePC - 4 == info->adrp_addr &&
  5333. (info->adrp_inst & 0x9f000000) == 0x90000000 &&
  5334. (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
  5335. uint32_t addxri_inst;
  5336. uint64_t adrp_imm, addxri_imm;
  5337. adrp_imm =
  5338. ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
  5339. if (info->adrp_inst & 0x0200000)
  5340. adrp_imm |= 0xfffffffffc000000LL;
  5341. addxri_inst = ReferenceValue;
  5342. addxri_imm = (addxri_inst >> 10) & 0xfff;
  5343. if (((addxri_inst >> 22) & 0x3) == 1)
  5344. addxri_imm <<= 12;
  5345. ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
  5346. (adrp_imm << 12) + addxri_imm;
  5347. *ReferenceName =
  5348. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  5349. if (*ReferenceName == nullptr)
  5350. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5351. // If this is arm64 and the reference is a load register instruction and we
  5352. // have seen an adrp instruction just before it and the adrp's Xd register
  5353. // matches this add's Xn register reconstruct the value being referenced and
  5354. // look to see if it is a literal pointer. Note the load register
  5355. // instruction is passed in ReferenceValue.
  5356. } else if (info->O->getArch() == Triple::aarch64 &&
  5357. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
  5358. ReferencePC - 4 == info->adrp_addr &&
  5359. (info->adrp_inst & 0x9f000000) == 0x90000000 &&
  5360. (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
  5361. uint32_t ldrxui_inst;
  5362. uint64_t adrp_imm, ldrxui_imm;
  5363. adrp_imm =
  5364. ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
  5365. if (info->adrp_inst & 0x0200000)
  5366. adrp_imm |= 0xfffffffffc000000LL;
  5367. ldrxui_inst = ReferenceValue;
  5368. ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
  5369. ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
  5370. (adrp_imm << 12) + (ldrxui_imm << 3);
  5371. *ReferenceName =
  5372. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  5373. if (*ReferenceName == nullptr)
  5374. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5375. }
  5376. // If this arm64 and is an load register (PC-relative) instruction the
  5377. // ReferenceValue is the PC plus the immediate value.
  5378. else if (info->O->getArch() == Triple::aarch64 &&
  5379. (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
  5380. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
  5381. *ReferenceName =
  5382. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  5383. if (*ReferenceName == nullptr)
  5384. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5385. }
  5386. #if HAVE_CXXABI_H
  5387. else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
  5388. if (info->demangled_name != nullptr)
  5389. free(info->demangled_name);
  5390. int status;
  5391. info->demangled_name =
  5392. abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
  5393. if (info->demangled_name != nullptr) {
  5394. *ReferenceName = info->demangled_name;
  5395. *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
  5396. }
  5397. }
  5398. #endif
  5399. else {
  5400. *ReferenceName = nullptr;
  5401. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5402. }
  5403. return SymbolName;
  5404. }
  5405. /// \brief Emits the comments that are stored in the CommentStream.
  5406. /// Each comment in the CommentStream must end with a newline.
  5407. static void emitComments(raw_svector_ostream &CommentStream,
  5408. SmallString<128> &CommentsToEmit,
  5409. formatted_raw_ostream &FormattedOS,
  5410. const MCAsmInfo &MAI) {
  5411. // Flush the stream before taking its content.
  5412. CommentStream.flush();
  5413. StringRef Comments = CommentsToEmit.str();
  5414. // Get the default information for printing a comment.
  5415. const char *CommentBegin = MAI.getCommentString();
  5416. unsigned CommentColumn = MAI.getCommentColumn();
  5417. bool IsFirst = true;
  5418. while (!Comments.empty()) {
  5419. if (!IsFirst)
  5420. FormattedOS << '\n';
  5421. // Emit a line of comments.
  5422. FormattedOS.PadToColumn(CommentColumn);
  5423. size_t Position = Comments.find('\n');
  5424. FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
  5425. // Move after the newline character.
  5426. Comments = Comments.substr(Position + 1);
  5427. IsFirst = false;
  5428. }
  5429. FormattedOS.flush();
  5430. // Tell the comment stream that the vector changed underneath it.
  5431. CommentsToEmit.clear();
  5432. CommentStream.resync();
  5433. }
  5434. static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
  5435. StringRef DisSegName, StringRef DisSectName) {
  5436. const char *McpuDefault = nullptr;
  5437. const Target *ThumbTarget = nullptr;
  5438. const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
  5439. if (!TheTarget) {
  5440. // GetTarget prints out stuff.
  5441. return;
  5442. }
  5443. if (MCPU.empty() && McpuDefault)
  5444. MCPU = McpuDefault;
  5445. std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
  5446. std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
  5447. if (ThumbTarget)
  5448. ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
  5449. // Package up features to be passed to target/subtarget
  5450. std::string FeaturesStr;
  5451. if (MAttrs.size()) {
  5452. SubtargetFeatures Features;
  5453. for (unsigned i = 0; i != MAttrs.size(); ++i)
  5454. Features.AddFeature(MAttrs[i]);
  5455. FeaturesStr = Features.getString();
  5456. }
  5457. // Set up disassembler.
  5458. std::unique_ptr<const MCRegisterInfo> MRI(
  5459. TheTarget->createMCRegInfo(TripleName));
  5460. std::unique_ptr<const MCAsmInfo> AsmInfo(
  5461. TheTarget->createMCAsmInfo(*MRI, TripleName));
  5462. std::unique_ptr<const MCSubtargetInfo> STI(
  5463. TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
  5464. MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
  5465. std::unique_ptr<MCDisassembler> DisAsm(
  5466. TheTarget->createMCDisassembler(*STI, Ctx));
  5467. std::unique_ptr<MCSymbolizer> Symbolizer;
  5468. struct DisassembleInfo SymbolizerInfo;
  5469. std::unique_ptr<MCRelocationInfo> RelInfo(
  5470. TheTarget->createMCRelocationInfo(TripleName, Ctx));
  5471. if (RelInfo) {
  5472. Symbolizer.reset(TheTarget->createMCSymbolizer(
  5473. TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
  5474. &SymbolizerInfo, &Ctx, std::move(RelInfo)));
  5475. DisAsm->setSymbolizer(std::move(Symbolizer));
  5476. }
  5477. int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
  5478. std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
  5479. Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
  5480. // Set the display preference for hex vs. decimal immediates.
  5481. IP->setPrintImmHex(PrintImmHex);
  5482. // Comment stream and backing vector.
  5483. SmallString<128> CommentsToEmit;
  5484. raw_svector_ostream CommentStream(CommentsToEmit);
  5485. // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
  5486. // if it is done then arm64 comments for string literals don't get printed
  5487. // and some constant get printed instead and not setting it causes intel
  5488. // (32-bit and 64-bit) comments printed with different spacing before the
  5489. // comment causing different diffs with the 'C' disassembler library API.
  5490. // IP->setCommentStream(CommentStream);
  5491. if (!AsmInfo || !STI || !DisAsm || !IP) {
  5492. errs() << "error: couldn't initialize disassembler for target "
  5493. << TripleName << '\n';
  5494. return;
  5495. }
  5496. // Set up thumb disassembler.
  5497. std::unique_ptr<const MCRegisterInfo> ThumbMRI;
  5498. std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
  5499. std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
  5500. std::unique_ptr<MCDisassembler> ThumbDisAsm;
  5501. std::unique_ptr<MCInstPrinter> ThumbIP;
  5502. std::unique_ptr<MCContext> ThumbCtx;
  5503. std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
  5504. struct DisassembleInfo ThumbSymbolizerInfo;
  5505. std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
  5506. if (ThumbTarget) {
  5507. ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
  5508. ThumbAsmInfo.reset(
  5509. ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
  5510. ThumbSTI.reset(
  5511. ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
  5512. ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
  5513. ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
  5514. MCContext *PtrThumbCtx = ThumbCtx.get();
  5515. ThumbRelInfo.reset(
  5516. ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
  5517. if (ThumbRelInfo) {
  5518. ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
  5519. ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
  5520. &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
  5521. ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
  5522. }
  5523. int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
  5524. ThumbIP.reset(ThumbTarget->createMCInstPrinter(
  5525. Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
  5526. *ThumbInstrInfo, *ThumbMRI));
  5527. // Set the display preference for hex vs. decimal immediates.
  5528. ThumbIP->setPrintImmHex(PrintImmHex);
  5529. }
  5530. if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
  5531. errs() << "error: couldn't initialize disassembler for target "
  5532. << ThumbTripleName << '\n';
  5533. return;
  5534. }
  5535. MachO::mach_header Header = MachOOF->getHeader();
  5536. // FIXME: Using the -cfg command line option, this code used to be able to
  5537. // annotate relocations with the referenced symbol's name, and if this was
  5538. // inside a __[cf]string section, the data it points to. This is now replaced
  5539. // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
  5540. std::vector<SectionRef> Sections;
  5541. std::vector<SymbolRef> Symbols;
  5542. SmallVector<uint64_t, 8> FoundFns;
  5543. uint64_t BaseSegmentAddress;
  5544. getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
  5545. BaseSegmentAddress);
  5546. // Sort the symbols by address, just in case they didn't come in that way.
  5547. std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
  5548. // Build a data in code table that is sorted on by the address of each entry.
  5549. uint64_t BaseAddress = 0;
  5550. if (Header.filetype == MachO::MH_OBJECT)
  5551. BaseAddress = Sections[0].getAddress();
  5552. else
  5553. BaseAddress = BaseSegmentAddress;
  5554. DiceTable Dices;
  5555. for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
  5556. DI != DE; ++DI) {
  5557. uint32_t Offset;
  5558. DI->getOffset(Offset);
  5559. Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
  5560. }
  5561. array_pod_sort(Dices.begin(), Dices.end());
  5562. #ifndef NDEBUG
  5563. raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
  5564. #else
  5565. raw_ostream &DebugOut = nulls();
  5566. #endif
  5567. std::unique_ptr<DIContext> diContext;
  5568. ObjectFile *DbgObj = MachOOF;
  5569. // Try to find debug info and set up the DIContext for it.
  5570. if (UseDbg) {
  5571. // A separate DSym file path was specified, parse it as a macho file,
  5572. // get the sections and supply it to the section name parsing machinery.
  5573. if (!DSYMFile.empty()) {
  5574. ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
  5575. MemoryBuffer::getFileOrSTDIN(DSYMFile);
  5576. if (std::error_code EC = BufOrErr.getError()) {
  5577. errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
  5578. return;
  5579. }
  5580. DbgObj =
  5581. ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
  5582. .get()
  5583. .release();
  5584. }
  5585. // Setup the DIContext
  5586. diContext.reset(new DWARFContextInMemory(*DbgObj));
  5587. }
  5588. if (DumpSections.size() == 0)
  5589. outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
  5590. for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
  5591. StringRef SectName;
  5592. if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
  5593. continue;
  5594. DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
  5595. StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
  5596. if (SegmentName != DisSegName)
  5597. continue;
  5598. StringRef BytesStr;
  5599. Sections[SectIdx].getContents(BytesStr);
  5600. ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
  5601. BytesStr.size());
  5602. uint64_t SectAddress = Sections[SectIdx].getAddress();
  5603. bool symbolTableWorked = false;
  5604. // Parse relocations.
  5605. std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
  5606. for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
  5607. uint64_t RelocOffset = Reloc.getOffset();
  5608. uint64_t SectionAddress = Sections[SectIdx].getAddress();
  5609. RelocOffset -= SectionAddress;
  5610. symbol_iterator RelocSym = Reloc.getSymbol();
  5611. Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
  5612. }
  5613. array_pod_sort(Relocs.begin(), Relocs.end());
  5614. // Create a map of symbol addresses to symbol names for use by
  5615. // the SymbolizerSymbolLookUp() routine.
  5616. SymbolAddressMap AddrMap;
  5617. bool DisSymNameFound = false;
  5618. for (const SymbolRef &Symbol : MachOOF->symbols()) {
  5619. SymbolRef::Type ST = Symbol.getType();
  5620. if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
  5621. ST == SymbolRef::ST_Other) {
  5622. uint64_t Address = Symbol.getValue();
  5623. ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
  5624. if (std::error_code EC = SymNameOrErr.getError())
  5625. report_fatal_error(EC.message());
  5626. StringRef SymName = *SymNameOrErr;
  5627. AddrMap[Address] = SymName;
  5628. if (!DisSymName.empty() && DisSymName == SymName)
  5629. DisSymNameFound = true;
  5630. }
  5631. }
  5632. if (!DisSymName.empty() && !DisSymNameFound) {
  5633. outs() << "Can't find -dis-symname: " << DisSymName << "\n";
  5634. return;
  5635. }
  5636. // Set up the block of info used by the Symbolizer call backs.
  5637. SymbolizerInfo.verbose = !NoSymbolicOperands;
  5638. SymbolizerInfo.O = MachOOF;
  5639. SymbolizerInfo.S = Sections[SectIdx];
  5640. SymbolizerInfo.AddrMap = &AddrMap;
  5641. SymbolizerInfo.Sections = &Sections;
  5642. SymbolizerInfo.class_name = nullptr;
  5643. SymbolizerInfo.selector_name = nullptr;
  5644. SymbolizerInfo.method = nullptr;
  5645. SymbolizerInfo.demangled_name = nullptr;
  5646. SymbolizerInfo.bindtable = nullptr;
  5647. SymbolizerInfo.adrp_addr = 0;
  5648. SymbolizerInfo.adrp_inst = 0;
  5649. // Same for the ThumbSymbolizer
  5650. ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
  5651. ThumbSymbolizerInfo.O = MachOOF;
  5652. ThumbSymbolizerInfo.S = Sections[SectIdx];
  5653. ThumbSymbolizerInfo.AddrMap = &AddrMap;
  5654. ThumbSymbolizerInfo.Sections = &Sections;
  5655. ThumbSymbolizerInfo.class_name = nullptr;
  5656. ThumbSymbolizerInfo.selector_name = nullptr;
  5657. ThumbSymbolizerInfo.method = nullptr;
  5658. ThumbSymbolizerInfo.demangled_name = nullptr;
  5659. ThumbSymbolizerInfo.bindtable = nullptr;
  5660. ThumbSymbolizerInfo.adrp_addr = 0;
  5661. ThumbSymbolizerInfo.adrp_inst = 0;
  5662. // Disassemble symbol by symbol.
  5663. for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
  5664. ErrorOr<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
  5665. if (std::error_code EC = SymNameOrErr.getError())
  5666. report_fatal_error(EC.message());
  5667. StringRef SymName = *SymNameOrErr;
  5668. SymbolRef::Type ST = Symbols[SymIdx].getType();
  5669. if (ST != SymbolRef::ST_Function)
  5670. continue;
  5671. // Make sure the symbol is defined in this section.
  5672. bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
  5673. if (!containsSym)
  5674. continue;
  5675. // If we are only disassembling one symbol see if this is that symbol.
  5676. if (!DisSymName.empty() && DisSymName != SymName)
  5677. continue;
  5678. // Start at the address of the symbol relative to the section's address.
  5679. uint64_t Start = Symbols[SymIdx].getValue();
  5680. uint64_t SectionAddress = Sections[SectIdx].getAddress();
  5681. Start -= SectionAddress;
  5682. // Stop disassembling either at the beginning of the next symbol or at
  5683. // the end of the section.
  5684. bool containsNextSym = false;
  5685. uint64_t NextSym = 0;
  5686. uint64_t NextSymIdx = SymIdx + 1;
  5687. while (Symbols.size() > NextSymIdx) {
  5688. SymbolRef::Type NextSymType = Symbols[NextSymIdx].getType();
  5689. if (NextSymType == SymbolRef::ST_Function) {
  5690. containsNextSym =
  5691. Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
  5692. NextSym = Symbols[NextSymIdx].getValue();
  5693. NextSym -= SectionAddress;
  5694. break;
  5695. }
  5696. ++NextSymIdx;
  5697. }
  5698. uint64_t SectSize = Sections[SectIdx].getSize();
  5699. uint64_t End = containsNextSym ? NextSym : SectSize;
  5700. uint64_t Size;
  5701. symbolTableWorked = true;
  5702. DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
  5703. bool isThumb =
  5704. (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
  5705. outs() << SymName << ":\n";
  5706. DILineInfo lastLine;
  5707. for (uint64_t Index = Start; Index < End; Index += Size) {
  5708. MCInst Inst;
  5709. uint64_t PC = SectAddress + Index;
  5710. if (!NoLeadingAddr) {
  5711. if (FullLeadingAddr) {
  5712. if (MachOOF->is64Bit())
  5713. outs() << format("%016" PRIx64, PC);
  5714. else
  5715. outs() << format("%08" PRIx64, PC);
  5716. } else {
  5717. outs() << format("%8" PRIx64 ":", PC);
  5718. }
  5719. }
  5720. if (!NoShowRawInsn)
  5721. outs() << "\t";
  5722. // Check the data in code table here to see if this is data not an
  5723. // instruction to be disassembled.
  5724. DiceTable Dice;
  5725. Dice.push_back(std::make_pair(PC, DiceRef()));
  5726. dice_table_iterator DTI =
  5727. std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
  5728. compareDiceTableEntries);
  5729. if (DTI != Dices.end()) {
  5730. uint16_t Length;
  5731. DTI->second.getLength(Length);
  5732. uint16_t Kind;
  5733. DTI->second.getKind(Kind);
  5734. Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
  5735. if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
  5736. (PC == (DTI->first + Length - 1)) && (Length & 1))
  5737. Size++;
  5738. continue;
  5739. }
  5740. SmallVector<char, 64> AnnotationsBytes;
  5741. raw_svector_ostream Annotations(AnnotationsBytes);
  5742. bool gotInst;
  5743. if (isThumb)
  5744. gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
  5745. PC, DebugOut, Annotations);
  5746. else
  5747. gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
  5748. DebugOut, Annotations);
  5749. if (gotInst) {
  5750. if (!NoShowRawInsn) {
  5751. dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size), outs());
  5752. }
  5753. formatted_raw_ostream FormattedOS(outs());
  5754. Annotations.flush();
  5755. StringRef AnnotationsStr = Annotations.str();
  5756. if (isThumb)
  5757. ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
  5758. else
  5759. IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
  5760. emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
  5761. // Print debug info.
  5762. if (diContext) {
  5763. DILineInfo dli = diContext->getLineInfoForAddress(PC);
  5764. // Print valid line info if it changed.
  5765. if (dli != lastLine && dli.Line != 0)
  5766. outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
  5767. << dli.Column;
  5768. lastLine = dli;
  5769. }
  5770. outs() << "\n";
  5771. } else {
  5772. unsigned int Arch = MachOOF->getArch();
  5773. if (Arch == Triple::x86_64 || Arch == Triple::x86) {
  5774. outs() << format("\t.byte 0x%02x #bad opcode\n",
  5775. *(Bytes.data() + Index) & 0xff);
  5776. Size = 1; // skip exactly one illegible byte and move on.
  5777. } else if (Arch == Triple::aarch64) {
  5778. uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
  5779. (*(Bytes.data() + Index + 1) & 0xff) << 8 |
  5780. (*(Bytes.data() + Index + 2) & 0xff) << 16 |
  5781. (*(Bytes.data() + Index + 3) & 0xff) << 24;
  5782. outs() << format("\t.long\t0x%08x\n", opcode);
  5783. Size = 4;
  5784. } else {
  5785. errs() << "llvm-objdump: warning: invalid instruction encoding\n";
  5786. if (Size == 0)
  5787. Size = 1; // skip illegible bytes
  5788. }
  5789. }
  5790. }
  5791. }
  5792. if (!symbolTableWorked) {
  5793. // Reading the symbol table didn't work, disassemble the whole section.
  5794. uint64_t SectAddress = Sections[SectIdx].getAddress();
  5795. uint64_t SectSize = Sections[SectIdx].getSize();
  5796. uint64_t InstSize;
  5797. for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
  5798. MCInst Inst;
  5799. uint64_t PC = SectAddress + Index;
  5800. if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
  5801. DebugOut, nulls())) {
  5802. if (!NoLeadingAddr) {
  5803. if (FullLeadingAddr) {
  5804. if (MachOOF->is64Bit())
  5805. outs() << format("%016" PRIx64, PC);
  5806. else
  5807. outs() << format("%08" PRIx64, PC);
  5808. } else {
  5809. outs() << format("%8" PRIx64 ":", PC);
  5810. }
  5811. }
  5812. if (!NoShowRawInsn) {
  5813. outs() << "\t";
  5814. dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize), outs());
  5815. }
  5816. IP->printInst(&Inst, outs(), "", *STI);
  5817. outs() << "\n";
  5818. } else {
  5819. unsigned int Arch = MachOOF->getArch();
  5820. if (Arch == Triple::x86_64 || Arch == Triple::x86) {
  5821. outs() << format("\t.byte 0x%02x #bad opcode\n",
  5822. *(Bytes.data() + Index) & 0xff);
  5823. InstSize = 1; // skip exactly one illegible byte and move on.
  5824. } else {
  5825. errs() << "llvm-objdump: warning: invalid instruction encoding\n";
  5826. if (InstSize == 0)
  5827. InstSize = 1; // skip illegible bytes
  5828. }
  5829. }
  5830. }
  5831. }
  5832. // The TripleName's need to be reset if we are called again for a different
  5833. // archtecture.
  5834. TripleName = "";
  5835. ThumbTripleName = "";
  5836. if (SymbolizerInfo.method != nullptr)
  5837. free(SymbolizerInfo.method);
  5838. if (SymbolizerInfo.demangled_name != nullptr)
  5839. free(SymbolizerInfo.demangled_name);
  5840. if (SymbolizerInfo.bindtable != nullptr)
  5841. delete SymbolizerInfo.bindtable;
  5842. if (ThumbSymbolizerInfo.method != nullptr)
  5843. free(ThumbSymbolizerInfo.method);
  5844. if (ThumbSymbolizerInfo.demangled_name != nullptr)
  5845. free(ThumbSymbolizerInfo.demangled_name);
  5846. if (ThumbSymbolizerInfo.bindtable != nullptr)
  5847. delete ThumbSymbolizerInfo.bindtable;
  5848. }
  5849. }
  5850. //===----------------------------------------------------------------------===//
  5851. // __compact_unwind section dumping
  5852. //===----------------------------------------------------------------------===//
  5853. namespace {
  5854. template <typename T> static uint64_t readNext(const char *&Buf) {
  5855. using llvm::support::little;
  5856. using llvm::support::unaligned;
  5857. uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
  5858. Buf += sizeof(T);
  5859. return Val;
  5860. }
  5861. struct CompactUnwindEntry {
  5862. uint32_t OffsetInSection;
  5863. uint64_t FunctionAddr;
  5864. uint32_t Length;
  5865. uint32_t CompactEncoding;
  5866. uint64_t PersonalityAddr;
  5867. uint64_t LSDAAddr;
  5868. RelocationRef FunctionReloc;
  5869. RelocationRef PersonalityReloc;
  5870. RelocationRef LSDAReloc;
  5871. CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
  5872. : OffsetInSection(Offset) {
  5873. if (Is64)
  5874. read<uint64_t>(Contents.data() + Offset);
  5875. else
  5876. read<uint32_t>(Contents.data() + Offset);
  5877. }
  5878. private:
  5879. template <typename UIntPtr> void read(const char *Buf) {
  5880. FunctionAddr = readNext<UIntPtr>(Buf);
  5881. Length = readNext<uint32_t>(Buf);
  5882. CompactEncoding = readNext<uint32_t>(Buf);
  5883. PersonalityAddr = readNext<UIntPtr>(Buf);
  5884. LSDAAddr = readNext<UIntPtr>(Buf);
  5885. }
  5886. };
  5887. }
  5888. /// Given a relocation from __compact_unwind, consisting of the RelocationRef
  5889. /// and data being relocated, determine the best base Name and Addend to use for
  5890. /// display purposes.
  5891. ///
  5892. /// 1. An Extern relocation will directly reference a symbol (and the data is
  5893. /// then already an addend), so use that.
  5894. /// 2. Otherwise the data is an offset in the object file's layout; try to find
  5895. // a symbol before it in the same section, and use the offset from there.
  5896. /// 3. Finally, if all that fails, fall back to an offset from the start of the
  5897. /// referenced section.
  5898. static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
  5899. std::map<uint64_t, SymbolRef> &Symbols,
  5900. const RelocationRef &Reloc, uint64_t Addr,
  5901. StringRef &Name, uint64_t &Addend) {
  5902. if (Reloc.getSymbol() != Obj->symbol_end()) {
  5903. ErrorOr<StringRef> NameOrErr = Reloc.getSymbol()->getName();
  5904. if (std::error_code EC = NameOrErr.getError())
  5905. report_fatal_error(EC.message());
  5906. Name = *NameOrErr;
  5907. Addend = Addr;
  5908. return;
  5909. }
  5910. auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
  5911. SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
  5912. uint64_t SectionAddr = RelocSection.getAddress();
  5913. auto Sym = Symbols.upper_bound(Addr);
  5914. if (Sym == Symbols.begin()) {
  5915. // The first symbol in the object is after this reference, the best we can
  5916. // do is section-relative notation.
  5917. RelocSection.getName(Name);
  5918. Addend = Addr - SectionAddr;
  5919. return;
  5920. }
  5921. // Go back one so that SymbolAddress <= Addr.
  5922. --Sym;
  5923. section_iterator SymSection = Obj->section_end();
  5924. Sym->second.getSection(SymSection);
  5925. if (RelocSection == *SymSection) {
  5926. // There's a valid symbol in the same section before this reference.
  5927. ErrorOr<StringRef> NameOrErr = Sym->second.getName();
  5928. if (std::error_code EC = NameOrErr.getError())
  5929. report_fatal_error(EC.message());
  5930. Name = *NameOrErr;
  5931. Addend = Addr - Sym->first;
  5932. return;
  5933. }
  5934. // There is a symbol before this reference, but it's in a different
  5935. // section. Probably not helpful to mention it, so use the section name.
  5936. RelocSection.getName(Name);
  5937. Addend = Addr - SectionAddr;
  5938. }
  5939. static void printUnwindRelocDest(const MachOObjectFile *Obj,
  5940. std::map<uint64_t, SymbolRef> &Symbols,
  5941. const RelocationRef &Reloc, uint64_t Addr) {
  5942. StringRef Name;
  5943. uint64_t Addend;
  5944. if (!Reloc.getObject())
  5945. return;
  5946. findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
  5947. outs() << Name;
  5948. if (Addend)
  5949. outs() << " + " << format("0x%" PRIx64, Addend);
  5950. }
  5951. static void
  5952. printMachOCompactUnwindSection(const MachOObjectFile *Obj,
  5953. std::map<uint64_t, SymbolRef> &Symbols,
  5954. const SectionRef &CompactUnwind) {
  5955. assert(Obj->isLittleEndian() &&
  5956. "There should not be a big-endian .o with __compact_unwind");
  5957. bool Is64 = Obj->is64Bit();
  5958. uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
  5959. uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
  5960. StringRef Contents;
  5961. CompactUnwind.getContents(Contents);
  5962. SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
  5963. // First populate the initial raw offsets, encodings and so on from the entry.
  5964. for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
  5965. CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
  5966. CompactUnwinds.push_back(Entry);
  5967. }
  5968. // Next we need to look at the relocations to find out what objects are
  5969. // actually being referred to.
  5970. for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
  5971. uint64_t RelocAddress = Reloc.getOffset();
  5972. uint32_t EntryIdx = RelocAddress / EntrySize;
  5973. uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
  5974. CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
  5975. if (OffsetInEntry == 0)
  5976. Entry.FunctionReloc = Reloc;
  5977. else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
  5978. Entry.PersonalityReloc = Reloc;
  5979. else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
  5980. Entry.LSDAReloc = Reloc;
  5981. else
  5982. llvm_unreachable("Unexpected relocation in __compact_unwind section");
  5983. }
  5984. // Finally, we're ready to print the data we've gathered.
  5985. outs() << "Contents of __compact_unwind section:\n";
  5986. for (auto &Entry : CompactUnwinds) {
  5987. outs() << " Entry at offset "
  5988. << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
  5989. // 1. Start of the region this entry applies to.
  5990. outs() << " start: " << format("0x%" PRIx64,
  5991. Entry.FunctionAddr) << ' ';
  5992. printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
  5993. outs() << '\n';
  5994. // 2. Length of the region this entry applies to.
  5995. outs() << " length: " << format("0x%" PRIx32, Entry.Length)
  5996. << '\n';
  5997. // 3. The 32-bit compact encoding.
  5998. outs() << " compact encoding: "
  5999. << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
  6000. // 4. The personality function, if present.
  6001. if (Entry.PersonalityReloc.getObject()) {
  6002. outs() << " personality function: "
  6003. << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
  6004. printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
  6005. Entry.PersonalityAddr);
  6006. outs() << '\n';
  6007. }
  6008. // 5. This entry's language-specific data area.
  6009. if (Entry.LSDAReloc.getObject()) {
  6010. outs() << " LSDA: " << format("0x%" PRIx64,
  6011. Entry.LSDAAddr) << ' ';
  6012. printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
  6013. outs() << '\n';
  6014. }
  6015. }
  6016. }
  6017. //===----------------------------------------------------------------------===//
  6018. // __unwind_info section dumping
  6019. //===----------------------------------------------------------------------===//
  6020. static void printRegularSecondLevelUnwindPage(const char *PageStart) {
  6021. const char *Pos = PageStart;
  6022. uint32_t Kind = readNext<uint32_t>(Pos);
  6023. (void)Kind;
  6024. assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
  6025. uint16_t EntriesStart = readNext<uint16_t>(Pos);
  6026. uint16_t NumEntries = readNext<uint16_t>(Pos);
  6027. Pos = PageStart + EntriesStart;
  6028. for (unsigned i = 0; i < NumEntries; ++i) {
  6029. uint32_t FunctionOffset = readNext<uint32_t>(Pos);
  6030. uint32_t Encoding = readNext<uint32_t>(Pos);
  6031. outs() << " [" << i << "]: "
  6032. << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
  6033. << ", "
  6034. << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
  6035. }
  6036. }
  6037. static void printCompressedSecondLevelUnwindPage(
  6038. const char *PageStart, uint32_t FunctionBase,
  6039. const SmallVectorImpl<uint32_t> &CommonEncodings) {
  6040. const char *Pos = PageStart;
  6041. uint32_t Kind = readNext<uint32_t>(Pos);
  6042. (void)Kind;
  6043. assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
  6044. uint16_t EntriesStart = readNext<uint16_t>(Pos);
  6045. uint16_t NumEntries = readNext<uint16_t>(Pos);
  6046. uint16_t EncodingsStart = readNext<uint16_t>(Pos);
  6047. readNext<uint16_t>(Pos);
  6048. const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
  6049. PageStart + EncodingsStart);
  6050. Pos = PageStart + EntriesStart;
  6051. for (unsigned i = 0; i < NumEntries; ++i) {
  6052. uint32_t Entry = readNext<uint32_t>(Pos);
  6053. uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
  6054. uint32_t EncodingIdx = Entry >> 24;
  6055. uint32_t Encoding;
  6056. if (EncodingIdx < CommonEncodings.size())
  6057. Encoding = CommonEncodings[EncodingIdx];
  6058. else
  6059. Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
  6060. outs() << " [" << i << "]: "
  6061. << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
  6062. << ", "
  6063. << "encoding[" << EncodingIdx
  6064. << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
  6065. }
  6066. }
  6067. static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
  6068. std::map<uint64_t, SymbolRef> &Symbols,
  6069. const SectionRef &UnwindInfo) {
  6070. assert(Obj->isLittleEndian() &&
  6071. "There should not be a big-endian .o with __unwind_info");
  6072. outs() << "Contents of __unwind_info section:\n";
  6073. StringRef Contents;
  6074. UnwindInfo.getContents(Contents);
  6075. const char *Pos = Contents.data();
  6076. //===----------------------------------
  6077. // Section header
  6078. //===----------------------------------
  6079. uint32_t Version = readNext<uint32_t>(Pos);
  6080. outs() << " Version: "
  6081. << format("0x%" PRIx32, Version) << '\n';
  6082. assert(Version == 1 && "only understand version 1");
  6083. uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
  6084. outs() << " Common encodings array section offset: "
  6085. << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
  6086. uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
  6087. outs() << " Number of common encodings in array: "
  6088. << format("0x%" PRIx32, NumCommonEncodings) << '\n';
  6089. uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
  6090. outs() << " Personality function array section offset: "
  6091. << format("0x%" PRIx32, PersonalitiesStart) << '\n';
  6092. uint32_t NumPersonalities = readNext<uint32_t>(Pos);
  6093. outs() << " Number of personality functions in array: "
  6094. << format("0x%" PRIx32, NumPersonalities) << '\n';
  6095. uint32_t IndicesStart = readNext<uint32_t>(Pos);
  6096. outs() << " Index array section offset: "
  6097. << format("0x%" PRIx32, IndicesStart) << '\n';
  6098. uint32_t NumIndices = readNext<uint32_t>(Pos);
  6099. outs() << " Number of indices in array: "
  6100. << format("0x%" PRIx32, NumIndices) << '\n';
  6101. //===----------------------------------
  6102. // A shared list of common encodings
  6103. //===----------------------------------
  6104. // These occupy indices in the range [0, N] whenever an encoding is referenced
  6105. // from a compressed 2nd level index table. In practice the linker only
  6106. // creates ~128 of these, so that indices are available to embed encodings in
  6107. // the 2nd level index.
  6108. SmallVector<uint32_t, 64> CommonEncodings;
  6109. outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
  6110. Pos = Contents.data() + CommonEncodingsStart;
  6111. for (unsigned i = 0; i < NumCommonEncodings; ++i) {
  6112. uint32_t Encoding = readNext<uint32_t>(Pos);
  6113. CommonEncodings.push_back(Encoding);
  6114. outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
  6115. << '\n';
  6116. }
  6117. //===----------------------------------
  6118. // Personality functions used in this executable
  6119. //===----------------------------------
  6120. // There should be only a handful of these (one per source language,
  6121. // roughly). Particularly since they only get 2 bits in the compact encoding.
  6122. outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
  6123. Pos = Contents.data() + PersonalitiesStart;
  6124. for (unsigned i = 0; i < NumPersonalities; ++i) {
  6125. uint32_t PersonalityFn = readNext<uint32_t>(Pos);
  6126. outs() << " personality[" << i + 1
  6127. << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
  6128. }
  6129. //===----------------------------------
  6130. // The level 1 index entries
  6131. //===----------------------------------
  6132. // These specify an approximate place to start searching for the more detailed
  6133. // information, sorted by PC.
  6134. struct IndexEntry {
  6135. uint32_t FunctionOffset;
  6136. uint32_t SecondLevelPageStart;
  6137. uint32_t LSDAStart;
  6138. };
  6139. SmallVector<IndexEntry, 4> IndexEntries;
  6140. outs() << " Top level indices: (count = " << NumIndices << ")\n";
  6141. Pos = Contents.data() + IndicesStart;
  6142. for (unsigned i = 0; i < NumIndices; ++i) {
  6143. IndexEntry Entry;
  6144. Entry.FunctionOffset = readNext<uint32_t>(Pos);
  6145. Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
  6146. Entry.LSDAStart = readNext<uint32_t>(Pos);
  6147. IndexEntries.push_back(Entry);
  6148. outs() << " [" << i << "]: "
  6149. << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
  6150. << ", "
  6151. << "2nd level page offset="
  6152. << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
  6153. << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
  6154. }
  6155. //===----------------------------------
  6156. // Next come the LSDA tables
  6157. //===----------------------------------
  6158. // The LSDA layout is rather implicit: it's a contiguous array of entries from
  6159. // the first top-level index's LSDAOffset to the last (sentinel).
  6160. outs() << " LSDA descriptors:\n";
  6161. Pos = Contents.data() + IndexEntries[0].LSDAStart;
  6162. int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
  6163. (2 * sizeof(uint32_t));
  6164. for (int i = 0; i < NumLSDAs; ++i) {
  6165. uint32_t FunctionOffset = readNext<uint32_t>(Pos);
  6166. uint32_t LSDAOffset = readNext<uint32_t>(Pos);
  6167. outs() << " [" << i << "]: "
  6168. << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
  6169. << ", "
  6170. << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
  6171. }
  6172. //===----------------------------------
  6173. // Finally, the 2nd level indices
  6174. //===----------------------------------
  6175. // Generally these are 4K in size, and have 2 possible forms:
  6176. // + Regular stores up to 511 entries with disparate encodings
  6177. // + Compressed stores up to 1021 entries if few enough compact encoding
  6178. // values are used.
  6179. outs() << " Second level indices:\n";
  6180. for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
  6181. // The final sentinel top-level index has no associated 2nd level page
  6182. if (IndexEntries[i].SecondLevelPageStart == 0)
  6183. break;
  6184. outs() << " Second level index[" << i << "]: "
  6185. << "offset in section="
  6186. << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
  6187. << ", "
  6188. << "base function offset="
  6189. << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
  6190. Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
  6191. uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
  6192. if (Kind == 2)
  6193. printRegularSecondLevelUnwindPage(Pos);
  6194. else if (Kind == 3)
  6195. printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
  6196. CommonEncodings);
  6197. else
  6198. llvm_unreachable("Do not know how to print this kind of 2nd level page");
  6199. }
  6200. }
  6201. void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
  6202. std::map<uint64_t, SymbolRef> Symbols;
  6203. for (const SymbolRef &SymRef : Obj->symbols()) {
  6204. // Discard any undefined or absolute symbols. They're not going to take part
  6205. // in the convenience lookup for unwind info and just take up resources.
  6206. section_iterator Section = Obj->section_end();
  6207. SymRef.getSection(Section);
  6208. if (Section == Obj->section_end())
  6209. continue;
  6210. uint64_t Addr = SymRef.getValue();
  6211. Symbols.insert(std::make_pair(Addr, SymRef));
  6212. }
  6213. for (const SectionRef &Section : Obj->sections()) {
  6214. StringRef SectName;
  6215. Section.getName(SectName);
  6216. if (SectName == "__compact_unwind")
  6217. printMachOCompactUnwindSection(Obj, Symbols, Section);
  6218. else if (SectName == "__unwind_info")
  6219. printMachOUnwindInfoSection(Obj, Symbols, Section);
  6220. else if (SectName == "__eh_frame")
  6221. outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
  6222. }
  6223. }
  6224. static void PrintMachHeader(uint32_t magic, uint32_t cputype,
  6225. uint32_t cpusubtype, uint32_t filetype,
  6226. uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
  6227. bool verbose) {
  6228. outs() << "Mach header\n";
  6229. outs() << " magic cputype cpusubtype caps filetype ncmds "
  6230. "sizeofcmds flags\n";
  6231. if (verbose) {
  6232. if (magic == MachO::MH_MAGIC)
  6233. outs() << " MH_MAGIC";
  6234. else if (magic == MachO::MH_MAGIC_64)
  6235. outs() << "MH_MAGIC_64";
  6236. else
  6237. outs() << format(" 0x%08" PRIx32, magic);
  6238. switch (cputype) {
  6239. case MachO::CPU_TYPE_I386:
  6240. outs() << " I386";
  6241. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6242. case MachO::CPU_SUBTYPE_I386_ALL:
  6243. outs() << " ALL";
  6244. break;
  6245. default:
  6246. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6247. break;
  6248. }
  6249. break;
  6250. case MachO::CPU_TYPE_X86_64:
  6251. outs() << " X86_64";
  6252. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6253. case MachO::CPU_SUBTYPE_X86_64_ALL:
  6254. outs() << " ALL";
  6255. break;
  6256. case MachO::CPU_SUBTYPE_X86_64_H:
  6257. outs() << " Haswell";
  6258. break;
  6259. default:
  6260. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6261. break;
  6262. }
  6263. break;
  6264. case MachO::CPU_TYPE_ARM:
  6265. outs() << " ARM";
  6266. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6267. case MachO::CPU_SUBTYPE_ARM_ALL:
  6268. outs() << " ALL";
  6269. break;
  6270. case MachO::CPU_SUBTYPE_ARM_V4T:
  6271. outs() << " V4T";
  6272. break;
  6273. case MachO::CPU_SUBTYPE_ARM_V5TEJ:
  6274. outs() << " V5TEJ";
  6275. break;
  6276. case MachO::CPU_SUBTYPE_ARM_XSCALE:
  6277. outs() << " XSCALE";
  6278. break;
  6279. case MachO::CPU_SUBTYPE_ARM_V6:
  6280. outs() << " V6";
  6281. break;
  6282. case MachO::CPU_SUBTYPE_ARM_V6M:
  6283. outs() << " V6M";
  6284. break;
  6285. case MachO::CPU_SUBTYPE_ARM_V7:
  6286. outs() << " V7";
  6287. break;
  6288. case MachO::CPU_SUBTYPE_ARM_V7EM:
  6289. outs() << " V7EM";
  6290. break;
  6291. case MachO::CPU_SUBTYPE_ARM_V7K:
  6292. outs() << " V7K";
  6293. break;
  6294. case MachO::CPU_SUBTYPE_ARM_V7M:
  6295. outs() << " V7M";
  6296. break;
  6297. case MachO::CPU_SUBTYPE_ARM_V7S:
  6298. outs() << " V7S";
  6299. break;
  6300. default:
  6301. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6302. break;
  6303. }
  6304. break;
  6305. case MachO::CPU_TYPE_ARM64:
  6306. outs() << " ARM64";
  6307. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6308. case MachO::CPU_SUBTYPE_ARM64_ALL:
  6309. outs() << " ALL";
  6310. break;
  6311. default:
  6312. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6313. break;
  6314. }
  6315. break;
  6316. case MachO::CPU_TYPE_POWERPC:
  6317. outs() << " PPC";
  6318. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6319. case MachO::CPU_SUBTYPE_POWERPC_ALL:
  6320. outs() << " ALL";
  6321. break;
  6322. default:
  6323. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6324. break;
  6325. }
  6326. break;
  6327. case MachO::CPU_TYPE_POWERPC64:
  6328. outs() << " PPC64";
  6329. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6330. case MachO::CPU_SUBTYPE_POWERPC_ALL:
  6331. outs() << " ALL";
  6332. break;
  6333. default:
  6334. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6335. break;
  6336. }
  6337. break;
  6338. }
  6339. if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
  6340. outs() << " LIB64";
  6341. } else {
  6342. outs() << format(" 0x%02" PRIx32,
  6343. (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
  6344. }
  6345. switch (filetype) {
  6346. case MachO::MH_OBJECT:
  6347. outs() << " OBJECT";
  6348. break;
  6349. case MachO::MH_EXECUTE:
  6350. outs() << " EXECUTE";
  6351. break;
  6352. case MachO::MH_FVMLIB:
  6353. outs() << " FVMLIB";
  6354. break;
  6355. case MachO::MH_CORE:
  6356. outs() << " CORE";
  6357. break;
  6358. case MachO::MH_PRELOAD:
  6359. outs() << " PRELOAD";
  6360. break;
  6361. case MachO::MH_DYLIB:
  6362. outs() << " DYLIB";
  6363. break;
  6364. case MachO::MH_DYLIB_STUB:
  6365. outs() << " DYLIB_STUB";
  6366. break;
  6367. case MachO::MH_DYLINKER:
  6368. outs() << " DYLINKER";
  6369. break;
  6370. case MachO::MH_BUNDLE:
  6371. outs() << " BUNDLE";
  6372. break;
  6373. case MachO::MH_DSYM:
  6374. outs() << " DSYM";
  6375. break;
  6376. case MachO::MH_KEXT_BUNDLE:
  6377. outs() << " KEXTBUNDLE";
  6378. break;
  6379. default:
  6380. outs() << format(" %10u", filetype);
  6381. break;
  6382. }
  6383. outs() << format(" %5u", ncmds);
  6384. outs() << format(" %10u", sizeofcmds);
  6385. uint32_t f = flags;
  6386. if (f & MachO::MH_NOUNDEFS) {
  6387. outs() << " NOUNDEFS";
  6388. f &= ~MachO::MH_NOUNDEFS;
  6389. }
  6390. if (f & MachO::MH_INCRLINK) {
  6391. outs() << " INCRLINK";
  6392. f &= ~MachO::MH_INCRLINK;
  6393. }
  6394. if (f & MachO::MH_DYLDLINK) {
  6395. outs() << " DYLDLINK";
  6396. f &= ~MachO::MH_DYLDLINK;
  6397. }
  6398. if (f & MachO::MH_BINDATLOAD) {
  6399. outs() << " BINDATLOAD";
  6400. f &= ~MachO::MH_BINDATLOAD;
  6401. }
  6402. if (f & MachO::MH_PREBOUND) {
  6403. outs() << " PREBOUND";
  6404. f &= ~MachO::MH_PREBOUND;
  6405. }
  6406. if (f & MachO::MH_SPLIT_SEGS) {
  6407. outs() << " SPLIT_SEGS";
  6408. f &= ~MachO::MH_SPLIT_SEGS;
  6409. }
  6410. if (f & MachO::MH_LAZY_INIT) {
  6411. outs() << " LAZY_INIT";
  6412. f &= ~MachO::MH_LAZY_INIT;
  6413. }
  6414. if (f & MachO::MH_TWOLEVEL) {
  6415. outs() << " TWOLEVEL";
  6416. f &= ~MachO::MH_TWOLEVEL;
  6417. }
  6418. if (f & MachO::MH_FORCE_FLAT) {
  6419. outs() << " FORCE_FLAT";
  6420. f &= ~MachO::MH_FORCE_FLAT;
  6421. }
  6422. if (f & MachO::MH_NOMULTIDEFS) {
  6423. outs() << " NOMULTIDEFS";
  6424. f &= ~MachO::MH_NOMULTIDEFS;
  6425. }
  6426. if (f & MachO::MH_NOFIXPREBINDING) {
  6427. outs() << " NOFIXPREBINDING";
  6428. f &= ~MachO::MH_NOFIXPREBINDING;
  6429. }
  6430. if (f & MachO::MH_PREBINDABLE) {
  6431. outs() << " PREBINDABLE";
  6432. f &= ~MachO::MH_PREBINDABLE;
  6433. }
  6434. if (f & MachO::MH_ALLMODSBOUND) {
  6435. outs() << " ALLMODSBOUND";
  6436. f &= ~MachO::MH_ALLMODSBOUND;
  6437. }
  6438. if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
  6439. outs() << " SUBSECTIONS_VIA_SYMBOLS";
  6440. f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
  6441. }
  6442. if (f & MachO::MH_CANONICAL) {
  6443. outs() << " CANONICAL";
  6444. f &= ~MachO::MH_CANONICAL;
  6445. }
  6446. if (f & MachO::MH_WEAK_DEFINES) {
  6447. outs() << " WEAK_DEFINES";
  6448. f &= ~MachO::MH_WEAK_DEFINES;
  6449. }
  6450. if (f & MachO::MH_BINDS_TO_WEAK) {
  6451. outs() << " BINDS_TO_WEAK";
  6452. f &= ~MachO::MH_BINDS_TO_WEAK;
  6453. }
  6454. if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
  6455. outs() << " ALLOW_STACK_EXECUTION";
  6456. f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
  6457. }
  6458. if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
  6459. outs() << " DEAD_STRIPPABLE_DYLIB";
  6460. f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
  6461. }
  6462. if (f & MachO::MH_PIE) {
  6463. outs() << " PIE";
  6464. f &= ~MachO::MH_PIE;
  6465. }
  6466. if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
  6467. outs() << " NO_REEXPORTED_DYLIBS";
  6468. f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
  6469. }
  6470. if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
  6471. outs() << " MH_HAS_TLV_DESCRIPTORS";
  6472. f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
  6473. }
  6474. if (f & MachO::MH_NO_HEAP_EXECUTION) {
  6475. outs() << " MH_NO_HEAP_EXECUTION";
  6476. f &= ~MachO::MH_NO_HEAP_EXECUTION;
  6477. }
  6478. if (f & MachO::MH_APP_EXTENSION_SAFE) {
  6479. outs() << " APP_EXTENSION_SAFE";
  6480. f &= ~MachO::MH_APP_EXTENSION_SAFE;
  6481. }
  6482. if (f != 0 || flags == 0)
  6483. outs() << format(" 0x%08" PRIx32, f);
  6484. } else {
  6485. outs() << format(" 0x%08" PRIx32, magic);
  6486. outs() << format(" %7d", cputype);
  6487. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6488. outs() << format(" 0x%02" PRIx32,
  6489. (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
  6490. outs() << format(" %10u", filetype);
  6491. outs() << format(" %5u", ncmds);
  6492. outs() << format(" %10u", sizeofcmds);
  6493. outs() << format(" 0x%08" PRIx32, flags);
  6494. }
  6495. outs() << "\n";
  6496. }
  6497. static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
  6498. StringRef SegName, uint64_t vmaddr,
  6499. uint64_t vmsize, uint64_t fileoff,
  6500. uint64_t filesize, uint32_t maxprot,
  6501. uint32_t initprot, uint32_t nsects,
  6502. uint32_t flags, uint32_t object_size,
  6503. bool verbose) {
  6504. uint64_t expected_cmdsize;
  6505. if (cmd == MachO::LC_SEGMENT) {
  6506. outs() << " cmd LC_SEGMENT\n";
  6507. expected_cmdsize = nsects;
  6508. expected_cmdsize *= sizeof(struct MachO::section);
  6509. expected_cmdsize += sizeof(struct MachO::segment_command);
  6510. } else {
  6511. outs() << " cmd LC_SEGMENT_64\n";
  6512. expected_cmdsize = nsects;
  6513. expected_cmdsize *= sizeof(struct MachO::section_64);
  6514. expected_cmdsize += sizeof(struct MachO::segment_command_64);
  6515. }
  6516. outs() << " cmdsize " << cmdsize;
  6517. if (cmdsize != expected_cmdsize)
  6518. outs() << " Inconsistent size\n";
  6519. else
  6520. outs() << "\n";
  6521. outs() << " segname " << SegName << "\n";
  6522. if (cmd == MachO::LC_SEGMENT_64) {
  6523. outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
  6524. outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
  6525. } else {
  6526. outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
  6527. outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
  6528. }
  6529. outs() << " fileoff " << fileoff;
  6530. if (fileoff > object_size)
  6531. outs() << " (past end of file)\n";
  6532. else
  6533. outs() << "\n";
  6534. outs() << " filesize " << filesize;
  6535. if (fileoff + filesize > object_size)
  6536. outs() << " (past end of file)\n";
  6537. else
  6538. outs() << "\n";
  6539. if (verbose) {
  6540. if ((maxprot &
  6541. ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
  6542. MachO::VM_PROT_EXECUTE)) != 0)
  6543. outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
  6544. else {
  6545. if (maxprot & MachO::VM_PROT_READ)
  6546. outs() << " maxprot r";
  6547. else
  6548. outs() << " maxprot -";
  6549. if (maxprot & MachO::VM_PROT_WRITE)
  6550. outs() << "w";
  6551. else
  6552. outs() << "-";
  6553. if (maxprot & MachO::VM_PROT_EXECUTE)
  6554. outs() << "x\n";
  6555. else
  6556. outs() << "-\n";
  6557. }
  6558. if ((initprot &
  6559. ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
  6560. MachO::VM_PROT_EXECUTE)) != 0)
  6561. outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
  6562. else {
  6563. if (initprot & MachO::VM_PROT_READ)
  6564. outs() << " initprot r";
  6565. else
  6566. outs() << " initprot -";
  6567. if (initprot & MachO::VM_PROT_WRITE)
  6568. outs() << "w";
  6569. else
  6570. outs() << "-";
  6571. if (initprot & MachO::VM_PROT_EXECUTE)
  6572. outs() << "x\n";
  6573. else
  6574. outs() << "-\n";
  6575. }
  6576. } else {
  6577. outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
  6578. outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
  6579. }
  6580. outs() << " nsects " << nsects << "\n";
  6581. if (verbose) {
  6582. outs() << " flags";
  6583. if (flags == 0)
  6584. outs() << " (none)\n";
  6585. else {
  6586. if (flags & MachO::SG_HIGHVM) {
  6587. outs() << " HIGHVM";
  6588. flags &= ~MachO::SG_HIGHVM;
  6589. }
  6590. if (flags & MachO::SG_FVMLIB) {
  6591. outs() << " FVMLIB";
  6592. flags &= ~MachO::SG_FVMLIB;
  6593. }
  6594. if (flags & MachO::SG_NORELOC) {
  6595. outs() << " NORELOC";
  6596. flags &= ~MachO::SG_NORELOC;
  6597. }
  6598. if (flags & MachO::SG_PROTECTED_VERSION_1) {
  6599. outs() << " PROTECTED_VERSION_1";
  6600. flags &= ~MachO::SG_PROTECTED_VERSION_1;
  6601. }
  6602. if (flags)
  6603. outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
  6604. else
  6605. outs() << "\n";
  6606. }
  6607. } else {
  6608. outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
  6609. }
  6610. }
  6611. static void PrintSection(const char *sectname, const char *segname,
  6612. uint64_t addr, uint64_t size, uint32_t offset,
  6613. uint32_t align, uint32_t reloff, uint32_t nreloc,
  6614. uint32_t flags, uint32_t reserved1, uint32_t reserved2,
  6615. uint32_t cmd, const char *sg_segname,
  6616. uint32_t filetype, uint32_t object_size,
  6617. bool verbose) {
  6618. outs() << "Section\n";
  6619. outs() << " sectname " << format("%.16s\n", sectname);
  6620. outs() << " segname " << format("%.16s", segname);
  6621. if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
  6622. outs() << " (does not match segment)\n";
  6623. else
  6624. outs() << "\n";
  6625. if (cmd == MachO::LC_SEGMENT_64) {
  6626. outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
  6627. outs() << " size " << format("0x%016" PRIx64, size);
  6628. } else {
  6629. outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
  6630. outs() << " size " << format("0x%08" PRIx64, size);
  6631. }
  6632. if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
  6633. outs() << " (past end of file)\n";
  6634. else
  6635. outs() << "\n";
  6636. outs() << " offset " << offset;
  6637. if (offset > object_size)
  6638. outs() << " (past end of file)\n";
  6639. else
  6640. outs() << "\n";
  6641. uint32_t align_shifted = 1 << align;
  6642. outs() << " align 2^" << align << " (" << align_shifted << ")\n";
  6643. outs() << " reloff " << reloff;
  6644. if (reloff > object_size)
  6645. outs() << " (past end of file)\n";
  6646. else
  6647. outs() << "\n";
  6648. outs() << " nreloc " << nreloc;
  6649. if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
  6650. outs() << " (past end of file)\n";
  6651. else
  6652. outs() << "\n";
  6653. uint32_t section_type = flags & MachO::SECTION_TYPE;
  6654. if (verbose) {
  6655. outs() << " type";
  6656. if (section_type == MachO::S_REGULAR)
  6657. outs() << " S_REGULAR\n";
  6658. else if (section_type == MachO::S_ZEROFILL)
  6659. outs() << " S_ZEROFILL\n";
  6660. else if (section_type == MachO::S_CSTRING_LITERALS)
  6661. outs() << " S_CSTRING_LITERALS\n";
  6662. else if (section_type == MachO::S_4BYTE_LITERALS)
  6663. outs() << " S_4BYTE_LITERALS\n";
  6664. else if (section_type == MachO::S_8BYTE_LITERALS)
  6665. outs() << " S_8BYTE_LITERALS\n";
  6666. else if (section_type == MachO::S_16BYTE_LITERALS)
  6667. outs() << " S_16BYTE_LITERALS\n";
  6668. else if (section_type == MachO::S_LITERAL_POINTERS)
  6669. outs() << " S_LITERAL_POINTERS\n";
  6670. else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
  6671. outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
  6672. else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
  6673. outs() << " S_LAZY_SYMBOL_POINTERS\n";
  6674. else if (section_type == MachO::S_SYMBOL_STUBS)
  6675. outs() << " S_SYMBOL_STUBS\n";
  6676. else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
  6677. outs() << " S_MOD_INIT_FUNC_POINTERS\n";
  6678. else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
  6679. outs() << " S_MOD_TERM_FUNC_POINTERS\n";
  6680. else if (section_type == MachO::S_COALESCED)
  6681. outs() << " S_COALESCED\n";
  6682. else if (section_type == MachO::S_INTERPOSING)
  6683. outs() << " S_INTERPOSING\n";
  6684. else if (section_type == MachO::S_DTRACE_DOF)
  6685. outs() << " S_DTRACE_DOF\n";
  6686. else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
  6687. outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
  6688. else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
  6689. outs() << " S_THREAD_LOCAL_REGULAR\n";
  6690. else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
  6691. outs() << " S_THREAD_LOCAL_ZEROFILL\n";
  6692. else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
  6693. outs() << " S_THREAD_LOCAL_VARIABLES\n";
  6694. else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
  6695. outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
  6696. else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
  6697. outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
  6698. else
  6699. outs() << format("0x%08" PRIx32, section_type) << "\n";
  6700. outs() << "attributes";
  6701. uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
  6702. if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
  6703. outs() << " PURE_INSTRUCTIONS";
  6704. if (section_attributes & MachO::S_ATTR_NO_TOC)
  6705. outs() << " NO_TOC";
  6706. if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
  6707. outs() << " STRIP_STATIC_SYMS";
  6708. if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
  6709. outs() << " NO_DEAD_STRIP";
  6710. if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
  6711. outs() << " LIVE_SUPPORT";
  6712. if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
  6713. outs() << " SELF_MODIFYING_CODE";
  6714. if (section_attributes & MachO::S_ATTR_DEBUG)
  6715. outs() << " DEBUG";
  6716. if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
  6717. outs() << " SOME_INSTRUCTIONS";
  6718. if (section_attributes & MachO::S_ATTR_EXT_RELOC)
  6719. outs() << " EXT_RELOC";
  6720. if (section_attributes & MachO::S_ATTR_LOC_RELOC)
  6721. outs() << " LOC_RELOC";
  6722. if (section_attributes == 0)
  6723. outs() << " (none)";
  6724. outs() << "\n";
  6725. } else
  6726. outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
  6727. outs() << " reserved1 " << reserved1;
  6728. if (section_type == MachO::S_SYMBOL_STUBS ||
  6729. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  6730. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  6731. section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  6732. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
  6733. outs() << " (index into indirect symbol table)\n";
  6734. else
  6735. outs() << "\n";
  6736. outs() << " reserved2 " << reserved2;
  6737. if (section_type == MachO::S_SYMBOL_STUBS)
  6738. outs() << " (size of stubs)\n";
  6739. else
  6740. outs() << "\n";
  6741. }
  6742. static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
  6743. uint32_t object_size) {
  6744. outs() << " cmd LC_SYMTAB\n";
  6745. outs() << " cmdsize " << st.cmdsize;
  6746. if (st.cmdsize != sizeof(struct MachO::symtab_command))
  6747. outs() << " Incorrect size\n";
  6748. else
  6749. outs() << "\n";
  6750. outs() << " symoff " << st.symoff;
  6751. if (st.symoff > object_size)
  6752. outs() << " (past end of file)\n";
  6753. else
  6754. outs() << "\n";
  6755. outs() << " nsyms " << st.nsyms;
  6756. uint64_t big_size;
  6757. if (Is64Bit) {
  6758. big_size = st.nsyms;
  6759. big_size *= sizeof(struct MachO::nlist_64);
  6760. big_size += st.symoff;
  6761. if (big_size > object_size)
  6762. outs() << " (past end of file)\n";
  6763. else
  6764. outs() << "\n";
  6765. } else {
  6766. big_size = st.nsyms;
  6767. big_size *= sizeof(struct MachO::nlist);
  6768. big_size += st.symoff;
  6769. if (big_size > object_size)
  6770. outs() << " (past end of file)\n";
  6771. else
  6772. outs() << "\n";
  6773. }
  6774. outs() << " stroff " << st.stroff;
  6775. if (st.stroff > object_size)
  6776. outs() << " (past end of file)\n";
  6777. else
  6778. outs() << "\n";
  6779. outs() << " strsize " << st.strsize;
  6780. big_size = st.stroff;
  6781. big_size += st.strsize;
  6782. if (big_size > object_size)
  6783. outs() << " (past end of file)\n";
  6784. else
  6785. outs() << "\n";
  6786. }
  6787. static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
  6788. uint32_t nsyms, uint32_t object_size,
  6789. bool Is64Bit) {
  6790. outs() << " cmd LC_DYSYMTAB\n";
  6791. outs() << " cmdsize " << dyst.cmdsize;
  6792. if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
  6793. outs() << " Incorrect size\n";
  6794. else
  6795. outs() << "\n";
  6796. outs() << " ilocalsym " << dyst.ilocalsym;
  6797. if (dyst.ilocalsym > nsyms)
  6798. outs() << " (greater than the number of symbols)\n";
  6799. else
  6800. outs() << "\n";
  6801. outs() << " nlocalsym " << dyst.nlocalsym;
  6802. uint64_t big_size;
  6803. big_size = dyst.ilocalsym;
  6804. big_size += dyst.nlocalsym;
  6805. if (big_size > nsyms)
  6806. outs() << " (past the end of the symbol table)\n";
  6807. else
  6808. outs() << "\n";
  6809. outs() << " iextdefsym " << dyst.iextdefsym;
  6810. if (dyst.iextdefsym > nsyms)
  6811. outs() << " (greater than the number of symbols)\n";
  6812. else
  6813. outs() << "\n";
  6814. outs() << " nextdefsym " << dyst.nextdefsym;
  6815. big_size = dyst.iextdefsym;
  6816. big_size += dyst.nextdefsym;
  6817. if (big_size > nsyms)
  6818. outs() << " (past the end of the symbol table)\n";
  6819. else
  6820. outs() << "\n";
  6821. outs() << " iundefsym " << dyst.iundefsym;
  6822. if (dyst.iundefsym > nsyms)
  6823. outs() << " (greater than the number of symbols)\n";
  6824. else
  6825. outs() << "\n";
  6826. outs() << " nundefsym " << dyst.nundefsym;
  6827. big_size = dyst.iundefsym;
  6828. big_size += dyst.nundefsym;
  6829. if (big_size > nsyms)
  6830. outs() << " (past the end of the symbol table)\n";
  6831. else
  6832. outs() << "\n";
  6833. outs() << " tocoff " << dyst.tocoff;
  6834. if (dyst.tocoff > object_size)
  6835. outs() << " (past end of file)\n";
  6836. else
  6837. outs() << "\n";
  6838. outs() << " ntoc " << dyst.ntoc;
  6839. big_size = dyst.ntoc;
  6840. big_size *= sizeof(struct MachO::dylib_table_of_contents);
  6841. big_size += dyst.tocoff;
  6842. if (big_size > object_size)
  6843. outs() << " (past end of file)\n";
  6844. else
  6845. outs() << "\n";
  6846. outs() << " modtaboff " << dyst.modtaboff;
  6847. if (dyst.modtaboff > object_size)
  6848. outs() << " (past end of file)\n";
  6849. else
  6850. outs() << "\n";
  6851. outs() << " nmodtab " << dyst.nmodtab;
  6852. uint64_t modtabend;
  6853. if (Is64Bit) {
  6854. modtabend = dyst.nmodtab;
  6855. modtabend *= sizeof(struct MachO::dylib_module_64);
  6856. modtabend += dyst.modtaboff;
  6857. } else {
  6858. modtabend = dyst.nmodtab;
  6859. modtabend *= sizeof(struct MachO::dylib_module);
  6860. modtabend += dyst.modtaboff;
  6861. }
  6862. if (modtabend > object_size)
  6863. outs() << " (past end of file)\n";
  6864. else
  6865. outs() << "\n";
  6866. outs() << " extrefsymoff " << dyst.extrefsymoff;
  6867. if (dyst.extrefsymoff > object_size)
  6868. outs() << " (past end of file)\n";
  6869. else
  6870. outs() << "\n";
  6871. outs() << " nextrefsyms " << dyst.nextrefsyms;
  6872. big_size = dyst.nextrefsyms;
  6873. big_size *= sizeof(struct MachO::dylib_reference);
  6874. big_size += dyst.extrefsymoff;
  6875. if (big_size > object_size)
  6876. outs() << " (past end of file)\n";
  6877. else
  6878. outs() << "\n";
  6879. outs() << " indirectsymoff " << dyst.indirectsymoff;
  6880. if (dyst.indirectsymoff > object_size)
  6881. outs() << " (past end of file)\n";
  6882. else
  6883. outs() << "\n";
  6884. outs() << " nindirectsyms " << dyst.nindirectsyms;
  6885. big_size = dyst.nindirectsyms;
  6886. big_size *= sizeof(uint32_t);
  6887. big_size += dyst.indirectsymoff;
  6888. if (big_size > object_size)
  6889. outs() << " (past end of file)\n";
  6890. else
  6891. outs() << "\n";
  6892. outs() << " extreloff " << dyst.extreloff;
  6893. if (dyst.extreloff > object_size)
  6894. outs() << " (past end of file)\n";
  6895. else
  6896. outs() << "\n";
  6897. outs() << " nextrel " << dyst.nextrel;
  6898. big_size = dyst.nextrel;
  6899. big_size *= sizeof(struct MachO::relocation_info);
  6900. big_size += dyst.extreloff;
  6901. if (big_size > object_size)
  6902. outs() << " (past end of file)\n";
  6903. else
  6904. outs() << "\n";
  6905. outs() << " locreloff " << dyst.locreloff;
  6906. if (dyst.locreloff > object_size)
  6907. outs() << " (past end of file)\n";
  6908. else
  6909. outs() << "\n";
  6910. outs() << " nlocrel " << dyst.nlocrel;
  6911. big_size = dyst.nlocrel;
  6912. big_size *= sizeof(struct MachO::relocation_info);
  6913. big_size += dyst.locreloff;
  6914. if (big_size > object_size)
  6915. outs() << " (past end of file)\n";
  6916. else
  6917. outs() << "\n";
  6918. }
  6919. static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
  6920. uint32_t object_size) {
  6921. if (dc.cmd == MachO::LC_DYLD_INFO)
  6922. outs() << " cmd LC_DYLD_INFO\n";
  6923. else
  6924. outs() << " cmd LC_DYLD_INFO_ONLY\n";
  6925. outs() << " cmdsize " << dc.cmdsize;
  6926. if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
  6927. outs() << " Incorrect size\n";
  6928. else
  6929. outs() << "\n";
  6930. outs() << " rebase_off " << dc.rebase_off;
  6931. if (dc.rebase_off > object_size)
  6932. outs() << " (past end of file)\n";
  6933. else
  6934. outs() << "\n";
  6935. outs() << " rebase_size " << dc.rebase_size;
  6936. uint64_t big_size;
  6937. big_size = dc.rebase_off;
  6938. big_size += dc.rebase_size;
  6939. if (big_size > object_size)
  6940. outs() << " (past end of file)\n";
  6941. else
  6942. outs() << "\n";
  6943. outs() << " bind_off " << dc.bind_off;
  6944. if (dc.bind_off > object_size)
  6945. outs() << " (past end of file)\n";
  6946. else
  6947. outs() << "\n";
  6948. outs() << " bind_size " << dc.bind_size;
  6949. big_size = dc.bind_off;
  6950. big_size += dc.bind_size;
  6951. if (big_size > object_size)
  6952. outs() << " (past end of file)\n";
  6953. else
  6954. outs() << "\n";
  6955. outs() << " weak_bind_off " << dc.weak_bind_off;
  6956. if (dc.weak_bind_off > object_size)
  6957. outs() << " (past end of file)\n";
  6958. else
  6959. outs() << "\n";
  6960. outs() << " weak_bind_size " << dc.weak_bind_size;
  6961. big_size = dc.weak_bind_off;
  6962. big_size += dc.weak_bind_size;
  6963. if (big_size > object_size)
  6964. outs() << " (past end of file)\n";
  6965. else
  6966. outs() << "\n";
  6967. outs() << " lazy_bind_off " << dc.lazy_bind_off;
  6968. if (dc.lazy_bind_off > object_size)
  6969. outs() << " (past end of file)\n";
  6970. else
  6971. outs() << "\n";
  6972. outs() << " lazy_bind_size " << dc.lazy_bind_size;
  6973. big_size = dc.lazy_bind_off;
  6974. big_size += dc.lazy_bind_size;
  6975. if (big_size > object_size)
  6976. outs() << " (past end of file)\n";
  6977. else
  6978. outs() << "\n";
  6979. outs() << " export_off " << dc.export_off;
  6980. if (dc.export_off > object_size)
  6981. outs() << " (past end of file)\n";
  6982. else
  6983. outs() << "\n";
  6984. outs() << " export_size " << dc.export_size;
  6985. big_size = dc.export_off;
  6986. big_size += dc.export_size;
  6987. if (big_size > object_size)
  6988. outs() << " (past end of file)\n";
  6989. else
  6990. outs() << "\n";
  6991. }
  6992. static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
  6993. const char *Ptr) {
  6994. if (dyld.cmd == MachO::LC_ID_DYLINKER)
  6995. outs() << " cmd LC_ID_DYLINKER\n";
  6996. else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
  6997. outs() << " cmd LC_LOAD_DYLINKER\n";
  6998. else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
  6999. outs() << " cmd LC_DYLD_ENVIRONMENT\n";
  7000. else
  7001. outs() << " cmd ?(" << dyld.cmd << ")\n";
  7002. outs() << " cmdsize " << dyld.cmdsize;
  7003. if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
  7004. outs() << " Incorrect size\n";
  7005. else
  7006. outs() << "\n";
  7007. if (dyld.name >= dyld.cmdsize)
  7008. outs() << " name ?(bad offset " << dyld.name << ")\n";
  7009. else {
  7010. const char *P = (const char *)(Ptr) + dyld.name;
  7011. outs() << " name " << P << " (offset " << dyld.name << ")\n";
  7012. }
  7013. }
  7014. static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
  7015. outs() << " cmd LC_UUID\n";
  7016. outs() << " cmdsize " << uuid.cmdsize;
  7017. if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
  7018. outs() << " Incorrect size\n";
  7019. else
  7020. outs() << "\n";
  7021. outs() << " uuid ";
  7022. outs() << format("%02" PRIX32, uuid.uuid[0]);
  7023. outs() << format("%02" PRIX32, uuid.uuid[1]);
  7024. outs() << format("%02" PRIX32, uuid.uuid[2]);
  7025. outs() << format("%02" PRIX32, uuid.uuid[3]);
  7026. outs() << "-";
  7027. outs() << format("%02" PRIX32, uuid.uuid[4]);
  7028. outs() << format("%02" PRIX32, uuid.uuid[5]);
  7029. outs() << "-";
  7030. outs() << format("%02" PRIX32, uuid.uuid[6]);
  7031. outs() << format("%02" PRIX32, uuid.uuid[7]);
  7032. outs() << "-";
  7033. outs() << format("%02" PRIX32, uuid.uuid[8]);
  7034. outs() << format("%02" PRIX32, uuid.uuid[9]);
  7035. outs() << "-";
  7036. outs() << format("%02" PRIX32, uuid.uuid[10]);
  7037. outs() << format("%02" PRIX32, uuid.uuid[11]);
  7038. outs() << format("%02" PRIX32, uuid.uuid[12]);
  7039. outs() << format("%02" PRIX32, uuid.uuid[13]);
  7040. outs() << format("%02" PRIX32, uuid.uuid[14]);
  7041. outs() << format("%02" PRIX32, uuid.uuid[15]);
  7042. outs() << "\n";
  7043. }
  7044. static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
  7045. outs() << " cmd LC_RPATH\n";
  7046. outs() << " cmdsize " << rpath.cmdsize;
  7047. if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
  7048. outs() << " Incorrect size\n";
  7049. else
  7050. outs() << "\n";
  7051. if (rpath.path >= rpath.cmdsize)
  7052. outs() << " path ?(bad offset " << rpath.path << ")\n";
  7053. else {
  7054. const char *P = (const char *)(Ptr) + rpath.path;
  7055. outs() << " path " << P << " (offset " << rpath.path << ")\n";
  7056. }
  7057. }
  7058. static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
  7059. if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
  7060. outs() << " cmd LC_VERSION_MIN_MACOSX\n";
  7061. else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
  7062. outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
  7063. else
  7064. outs() << " cmd " << vd.cmd << " (?)\n";
  7065. outs() << " cmdsize " << vd.cmdsize;
  7066. if (vd.cmdsize != sizeof(struct MachO::version_min_command))
  7067. outs() << " Incorrect size\n";
  7068. else
  7069. outs() << "\n";
  7070. outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
  7071. << ((vd.version >> 8) & 0xff);
  7072. if ((vd.version & 0xff) != 0)
  7073. outs() << "." << (vd.version & 0xff);
  7074. outs() << "\n";
  7075. if (vd.sdk == 0)
  7076. outs() << " sdk n/a";
  7077. else {
  7078. outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
  7079. << ((vd.sdk >> 8) & 0xff);
  7080. }
  7081. if ((vd.sdk & 0xff) != 0)
  7082. outs() << "." << (vd.sdk & 0xff);
  7083. outs() << "\n";
  7084. }
  7085. static void PrintSourceVersionCommand(MachO::source_version_command sd) {
  7086. outs() << " cmd LC_SOURCE_VERSION\n";
  7087. outs() << " cmdsize " << sd.cmdsize;
  7088. if (sd.cmdsize != sizeof(struct MachO::source_version_command))
  7089. outs() << " Incorrect size\n";
  7090. else
  7091. outs() << "\n";
  7092. uint64_t a = (sd.version >> 40) & 0xffffff;
  7093. uint64_t b = (sd.version >> 30) & 0x3ff;
  7094. uint64_t c = (sd.version >> 20) & 0x3ff;
  7095. uint64_t d = (sd.version >> 10) & 0x3ff;
  7096. uint64_t e = sd.version & 0x3ff;
  7097. outs() << " version " << a << "." << b;
  7098. if (e != 0)
  7099. outs() << "." << c << "." << d << "." << e;
  7100. else if (d != 0)
  7101. outs() << "." << c << "." << d;
  7102. else if (c != 0)
  7103. outs() << "." << c;
  7104. outs() << "\n";
  7105. }
  7106. static void PrintEntryPointCommand(MachO::entry_point_command ep) {
  7107. outs() << " cmd LC_MAIN\n";
  7108. outs() << " cmdsize " << ep.cmdsize;
  7109. if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
  7110. outs() << " Incorrect size\n";
  7111. else
  7112. outs() << "\n";
  7113. outs() << " entryoff " << ep.entryoff << "\n";
  7114. outs() << " stacksize " << ep.stacksize << "\n";
  7115. }
  7116. static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
  7117. uint32_t object_size) {
  7118. outs() << " cmd LC_ENCRYPTION_INFO\n";
  7119. outs() << " cmdsize " << ec.cmdsize;
  7120. if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
  7121. outs() << " Incorrect size\n";
  7122. else
  7123. outs() << "\n";
  7124. outs() << " cryptoff " << ec.cryptoff;
  7125. if (ec.cryptoff > object_size)
  7126. outs() << " (past end of file)\n";
  7127. else
  7128. outs() << "\n";
  7129. outs() << " cryptsize " << ec.cryptsize;
  7130. if (ec.cryptsize > object_size)
  7131. outs() << " (past end of file)\n";
  7132. else
  7133. outs() << "\n";
  7134. outs() << " cryptid " << ec.cryptid << "\n";
  7135. }
  7136. static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
  7137. uint32_t object_size) {
  7138. outs() << " cmd LC_ENCRYPTION_INFO_64\n";
  7139. outs() << " cmdsize " << ec.cmdsize;
  7140. if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
  7141. outs() << " Incorrect size\n";
  7142. else
  7143. outs() << "\n";
  7144. outs() << " cryptoff " << ec.cryptoff;
  7145. if (ec.cryptoff > object_size)
  7146. outs() << " (past end of file)\n";
  7147. else
  7148. outs() << "\n";
  7149. outs() << " cryptsize " << ec.cryptsize;
  7150. if (ec.cryptsize > object_size)
  7151. outs() << " (past end of file)\n";
  7152. else
  7153. outs() << "\n";
  7154. outs() << " cryptid " << ec.cryptid << "\n";
  7155. outs() << " pad " << ec.pad << "\n";
  7156. }
  7157. static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
  7158. const char *Ptr) {
  7159. outs() << " cmd LC_LINKER_OPTION\n";
  7160. outs() << " cmdsize " << lo.cmdsize;
  7161. if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
  7162. outs() << " Incorrect size\n";
  7163. else
  7164. outs() << "\n";
  7165. outs() << " count " << lo.count << "\n";
  7166. const char *string = Ptr + sizeof(struct MachO::linker_option_command);
  7167. uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
  7168. uint32_t i = 0;
  7169. while (left > 0) {
  7170. while (*string == '\0' && left > 0) {
  7171. string++;
  7172. left--;
  7173. }
  7174. if (left > 0) {
  7175. i++;
  7176. outs() << " string #" << i << " " << format("%.*s\n", left, string);
  7177. uint32_t NullPos = StringRef(string, left).find('\0');
  7178. uint32_t len = std::min(NullPos, left) + 1;
  7179. string += len;
  7180. left -= len;
  7181. }
  7182. }
  7183. if (lo.count != i)
  7184. outs() << " count " << lo.count << " does not match number of strings "
  7185. << i << "\n";
  7186. }
  7187. static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
  7188. const char *Ptr) {
  7189. outs() << " cmd LC_SUB_FRAMEWORK\n";
  7190. outs() << " cmdsize " << sub.cmdsize;
  7191. if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
  7192. outs() << " Incorrect size\n";
  7193. else
  7194. outs() << "\n";
  7195. if (sub.umbrella < sub.cmdsize) {
  7196. const char *P = Ptr + sub.umbrella;
  7197. outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
  7198. } else {
  7199. outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
  7200. }
  7201. }
  7202. static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
  7203. const char *Ptr) {
  7204. outs() << " cmd LC_SUB_UMBRELLA\n";
  7205. outs() << " cmdsize " << sub.cmdsize;
  7206. if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
  7207. outs() << " Incorrect size\n";
  7208. else
  7209. outs() << "\n";
  7210. if (sub.sub_umbrella < sub.cmdsize) {
  7211. const char *P = Ptr + sub.sub_umbrella;
  7212. outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
  7213. } else {
  7214. outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
  7215. }
  7216. }
  7217. static void PrintSubLibraryCommand(MachO::sub_library_command sub,
  7218. const char *Ptr) {
  7219. outs() << " cmd LC_SUB_LIBRARY\n";
  7220. outs() << " cmdsize " << sub.cmdsize;
  7221. if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
  7222. outs() << " Incorrect size\n";
  7223. else
  7224. outs() << "\n";
  7225. if (sub.sub_library < sub.cmdsize) {
  7226. const char *P = Ptr + sub.sub_library;
  7227. outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
  7228. } else {
  7229. outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
  7230. }
  7231. }
  7232. static void PrintSubClientCommand(MachO::sub_client_command sub,
  7233. const char *Ptr) {
  7234. outs() << " cmd LC_SUB_CLIENT\n";
  7235. outs() << " cmdsize " << sub.cmdsize;
  7236. if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
  7237. outs() << " Incorrect size\n";
  7238. else
  7239. outs() << "\n";
  7240. if (sub.client < sub.cmdsize) {
  7241. const char *P = Ptr + sub.client;
  7242. outs() << " client " << P << " (offset " << sub.client << ")\n";
  7243. } else {
  7244. outs() << " client ?(bad offset " << sub.client << ")\n";
  7245. }
  7246. }
  7247. static void PrintRoutinesCommand(MachO::routines_command r) {
  7248. outs() << " cmd LC_ROUTINES\n";
  7249. outs() << " cmdsize " << r.cmdsize;
  7250. if (r.cmdsize != sizeof(struct MachO::routines_command))
  7251. outs() << " Incorrect size\n";
  7252. else
  7253. outs() << "\n";
  7254. outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
  7255. outs() << " init_module " << r.init_module << "\n";
  7256. outs() << " reserved1 " << r.reserved1 << "\n";
  7257. outs() << " reserved2 " << r.reserved2 << "\n";
  7258. outs() << " reserved3 " << r.reserved3 << "\n";
  7259. outs() << " reserved4 " << r.reserved4 << "\n";
  7260. outs() << " reserved5 " << r.reserved5 << "\n";
  7261. outs() << " reserved6 " << r.reserved6 << "\n";
  7262. }
  7263. static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
  7264. outs() << " cmd LC_ROUTINES_64\n";
  7265. outs() << " cmdsize " << r.cmdsize;
  7266. if (r.cmdsize != sizeof(struct MachO::routines_command_64))
  7267. outs() << " Incorrect size\n";
  7268. else
  7269. outs() << "\n";
  7270. outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
  7271. outs() << " init_module " << r.init_module << "\n";
  7272. outs() << " reserved1 " << r.reserved1 << "\n";
  7273. outs() << " reserved2 " << r.reserved2 << "\n";
  7274. outs() << " reserved3 " << r.reserved3 << "\n";
  7275. outs() << " reserved4 " << r.reserved4 << "\n";
  7276. outs() << " reserved5 " << r.reserved5 << "\n";
  7277. outs() << " reserved6 " << r.reserved6 << "\n";
  7278. }
  7279. static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
  7280. outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
  7281. outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
  7282. outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
  7283. outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
  7284. outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
  7285. outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
  7286. outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
  7287. outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
  7288. outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
  7289. outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
  7290. outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
  7291. outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
  7292. outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
  7293. outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
  7294. outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
  7295. outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
  7296. outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
  7297. outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
  7298. outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
  7299. outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
  7300. outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
  7301. }
  7302. static void Print_mmst_reg(MachO::mmst_reg_t &r) {
  7303. uint32_t f;
  7304. outs() << "\t mmst_reg ";
  7305. for (f = 0; f < 10; f++)
  7306. outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
  7307. outs() << "\n";
  7308. outs() << "\t mmst_rsrv ";
  7309. for (f = 0; f < 6; f++)
  7310. outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
  7311. outs() << "\n";
  7312. }
  7313. static void Print_xmm_reg(MachO::xmm_reg_t &r) {
  7314. uint32_t f;
  7315. outs() << "\t xmm_reg ";
  7316. for (f = 0; f < 16; f++)
  7317. outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
  7318. outs() << "\n";
  7319. }
  7320. static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
  7321. outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
  7322. outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
  7323. outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
  7324. outs() << " denorm " << fpu.fpu_fcw.denorm;
  7325. outs() << " zdiv " << fpu.fpu_fcw.zdiv;
  7326. outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
  7327. outs() << " undfl " << fpu.fpu_fcw.undfl;
  7328. outs() << " precis " << fpu.fpu_fcw.precis << "\n";
  7329. outs() << "\t\t pc ";
  7330. if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
  7331. outs() << "FP_PREC_24B ";
  7332. else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
  7333. outs() << "FP_PREC_53B ";
  7334. else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
  7335. outs() << "FP_PREC_64B ";
  7336. else
  7337. outs() << fpu.fpu_fcw.pc << " ";
  7338. outs() << "rc ";
  7339. if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
  7340. outs() << "FP_RND_NEAR ";
  7341. else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
  7342. outs() << "FP_RND_DOWN ";
  7343. else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
  7344. outs() << "FP_RND_UP ";
  7345. else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
  7346. outs() << "FP_CHOP ";
  7347. outs() << "\n";
  7348. outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
  7349. outs() << " denorm " << fpu.fpu_fsw.denorm;
  7350. outs() << " zdiv " << fpu.fpu_fsw.zdiv;
  7351. outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
  7352. outs() << " undfl " << fpu.fpu_fsw.undfl;
  7353. outs() << " precis " << fpu.fpu_fsw.precis;
  7354. outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
  7355. outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
  7356. outs() << " c0 " << fpu.fpu_fsw.c0;
  7357. outs() << " c1 " << fpu.fpu_fsw.c1;
  7358. outs() << " c2 " << fpu.fpu_fsw.c2;
  7359. outs() << " tos " << fpu.fpu_fsw.tos;
  7360. outs() << " c3 " << fpu.fpu_fsw.c3;
  7361. outs() << " busy " << fpu.fpu_fsw.busy << "\n";
  7362. outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
  7363. outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
  7364. outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
  7365. outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
  7366. outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
  7367. outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
  7368. outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
  7369. outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
  7370. outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
  7371. outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
  7372. outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
  7373. outs() << "\n";
  7374. outs() << "\t fpu_stmm0:\n";
  7375. Print_mmst_reg(fpu.fpu_stmm0);
  7376. outs() << "\t fpu_stmm1:\n";
  7377. Print_mmst_reg(fpu.fpu_stmm1);
  7378. outs() << "\t fpu_stmm2:\n";
  7379. Print_mmst_reg(fpu.fpu_stmm2);
  7380. outs() << "\t fpu_stmm3:\n";
  7381. Print_mmst_reg(fpu.fpu_stmm3);
  7382. outs() << "\t fpu_stmm4:\n";
  7383. Print_mmst_reg(fpu.fpu_stmm4);
  7384. outs() << "\t fpu_stmm5:\n";
  7385. Print_mmst_reg(fpu.fpu_stmm5);
  7386. outs() << "\t fpu_stmm6:\n";
  7387. Print_mmst_reg(fpu.fpu_stmm6);
  7388. outs() << "\t fpu_stmm7:\n";
  7389. Print_mmst_reg(fpu.fpu_stmm7);
  7390. outs() << "\t fpu_xmm0:\n";
  7391. Print_xmm_reg(fpu.fpu_xmm0);
  7392. outs() << "\t fpu_xmm1:\n";
  7393. Print_xmm_reg(fpu.fpu_xmm1);
  7394. outs() << "\t fpu_xmm2:\n";
  7395. Print_xmm_reg(fpu.fpu_xmm2);
  7396. outs() << "\t fpu_xmm3:\n";
  7397. Print_xmm_reg(fpu.fpu_xmm3);
  7398. outs() << "\t fpu_xmm4:\n";
  7399. Print_xmm_reg(fpu.fpu_xmm4);
  7400. outs() << "\t fpu_xmm5:\n";
  7401. Print_xmm_reg(fpu.fpu_xmm5);
  7402. outs() << "\t fpu_xmm6:\n";
  7403. Print_xmm_reg(fpu.fpu_xmm6);
  7404. outs() << "\t fpu_xmm7:\n";
  7405. Print_xmm_reg(fpu.fpu_xmm7);
  7406. outs() << "\t fpu_xmm8:\n";
  7407. Print_xmm_reg(fpu.fpu_xmm8);
  7408. outs() << "\t fpu_xmm9:\n";
  7409. Print_xmm_reg(fpu.fpu_xmm9);
  7410. outs() << "\t fpu_xmm10:\n";
  7411. Print_xmm_reg(fpu.fpu_xmm10);
  7412. outs() << "\t fpu_xmm11:\n";
  7413. Print_xmm_reg(fpu.fpu_xmm11);
  7414. outs() << "\t fpu_xmm12:\n";
  7415. Print_xmm_reg(fpu.fpu_xmm12);
  7416. outs() << "\t fpu_xmm13:\n";
  7417. Print_xmm_reg(fpu.fpu_xmm13);
  7418. outs() << "\t fpu_xmm14:\n";
  7419. Print_xmm_reg(fpu.fpu_xmm14);
  7420. outs() << "\t fpu_xmm15:\n";
  7421. Print_xmm_reg(fpu.fpu_xmm15);
  7422. outs() << "\t fpu_rsrv4:\n";
  7423. for (uint32_t f = 0; f < 6; f++) {
  7424. outs() << "\t ";
  7425. for (uint32_t g = 0; g < 16; g++)
  7426. outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
  7427. outs() << "\n";
  7428. }
  7429. outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
  7430. outs() << "\n";
  7431. }
  7432. static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
  7433. outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
  7434. outs() << " err " << format("0x%08" PRIx32, exc64.err);
  7435. outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
  7436. }
  7437. static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
  7438. bool isLittleEndian, uint32_t cputype) {
  7439. if (t.cmd == MachO::LC_THREAD)
  7440. outs() << " cmd LC_THREAD\n";
  7441. else if (t.cmd == MachO::LC_UNIXTHREAD)
  7442. outs() << " cmd LC_UNIXTHREAD\n";
  7443. else
  7444. outs() << " cmd " << t.cmd << " (unknown)\n";
  7445. outs() << " cmdsize " << t.cmdsize;
  7446. if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
  7447. outs() << " Incorrect size\n";
  7448. else
  7449. outs() << "\n";
  7450. const char *begin = Ptr + sizeof(struct MachO::thread_command);
  7451. const char *end = Ptr + t.cmdsize;
  7452. uint32_t flavor, count, left;
  7453. if (cputype == MachO::CPU_TYPE_X86_64) {
  7454. while (begin < end) {
  7455. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  7456. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  7457. begin += sizeof(uint32_t);
  7458. } else {
  7459. flavor = 0;
  7460. begin = end;
  7461. }
  7462. if (isLittleEndian != sys::IsLittleEndianHost)
  7463. sys::swapByteOrder(flavor);
  7464. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  7465. memcpy((char *)&count, begin, sizeof(uint32_t));
  7466. begin += sizeof(uint32_t);
  7467. } else {
  7468. count = 0;
  7469. begin = end;
  7470. }
  7471. if (isLittleEndian != sys::IsLittleEndianHost)
  7472. sys::swapByteOrder(count);
  7473. if (flavor == MachO::x86_THREAD_STATE64) {
  7474. outs() << " flavor x86_THREAD_STATE64\n";
  7475. if (count == MachO::x86_THREAD_STATE64_COUNT)
  7476. outs() << " count x86_THREAD_STATE64_COUNT\n";
  7477. else
  7478. outs() << " count " << count
  7479. << " (not x86_THREAD_STATE64_COUNT)\n";
  7480. MachO::x86_thread_state64_t cpu64;
  7481. left = end - begin;
  7482. if (left >= sizeof(MachO::x86_thread_state64_t)) {
  7483. memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
  7484. begin += sizeof(MachO::x86_thread_state64_t);
  7485. } else {
  7486. memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
  7487. memcpy(&cpu64, begin, left);
  7488. begin += left;
  7489. }
  7490. if (isLittleEndian != sys::IsLittleEndianHost)
  7491. swapStruct(cpu64);
  7492. Print_x86_thread_state64_t(cpu64);
  7493. } else if (flavor == MachO::x86_THREAD_STATE) {
  7494. outs() << " flavor x86_THREAD_STATE\n";
  7495. if (count == MachO::x86_THREAD_STATE_COUNT)
  7496. outs() << " count x86_THREAD_STATE_COUNT\n";
  7497. else
  7498. outs() << " count " << count
  7499. << " (not x86_THREAD_STATE_COUNT)\n";
  7500. struct MachO::x86_thread_state_t ts;
  7501. left = end - begin;
  7502. if (left >= sizeof(MachO::x86_thread_state_t)) {
  7503. memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
  7504. begin += sizeof(MachO::x86_thread_state_t);
  7505. } else {
  7506. memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
  7507. memcpy(&ts, begin, left);
  7508. begin += left;
  7509. }
  7510. if (isLittleEndian != sys::IsLittleEndianHost)
  7511. swapStruct(ts);
  7512. if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
  7513. outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
  7514. if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
  7515. outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
  7516. else
  7517. outs() << "tsh.count " << ts.tsh.count
  7518. << " (not x86_THREAD_STATE64_COUNT\n";
  7519. Print_x86_thread_state64_t(ts.uts.ts64);
  7520. } else {
  7521. outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
  7522. << ts.tsh.count << "\n";
  7523. }
  7524. } else if (flavor == MachO::x86_FLOAT_STATE) {
  7525. outs() << " flavor x86_FLOAT_STATE\n";
  7526. if (count == MachO::x86_FLOAT_STATE_COUNT)
  7527. outs() << " count x86_FLOAT_STATE_COUNT\n";
  7528. else
  7529. outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
  7530. struct MachO::x86_float_state_t fs;
  7531. left = end - begin;
  7532. if (left >= sizeof(MachO::x86_float_state_t)) {
  7533. memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
  7534. begin += sizeof(MachO::x86_float_state_t);
  7535. } else {
  7536. memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
  7537. memcpy(&fs, begin, left);
  7538. begin += left;
  7539. }
  7540. if (isLittleEndian != sys::IsLittleEndianHost)
  7541. swapStruct(fs);
  7542. if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
  7543. outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
  7544. if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
  7545. outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
  7546. else
  7547. outs() << "fsh.count " << fs.fsh.count
  7548. << " (not x86_FLOAT_STATE64_COUNT\n";
  7549. Print_x86_float_state_t(fs.ufs.fs64);
  7550. } else {
  7551. outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
  7552. << fs.fsh.count << "\n";
  7553. }
  7554. } else if (flavor == MachO::x86_EXCEPTION_STATE) {
  7555. outs() << " flavor x86_EXCEPTION_STATE\n";
  7556. if (count == MachO::x86_EXCEPTION_STATE_COUNT)
  7557. outs() << " count x86_EXCEPTION_STATE_COUNT\n";
  7558. else
  7559. outs() << " count " << count
  7560. << " (not x86_EXCEPTION_STATE_COUNT)\n";
  7561. struct MachO::x86_exception_state_t es;
  7562. left = end - begin;
  7563. if (left >= sizeof(MachO::x86_exception_state_t)) {
  7564. memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
  7565. begin += sizeof(MachO::x86_exception_state_t);
  7566. } else {
  7567. memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
  7568. memcpy(&es, begin, left);
  7569. begin += left;
  7570. }
  7571. if (isLittleEndian != sys::IsLittleEndianHost)
  7572. swapStruct(es);
  7573. if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
  7574. outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
  7575. if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
  7576. outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
  7577. else
  7578. outs() << "\t esh.count " << es.esh.count
  7579. << " (not x86_EXCEPTION_STATE64_COUNT\n";
  7580. Print_x86_exception_state_t(es.ues.es64);
  7581. } else {
  7582. outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
  7583. << es.esh.count << "\n";
  7584. }
  7585. } else {
  7586. outs() << " flavor " << flavor << " (unknown)\n";
  7587. outs() << " count " << count << "\n";
  7588. outs() << " state (unknown)\n";
  7589. begin += count * sizeof(uint32_t);
  7590. }
  7591. }
  7592. } else {
  7593. while (begin < end) {
  7594. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  7595. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  7596. begin += sizeof(uint32_t);
  7597. } else {
  7598. flavor = 0;
  7599. begin = end;
  7600. }
  7601. if (isLittleEndian != sys::IsLittleEndianHost)
  7602. sys::swapByteOrder(flavor);
  7603. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  7604. memcpy((char *)&count, begin, sizeof(uint32_t));
  7605. begin += sizeof(uint32_t);
  7606. } else {
  7607. count = 0;
  7608. begin = end;
  7609. }
  7610. if (isLittleEndian != sys::IsLittleEndianHost)
  7611. sys::swapByteOrder(count);
  7612. outs() << " flavor " << flavor << "\n";
  7613. outs() << " count " << count << "\n";
  7614. outs() << " state (Unknown cputype/cpusubtype)\n";
  7615. begin += count * sizeof(uint32_t);
  7616. }
  7617. }
  7618. }
  7619. static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
  7620. if (dl.cmd == MachO::LC_ID_DYLIB)
  7621. outs() << " cmd LC_ID_DYLIB\n";
  7622. else if (dl.cmd == MachO::LC_LOAD_DYLIB)
  7623. outs() << " cmd LC_LOAD_DYLIB\n";
  7624. else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
  7625. outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
  7626. else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
  7627. outs() << " cmd LC_REEXPORT_DYLIB\n";
  7628. else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
  7629. outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
  7630. else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
  7631. outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
  7632. else
  7633. outs() << " cmd " << dl.cmd << " (unknown)\n";
  7634. outs() << " cmdsize " << dl.cmdsize;
  7635. if (dl.cmdsize < sizeof(struct MachO::dylib_command))
  7636. outs() << " Incorrect size\n";
  7637. else
  7638. outs() << "\n";
  7639. if (dl.dylib.name < dl.cmdsize) {
  7640. const char *P = (const char *)(Ptr) + dl.dylib.name;
  7641. outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
  7642. } else {
  7643. outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
  7644. }
  7645. outs() << " time stamp " << dl.dylib.timestamp << " ";
  7646. time_t t = dl.dylib.timestamp;
  7647. outs() << ctime(&t);
  7648. outs() << " current version ";
  7649. if (dl.dylib.current_version == 0xffffffff)
  7650. outs() << "n/a\n";
  7651. else
  7652. outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
  7653. << ((dl.dylib.current_version >> 8) & 0xff) << "."
  7654. << (dl.dylib.current_version & 0xff) << "\n";
  7655. outs() << "compatibility version ";
  7656. if (dl.dylib.compatibility_version == 0xffffffff)
  7657. outs() << "n/a\n";
  7658. else
  7659. outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
  7660. << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
  7661. << (dl.dylib.compatibility_version & 0xff) << "\n";
  7662. }
  7663. static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
  7664. uint32_t object_size) {
  7665. if (ld.cmd == MachO::LC_CODE_SIGNATURE)
  7666. outs() << " cmd LC_FUNCTION_STARTS\n";
  7667. else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
  7668. outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
  7669. else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
  7670. outs() << " cmd LC_FUNCTION_STARTS\n";
  7671. else if (ld.cmd == MachO::LC_DATA_IN_CODE)
  7672. outs() << " cmd LC_DATA_IN_CODE\n";
  7673. else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
  7674. outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
  7675. else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
  7676. outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
  7677. else
  7678. outs() << " cmd " << ld.cmd << " (?)\n";
  7679. outs() << " cmdsize " << ld.cmdsize;
  7680. if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
  7681. outs() << " Incorrect size\n";
  7682. else
  7683. outs() << "\n";
  7684. outs() << " dataoff " << ld.dataoff;
  7685. if (ld.dataoff > object_size)
  7686. outs() << " (past end of file)\n";
  7687. else
  7688. outs() << "\n";
  7689. outs() << " datasize " << ld.datasize;
  7690. uint64_t big_size = ld.dataoff;
  7691. big_size += ld.datasize;
  7692. if (big_size > object_size)
  7693. outs() << " (past end of file)\n";
  7694. else
  7695. outs() << "\n";
  7696. }
  7697. static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
  7698. uint32_t cputype, bool verbose) {
  7699. StringRef Buf = Obj->getData();
  7700. unsigned Index = 0;
  7701. for (const auto &Command : Obj->load_commands()) {
  7702. outs() << "Load command " << Index++ << "\n";
  7703. if (Command.C.cmd == MachO::LC_SEGMENT) {
  7704. MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
  7705. const char *sg_segname = SLC.segname;
  7706. PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
  7707. SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
  7708. SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
  7709. verbose);
  7710. for (unsigned j = 0; j < SLC.nsects; j++) {
  7711. MachO::section S = Obj->getSection(Command, j);
  7712. PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
  7713. S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
  7714. SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
  7715. }
  7716. } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
  7717. MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
  7718. const char *sg_segname = SLC_64.segname;
  7719. PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
  7720. SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
  7721. SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
  7722. SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
  7723. for (unsigned j = 0; j < SLC_64.nsects; j++) {
  7724. MachO::section_64 S_64 = Obj->getSection64(Command, j);
  7725. PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
  7726. S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
  7727. S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
  7728. sg_segname, filetype, Buf.size(), verbose);
  7729. }
  7730. } else if (Command.C.cmd == MachO::LC_SYMTAB) {
  7731. MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
  7732. PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
  7733. } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
  7734. MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
  7735. MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
  7736. PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
  7737. Obj->is64Bit());
  7738. } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
  7739. Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
  7740. MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
  7741. PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
  7742. } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
  7743. Command.C.cmd == MachO::LC_ID_DYLINKER ||
  7744. Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
  7745. MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
  7746. PrintDyldLoadCommand(Dyld, Command.Ptr);
  7747. } else if (Command.C.cmd == MachO::LC_UUID) {
  7748. MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
  7749. PrintUuidLoadCommand(Uuid);
  7750. } else if (Command.C.cmd == MachO::LC_RPATH) {
  7751. MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
  7752. PrintRpathLoadCommand(Rpath, Command.Ptr);
  7753. } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
  7754. Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
  7755. MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
  7756. PrintVersionMinLoadCommand(Vd);
  7757. } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
  7758. MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
  7759. PrintSourceVersionCommand(Sd);
  7760. } else if (Command.C.cmd == MachO::LC_MAIN) {
  7761. MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
  7762. PrintEntryPointCommand(Ep);
  7763. } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
  7764. MachO::encryption_info_command Ei =
  7765. Obj->getEncryptionInfoCommand(Command);
  7766. PrintEncryptionInfoCommand(Ei, Buf.size());
  7767. } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
  7768. MachO::encryption_info_command_64 Ei =
  7769. Obj->getEncryptionInfoCommand64(Command);
  7770. PrintEncryptionInfoCommand64(Ei, Buf.size());
  7771. } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
  7772. MachO::linker_option_command Lo =
  7773. Obj->getLinkerOptionLoadCommand(Command);
  7774. PrintLinkerOptionCommand(Lo, Command.Ptr);
  7775. } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
  7776. MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
  7777. PrintSubFrameworkCommand(Sf, Command.Ptr);
  7778. } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
  7779. MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
  7780. PrintSubUmbrellaCommand(Sf, Command.Ptr);
  7781. } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
  7782. MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
  7783. PrintSubLibraryCommand(Sl, Command.Ptr);
  7784. } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
  7785. MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
  7786. PrintSubClientCommand(Sc, Command.Ptr);
  7787. } else if (Command.C.cmd == MachO::LC_ROUTINES) {
  7788. MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
  7789. PrintRoutinesCommand(Rc);
  7790. } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
  7791. MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
  7792. PrintRoutinesCommand64(Rc);
  7793. } else if (Command.C.cmd == MachO::LC_THREAD ||
  7794. Command.C.cmd == MachO::LC_UNIXTHREAD) {
  7795. MachO::thread_command Tc = Obj->getThreadCommand(Command);
  7796. PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
  7797. } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
  7798. Command.C.cmd == MachO::LC_ID_DYLIB ||
  7799. Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
  7800. Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
  7801. Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
  7802. Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
  7803. MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
  7804. PrintDylibCommand(Dl, Command.Ptr);
  7805. } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
  7806. Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
  7807. Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
  7808. Command.C.cmd == MachO::LC_DATA_IN_CODE ||
  7809. Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
  7810. Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
  7811. MachO::linkedit_data_command Ld =
  7812. Obj->getLinkeditDataLoadCommand(Command);
  7813. PrintLinkEditDataCommand(Ld, Buf.size());
  7814. } else {
  7815. outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
  7816. << ")\n";
  7817. outs() << " cmdsize " << Command.C.cmdsize << "\n";
  7818. // TODO: get and print the raw bytes of the load command.
  7819. }
  7820. // TODO: print all the other kinds of load commands.
  7821. }
  7822. }
  7823. static void getAndPrintMachHeader(const MachOObjectFile *Obj,
  7824. uint32_t &filetype, uint32_t &cputype,
  7825. bool verbose) {
  7826. if (Obj->is64Bit()) {
  7827. MachO::mach_header_64 H_64;
  7828. H_64 = Obj->getHeader64();
  7829. PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
  7830. H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
  7831. filetype = H_64.filetype;
  7832. cputype = H_64.cputype;
  7833. } else {
  7834. MachO::mach_header H;
  7835. H = Obj->getHeader();
  7836. PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
  7837. H.sizeofcmds, H.flags, verbose);
  7838. filetype = H.filetype;
  7839. cputype = H.cputype;
  7840. }
  7841. }
  7842. void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
  7843. const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
  7844. uint32_t filetype = 0;
  7845. uint32_t cputype = 0;
  7846. getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
  7847. PrintLoadCommands(file, filetype, cputype, !NonVerbose);
  7848. }
  7849. //===----------------------------------------------------------------------===//
  7850. // export trie dumping
  7851. //===----------------------------------------------------------------------===//
  7852. void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
  7853. for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
  7854. uint64_t Flags = Entry.flags();
  7855. bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
  7856. bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
  7857. bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
  7858. MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
  7859. bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
  7860. MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
  7861. bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
  7862. if (ReExport)
  7863. outs() << "[re-export] ";
  7864. else
  7865. outs() << format("0x%08llX ",
  7866. Entry.address()); // FIXME:add in base address
  7867. outs() << Entry.name();
  7868. if (WeakDef || ThreadLocal || Resolver || Abs) {
  7869. bool NeedsComma = false;
  7870. outs() << " [";
  7871. if (WeakDef) {
  7872. outs() << "weak_def";
  7873. NeedsComma = true;
  7874. }
  7875. if (ThreadLocal) {
  7876. if (NeedsComma)
  7877. outs() << ", ";
  7878. outs() << "per-thread";
  7879. NeedsComma = true;
  7880. }
  7881. if (Abs) {
  7882. if (NeedsComma)
  7883. outs() << ", ";
  7884. outs() << "absolute";
  7885. NeedsComma = true;
  7886. }
  7887. if (Resolver) {
  7888. if (NeedsComma)
  7889. outs() << ", ";
  7890. outs() << format("resolver=0x%08llX", Entry.other());
  7891. NeedsComma = true;
  7892. }
  7893. outs() << "]";
  7894. }
  7895. if (ReExport) {
  7896. StringRef DylibName = "unknown";
  7897. int Ordinal = Entry.other() - 1;
  7898. Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
  7899. if (Entry.otherName().empty())
  7900. outs() << " (from " << DylibName << ")";
  7901. else
  7902. outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
  7903. }
  7904. outs() << "\n";
  7905. }
  7906. }
  7907. //===----------------------------------------------------------------------===//
  7908. // rebase table dumping
  7909. //===----------------------------------------------------------------------===//
  7910. namespace {
  7911. class SegInfo {
  7912. public:
  7913. SegInfo(const object::MachOObjectFile *Obj);
  7914. StringRef segmentName(uint32_t SegIndex);
  7915. StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
  7916. uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
  7917. private:
  7918. struct SectionInfo {
  7919. uint64_t Address;
  7920. uint64_t Size;
  7921. StringRef SectionName;
  7922. StringRef SegmentName;
  7923. uint64_t OffsetInSegment;
  7924. uint64_t SegmentStartAddress;
  7925. uint32_t SegmentIndex;
  7926. };
  7927. const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
  7928. SmallVector<SectionInfo, 32> Sections;
  7929. };
  7930. }
  7931. SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
  7932. // Build table of sections so segIndex/offset pairs can be translated.
  7933. uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
  7934. StringRef CurSegName;
  7935. uint64_t CurSegAddress;
  7936. for (const SectionRef &Section : Obj->sections()) {
  7937. SectionInfo Info;
  7938. if (error(Section.getName(Info.SectionName)))
  7939. return;
  7940. Info.Address = Section.getAddress();
  7941. Info.Size = Section.getSize();
  7942. Info.SegmentName =
  7943. Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
  7944. if (!Info.SegmentName.equals(CurSegName)) {
  7945. ++CurSegIndex;
  7946. CurSegName = Info.SegmentName;
  7947. CurSegAddress = Info.Address;
  7948. }
  7949. Info.SegmentIndex = CurSegIndex - 1;
  7950. Info.OffsetInSegment = Info.Address - CurSegAddress;
  7951. Info.SegmentStartAddress = CurSegAddress;
  7952. Sections.push_back(Info);
  7953. }
  7954. }
  7955. StringRef SegInfo::segmentName(uint32_t SegIndex) {
  7956. for (const SectionInfo &SI : Sections) {
  7957. if (SI.SegmentIndex == SegIndex)
  7958. return SI.SegmentName;
  7959. }
  7960. llvm_unreachable("invalid segIndex");
  7961. }
  7962. const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
  7963. uint64_t OffsetInSeg) {
  7964. for (const SectionInfo &SI : Sections) {
  7965. if (SI.SegmentIndex != SegIndex)
  7966. continue;
  7967. if (SI.OffsetInSegment > OffsetInSeg)
  7968. continue;
  7969. if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
  7970. continue;
  7971. return SI;
  7972. }
  7973. llvm_unreachable("segIndex and offset not in any section");
  7974. }
  7975. StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
  7976. return findSection(SegIndex, OffsetInSeg).SectionName;
  7977. }
  7978. uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
  7979. const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
  7980. return SI.SegmentStartAddress + OffsetInSeg;
  7981. }
  7982. void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
  7983. // Build table of sections so names can used in final output.
  7984. SegInfo sectionTable(Obj);
  7985. outs() << "segment section address type\n";
  7986. for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
  7987. uint32_t SegIndex = Entry.segmentIndex();
  7988. uint64_t OffsetInSeg = Entry.segmentOffset();
  7989. StringRef SegmentName = sectionTable.segmentName(SegIndex);
  7990. StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
  7991. uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
  7992. // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
  7993. outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
  7994. SegmentName.str().c_str(), SectionName.str().c_str(),
  7995. Address, Entry.typeName().str().c_str());
  7996. }
  7997. }
  7998. static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
  7999. StringRef DylibName;
  8000. switch (Ordinal) {
  8001. case MachO::BIND_SPECIAL_DYLIB_SELF:
  8002. return "this-image";
  8003. case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
  8004. return "main-executable";
  8005. case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
  8006. return "flat-namespace";
  8007. default:
  8008. if (Ordinal > 0) {
  8009. std::error_code EC =
  8010. Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
  8011. if (EC)
  8012. return "<<bad library ordinal>>";
  8013. return DylibName;
  8014. }
  8015. }
  8016. return "<<unknown special ordinal>>";
  8017. }
  8018. //===----------------------------------------------------------------------===//
  8019. // bind table dumping
  8020. //===----------------------------------------------------------------------===//
  8021. void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
  8022. // Build table of sections so names can used in final output.
  8023. SegInfo sectionTable(Obj);
  8024. outs() << "segment section address type "
  8025. "addend dylib symbol\n";
  8026. for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
  8027. uint32_t SegIndex = Entry.segmentIndex();
  8028. uint64_t OffsetInSeg = Entry.segmentOffset();
  8029. StringRef SegmentName = sectionTable.segmentName(SegIndex);
  8030. StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
  8031. uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
  8032. // Table lines look like:
  8033. // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
  8034. StringRef Attr;
  8035. if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
  8036. Attr = " (weak_import)";
  8037. outs() << left_justify(SegmentName, 8) << " "
  8038. << left_justify(SectionName, 18) << " "
  8039. << format_hex(Address, 10, true) << " "
  8040. << left_justify(Entry.typeName(), 8) << " "
  8041. << format_decimal(Entry.addend(), 8) << " "
  8042. << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
  8043. << Entry.symbolName() << Attr << "\n";
  8044. }
  8045. }
  8046. //===----------------------------------------------------------------------===//
  8047. // lazy bind table dumping
  8048. //===----------------------------------------------------------------------===//
  8049. void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
  8050. // Build table of sections so names can used in final output.
  8051. SegInfo sectionTable(Obj);
  8052. outs() << "segment section address "
  8053. "dylib symbol\n";
  8054. for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
  8055. uint32_t SegIndex = Entry.segmentIndex();
  8056. uint64_t OffsetInSeg = Entry.segmentOffset();
  8057. StringRef SegmentName = sectionTable.segmentName(SegIndex);
  8058. StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
  8059. uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
  8060. // Table lines look like:
  8061. // __DATA __got 0x00012010 libSystem ___stack_chk_guard
  8062. outs() << left_justify(SegmentName, 8) << " "
  8063. << left_justify(SectionName, 18) << " "
  8064. << format_hex(Address, 10, true) << " "
  8065. << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
  8066. << Entry.symbolName() << "\n";
  8067. }
  8068. }
  8069. //===----------------------------------------------------------------------===//
  8070. // weak bind table dumping
  8071. //===----------------------------------------------------------------------===//
  8072. void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
  8073. // Build table of sections so names can used in final output.
  8074. SegInfo sectionTable(Obj);
  8075. outs() << "segment section address "
  8076. "type addend symbol\n";
  8077. for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
  8078. // Strong symbols don't have a location to update.
  8079. if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
  8080. outs() << " strong "
  8081. << Entry.symbolName() << "\n";
  8082. continue;
  8083. }
  8084. uint32_t SegIndex = Entry.segmentIndex();
  8085. uint64_t OffsetInSeg = Entry.segmentOffset();
  8086. StringRef SegmentName = sectionTable.segmentName(SegIndex);
  8087. StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
  8088. uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
  8089. // Table lines look like:
  8090. // __DATA __data 0x00001000 pointer 0 _foo
  8091. outs() << left_justify(SegmentName, 8) << " "
  8092. << left_justify(SectionName, 18) << " "
  8093. << format_hex(Address, 10, true) << " "
  8094. << left_justify(Entry.typeName(), 8) << " "
  8095. << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
  8096. << "\n";
  8097. }
  8098. }
  8099. // get_dyld_bind_info_symbolname() is used for disassembly and passed an
  8100. // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
  8101. // information for that address. If the address is found its binding symbol
  8102. // name is returned. If not nullptr is returned.
  8103. static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
  8104. struct DisassembleInfo *info) {
  8105. if (info->bindtable == nullptr) {
  8106. info->bindtable = new (BindTable);
  8107. SegInfo sectionTable(info->O);
  8108. for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
  8109. uint32_t SegIndex = Entry.segmentIndex();
  8110. uint64_t OffsetInSeg = Entry.segmentOffset();
  8111. uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
  8112. const char *SymbolName = nullptr;
  8113. StringRef name = Entry.symbolName();
  8114. if (!name.empty())
  8115. SymbolName = name.data();
  8116. info->bindtable->push_back(std::make_pair(Address, SymbolName));
  8117. }
  8118. }
  8119. for (bind_table_iterator BI = info->bindtable->begin(),
  8120. BE = info->bindtable->end();
  8121. BI != BE; ++BI) {
  8122. uint64_t Address = BI->first;
  8123. if (ReferenceValue == Address) {
  8124. const char *SymbolName = BI->second;
  8125. return SymbolName;
  8126. }
  8127. }
  8128. return nullptr;
  8129. }