MachODump.cpp 349 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619
  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/BinaryFormat/MachO.h"
  19. #include "llvm/Config/config.h"
  20. #include "llvm/DebugInfo/DIContext.h"
  21. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  22. #include "llvm/Demangle/Demangle.h"
  23. #include "llvm/MC/MCAsmInfo.h"
  24. #include "llvm/MC/MCContext.h"
  25. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  26. #include "llvm/MC/MCInst.h"
  27. #include "llvm/MC/MCInstPrinter.h"
  28. #include "llvm/MC/MCInstrDesc.h"
  29. #include "llvm/MC/MCInstrInfo.h"
  30. #include "llvm/MC/MCRegisterInfo.h"
  31. #include "llvm/MC/MCSubtargetInfo.h"
  32. #include "llvm/Object/MachO.h"
  33. #include "llvm/Object/MachOUniversal.h"
  34. #include "llvm/Support/Casting.h"
  35. #include "llvm/Support/CommandLine.h"
  36. #include "llvm/Support/Debug.h"
  37. #include "llvm/Support/Endian.h"
  38. #include "llvm/Support/Format.h"
  39. #include "llvm/Support/FormattedStream.h"
  40. #include "llvm/Support/GraphWriter.h"
  41. #include "llvm/Support/LEB128.h"
  42. #include "llvm/Support/MemoryBuffer.h"
  43. #include "llvm/Support/TargetRegistry.h"
  44. #include "llvm/Support/TargetSelect.h"
  45. #include "llvm/Support/ToolOutputFile.h"
  46. #include "llvm/Support/raw_ostream.h"
  47. #include <algorithm>
  48. #include <cstring>
  49. #include <system_error>
  50. #ifdef HAVE_LIBXAR
  51. extern "C" {
  52. #include <xar/xar.h>
  53. }
  54. #endif
  55. using namespace llvm;
  56. using namespace object;
  57. static cl::opt<bool>
  58. UseDbg("g",
  59. cl::desc("Print line information from debug info if available"));
  60. static cl::opt<std::string> DSYMFile("dsym",
  61. cl::desc("Use .dSYM file for debug info"));
  62. static cl::opt<bool> FullLeadingAddr("full-leading-addr",
  63. cl::desc("Print full leading address"));
  64. static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
  65. cl::desc("Print no leading headers"));
  66. cl::opt<bool> llvm::UniversalHeaders("universal-headers",
  67. cl::desc("Print Mach-O universal headers "
  68. "(requires -macho)"));
  69. cl::opt<bool>
  70. llvm::ArchiveHeaders("archive-headers",
  71. cl::desc("Print archive headers for Mach-O archives "
  72. "(requires -macho)"));
  73. cl::opt<bool>
  74. ArchiveMemberOffsets("archive-member-offsets",
  75. cl::desc("Print the offset to each archive member for "
  76. "Mach-O archives (requires -macho and "
  77. "-archive-headers)"));
  78. cl::opt<bool>
  79. llvm::IndirectSymbols("indirect-symbols",
  80. cl::desc("Print indirect symbol table for Mach-O "
  81. "objects (requires -macho)"));
  82. cl::opt<bool>
  83. llvm::DataInCode("data-in-code",
  84. cl::desc("Print the data in code table for Mach-O objects "
  85. "(requires -macho)"));
  86. cl::opt<bool>
  87. llvm::LinkOptHints("link-opt-hints",
  88. cl::desc("Print the linker optimization hints 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. llvm::Triple TT(TripleName);
  126. if (TripleName.empty()) {
  127. TT = MachOObj->getArchTriple(McpuDefault);
  128. TripleName = TT.str();
  129. }
  130. if (TT.getArch() == Triple::arm) {
  131. // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
  132. // that support ARM are also capable of Thumb mode.
  133. llvm::Triple ThumbTriple = TT;
  134. std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
  135. ThumbTriple.setArchName(ThumbName);
  136. ThumbTripleName = ThumbTriple.str();
  137. }
  138. // Get the target specific parser.
  139. std::string Error;
  140. const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
  141. if (TheTarget && ThumbTripleName.empty())
  142. return TheTarget;
  143. *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
  144. if (*ThumbTarget)
  145. return TheTarget;
  146. errs() << "llvm-objdump: error: unable to get target for '";
  147. if (!TheTarget)
  148. errs() << TripleName;
  149. else
  150. errs() << ThumbTripleName;
  151. errs() << "', see --version and --triple.\n";
  152. return nullptr;
  153. }
  154. struct SymbolSorter {
  155. bool operator()(const SymbolRef &A, const SymbolRef &B) {
  156. Expected<SymbolRef::Type> ATypeOrErr = A.getType();
  157. if (!ATypeOrErr)
  158. report_error(A.getObject()->getFileName(), ATypeOrErr.takeError());
  159. SymbolRef::Type AType = *ATypeOrErr;
  160. Expected<SymbolRef::Type> BTypeOrErr = B.getType();
  161. if (!BTypeOrErr)
  162. report_error(B.getObject()->getFileName(), BTypeOrErr.takeError());
  163. SymbolRef::Type BType = *BTypeOrErr;
  164. uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
  165. uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
  166. return AAddr < BAddr;
  167. }
  168. };
  169. // Types for the storted data in code table that is built before disassembly
  170. // and the predicate function to sort them.
  171. typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
  172. typedef std::vector<DiceTableEntry> DiceTable;
  173. typedef DiceTable::iterator dice_table_iterator;
  174. // This is used to search for a data in code table entry for the PC being
  175. // disassembled. The j parameter has the PC in j.first. A single data in code
  176. // table entry can cover many bytes for each of its Kind's. So if the offset,
  177. // aka the i.first value, of the data in code table entry plus its Length
  178. // covers the PC being searched for this will return true. If not it will
  179. // return false.
  180. static bool compareDiceTableEntries(const DiceTableEntry &i,
  181. const DiceTableEntry &j) {
  182. uint16_t Length;
  183. i.second.getLength(Length);
  184. return j.first >= i.first && j.first < i.first + Length;
  185. }
  186. static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
  187. unsigned short Kind) {
  188. uint32_t Value, Size = 1;
  189. switch (Kind) {
  190. default:
  191. case MachO::DICE_KIND_DATA:
  192. if (Length >= 4) {
  193. if (!NoShowRawInsn)
  194. dumpBytes(makeArrayRef(bytes, 4), outs());
  195. Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
  196. outs() << "\t.long " << Value;
  197. Size = 4;
  198. } else if (Length >= 2) {
  199. if (!NoShowRawInsn)
  200. dumpBytes(makeArrayRef(bytes, 2), outs());
  201. Value = bytes[1] << 8 | bytes[0];
  202. outs() << "\t.short " << Value;
  203. Size = 2;
  204. } else {
  205. if (!NoShowRawInsn)
  206. dumpBytes(makeArrayRef(bytes, 2), outs());
  207. Value = bytes[0];
  208. outs() << "\t.byte " << Value;
  209. Size = 1;
  210. }
  211. if (Kind == MachO::DICE_KIND_DATA)
  212. outs() << "\t@ KIND_DATA\n";
  213. else
  214. outs() << "\t@ data in code kind = " << Kind << "\n";
  215. break;
  216. case MachO::DICE_KIND_JUMP_TABLE8:
  217. if (!NoShowRawInsn)
  218. dumpBytes(makeArrayRef(bytes, 1), outs());
  219. Value = bytes[0];
  220. outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
  221. Size = 1;
  222. break;
  223. case MachO::DICE_KIND_JUMP_TABLE16:
  224. if (!NoShowRawInsn)
  225. dumpBytes(makeArrayRef(bytes, 2), outs());
  226. Value = bytes[1] << 8 | bytes[0];
  227. outs() << "\t.short " << format("%5u", Value & 0xffff)
  228. << "\t@ KIND_JUMP_TABLE16\n";
  229. Size = 2;
  230. break;
  231. case MachO::DICE_KIND_JUMP_TABLE32:
  232. case MachO::DICE_KIND_ABS_JUMP_TABLE32:
  233. if (!NoShowRawInsn)
  234. dumpBytes(makeArrayRef(bytes, 4), outs());
  235. Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
  236. outs() << "\t.long " << Value;
  237. if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
  238. outs() << "\t@ KIND_JUMP_TABLE32\n";
  239. else
  240. outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
  241. Size = 4;
  242. break;
  243. }
  244. return Size;
  245. }
  246. static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
  247. std::vector<SectionRef> &Sections,
  248. std::vector<SymbolRef> &Symbols,
  249. SmallVectorImpl<uint64_t> &FoundFns,
  250. uint64_t &BaseSegmentAddress) {
  251. for (const SymbolRef &Symbol : MachOObj->symbols()) {
  252. Expected<StringRef> SymName = Symbol.getName();
  253. if (!SymName)
  254. report_error(MachOObj->getFileName(), SymName.takeError());
  255. if (!SymName->startswith("ltmp"))
  256. Symbols.push_back(Symbol);
  257. }
  258. for (const SectionRef &Section : MachOObj->sections()) {
  259. StringRef SectName;
  260. Section.getName(SectName);
  261. Sections.push_back(Section);
  262. }
  263. bool BaseSegmentAddressSet = false;
  264. for (const auto &Command : MachOObj->load_commands()) {
  265. if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
  266. // We found a function starts segment, parse the addresses for later
  267. // consumption.
  268. MachO::linkedit_data_command LLC =
  269. MachOObj->getLinkeditDataLoadCommand(Command);
  270. MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
  271. } else if (Command.C.cmd == MachO::LC_SEGMENT) {
  272. MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
  273. StringRef SegName = SLC.segname;
  274. if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
  275. BaseSegmentAddressSet = true;
  276. BaseSegmentAddress = SLC.vmaddr;
  277. }
  278. }
  279. }
  280. }
  281. static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
  282. uint32_t n, uint32_t count,
  283. uint32_t stride, uint64_t addr) {
  284. MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
  285. uint32_t nindirectsyms = Dysymtab.nindirectsyms;
  286. if (n > nindirectsyms)
  287. outs() << " (entries start past the end of the indirect symbol "
  288. "table) (reserved1 field greater than the table size)";
  289. else if (n + count > nindirectsyms)
  290. outs() << " (entries extends past the end of the indirect symbol "
  291. "table)";
  292. outs() << "\n";
  293. uint32_t cputype = O->getHeader().cputype;
  294. if (cputype & MachO::CPU_ARCH_ABI64)
  295. outs() << "address index";
  296. else
  297. outs() << "address index";
  298. if (verbose)
  299. outs() << " name\n";
  300. else
  301. outs() << "\n";
  302. for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
  303. if (cputype & MachO::CPU_ARCH_ABI64)
  304. outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
  305. else
  306. outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
  307. MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
  308. uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
  309. if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
  310. outs() << "LOCAL\n";
  311. continue;
  312. }
  313. if (indirect_symbol ==
  314. (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
  315. outs() << "LOCAL ABSOLUTE\n";
  316. continue;
  317. }
  318. if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
  319. outs() << "ABSOLUTE\n";
  320. continue;
  321. }
  322. outs() << format("%5u ", indirect_symbol);
  323. if (verbose) {
  324. MachO::symtab_command Symtab = O->getSymtabLoadCommand();
  325. if (indirect_symbol < Symtab.nsyms) {
  326. symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
  327. SymbolRef Symbol = *Sym;
  328. Expected<StringRef> SymName = Symbol.getName();
  329. if (!SymName)
  330. report_error(O->getFileName(), SymName.takeError());
  331. outs() << *SymName;
  332. } else {
  333. outs() << "?";
  334. }
  335. }
  336. outs() << "\n";
  337. }
  338. }
  339. static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
  340. for (const auto &Load : O->load_commands()) {
  341. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  342. MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
  343. for (unsigned J = 0; J < Seg.nsects; ++J) {
  344. MachO::section_64 Sec = O->getSection64(Load, J);
  345. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  346. if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  347. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  348. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  349. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  350. section_type == MachO::S_SYMBOL_STUBS) {
  351. uint32_t stride;
  352. if (section_type == MachO::S_SYMBOL_STUBS)
  353. stride = Sec.reserved2;
  354. else
  355. stride = 8;
  356. if (stride == 0) {
  357. outs() << "Can't print indirect symbols for (" << Sec.segname << ","
  358. << Sec.sectname << ") "
  359. << "(size of stubs in reserved2 field is zero)\n";
  360. continue;
  361. }
  362. uint32_t count = Sec.size / stride;
  363. outs() << "Indirect symbols for (" << Sec.segname << ","
  364. << Sec.sectname << ") " << count << " entries";
  365. uint32_t n = Sec.reserved1;
  366. PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
  367. }
  368. }
  369. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  370. MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
  371. for (unsigned J = 0; J < Seg.nsects; ++J) {
  372. MachO::section Sec = O->getSection(Load, J);
  373. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  374. if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  375. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  376. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  377. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  378. section_type == MachO::S_SYMBOL_STUBS) {
  379. uint32_t stride;
  380. if (section_type == MachO::S_SYMBOL_STUBS)
  381. stride = Sec.reserved2;
  382. else
  383. stride = 4;
  384. if (stride == 0) {
  385. outs() << "Can't print indirect symbols for (" << Sec.segname << ","
  386. << Sec.sectname << ") "
  387. << "(size of stubs in reserved2 field is zero)\n";
  388. continue;
  389. }
  390. uint32_t count = Sec.size / stride;
  391. outs() << "Indirect symbols for (" << Sec.segname << ","
  392. << Sec.sectname << ") " << count << " entries";
  393. uint32_t n = Sec.reserved1;
  394. PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
  395. }
  396. }
  397. }
  398. }
  399. }
  400. static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
  401. MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
  402. uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
  403. outs() << "Data in code table (" << nentries << " entries)\n";
  404. outs() << "offset length kind\n";
  405. for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
  406. ++DI) {
  407. uint32_t Offset;
  408. DI->getOffset(Offset);
  409. outs() << format("0x%08" PRIx32, Offset) << " ";
  410. uint16_t Length;
  411. DI->getLength(Length);
  412. outs() << format("%6u", Length) << " ";
  413. uint16_t Kind;
  414. DI->getKind(Kind);
  415. if (verbose) {
  416. switch (Kind) {
  417. case MachO::DICE_KIND_DATA:
  418. outs() << "DATA";
  419. break;
  420. case MachO::DICE_KIND_JUMP_TABLE8:
  421. outs() << "JUMP_TABLE8";
  422. break;
  423. case MachO::DICE_KIND_JUMP_TABLE16:
  424. outs() << "JUMP_TABLE16";
  425. break;
  426. case MachO::DICE_KIND_JUMP_TABLE32:
  427. outs() << "JUMP_TABLE32";
  428. break;
  429. case MachO::DICE_KIND_ABS_JUMP_TABLE32:
  430. outs() << "ABS_JUMP_TABLE32";
  431. break;
  432. default:
  433. outs() << format("0x%04" PRIx32, Kind);
  434. break;
  435. }
  436. } else
  437. outs() << format("0x%04" PRIx32, Kind);
  438. outs() << "\n";
  439. }
  440. }
  441. static void PrintLinkOptHints(MachOObjectFile *O) {
  442. MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
  443. const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
  444. uint32_t nloh = LohLC.datasize;
  445. outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
  446. for (uint32_t i = 0; i < nloh;) {
  447. unsigned n;
  448. uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
  449. i += n;
  450. outs() << " identifier " << identifier << " ";
  451. if (i >= nloh)
  452. return;
  453. switch (identifier) {
  454. case 1:
  455. outs() << "AdrpAdrp\n";
  456. break;
  457. case 2:
  458. outs() << "AdrpLdr\n";
  459. break;
  460. case 3:
  461. outs() << "AdrpAddLdr\n";
  462. break;
  463. case 4:
  464. outs() << "AdrpLdrGotLdr\n";
  465. break;
  466. case 5:
  467. outs() << "AdrpAddStr\n";
  468. break;
  469. case 6:
  470. outs() << "AdrpLdrGotStr\n";
  471. break;
  472. case 7:
  473. outs() << "AdrpAdd\n";
  474. break;
  475. case 8:
  476. outs() << "AdrpLdrGot\n";
  477. break;
  478. default:
  479. outs() << "Unknown identifier value\n";
  480. break;
  481. }
  482. uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
  483. i += n;
  484. outs() << " narguments " << narguments << "\n";
  485. if (i >= nloh)
  486. return;
  487. for (uint32_t j = 0; j < narguments; j++) {
  488. uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
  489. i += n;
  490. outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
  491. if (i >= nloh)
  492. return;
  493. }
  494. }
  495. }
  496. static void PrintDylibs(MachOObjectFile *O, bool JustId) {
  497. unsigned Index = 0;
  498. for (const auto &Load : O->load_commands()) {
  499. if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
  500. (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
  501. Load.C.cmd == MachO::LC_LOAD_DYLIB ||
  502. Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
  503. Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
  504. Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
  505. Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
  506. MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
  507. if (dl.dylib.name < dl.cmdsize) {
  508. const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
  509. if (JustId)
  510. outs() << p << "\n";
  511. else {
  512. outs() << "\t" << p;
  513. outs() << " (compatibility version "
  514. << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
  515. << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
  516. << (dl.dylib.compatibility_version & 0xff) << ",";
  517. outs() << " current version "
  518. << ((dl.dylib.current_version >> 16) & 0xffff) << "."
  519. << ((dl.dylib.current_version >> 8) & 0xff) << "."
  520. << (dl.dylib.current_version & 0xff) << ")\n";
  521. }
  522. } else {
  523. outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
  524. if (Load.C.cmd == MachO::LC_ID_DYLIB)
  525. outs() << "LC_ID_DYLIB ";
  526. else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
  527. outs() << "LC_LOAD_DYLIB ";
  528. else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
  529. outs() << "LC_LOAD_WEAK_DYLIB ";
  530. else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
  531. outs() << "LC_LAZY_LOAD_DYLIB ";
  532. else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
  533. outs() << "LC_REEXPORT_DYLIB ";
  534. else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
  535. outs() << "LC_LOAD_UPWARD_DYLIB ";
  536. else
  537. outs() << "LC_??? ";
  538. outs() << "command " << Index++ << "\n";
  539. }
  540. }
  541. }
  542. }
  543. typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
  544. static void CreateSymbolAddressMap(MachOObjectFile *O,
  545. SymbolAddressMap *AddrMap) {
  546. // Create a map of symbol addresses to symbol names.
  547. for (const SymbolRef &Symbol : O->symbols()) {
  548. Expected<SymbolRef::Type> STOrErr = Symbol.getType();
  549. if (!STOrErr)
  550. report_error(O->getFileName(), STOrErr.takeError());
  551. SymbolRef::Type ST = *STOrErr;
  552. if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
  553. ST == SymbolRef::ST_Other) {
  554. uint64_t Address = Symbol.getValue();
  555. Expected<StringRef> SymNameOrErr = Symbol.getName();
  556. if (!SymNameOrErr)
  557. report_error(O->getFileName(), SymNameOrErr.takeError());
  558. StringRef SymName = *SymNameOrErr;
  559. if (!SymName.startswith(".objc"))
  560. (*AddrMap)[Address] = SymName;
  561. }
  562. }
  563. }
  564. // GuessSymbolName is passed the address of what might be a symbol and a
  565. // pointer to the SymbolAddressMap. It returns the name of a symbol
  566. // with that address or nullptr if no symbol is found with that address.
  567. static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
  568. const char *SymbolName = nullptr;
  569. // A DenseMap can't lookup up some values.
  570. if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
  571. StringRef name = AddrMap->lookup(value);
  572. if (!name.empty())
  573. SymbolName = name.data();
  574. }
  575. return SymbolName;
  576. }
  577. static void DumpCstringChar(const char c) {
  578. char p[2];
  579. p[0] = c;
  580. p[1] = '\0';
  581. outs().write_escaped(p);
  582. }
  583. static void DumpCstringSection(MachOObjectFile *O, const char *sect,
  584. uint32_t sect_size, uint64_t sect_addr,
  585. bool print_addresses) {
  586. for (uint32_t i = 0; i < sect_size; i++) {
  587. if (print_addresses) {
  588. if (O->is64Bit())
  589. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  590. else
  591. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  592. }
  593. for (; i < sect_size && sect[i] != '\0'; i++)
  594. DumpCstringChar(sect[i]);
  595. if (i < sect_size && sect[i] == '\0')
  596. outs() << "\n";
  597. }
  598. }
  599. static void DumpLiteral4(uint32_t l, float f) {
  600. outs() << format("0x%08" PRIx32, l);
  601. if ((l & 0x7f800000) != 0x7f800000)
  602. outs() << format(" (%.16e)\n", f);
  603. else {
  604. if (l == 0x7f800000)
  605. outs() << " (+Infinity)\n";
  606. else if (l == 0xff800000)
  607. outs() << " (-Infinity)\n";
  608. else if ((l & 0x00400000) == 0x00400000)
  609. outs() << " (non-signaling Not-a-Number)\n";
  610. else
  611. outs() << " (signaling Not-a-Number)\n";
  612. }
  613. }
  614. static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
  615. uint32_t sect_size, uint64_t sect_addr,
  616. bool print_addresses) {
  617. for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
  618. if (print_addresses) {
  619. if (O->is64Bit())
  620. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  621. else
  622. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  623. }
  624. float f;
  625. memcpy(&f, sect + i, sizeof(float));
  626. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  627. sys::swapByteOrder(f);
  628. uint32_t l;
  629. memcpy(&l, sect + i, sizeof(uint32_t));
  630. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  631. sys::swapByteOrder(l);
  632. DumpLiteral4(l, f);
  633. }
  634. }
  635. static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
  636. double d) {
  637. outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
  638. uint32_t Hi, Lo;
  639. Hi = (O->isLittleEndian()) ? l1 : l0;
  640. Lo = (O->isLittleEndian()) ? l0 : l1;
  641. // Hi is the high word, so this is equivalent to if(isfinite(d))
  642. if ((Hi & 0x7ff00000) != 0x7ff00000)
  643. outs() << format(" (%.16e)\n", d);
  644. else {
  645. if (Hi == 0x7ff00000 && Lo == 0)
  646. outs() << " (+Infinity)\n";
  647. else if (Hi == 0xfff00000 && Lo == 0)
  648. outs() << " (-Infinity)\n";
  649. else if ((Hi & 0x00080000) == 0x00080000)
  650. outs() << " (non-signaling Not-a-Number)\n";
  651. else
  652. outs() << " (signaling Not-a-Number)\n";
  653. }
  654. }
  655. static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
  656. uint32_t sect_size, uint64_t sect_addr,
  657. bool print_addresses) {
  658. for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
  659. if (print_addresses) {
  660. if (O->is64Bit())
  661. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  662. else
  663. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  664. }
  665. double d;
  666. memcpy(&d, sect + i, sizeof(double));
  667. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  668. sys::swapByteOrder(d);
  669. uint32_t l0, l1;
  670. memcpy(&l0, sect + i, sizeof(uint32_t));
  671. memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
  672. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  673. sys::swapByteOrder(l0);
  674. sys::swapByteOrder(l1);
  675. }
  676. DumpLiteral8(O, l0, l1, d);
  677. }
  678. }
  679. static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
  680. outs() << format("0x%08" PRIx32, l0) << " ";
  681. outs() << format("0x%08" PRIx32, l1) << " ";
  682. outs() << format("0x%08" PRIx32, l2) << " ";
  683. outs() << format("0x%08" PRIx32, l3) << "\n";
  684. }
  685. static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
  686. uint32_t sect_size, uint64_t sect_addr,
  687. bool print_addresses) {
  688. for (uint32_t i = 0; i < sect_size; i += 16) {
  689. if (print_addresses) {
  690. if (O->is64Bit())
  691. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  692. else
  693. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  694. }
  695. uint32_t l0, l1, l2, l3;
  696. memcpy(&l0, sect + i, sizeof(uint32_t));
  697. memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
  698. memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
  699. memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
  700. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  701. sys::swapByteOrder(l0);
  702. sys::swapByteOrder(l1);
  703. sys::swapByteOrder(l2);
  704. sys::swapByteOrder(l3);
  705. }
  706. DumpLiteral16(l0, l1, l2, l3);
  707. }
  708. }
  709. static void DumpLiteralPointerSection(MachOObjectFile *O,
  710. const SectionRef &Section,
  711. const char *sect, uint32_t sect_size,
  712. uint64_t sect_addr,
  713. bool print_addresses) {
  714. // Collect the literal sections in this Mach-O file.
  715. std::vector<SectionRef> LiteralSections;
  716. for (const SectionRef &Section : O->sections()) {
  717. DataRefImpl Ref = Section.getRawDataRefImpl();
  718. uint32_t section_type;
  719. if (O->is64Bit()) {
  720. const MachO::section_64 Sec = O->getSection64(Ref);
  721. section_type = Sec.flags & MachO::SECTION_TYPE;
  722. } else {
  723. const MachO::section Sec = O->getSection(Ref);
  724. section_type = Sec.flags & MachO::SECTION_TYPE;
  725. }
  726. if (section_type == MachO::S_CSTRING_LITERALS ||
  727. section_type == MachO::S_4BYTE_LITERALS ||
  728. section_type == MachO::S_8BYTE_LITERALS ||
  729. section_type == MachO::S_16BYTE_LITERALS)
  730. LiteralSections.push_back(Section);
  731. }
  732. // Set the size of the literal pointer.
  733. uint32_t lp_size = O->is64Bit() ? 8 : 4;
  734. // Collect the external relocation symbols for the literal pointers.
  735. std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
  736. for (const RelocationRef &Reloc : Section.relocations()) {
  737. DataRefImpl Rel;
  738. MachO::any_relocation_info RE;
  739. bool isExtern = false;
  740. Rel = Reloc.getRawDataRefImpl();
  741. RE = O->getRelocation(Rel);
  742. isExtern = O->getPlainRelocationExternal(RE);
  743. if (isExtern) {
  744. uint64_t RelocOffset = Reloc.getOffset();
  745. symbol_iterator RelocSym = Reloc.getSymbol();
  746. Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
  747. }
  748. }
  749. array_pod_sort(Relocs.begin(), Relocs.end());
  750. // Dump each literal pointer.
  751. for (uint32_t i = 0; i < sect_size; i += lp_size) {
  752. if (print_addresses) {
  753. if (O->is64Bit())
  754. outs() << format("%016" PRIx64, sect_addr + i) << " ";
  755. else
  756. outs() << format("%08" PRIx64, sect_addr + i) << " ";
  757. }
  758. uint64_t lp;
  759. if (O->is64Bit()) {
  760. memcpy(&lp, sect + i, sizeof(uint64_t));
  761. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  762. sys::swapByteOrder(lp);
  763. } else {
  764. uint32_t li;
  765. memcpy(&li, sect + i, sizeof(uint32_t));
  766. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  767. sys::swapByteOrder(li);
  768. lp = li;
  769. }
  770. // First look for an external relocation entry for this literal pointer.
  771. auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
  772. return P.first == i;
  773. });
  774. if (Reloc != Relocs.end()) {
  775. symbol_iterator RelocSym = Reloc->second;
  776. Expected<StringRef> SymName = RelocSym->getName();
  777. if (!SymName)
  778. report_error(O->getFileName(), SymName.takeError());
  779. outs() << "external relocation entry for symbol:" << *SymName << "\n";
  780. continue;
  781. }
  782. // For local references see what the section the literal pointer points to.
  783. auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
  784. return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
  785. });
  786. if (Sect == LiteralSections.end()) {
  787. outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
  788. continue;
  789. }
  790. uint64_t SectAddress = Sect->getAddress();
  791. uint64_t SectSize = Sect->getSize();
  792. StringRef SectName;
  793. Sect->getName(SectName);
  794. DataRefImpl Ref = Sect->getRawDataRefImpl();
  795. StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
  796. outs() << SegmentName << ":" << SectName << ":";
  797. uint32_t section_type;
  798. if (O->is64Bit()) {
  799. const MachO::section_64 Sec = O->getSection64(Ref);
  800. section_type = Sec.flags & MachO::SECTION_TYPE;
  801. } else {
  802. const MachO::section Sec = O->getSection(Ref);
  803. section_type = Sec.flags & MachO::SECTION_TYPE;
  804. }
  805. StringRef BytesStr;
  806. Sect->getContents(BytesStr);
  807. const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
  808. switch (section_type) {
  809. case MachO::S_CSTRING_LITERALS:
  810. for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
  811. i++) {
  812. DumpCstringChar(Contents[i]);
  813. }
  814. outs() << "\n";
  815. break;
  816. case MachO::S_4BYTE_LITERALS:
  817. float f;
  818. memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
  819. uint32_t l;
  820. memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
  821. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  822. sys::swapByteOrder(f);
  823. sys::swapByteOrder(l);
  824. }
  825. DumpLiteral4(l, f);
  826. break;
  827. case MachO::S_8BYTE_LITERALS: {
  828. double d;
  829. memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
  830. uint32_t l0, l1;
  831. memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
  832. memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
  833. sizeof(uint32_t));
  834. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  835. sys::swapByteOrder(f);
  836. sys::swapByteOrder(l0);
  837. sys::swapByteOrder(l1);
  838. }
  839. DumpLiteral8(O, l0, l1, d);
  840. break;
  841. }
  842. case MachO::S_16BYTE_LITERALS: {
  843. uint32_t l0, l1, l2, l3;
  844. memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
  845. memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
  846. sizeof(uint32_t));
  847. memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
  848. sizeof(uint32_t));
  849. memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
  850. sizeof(uint32_t));
  851. if (O->isLittleEndian() != sys::IsLittleEndianHost) {
  852. sys::swapByteOrder(l0);
  853. sys::swapByteOrder(l1);
  854. sys::swapByteOrder(l2);
  855. sys::swapByteOrder(l3);
  856. }
  857. DumpLiteral16(l0, l1, l2, l3);
  858. break;
  859. }
  860. }
  861. }
  862. }
  863. static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
  864. uint32_t sect_size, uint64_t sect_addr,
  865. SymbolAddressMap *AddrMap,
  866. bool verbose) {
  867. uint32_t stride;
  868. stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
  869. for (uint32_t i = 0; i < sect_size; i += stride) {
  870. const char *SymbolName = nullptr;
  871. if (O->is64Bit()) {
  872. outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
  873. uint64_t pointer_value;
  874. memcpy(&pointer_value, sect + i, stride);
  875. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  876. sys::swapByteOrder(pointer_value);
  877. outs() << format("0x%016" PRIx64, pointer_value);
  878. if (verbose)
  879. SymbolName = GuessSymbolName(pointer_value, AddrMap);
  880. } else {
  881. outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
  882. uint32_t pointer_value;
  883. memcpy(&pointer_value, sect + i, stride);
  884. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  885. sys::swapByteOrder(pointer_value);
  886. outs() << format("0x%08" PRIx32, pointer_value);
  887. if (verbose)
  888. SymbolName = GuessSymbolName(pointer_value, AddrMap);
  889. }
  890. if (SymbolName)
  891. outs() << " " << SymbolName;
  892. outs() << "\n";
  893. }
  894. }
  895. static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
  896. uint32_t size, uint64_t addr) {
  897. uint32_t cputype = O->getHeader().cputype;
  898. if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
  899. uint32_t j;
  900. for (uint32_t i = 0; i < size; i += j, addr += j) {
  901. if (O->is64Bit())
  902. outs() << format("%016" PRIx64, addr) << "\t";
  903. else
  904. outs() << format("%08" PRIx64, addr) << "\t";
  905. for (j = 0; j < 16 && i + j < size; j++) {
  906. uint8_t byte_word = *(sect + i + j);
  907. outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
  908. }
  909. outs() << "\n";
  910. }
  911. } else {
  912. uint32_t j;
  913. for (uint32_t i = 0; i < size; i += j, addr += j) {
  914. if (O->is64Bit())
  915. outs() << format("%016" PRIx64, addr) << "\t";
  916. else
  917. outs() << format("%08" PRIx64, addr) << "\t";
  918. for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
  919. j += sizeof(int32_t)) {
  920. if (i + j + sizeof(int32_t) <= size) {
  921. uint32_t long_word;
  922. memcpy(&long_word, sect + i + j, sizeof(int32_t));
  923. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  924. sys::swapByteOrder(long_word);
  925. outs() << format("%08" PRIx32, long_word) << " ";
  926. } else {
  927. for (uint32_t k = 0; i + j + k < size; k++) {
  928. uint8_t byte_word = *(sect + i + j + k);
  929. outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
  930. }
  931. }
  932. }
  933. outs() << "\n";
  934. }
  935. }
  936. }
  937. static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
  938. StringRef DisSegName, StringRef DisSectName);
  939. static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
  940. uint32_t size, uint32_t addr);
  941. #ifdef HAVE_LIBXAR
  942. static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
  943. uint32_t size, bool verbose,
  944. bool PrintXarHeader, bool PrintXarFileHeaders,
  945. std::string XarMemberName);
  946. #endif // defined(HAVE_LIBXAR)
  947. static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
  948. bool verbose) {
  949. SymbolAddressMap AddrMap;
  950. if (verbose)
  951. CreateSymbolAddressMap(O, &AddrMap);
  952. for (unsigned i = 0; i < FilterSections.size(); ++i) {
  953. StringRef DumpSection = FilterSections[i];
  954. std::pair<StringRef, StringRef> DumpSegSectName;
  955. DumpSegSectName = DumpSection.split(',');
  956. StringRef DumpSegName, DumpSectName;
  957. if (DumpSegSectName.second.size()) {
  958. DumpSegName = DumpSegSectName.first;
  959. DumpSectName = DumpSegSectName.second;
  960. } else {
  961. DumpSegName = "";
  962. DumpSectName = DumpSegSectName.first;
  963. }
  964. for (const SectionRef &Section : O->sections()) {
  965. StringRef SectName;
  966. Section.getName(SectName);
  967. DataRefImpl Ref = Section.getRawDataRefImpl();
  968. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  969. if ((DumpSegName.empty() || SegName == DumpSegName) &&
  970. (SectName == DumpSectName)) {
  971. uint32_t section_flags;
  972. if (O->is64Bit()) {
  973. const MachO::section_64 Sec = O->getSection64(Ref);
  974. section_flags = Sec.flags;
  975. } else {
  976. const MachO::section Sec = O->getSection(Ref);
  977. section_flags = Sec.flags;
  978. }
  979. uint32_t section_type = section_flags & MachO::SECTION_TYPE;
  980. StringRef BytesStr;
  981. Section.getContents(BytesStr);
  982. const char *sect = reinterpret_cast<const char *>(BytesStr.data());
  983. uint32_t sect_size = BytesStr.size();
  984. uint64_t sect_addr = Section.getAddress();
  985. outs() << "Contents of (" << SegName << "," << SectName
  986. << ") section\n";
  987. if (verbose) {
  988. if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
  989. (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
  990. DisassembleMachO(Filename, O, SegName, SectName);
  991. continue;
  992. }
  993. if (SegName == "__TEXT" && SectName == "__info_plist") {
  994. outs() << sect;
  995. continue;
  996. }
  997. if (SegName == "__OBJC" && SectName == "__protocol") {
  998. DumpProtocolSection(O, sect, sect_size, sect_addr);
  999. continue;
  1000. }
  1001. #ifdef HAVE_LIBXAR
  1002. if (SegName == "__LLVM" && SectName == "__bundle") {
  1003. DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
  1004. ArchiveHeaders, "");
  1005. continue;
  1006. }
  1007. #endif // defined(HAVE_LIBXAR)
  1008. switch (section_type) {
  1009. case MachO::S_REGULAR:
  1010. DumpRawSectionContents(O, sect, sect_size, sect_addr);
  1011. break;
  1012. case MachO::S_ZEROFILL:
  1013. outs() << "zerofill section and has no contents in the file\n";
  1014. break;
  1015. case MachO::S_CSTRING_LITERALS:
  1016. DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1017. break;
  1018. case MachO::S_4BYTE_LITERALS:
  1019. DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1020. break;
  1021. case MachO::S_8BYTE_LITERALS:
  1022. DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1023. break;
  1024. case MachO::S_16BYTE_LITERALS:
  1025. DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
  1026. break;
  1027. case MachO::S_LITERAL_POINTERS:
  1028. DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
  1029. !NoLeadingAddr);
  1030. break;
  1031. case MachO::S_MOD_INIT_FUNC_POINTERS:
  1032. case MachO::S_MOD_TERM_FUNC_POINTERS:
  1033. DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
  1034. verbose);
  1035. break;
  1036. default:
  1037. outs() << "Unknown section type ("
  1038. << format("0x%08" PRIx32, section_type) << ")\n";
  1039. DumpRawSectionContents(O, sect, sect_size, sect_addr);
  1040. break;
  1041. }
  1042. } else {
  1043. if (section_type == MachO::S_ZEROFILL)
  1044. outs() << "zerofill section and has no contents in the file\n";
  1045. else
  1046. DumpRawSectionContents(O, sect, sect_size, sect_addr);
  1047. }
  1048. }
  1049. }
  1050. }
  1051. }
  1052. static void DumpInfoPlistSectionContents(StringRef Filename,
  1053. MachOObjectFile *O) {
  1054. for (const SectionRef &Section : O->sections()) {
  1055. StringRef SectName;
  1056. Section.getName(SectName);
  1057. DataRefImpl Ref = Section.getRawDataRefImpl();
  1058. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  1059. if (SegName == "__TEXT" && SectName == "__info_plist") {
  1060. if (!NoLeadingHeaders)
  1061. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  1062. StringRef BytesStr;
  1063. Section.getContents(BytesStr);
  1064. const char *sect = reinterpret_cast<const char *>(BytesStr.data());
  1065. outs() << format("%.*s", BytesStr.size(), sect) << "\n";
  1066. return;
  1067. }
  1068. }
  1069. }
  1070. // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
  1071. // and if it is and there is a list of architecture flags is specified then
  1072. // check to make sure this Mach-O file is one of those architectures or all
  1073. // architectures were specified. If not then an error is generated and this
  1074. // routine returns false. Else it returns true.
  1075. static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
  1076. auto *MachO = dyn_cast<MachOObjectFile>(O);
  1077. if (!MachO || ArchAll || ArchFlags.empty())
  1078. return true;
  1079. MachO::mach_header H;
  1080. MachO::mach_header_64 H_64;
  1081. Triple T;
  1082. const char *McpuDefault, *ArchFlag;
  1083. if (MachO->is64Bit()) {
  1084. H_64 = MachO->MachOObjectFile::getHeader64();
  1085. T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
  1086. &McpuDefault, &ArchFlag);
  1087. } else {
  1088. H = MachO->MachOObjectFile::getHeader();
  1089. T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
  1090. &McpuDefault, &ArchFlag);
  1091. }
  1092. const std::string ArchFlagName(ArchFlag);
  1093. if (none_of(ArchFlags, [&](const std::string &Name) {
  1094. return Name == ArchFlagName;
  1095. })) {
  1096. errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n";
  1097. return false;
  1098. }
  1099. return true;
  1100. }
  1101. static void printObjcMetaData(MachOObjectFile *O, bool verbose);
  1102. // ProcessMachO() is passed a single opened Mach-O file, which may be an
  1103. // archive member and or in a slice of a universal file. It prints the
  1104. // the file name and header info and then processes it according to the
  1105. // command line options.
  1106. static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
  1107. StringRef ArchiveMemberName = StringRef(),
  1108. StringRef ArchitectureName = StringRef()) {
  1109. // If we are doing some processing here on the Mach-O file print the header
  1110. // info. And don't print it otherwise like in the case of printing the
  1111. // UniversalHeaders or ArchiveHeaders.
  1112. if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind || SymbolTable ||
  1113. LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
  1114. DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
  1115. if (!NoLeadingHeaders) {
  1116. outs() << Name;
  1117. if (!ArchiveMemberName.empty())
  1118. outs() << '(' << ArchiveMemberName << ')';
  1119. if (!ArchitectureName.empty())
  1120. outs() << " (architecture " << ArchitectureName << ")";
  1121. outs() << ":\n";
  1122. }
  1123. }
  1124. // To use the report_error() form with an ArchiveName and FileName set
  1125. // these up based on what is passed for Name and ArchiveMemberName.
  1126. StringRef ArchiveName;
  1127. StringRef FileName;
  1128. if (!ArchiveMemberName.empty()) {
  1129. ArchiveName = Name;
  1130. FileName = ArchiveMemberName;
  1131. } else {
  1132. ArchiveName = StringRef();
  1133. FileName = Name;
  1134. }
  1135. // If we need the symbol table to do the operation then check it here to
  1136. // produce a good error message as to where the Mach-O file comes from in
  1137. // the error message.
  1138. if (Disassemble || IndirectSymbols || FilterSections.size() != 0 ||
  1139. UnwindInfo)
  1140. if (Error Err = MachOOF->checkSymbolTable())
  1141. report_error(ArchiveName, FileName, std::move(Err), ArchitectureName);
  1142. if (Disassemble) {
  1143. if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
  1144. MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
  1145. DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
  1146. else
  1147. DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
  1148. }
  1149. if (IndirectSymbols)
  1150. PrintIndirectSymbols(MachOOF, !NonVerbose);
  1151. if (DataInCode)
  1152. PrintDataInCodeTable(MachOOF, !NonVerbose);
  1153. if (LinkOptHints)
  1154. PrintLinkOptHints(MachOOF);
  1155. if (Relocations)
  1156. PrintRelocations(MachOOF);
  1157. if (SectionHeaders)
  1158. PrintSectionHeaders(MachOOF);
  1159. if (SectionContents)
  1160. PrintSectionContents(MachOOF);
  1161. if (FilterSections.size() != 0)
  1162. DumpSectionContents(FileName, MachOOF, !NonVerbose);
  1163. if (InfoPlist)
  1164. DumpInfoPlistSectionContents(FileName, MachOOF);
  1165. if (DylibsUsed)
  1166. PrintDylibs(MachOOF, false);
  1167. if (DylibId)
  1168. PrintDylibs(MachOOF, true);
  1169. if (SymbolTable)
  1170. PrintSymbolTable(MachOOF, ArchiveName, ArchitectureName);
  1171. if (UnwindInfo)
  1172. printMachOUnwindInfo(MachOOF);
  1173. if (PrivateHeaders) {
  1174. printMachOFileHeader(MachOOF);
  1175. printMachOLoadCommands(MachOOF);
  1176. }
  1177. if (FirstPrivateHeader)
  1178. printMachOFileHeader(MachOOF);
  1179. if (ObjcMetaData)
  1180. printObjcMetaData(MachOOF, !NonVerbose);
  1181. if (ExportsTrie)
  1182. printExportsTrie(MachOOF);
  1183. if (Rebase)
  1184. printRebaseTable(MachOOF);
  1185. if (Bind)
  1186. printBindTable(MachOOF);
  1187. if (LazyBind)
  1188. printLazyBindTable(MachOOF);
  1189. if (WeakBind)
  1190. printWeakBindTable(MachOOF);
  1191. if (DwarfDumpType != DIDT_Null) {
  1192. std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
  1193. // Dump the complete DWARF structure.
  1194. DIDumpOptions DumpOpts;
  1195. DumpOpts.DumpType = DwarfDumpType;
  1196. DICtx->dump(outs(), DumpOpts);
  1197. }
  1198. }
  1199. // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
  1200. static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
  1201. outs() << " cputype (" << cputype << ")\n";
  1202. outs() << " cpusubtype (" << cpusubtype << ")\n";
  1203. }
  1204. // printCPUType() helps print_fat_headers by printing the cputype and
  1205. // pusubtype (symbolically for the one's it knows about).
  1206. static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
  1207. switch (cputype) {
  1208. case MachO::CPU_TYPE_I386:
  1209. switch (cpusubtype) {
  1210. case MachO::CPU_SUBTYPE_I386_ALL:
  1211. outs() << " cputype CPU_TYPE_I386\n";
  1212. outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
  1213. break;
  1214. default:
  1215. printUnknownCPUType(cputype, cpusubtype);
  1216. break;
  1217. }
  1218. break;
  1219. case MachO::CPU_TYPE_X86_64:
  1220. switch (cpusubtype) {
  1221. case MachO::CPU_SUBTYPE_X86_64_ALL:
  1222. outs() << " cputype CPU_TYPE_X86_64\n";
  1223. outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
  1224. break;
  1225. case MachO::CPU_SUBTYPE_X86_64_H:
  1226. outs() << " cputype CPU_TYPE_X86_64\n";
  1227. outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
  1228. break;
  1229. default:
  1230. printUnknownCPUType(cputype, cpusubtype);
  1231. break;
  1232. }
  1233. break;
  1234. case MachO::CPU_TYPE_ARM:
  1235. switch (cpusubtype) {
  1236. case MachO::CPU_SUBTYPE_ARM_ALL:
  1237. outs() << " cputype CPU_TYPE_ARM\n";
  1238. outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
  1239. break;
  1240. case MachO::CPU_SUBTYPE_ARM_V4T:
  1241. outs() << " cputype CPU_TYPE_ARM\n";
  1242. outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
  1243. break;
  1244. case MachO::CPU_SUBTYPE_ARM_V5TEJ:
  1245. outs() << " cputype CPU_TYPE_ARM\n";
  1246. outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
  1247. break;
  1248. case MachO::CPU_SUBTYPE_ARM_XSCALE:
  1249. outs() << " cputype CPU_TYPE_ARM\n";
  1250. outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
  1251. break;
  1252. case MachO::CPU_SUBTYPE_ARM_V6:
  1253. outs() << " cputype CPU_TYPE_ARM\n";
  1254. outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
  1255. break;
  1256. case MachO::CPU_SUBTYPE_ARM_V6M:
  1257. outs() << " cputype CPU_TYPE_ARM\n";
  1258. outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
  1259. break;
  1260. case MachO::CPU_SUBTYPE_ARM_V7:
  1261. outs() << " cputype CPU_TYPE_ARM\n";
  1262. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
  1263. break;
  1264. case MachO::CPU_SUBTYPE_ARM_V7EM:
  1265. outs() << " cputype CPU_TYPE_ARM\n";
  1266. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
  1267. break;
  1268. case MachO::CPU_SUBTYPE_ARM_V7K:
  1269. outs() << " cputype CPU_TYPE_ARM\n";
  1270. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
  1271. break;
  1272. case MachO::CPU_SUBTYPE_ARM_V7M:
  1273. outs() << " cputype CPU_TYPE_ARM\n";
  1274. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
  1275. break;
  1276. case MachO::CPU_SUBTYPE_ARM_V7S:
  1277. outs() << " cputype CPU_TYPE_ARM\n";
  1278. outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
  1279. break;
  1280. default:
  1281. printUnknownCPUType(cputype, cpusubtype);
  1282. break;
  1283. }
  1284. break;
  1285. case MachO::CPU_TYPE_ARM64:
  1286. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  1287. case MachO::CPU_SUBTYPE_ARM64_ALL:
  1288. outs() << " cputype CPU_TYPE_ARM64\n";
  1289. outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
  1290. break;
  1291. default:
  1292. printUnknownCPUType(cputype, cpusubtype);
  1293. break;
  1294. }
  1295. break;
  1296. default:
  1297. printUnknownCPUType(cputype, cpusubtype);
  1298. break;
  1299. }
  1300. }
  1301. static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
  1302. bool verbose) {
  1303. outs() << "Fat headers\n";
  1304. if (verbose) {
  1305. if (UB->getMagic() == MachO::FAT_MAGIC)
  1306. outs() << "fat_magic FAT_MAGIC\n";
  1307. else // UB->getMagic() == MachO::FAT_MAGIC_64
  1308. outs() << "fat_magic FAT_MAGIC_64\n";
  1309. } else
  1310. outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
  1311. uint32_t nfat_arch = UB->getNumberOfObjects();
  1312. StringRef Buf = UB->getData();
  1313. uint64_t size = Buf.size();
  1314. uint64_t big_size = sizeof(struct MachO::fat_header) +
  1315. nfat_arch * sizeof(struct MachO::fat_arch);
  1316. outs() << "nfat_arch " << UB->getNumberOfObjects();
  1317. if (nfat_arch == 0)
  1318. outs() << " (malformed, contains zero architecture types)\n";
  1319. else if (big_size > size)
  1320. outs() << " (malformed, architectures past end of file)\n";
  1321. else
  1322. outs() << "\n";
  1323. for (uint32_t i = 0; i < nfat_arch; ++i) {
  1324. MachOUniversalBinary::ObjectForArch OFA(UB, i);
  1325. uint32_t cputype = OFA.getCPUType();
  1326. uint32_t cpusubtype = OFA.getCPUSubType();
  1327. outs() << "architecture ";
  1328. for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
  1329. MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
  1330. uint32_t other_cputype = other_OFA.getCPUType();
  1331. uint32_t other_cpusubtype = other_OFA.getCPUSubType();
  1332. if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
  1333. (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
  1334. (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
  1335. outs() << "(illegal duplicate architecture) ";
  1336. break;
  1337. }
  1338. }
  1339. if (verbose) {
  1340. outs() << OFA.getArchFlagName() << "\n";
  1341. printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  1342. } else {
  1343. outs() << i << "\n";
  1344. outs() << " cputype " << cputype << "\n";
  1345. outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
  1346. << "\n";
  1347. }
  1348. if (verbose &&
  1349. (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
  1350. outs() << " capabilities CPU_SUBTYPE_LIB64\n";
  1351. else
  1352. outs() << " capabilities "
  1353. << format("0x%" PRIx32,
  1354. (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
  1355. outs() << " offset " << OFA.getOffset();
  1356. if (OFA.getOffset() > size)
  1357. outs() << " (past end of file)";
  1358. if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
  1359. outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
  1360. outs() << "\n";
  1361. outs() << " size " << OFA.getSize();
  1362. big_size = OFA.getOffset() + OFA.getSize();
  1363. if (big_size > size)
  1364. outs() << " (past end of file)";
  1365. outs() << "\n";
  1366. outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
  1367. << ")\n";
  1368. }
  1369. }
  1370. static void printArchiveChild(StringRef Filename, const Archive::Child &C,
  1371. bool verbose, bool print_offset,
  1372. StringRef ArchitectureName = StringRef()) {
  1373. if (print_offset)
  1374. outs() << C.getChildOffset() << "\t";
  1375. Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
  1376. if (!ModeOrErr)
  1377. report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
  1378. sys::fs::perms Mode = ModeOrErr.get();
  1379. if (verbose) {
  1380. // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
  1381. // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
  1382. outs() << "-";
  1383. outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
  1384. outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
  1385. outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
  1386. outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
  1387. outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
  1388. outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
  1389. outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
  1390. outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
  1391. outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
  1392. } else {
  1393. outs() << format("0%o ", Mode);
  1394. }
  1395. Expected<unsigned> UIDOrErr = C.getUID();
  1396. if (!UIDOrErr)
  1397. report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
  1398. unsigned UID = UIDOrErr.get();
  1399. outs() << format("%3d/", UID);
  1400. Expected<unsigned> GIDOrErr = C.getGID();
  1401. if (!GIDOrErr)
  1402. report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
  1403. unsigned GID = GIDOrErr.get();
  1404. outs() << format("%-3d ", GID);
  1405. Expected<uint64_t> Size = C.getRawSize();
  1406. if (!Size)
  1407. report_error(Filename, C, Size.takeError(), ArchitectureName);
  1408. outs() << format("%5" PRId64, Size.get()) << " ";
  1409. StringRef RawLastModified = C.getRawLastModified();
  1410. if (verbose) {
  1411. unsigned Seconds;
  1412. if (RawLastModified.getAsInteger(10, Seconds))
  1413. outs() << "(date: \"" << RawLastModified
  1414. << "\" contains non-decimal chars) ";
  1415. else {
  1416. // Since cime(3) returns a 26 character string of the form:
  1417. // "Sun Sep 16 01:03:52 1973\n\0"
  1418. // just print 24 characters.
  1419. time_t t = Seconds;
  1420. outs() << format("%.24s ", ctime(&t));
  1421. }
  1422. } else {
  1423. outs() << RawLastModified << " ";
  1424. }
  1425. if (verbose) {
  1426. Expected<StringRef> NameOrErr = C.getName();
  1427. if (!NameOrErr) {
  1428. consumeError(NameOrErr.takeError());
  1429. Expected<StringRef> NameOrErr = C.getRawName();
  1430. if (!NameOrErr)
  1431. report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
  1432. StringRef RawName = NameOrErr.get();
  1433. outs() << RawName << "\n";
  1434. } else {
  1435. StringRef Name = NameOrErr.get();
  1436. outs() << Name << "\n";
  1437. }
  1438. } else {
  1439. Expected<StringRef> NameOrErr = C.getRawName();
  1440. if (!NameOrErr)
  1441. report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
  1442. StringRef RawName = NameOrErr.get();
  1443. outs() << RawName << "\n";
  1444. }
  1445. }
  1446. static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
  1447. bool print_offset,
  1448. StringRef ArchitectureName = StringRef()) {
  1449. Error Err = Error::success();
  1450. ;
  1451. for (const auto &C : A->children(Err, false))
  1452. printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
  1453. if (Err)
  1454. report_error(StringRef(), Filename, std::move(Err), ArchitectureName);
  1455. }
  1456. // ParseInputMachO() parses the named Mach-O file in Filename and handles the
  1457. // -arch flags selecting just those slices as specified by them and also parses
  1458. // archive files. Then for each individual Mach-O file ProcessMachO() is
  1459. // called to process the file based on the command line options.
  1460. void llvm::ParseInputMachO(StringRef Filename) {
  1461. // Check for -arch all and verifiy the -arch flags are valid.
  1462. for (unsigned i = 0; i < ArchFlags.size(); ++i) {
  1463. if (ArchFlags[i] == "all") {
  1464. ArchAll = true;
  1465. } else {
  1466. if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
  1467. errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
  1468. "'for the -arch option\n";
  1469. return;
  1470. }
  1471. }
  1472. }
  1473. // Attempt to open the binary.
  1474. Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
  1475. if (!BinaryOrErr) {
  1476. if (auto E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
  1477. report_error(Filename, std::move(E));
  1478. else
  1479. outs() << Filename << ": is not an object file\n";
  1480. return;
  1481. }
  1482. Binary &Bin = *BinaryOrErr.get().getBinary();
  1483. if (Archive *A = dyn_cast<Archive>(&Bin)) {
  1484. outs() << "Archive : " << Filename << "\n";
  1485. if (ArchiveHeaders)
  1486. printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
  1487. Error Err = Error::success();
  1488. for (auto &C : A->children(Err)) {
  1489. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  1490. if (!ChildOrErr) {
  1491. if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  1492. report_error(Filename, C, std::move(E));
  1493. continue;
  1494. }
  1495. if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
  1496. if (!checkMachOAndArchFlags(O, Filename))
  1497. return;
  1498. ProcessMachO(Filename, O, O->getFileName());
  1499. }
  1500. }
  1501. if (Err)
  1502. report_error(Filename, std::move(Err));
  1503. return;
  1504. }
  1505. if (UniversalHeaders) {
  1506. if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
  1507. printMachOUniversalHeaders(UB, !NonVerbose);
  1508. }
  1509. if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
  1510. // If we have a list of architecture flags specified dump only those.
  1511. if (!ArchAll && ArchFlags.size() != 0) {
  1512. // Look for a slice in the universal binary that matches each ArchFlag.
  1513. bool ArchFound;
  1514. for (unsigned i = 0; i < ArchFlags.size(); ++i) {
  1515. ArchFound = false;
  1516. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  1517. E = UB->end_objects();
  1518. I != E; ++I) {
  1519. if (ArchFlags[i] == I->getArchFlagName()) {
  1520. ArchFound = true;
  1521. Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
  1522. I->getAsObjectFile();
  1523. std::string ArchitectureName = "";
  1524. if (ArchFlags.size() > 1)
  1525. ArchitectureName = I->getArchFlagName();
  1526. if (ObjOrErr) {
  1527. ObjectFile &O = *ObjOrErr.get();
  1528. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
  1529. ProcessMachO(Filename, MachOOF, "", ArchitectureName);
  1530. } else if (auto E = isNotObjectErrorInvalidFileType(
  1531. ObjOrErr.takeError())) {
  1532. report_error(Filename, StringRef(), std::move(E),
  1533. ArchitectureName);
  1534. continue;
  1535. } else if (Expected<std::unique_ptr<Archive>> AOrErr =
  1536. I->getAsArchive()) {
  1537. std::unique_ptr<Archive> &A = *AOrErr;
  1538. outs() << "Archive : " << Filename;
  1539. if (!ArchitectureName.empty())
  1540. outs() << " (architecture " << ArchitectureName << ")";
  1541. outs() << "\n";
  1542. if (ArchiveHeaders)
  1543. printArchiveHeaders(Filename, A.get(), !NonVerbose,
  1544. ArchiveMemberOffsets, ArchitectureName);
  1545. Error Err = Error::success();
  1546. for (auto &C : A->children(Err)) {
  1547. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  1548. if (!ChildOrErr) {
  1549. if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  1550. report_error(Filename, C, std::move(E), ArchitectureName);
  1551. continue;
  1552. }
  1553. if (MachOObjectFile *O =
  1554. dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
  1555. ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
  1556. }
  1557. if (Err)
  1558. report_error(Filename, std::move(Err));
  1559. } else {
  1560. consumeError(AOrErr.takeError());
  1561. error("Mach-O universal file: " + Filename + " for " +
  1562. "architecture " + StringRef(I->getArchFlagName()) +
  1563. " is not a Mach-O file or an archive file");
  1564. }
  1565. }
  1566. }
  1567. if (!ArchFound) {
  1568. errs() << "llvm-objdump: file: " + Filename + " does not contain "
  1569. << "architecture: " + ArchFlags[i] + "\n";
  1570. return;
  1571. }
  1572. }
  1573. return;
  1574. }
  1575. // No architecture flags were specified so if this contains a slice that
  1576. // matches the host architecture dump only that.
  1577. if (!ArchAll) {
  1578. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  1579. E = UB->end_objects();
  1580. I != E; ++I) {
  1581. if (MachOObjectFile::getHostArch().getArchName() ==
  1582. I->getArchFlagName()) {
  1583. Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
  1584. std::string ArchiveName;
  1585. ArchiveName.clear();
  1586. if (ObjOrErr) {
  1587. ObjectFile &O = *ObjOrErr.get();
  1588. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
  1589. ProcessMachO(Filename, MachOOF);
  1590. } else if (auto E = isNotObjectErrorInvalidFileType(
  1591. ObjOrErr.takeError())) {
  1592. report_error(Filename, std::move(E));
  1593. continue;
  1594. } else if (Expected<std::unique_ptr<Archive>> AOrErr =
  1595. I->getAsArchive()) {
  1596. std::unique_ptr<Archive> &A = *AOrErr;
  1597. outs() << "Archive : " << Filename << "\n";
  1598. if (ArchiveHeaders)
  1599. printArchiveHeaders(Filename, A.get(), !NonVerbose,
  1600. ArchiveMemberOffsets);
  1601. Error Err = Error::success();
  1602. for (auto &C : A->children(Err)) {
  1603. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  1604. if (!ChildOrErr) {
  1605. if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  1606. report_error(Filename, C, std::move(E));
  1607. continue;
  1608. }
  1609. if (MachOObjectFile *O =
  1610. dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
  1611. ProcessMachO(Filename, O, O->getFileName());
  1612. }
  1613. if (Err)
  1614. report_error(Filename, std::move(Err));
  1615. } else {
  1616. consumeError(AOrErr.takeError());
  1617. error("Mach-O universal file: " + Filename + " for architecture " +
  1618. StringRef(I->getArchFlagName()) +
  1619. " is not a Mach-O file or an archive file");
  1620. }
  1621. return;
  1622. }
  1623. }
  1624. }
  1625. // Either all architectures have been specified or none have been specified
  1626. // and this does not contain the host architecture so dump all the slices.
  1627. bool moreThanOneArch = UB->getNumberOfObjects() > 1;
  1628. for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
  1629. E = UB->end_objects();
  1630. I != E; ++I) {
  1631. Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
  1632. std::string ArchitectureName = "";
  1633. if (moreThanOneArch)
  1634. ArchitectureName = I->getArchFlagName();
  1635. if (ObjOrErr) {
  1636. ObjectFile &Obj = *ObjOrErr.get();
  1637. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
  1638. ProcessMachO(Filename, MachOOF, "", ArchitectureName);
  1639. } else if (auto E = isNotObjectErrorInvalidFileType(
  1640. ObjOrErr.takeError())) {
  1641. report_error(StringRef(), Filename, std::move(E), ArchitectureName);
  1642. continue;
  1643. } else if (Expected<std::unique_ptr<Archive>> AOrErr =
  1644. I->getAsArchive()) {
  1645. std::unique_ptr<Archive> &A = *AOrErr;
  1646. outs() << "Archive : " << Filename;
  1647. if (!ArchitectureName.empty())
  1648. outs() << " (architecture " << ArchitectureName << ")";
  1649. outs() << "\n";
  1650. if (ArchiveHeaders)
  1651. printArchiveHeaders(Filename, A.get(), !NonVerbose,
  1652. ArchiveMemberOffsets, ArchitectureName);
  1653. Error Err = Error::success();
  1654. for (auto &C : A->children(Err)) {
  1655. Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
  1656. if (!ChildOrErr) {
  1657. if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
  1658. report_error(Filename, C, std::move(E), ArchitectureName);
  1659. continue;
  1660. }
  1661. if (MachOObjectFile *O =
  1662. dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
  1663. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
  1664. ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
  1665. ArchitectureName);
  1666. }
  1667. }
  1668. if (Err)
  1669. report_error(Filename, std::move(Err));
  1670. } else {
  1671. consumeError(AOrErr.takeError());
  1672. error("Mach-O universal file: " + Filename + " for architecture " +
  1673. StringRef(I->getArchFlagName()) +
  1674. " is not a Mach-O file or an archive file");
  1675. }
  1676. }
  1677. return;
  1678. }
  1679. if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
  1680. if (!checkMachOAndArchFlags(O, Filename))
  1681. return;
  1682. if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
  1683. ProcessMachO(Filename, MachOOF);
  1684. } else
  1685. errs() << "llvm-objdump: '" << Filename << "': "
  1686. << "Object is not a Mach-O file type.\n";
  1687. return;
  1688. }
  1689. llvm_unreachable("Input object can't be invalid at this point");
  1690. }
  1691. // The block of info used by the Symbolizer call backs.
  1692. struct DisassembleInfo {
  1693. bool verbose;
  1694. MachOObjectFile *O;
  1695. SectionRef S;
  1696. SymbolAddressMap *AddrMap;
  1697. std::vector<SectionRef> *Sections;
  1698. const char *class_name;
  1699. const char *selector_name;
  1700. char *method;
  1701. char *demangled_name;
  1702. uint64_t adrp_addr;
  1703. uint32_t adrp_inst;
  1704. std::unique_ptr<SymbolAddressMap> bindtable;
  1705. uint32_t depth;
  1706. };
  1707. // SymbolizerGetOpInfo() is the operand information call back function.
  1708. // This is called to get the symbolic information for operand(s) of an
  1709. // instruction when it is being done. This routine does this from
  1710. // the relocation information, symbol table, etc. That block of information
  1711. // is a pointer to the struct DisassembleInfo that was passed when the
  1712. // disassembler context was created and passed to back to here when
  1713. // called back by the disassembler for instruction operands that could have
  1714. // relocation information. The address of the instruction containing operand is
  1715. // at the Pc parameter. The immediate value the operand has is passed in
  1716. // op_info->Value and is at Offset past the start of the instruction and has a
  1717. // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
  1718. // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
  1719. // names and addends of the symbolic expression to add for the operand. The
  1720. // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
  1721. // information is returned then this function returns 1 else it returns 0.
  1722. static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
  1723. uint64_t Size, int TagType, void *TagBuf) {
  1724. struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
  1725. struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
  1726. uint64_t value = op_info->Value;
  1727. // Make sure all fields returned are zero if we don't set them.
  1728. memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
  1729. op_info->Value = value;
  1730. // If the TagType is not the value 1 which it code knows about or if no
  1731. // verbose symbolic information is wanted then just return 0, indicating no
  1732. // information is being returned.
  1733. if (TagType != 1 || !info->verbose)
  1734. return 0;
  1735. unsigned int Arch = info->O->getArch();
  1736. if (Arch == Triple::x86) {
  1737. if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
  1738. return 0;
  1739. if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
  1740. // TODO:
  1741. // Search the external relocation entries of a fully linked image
  1742. // (if any) for an entry that matches this segment offset.
  1743. // uint32_t seg_offset = (Pc + Offset);
  1744. return 0;
  1745. }
  1746. // In MH_OBJECT filetypes search the section's relocation entries (if any)
  1747. // for an entry for this section offset.
  1748. uint32_t sect_addr = info->S.getAddress();
  1749. uint32_t sect_offset = (Pc + Offset) - sect_addr;
  1750. bool reloc_found = false;
  1751. DataRefImpl Rel;
  1752. MachO::any_relocation_info RE;
  1753. bool isExtern = false;
  1754. SymbolRef Symbol;
  1755. bool r_scattered = false;
  1756. uint32_t r_value, pair_r_value, r_type;
  1757. for (const RelocationRef &Reloc : info->S.relocations()) {
  1758. uint64_t RelocOffset = Reloc.getOffset();
  1759. if (RelocOffset == sect_offset) {
  1760. Rel = Reloc.getRawDataRefImpl();
  1761. RE = info->O->getRelocation(Rel);
  1762. r_type = info->O->getAnyRelocationType(RE);
  1763. r_scattered = info->O->isRelocationScattered(RE);
  1764. if (r_scattered) {
  1765. r_value = info->O->getScatteredRelocationValue(RE);
  1766. if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
  1767. r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
  1768. DataRefImpl RelNext = Rel;
  1769. info->O->moveRelocationNext(RelNext);
  1770. MachO::any_relocation_info RENext;
  1771. RENext = info->O->getRelocation(RelNext);
  1772. if (info->O->isRelocationScattered(RENext))
  1773. pair_r_value = info->O->getScatteredRelocationValue(RENext);
  1774. else
  1775. return 0;
  1776. }
  1777. } else {
  1778. isExtern = info->O->getPlainRelocationExternal(RE);
  1779. if (isExtern) {
  1780. symbol_iterator RelocSym = Reloc.getSymbol();
  1781. Symbol = *RelocSym;
  1782. }
  1783. }
  1784. reloc_found = true;
  1785. break;
  1786. }
  1787. }
  1788. if (reloc_found && isExtern) {
  1789. Expected<StringRef> SymName = Symbol.getName();
  1790. if (!SymName)
  1791. report_error(info->O->getFileName(), SymName.takeError());
  1792. const char *name = SymName->data();
  1793. op_info->AddSymbol.Present = 1;
  1794. op_info->AddSymbol.Name = name;
  1795. // For i386 extern relocation entries the value in the instruction is
  1796. // the offset from the symbol, and value is already set in op_info->Value.
  1797. return 1;
  1798. }
  1799. if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
  1800. r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
  1801. const char *add = GuessSymbolName(r_value, info->AddrMap);
  1802. const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
  1803. uint32_t offset = value - (r_value - pair_r_value);
  1804. op_info->AddSymbol.Present = 1;
  1805. if (add != nullptr)
  1806. op_info->AddSymbol.Name = add;
  1807. else
  1808. op_info->AddSymbol.Value = r_value;
  1809. op_info->SubtractSymbol.Present = 1;
  1810. if (sub != nullptr)
  1811. op_info->SubtractSymbol.Name = sub;
  1812. else
  1813. op_info->SubtractSymbol.Value = pair_r_value;
  1814. op_info->Value = offset;
  1815. return 1;
  1816. }
  1817. return 0;
  1818. }
  1819. if (Arch == Triple::x86_64) {
  1820. if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
  1821. return 0;
  1822. // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
  1823. // relocation entries of a linked image (if any) for an entry that matches
  1824. // this segment offset.
  1825. if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
  1826. uint64_t seg_offset = Pc + Offset;
  1827. bool reloc_found = false;
  1828. DataRefImpl Rel;
  1829. MachO::any_relocation_info RE;
  1830. bool isExtern = false;
  1831. SymbolRef Symbol;
  1832. for (const RelocationRef &Reloc : info->O->external_relocations()) {
  1833. uint64_t RelocOffset = Reloc.getOffset();
  1834. if (RelocOffset == seg_offset) {
  1835. Rel = Reloc.getRawDataRefImpl();
  1836. RE = info->O->getRelocation(Rel);
  1837. // external relocation entries should always be external.
  1838. isExtern = info->O->getPlainRelocationExternal(RE);
  1839. if (isExtern) {
  1840. symbol_iterator RelocSym = Reloc.getSymbol();
  1841. Symbol = *RelocSym;
  1842. }
  1843. reloc_found = true;
  1844. break;
  1845. }
  1846. }
  1847. if (reloc_found && isExtern) {
  1848. // The Value passed in will be adjusted by the Pc if the instruction
  1849. // adds the Pc. But for x86_64 external relocation entries the Value
  1850. // is the offset from the external symbol.
  1851. if (info->O->getAnyRelocationPCRel(RE))
  1852. op_info->Value -= Pc + Offset + Size;
  1853. Expected<StringRef> SymName = Symbol.getName();
  1854. if (!SymName)
  1855. report_error(info->O->getFileName(), SymName.takeError());
  1856. const char *name = SymName->data();
  1857. op_info->AddSymbol.Present = 1;
  1858. op_info->AddSymbol.Name = name;
  1859. return 1;
  1860. }
  1861. return 0;
  1862. }
  1863. // In MH_OBJECT filetypes search the section's relocation entries (if any)
  1864. // for an entry for this section offset.
  1865. uint64_t sect_addr = info->S.getAddress();
  1866. uint64_t sect_offset = (Pc + Offset) - sect_addr;
  1867. bool reloc_found = false;
  1868. DataRefImpl Rel;
  1869. MachO::any_relocation_info RE;
  1870. bool isExtern = false;
  1871. SymbolRef Symbol;
  1872. for (const RelocationRef &Reloc : info->S.relocations()) {
  1873. uint64_t RelocOffset = Reloc.getOffset();
  1874. if (RelocOffset == sect_offset) {
  1875. Rel = Reloc.getRawDataRefImpl();
  1876. RE = info->O->getRelocation(Rel);
  1877. // NOTE: Scattered relocations don't exist on x86_64.
  1878. isExtern = info->O->getPlainRelocationExternal(RE);
  1879. if (isExtern) {
  1880. symbol_iterator RelocSym = Reloc.getSymbol();
  1881. Symbol = *RelocSym;
  1882. }
  1883. reloc_found = true;
  1884. break;
  1885. }
  1886. }
  1887. if (reloc_found && isExtern) {
  1888. // The Value passed in will be adjusted by the Pc if the instruction
  1889. // adds the Pc. But for x86_64 external relocation entries the Value
  1890. // is the offset from the external symbol.
  1891. if (info->O->getAnyRelocationPCRel(RE))
  1892. op_info->Value -= Pc + Offset + Size;
  1893. Expected<StringRef> SymName = Symbol.getName();
  1894. if (!SymName)
  1895. report_error(info->O->getFileName(), SymName.takeError());
  1896. const char *name = SymName->data();
  1897. unsigned Type = info->O->getAnyRelocationType(RE);
  1898. if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
  1899. DataRefImpl RelNext = Rel;
  1900. info->O->moveRelocationNext(RelNext);
  1901. MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
  1902. unsigned TypeNext = info->O->getAnyRelocationType(RENext);
  1903. bool isExternNext = info->O->getPlainRelocationExternal(RENext);
  1904. unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
  1905. if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
  1906. op_info->SubtractSymbol.Present = 1;
  1907. op_info->SubtractSymbol.Name = name;
  1908. symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
  1909. Symbol = *RelocSymNext;
  1910. Expected<StringRef> SymNameNext = Symbol.getName();
  1911. if (!SymNameNext)
  1912. report_error(info->O->getFileName(), SymNameNext.takeError());
  1913. name = SymNameNext->data();
  1914. }
  1915. }
  1916. // TODO: add the VariantKinds to op_info->VariantKind for relocation types
  1917. // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
  1918. op_info->AddSymbol.Present = 1;
  1919. op_info->AddSymbol.Name = name;
  1920. return 1;
  1921. }
  1922. return 0;
  1923. }
  1924. if (Arch == Triple::arm) {
  1925. if (Offset != 0 || (Size != 4 && Size != 2))
  1926. return 0;
  1927. if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
  1928. // TODO:
  1929. // Search the external relocation entries of a fully linked image
  1930. // (if any) for an entry that matches this segment offset.
  1931. // uint32_t seg_offset = (Pc + Offset);
  1932. return 0;
  1933. }
  1934. // In MH_OBJECT filetypes search the section's relocation entries (if any)
  1935. // for an entry for this section offset.
  1936. uint32_t sect_addr = info->S.getAddress();
  1937. uint32_t sect_offset = (Pc + Offset) - sect_addr;
  1938. DataRefImpl Rel;
  1939. MachO::any_relocation_info RE;
  1940. bool isExtern = false;
  1941. SymbolRef Symbol;
  1942. bool r_scattered = false;
  1943. uint32_t r_value, pair_r_value, r_type, r_length, other_half;
  1944. auto Reloc =
  1945. find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
  1946. uint64_t RelocOffset = Reloc.getOffset();
  1947. return RelocOffset == sect_offset;
  1948. });
  1949. if (Reloc == info->S.relocations().end())
  1950. return 0;
  1951. Rel = Reloc->getRawDataRefImpl();
  1952. RE = info->O->getRelocation(Rel);
  1953. r_length = info->O->getAnyRelocationLength(RE);
  1954. r_scattered = info->O->isRelocationScattered(RE);
  1955. if (r_scattered) {
  1956. r_value = info->O->getScatteredRelocationValue(RE);
  1957. r_type = info->O->getScatteredRelocationType(RE);
  1958. } else {
  1959. r_type = info->O->getAnyRelocationType(RE);
  1960. isExtern = info->O->getPlainRelocationExternal(RE);
  1961. if (isExtern) {
  1962. symbol_iterator RelocSym = Reloc->getSymbol();
  1963. Symbol = *RelocSym;
  1964. }
  1965. }
  1966. if (r_type == MachO::ARM_RELOC_HALF ||
  1967. r_type == MachO::ARM_RELOC_SECTDIFF ||
  1968. r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
  1969. r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  1970. DataRefImpl RelNext = Rel;
  1971. info->O->moveRelocationNext(RelNext);
  1972. MachO::any_relocation_info RENext;
  1973. RENext = info->O->getRelocation(RelNext);
  1974. other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
  1975. if (info->O->isRelocationScattered(RENext))
  1976. pair_r_value = info->O->getScatteredRelocationValue(RENext);
  1977. }
  1978. if (isExtern) {
  1979. Expected<StringRef> SymName = Symbol.getName();
  1980. if (!SymName)
  1981. report_error(info->O->getFileName(), SymName.takeError());
  1982. const char *name = SymName->data();
  1983. op_info->AddSymbol.Present = 1;
  1984. op_info->AddSymbol.Name = name;
  1985. switch (r_type) {
  1986. case MachO::ARM_RELOC_HALF:
  1987. if ((r_length & 0x1) == 1) {
  1988. op_info->Value = value << 16 | other_half;
  1989. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
  1990. } else {
  1991. op_info->Value = other_half << 16 | value;
  1992. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
  1993. }
  1994. break;
  1995. default:
  1996. break;
  1997. }
  1998. return 1;
  1999. }
  2000. // If we have a branch that is not an external relocation entry then
  2001. // return 0 so the code in tryAddingSymbolicOperand() can use the
  2002. // SymbolLookUp call back with the branch target address to look up the
  2003. // symbol and possibility add an annotation for a symbol stub.
  2004. if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
  2005. r_type == MachO::ARM_THUMB_RELOC_BR22))
  2006. return 0;
  2007. uint32_t offset = 0;
  2008. if (r_type == MachO::ARM_RELOC_HALF ||
  2009. r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  2010. if ((r_length & 0x1) == 1)
  2011. value = value << 16 | other_half;
  2012. else
  2013. value = other_half << 16 | value;
  2014. }
  2015. if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
  2016. r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
  2017. offset = value - r_value;
  2018. value = r_value;
  2019. }
  2020. if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  2021. if ((r_length & 0x1) == 1)
  2022. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
  2023. else
  2024. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
  2025. const char *add = GuessSymbolName(r_value, info->AddrMap);
  2026. const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
  2027. int32_t offset = value - (r_value - pair_r_value);
  2028. op_info->AddSymbol.Present = 1;
  2029. if (add != nullptr)
  2030. op_info->AddSymbol.Name = add;
  2031. else
  2032. op_info->AddSymbol.Value = r_value;
  2033. op_info->SubtractSymbol.Present = 1;
  2034. if (sub != nullptr)
  2035. op_info->SubtractSymbol.Name = sub;
  2036. else
  2037. op_info->SubtractSymbol.Value = pair_r_value;
  2038. op_info->Value = offset;
  2039. return 1;
  2040. }
  2041. op_info->AddSymbol.Present = 1;
  2042. op_info->Value = offset;
  2043. if (r_type == MachO::ARM_RELOC_HALF) {
  2044. if ((r_length & 0x1) == 1)
  2045. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
  2046. else
  2047. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
  2048. }
  2049. const char *add = GuessSymbolName(value, info->AddrMap);
  2050. if (add != nullptr) {
  2051. op_info->AddSymbol.Name = add;
  2052. return 1;
  2053. }
  2054. op_info->AddSymbol.Value = value;
  2055. return 1;
  2056. }
  2057. if (Arch == Triple::aarch64) {
  2058. if (Offset != 0 || Size != 4)
  2059. return 0;
  2060. if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
  2061. // TODO:
  2062. // Search the external relocation entries of a fully linked image
  2063. // (if any) for an entry that matches this segment offset.
  2064. // uint64_t seg_offset = (Pc + Offset);
  2065. return 0;
  2066. }
  2067. // In MH_OBJECT filetypes search the section's relocation entries (if any)
  2068. // for an entry for this section offset.
  2069. uint64_t sect_addr = info->S.getAddress();
  2070. uint64_t sect_offset = (Pc + Offset) - sect_addr;
  2071. auto Reloc =
  2072. find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
  2073. uint64_t RelocOffset = Reloc.getOffset();
  2074. return RelocOffset == sect_offset;
  2075. });
  2076. if (Reloc == info->S.relocations().end())
  2077. return 0;
  2078. DataRefImpl Rel = Reloc->getRawDataRefImpl();
  2079. MachO::any_relocation_info RE = info->O->getRelocation(Rel);
  2080. uint32_t r_type = info->O->getAnyRelocationType(RE);
  2081. if (r_type == MachO::ARM64_RELOC_ADDEND) {
  2082. DataRefImpl RelNext = Rel;
  2083. info->O->moveRelocationNext(RelNext);
  2084. MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
  2085. if (value == 0) {
  2086. value = info->O->getPlainRelocationSymbolNum(RENext);
  2087. op_info->Value = value;
  2088. }
  2089. }
  2090. // NOTE: Scattered relocations don't exist on arm64.
  2091. if (!info->O->getPlainRelocationExternal(RE))
  2092. return 0;
  2093. Expected<StringRef> SymName = Reloc->getSymbol()->getName();
  2094. if (!SymName)
  2095. report_error(info->O->getFileName(), SymName.takeError());
  2096. const char *name = SymName->data();
  2097. op_info->AddSymbol.Present = 1;
  2098. op_info->AddSymbol.Name = name;
  2099. switch (r_type) {
  2100. case MachO::ARM64_RELOC_PAGE21:
  2101. /* @page */
  2102. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
  2103. break;
  2104. case MachO::ARM64_RELOC_PAGEOFF12:
  2105. /* @pageoff */
  2106. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
  2107. break;
  2108. case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
  2109. /* @gotpage */
  2110. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
  2111. break;
  2112. case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
  2113. /* @gotpageoff */
  2114. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
  2115. break;
  2116. case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
  2117. /* @tvlppage is not implemented in llvm-mc */
  2118. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
  2119. break;
  2120. case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
  2121. /* @tvlppageoff is not implemented in llvm-mc */
  2122. op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
  2123. break;
  2124. default:
  2125. case MachO::ARM64_RELOC_BRANCH26:
  2126. op_info->VariantKind = LLVMDisassembler_VariantKind_None;
  2127. break;
  2128. }
  2129. return 1;
  2130. }
  2131. return 0;
  2132. }
  2133. // GuessCstringPointer is passed the address of what might be a pointer to a
  2134. // literal string in a cstring section. If that address is in a cstring section
  2135. // it returns a pointer to that string. Else it returns nullptr.
  2136. static const char *GuessCstringPointer(uint64_t ReferenceValue,
  2137. struct DisassembleInfo *info) {
  2138. for (const auto &Load : info->O->load_commands()) {
  2139. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  2140. MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
  2141. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2142. MachO::section_64 Sec = info->O->getSection64(Load, J);
  2143. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2144. if (section_type == MachO::S_CSTRING_LITERALS &&
  2145. ReferenceValue >= Sec.addr &&
  2146. ReferenceValue < Sec.addr + Sec.size) {
  2147. uint64_t sect_offset = ReferenceValue - Sec.addr;
  2148. uint64_t object_offset = Sec.offset + sect_offset;
  2149. StringRef MachOContents = info->O->getData();
  2150. uint64_t object_size = MachOContents.size();
  2151. const char *object_addr = (const char *)MachOContents.data();
  2152. if (object_offset < object_size) {
  2153. const char *name = object_addr + object_offset;
  2154. return name;
  2155. } else {
  2156. return nullptr;
  2157. }
  2158. }
  2159. }
  2160. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  2161. MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
  2162. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2163. MachO::section Sec = info->O->getSection(Load, J);
  2164. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2165. if (section_type == MachO::S_CSTRING_LITERALS &&
  2166. ReferenceValue >= Sec.addr &&
  2167. ReferenceValue < Sec.addr + Sec.size) {
  2168. uint64_t sect_offset = ReferenceValue - Sec.addr;
  2169. uint64_t object_offset = Sec.offset + sect_offset;
  2170. StringRef MachOContents = info->O->getData();
  2171. uint64_t object_size = MachOContents.size();
  2172. const char *object_addr = (const char *)MachOContents.data();
  2173. if (object_offset < object_size) {
  2174. const char *name = object_addr + object_offset;
  2175. return name;
  2176. } else {
  2177. return nullptr;
  2178. }
  2179. }
  2180. }
  2181. }
  2182. }
  2183. return nullptr;
  2184. }
  2185. // GuessIndirectSymbol returns the name of the indirect symbol for the
  2186. // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
  2187. // an address of a symbol stub or a lazy or non-lazy pointer to associate the
  2188. // symbol name being referenced by the stub or pointer.
  2189. static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
  2190. struct DisassembleInfo *info) {
  2191. MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
  2192. MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
  2193. for (const auto &Load : info->O->load_commands()) {
  2194. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  2195. MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
  2196. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2197. MachO::section_64 Sec = info->O->getSection64(Load, J);
  2198. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2199. if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  2200. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  2201. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  2202. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  2203. section_type == MachO::S_SYMBOL_STUBS) &&
  2204. ReferenceValue >= Sec.addr &&
  2205. ReferenceValue < Sec.addr + Sec.size) {
  2206. uint32_t stride;
  2207. if (section_type == MachO::S_SYMBOL_STUBS)
  2208. stride = Sec.reserved2;
  2209. else
  2210. stride = 8;
  2211. if (stride == 0)
  2212. return nullptr;
  2213. uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
  2214. if (index < Dysymtab.nindirectsyms) {
  2215. uint32_t indirect_symbol =
  2216. info->O->getIndirectSymbolTableEntry(Dysymtab, index);
  2217. if (indirect_symbol < Symtab.nsyms) {
  2218. symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
  2219. SymbolRef Symbol = *Sym;
  2220. Expected<StringRef> SymName = Symbol.getName();
  2221. if (!SymName)
  2222. report_error(info->O->getFileName(), SymName.takeError());
  2223. const char *name = SymName->data();
  2224. return name;
  2225. }
  2226. }
  2227. }
  2228. }
  2229. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  2230. MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
  2231. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2232. MachO::section Sec = info->O->getSection(Load, J);
  2233. uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
  2234. if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  2235. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  2236. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  2237. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
  2238. section_type == MachO::S_SYMBOL_STUBS) &&
  2239. ReferenceValue >= Sec.addr &&
  2240. ReferenceValue < Sec.addr + Sec.size) {
  2241. uint32_t stride;
  2242. if (section_type == MachO::S_SYMBOL_STUBS)
  2243. stride = Sec.reserved2;
  2244. else
  2245. stride = 4;
  2246. if (stride == 0)
  2247. return nullptr;
  2248. uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
  2249. if (index < Dysymtab.nindirectsyms) {
  2250. uint32_t indirect_symbol =
  2251. info->O->getIndirectSymbolTableEntry(Dysymtab, index);
  2252. if (indirect_symbol < Symtab.nsyms) {
  2253. symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
  2254. SymbolRef Symbol = *Sym;
  2255. Expected<StringRef> SymName = Symbol.getName();
  2256. if (!SymName)
  2257. report_error(info->O->getFileName(), SymName.takeError());
  2258. const char *name = SymName->data();
  2259. return name;
  2260. }
  2261. }
  2262. }
  2263. }
  2264. }
  2265. }
  2266. return nullptr;
  2267. }
  2268. // method_reference() is called passing it the ReferenceName that might be
  2269. // a reference it to an Objective-C method call. If so then it allocates and
  2270. // assembles a method call string with the values last seen and saved in
  2271. // the DisassembleInfo's class_name and selector_name fields. This is saved
  2272. // into the method field of the info and any previous string is free'ed.
  2273. // Then the class_name field in the info is set to nullptr. The method call
  2274. // string is set into ReferenceName and ReferenceType is set to
  2275. // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
  2276. // then both ReferenceType and ReferenceName are left unchanged.
  2277. static void method_reference(struct DisassembleInfo *info,
  2278. uint64_t *ReferenceType,
  2279. const char **ReferenceName) {
  2280. unsigned int Arch = info->O->getArch();
  2281. if (*ReferenceName != nullptr) {
  2282. if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
  2283. if (info->selector_name != nullptr) {
  2284. if (info->method != nullptr)
  2285. free(info->method);
  2286. if (info->class_name != nullptr) {
  2287. info->method = (char *)malloc(5 + strlen(info->class_name) +
  2288. strlen(info->selector_name));
  2289. if (info->method != nullptr) {
  2290. strcpy(info->method, "+[");
  2291. strcat(info->method, info->class_name);
  2292. strcat(info->method, " ");
  2293. strcat(info->method, info->selector_name);
  2294. strcat(info->method, "]");
  2295. *ReferenceName = info->method;
  2296. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
  2297. }
  2298. } else {
  2299. info->method = (char *)malloc(9 + strlen(info->selector_name));
  2300. if (info->method != nullptr) {
  2301. if (Arch == Triple::x86_64)
  2302. strcpy(info->method, "-[%rdi ");
  2303. else if (Arch == Triple::aarch64)
  2304. strcpy(info->method, "-[x0 ");
  2305. else
  2306. strcpy(info->method, "-[r? ");
  2307. strcat(info->method, info->selector_name);
  2308. strcat(info->method, "]");
  2309. *ReferenceName = info->method;
  2310. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
  2311. }
  2312. }
  2313. info->class_name = nullptr;
  2314. }
  2315. } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
  2316. if (info->selector_name != nullptr) {
  2317. if (info->method != nullptr)
  2318. free(info->method);
  2319. info->method = (char *)malloc(17 + strlen(info->selector_name));
  2320. if (info->method != nullptr) {
  2321. if (Arch == Triple::x86_64)
  2322. strcpy(info->method, "-[[%rdi super] ");
  2323. else if (Arch == Triple::aarch64)
  2324. strcpy(info->method, "-[[x0 super] ");
  2325. else
  2326. strcpy(info->method, "-[[r? super] ");
  2327. strcat(info->method, info->selector_name);
  2328. strcat(info->method, "]");
  2329. *ReferenceName = info->method;
  2330. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
  2331. }
  2332. info->class_name = nullptr;
  2333. }
  2334. }
  2335. }
  2336. }
  2337. // GuessPointerPointer() is passed the address of what might be a pointer to
  2338. // a reference to an Objective-C class, selector, message ref or cfstring.
  2339. // If so the value of the pointer is returned and one of the booleans are set
  2340. // to true. If not zero is returned and all the booleans are set to false.
  2341. static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
  2342. struct DisassembleInfo *info,
  2343. bool &classref, bool &selref, bool &msgref,
  2344. bool &cfstring) {
  2345. classref = false;
  2346. selref = false;
  2347. msgref = false;
  2348. cfstring = false;
  2349. for (const auto &Load : info->O->load_commands()) {
  2350. if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  2351. MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
  2352. for (unsigned J = 0; J < Seg.nsects; ++J) {
  2353. MachO::section_64 Sec = info->O->getSection64(Load, J);
  2354. if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
  2355. strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
  2356. strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
  2357. strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
  2358. strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
  2359. ReferenceValue >= Sec.addr &&
  2360. ReferenceValue < Sec.addr + Sec.size) {
  2361. uint64_t sect_offset = ReferenceValue - Sec.addr;
  2362. uint64_t object_offset = Sec.offset + sect_offset;
  2363. StringRef MachOContents = info->O->getData();
  2364. uint64_t object_size = MachOContents.size();
  2365. const char *object_addr = (const char *)MachOContents.data();
  2366. if (object_offset < object_size) {
  2367. uint64_t pointer_value;
  2368. memcpy(&pointer_value, object_addr + object_offset,
  2369. sizeof(uint64_t));
  2370. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  2371. sys::swapByteOrder(pointer_value);
  2372. if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
  2373. selref = true;
  2374. else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
  2375. strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
  2376. classref = true;
  2377. else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
  2378. ReferenceValue + 8 < Sec.addr + Sec.size) {
  2379. msgref = true;
  2380. memcpy(&pointer_value, object_addr + object_offset + 8,
  2381. sizeof(uint64_t));
  2382. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  2383. sys::swapByteOrder(pointer_value);
  2384. } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
  2385. cfstring = true;
  2386. return pointer_value;
  2387. } else {
  2388. return 0;
  2389. }
  2390. }
  2391. }
  2392. }
  2393. // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
  2394. }
  2395. return 0;
  2396. }
  2397. // get_pointer_64 returns a pointer to the bytes in the object file at the
  2398. // Address from a section in the Mach-O file. And indirectly returns the
  2399. // offset into the section, number of bytes left in the section past the offset
  2400. // and which section is was being referenced. If the Address is not in a
  2401. // section nullptr is returned.
  2402. static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
  2403. uint32_t &left, SectionRef &S,
  2404. DisassembleInfo *info,
  2405. bool objc_only = false) {
  2406. offset = 0;
  2407. left = 0;
  2408. S = SectionRef();
  2409. for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
  2410. uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
  2411. uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
  2412. if (SectSize == 0)
  2413. continue;
  2414. if (objc_only) {
  2415. StringRef SectName;
  2416. ((*(info->Sections))[SectIdx]).getName(SectName);
  2417. DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
  2418. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  2419. if (SegName != "__OBJC" && SectName != "__cstring")
  2420. continue;
  2421. }
  2422. if (Address >= SectAddress && Address < SectAddress + SectSize) {
  2423. S = (*(info->Sections))[SectIdx];
  2424. offset = Address - SectAddress;
  2425. left = SectSize - offset;
  2426. StringRef SectContents;
  2427. ((*(info->Sections))[SectIdx]).getContents(SectContents);
  2428. return SectContents.data() + offset;
  2429. }
  2430. }
  2431. return nullptr;
  2432. }
  2433. static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
  2434. uint32_t &left, SectionRef &S,
  2435. DisassembleInfo *info,
  2436. bool objc_only = false) {
  2437. return get_pointer_64(Address, offset, left, S, info, objc_only);
  2438. }
  2439. // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
  2440. // the symbol indirectly through n_value. Based on the relocation information
  2441. // for the specified section offset in the specified section reference.
  2442. // If no relocation information is found and a non-zero ReferenceValue for the
  2443. // symbol is passed, look up that address in the info's AddrMap.
  2444. static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
  2445. DisassembleInfo *info, uint64_t &n_value,
  2446. uint64_t ReferenceValue = 0) {
  2447. n_value = 0;
  2448. if (!info->verbose)
  2449. return nullptr;
  2450. // See if there is an external relocation entry at the sect_offset.
  2451. bool reloc_found = false;
  2452. DataRefImpl Rel;
  2453. MachO::any_relocation_info RE;
  2454. bool isExtern = false;
  2455. SymbolRef Symbol;
  2456. for (const RelocationRef &Reloc : S.relocations()) {
  2457. uint64_t RelocOffset = Reloc.getOffset();
  2458. if (RelocOffset == sect_offset) {
  2459. Rel = Reloc.getRawDataRefImpl();
  2460. RE = info->O->getRelocation(Rel);
  2461. if (info->O->isRelocationScattered(RE))
  2462. continue;
  2463. isExtern = info->O->getPlainRelocationExternal(RE);
  2464. if (isExtern) {
  2465. symbol_iterator RelocSym = Reloc.getSymbol();
  2466. Symbol = *RelocSym;
  2467. }
  2468. reloc_found = true;
  2469. break;
  2470. }
  2471. }
  2472. // If there is an external relocation entry for a symbol in this section
  2473. // at this section_offset then use that symbol's value for the n_value
  2474. // and return its name.
  2475. const char *SymbolName = nullptr;
  2476. if (reloc_found && isExtern) {
  2477. n_value = Symbol.getValue();
  2478. Expected<StringRef> NameOrError = Symbol.getName();
  2479. if (!NameOrError)
  2480. report_error(info->O->getFileName(), NameOrError.takeError());
  2481. StringRef Name = *NameOrError;
  2482. if (!Name.empty()) {
  2483. SymbolName = Name.data();
  2484. return SymbolName;
  2485. }
  2486. }
  2487. // TODO: For fully linked images, look through the external relocation
  2488. // entries off the dynamic symtab command. For these the r_offset is from the
  2489. // start of the first writeable segment in the Mach-O file. So the offset
  2490. // to this section from that segment is passed to this routine by the caller,
  2491. // as the database_offset. Which is the difference of the section's starting
  2492. // address and the first writable segment.
  2493. //
  2494. // NOTE: need add passing the database_offset to this routine.
  2495. // We did not find an external relocation entry so look up the ReferenceValue
  2496. // as an address of a symbol and if found return that symbol's name.
  2497. SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
  2498. return SymbolName;
  2499. }
  2500. static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
  2501. DisassembleInfo *info,
  2502. uint32_t ReferenceValue) {
  2503. uint64_t n_value64;
  2504. return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
  2505. }
  2506. // These are structs in the Objective-C meta data and read to produce the
  2507. // comments for disassembly. While these are part of the ABI they are no
  2508. // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
  2509. // .
  2510. // The cfstring object in a 64-bit Mach-O file.
  2511. struct cfstring64_t {
  2512. uint64_t isa; // class64_t * (64-bit pointer)
  2513. uint64_t flags; // flag bits
  2514. uint64_t characters; // char * (64-bit pointer)
  2515. uint64_t length; // number of non-NULL characters in above
  2516. };
  2517. // The class object in a 64-bit Mach-O file.
  2518. struct class64_t {
  2519. uint64_t isa; // class64_t * (64-bit pointer)
  2520. uint64_t superclass; // class64_t * (64-bit pointer)
  2521. uint64_t cache; // Cache (64-bit pointer)
  2522. uint64_t vtable; // IMP * (64-bit pointer)
  2523. uint64_t data; // class_ro64_t * (64-bit pointer)
  2524. };
  2525. struct class32_t {
  2526. uint32_t isa; /* class32_t * (32-bit pointer) */
  2527. uint32_t superclass; /* class32_t * (32-bit pointer) */
  2528. uint32_t cache; /* Cache (32-bit pointer) */
  2529. uint32_t vtable; /* IMP * (32-bit pointer) */
  2530. uint32_t data; /* class_ro32_t * (32-bit pointer) */
  2531. };
  2532. struct class_ro64_t {
  2533. uint32_t flags;
  2534. uint32_t instanceStart;
  2535. uint32_t instanceSize;
  2536. uint32_t reserved;
  2537. uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
  2538. uint64_t name; // const char * (64-bit pointer)
  2539. uint64_t baseMethods; // const method_list_t * (64-bit pointer)
  2540. uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
  2541. uint64_t ivars; // const ivar_list_t * (64-bit pointer)
  2542. uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
  2543. uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
  2544. };
  2545. struct class_ro32_t {
  2546. uint32_t flags;
  2547. uint32_t instanceStart;
  2548. uint32_t instanceSize;
  2549. uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
  2550. uint32_t name; /* const char * (32-bit pointer) */
  2551. uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
  2552. uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
  2553. uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
  2554. uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
  2555. uint32_t baseProperties; /* const struct objc_property_list *
  2556. (32-bit pointer) */
  2557. };
  2558. /* Values for class_ro{64,32}_t->flags */
  2559. #define RO_META (1 << 0)
  2560. #define RO_ROOT (1 << 1)
  2561. #define RO_HAS_CXX_STRUCTORS (1 << 2)
  2562. struct method_list64_t {
  2563. uint32_t entsize;
  2564. uint32_t count;
  2565. /* struct method64_t first; These structures follow inline */
  2566. };
  2567. struct method_list32_t {
  2568. uint32_t entsize;
  2569. uint32_t count;
  2570. /* struct method32_t first; These structures follow inline */
  2571. };
  2572. struct method64_t {
  2573. uint64_t name; /* SEL (64-bit pointer) */
  2574. uint64_t types; /* const char * (64-bit pointer) */
  2575. uint64_t imp; /* IMP (64-bit pointer) */
  2576. };
  2577. struct method32_t {
  2578. uint32_t name; /* SEL (32-bit pointer) */
  2579. uint32_t types; /* const char * (32-bit pointer) */
  2580. uint32_t imp; /* IMP (32-bit pointer) */
  2581. };
  2582. struct protocol_list64_t {
  2583. uint64_t count; /* uintptr_t (a 64-bit value) */
  2584. /* struct protocol64_t * list[0]; These pointers follow inline */
  2585. };
  2586. struct protocol_list32_t {
  2587. uint32_t count; /* uintptr_t (a 32-bit value) */
  2588. /* struct protocol32_t * list[0]; These pointers follow inline */
  2589. };
  2590. struct protocol64_t {
  2591. uint64_t isa; /* id * (64-bit pointer) */
  2592. uint64_t name; /* const char * (64-bit pointer) */
  2593. uint64_t protocols; /* struct protocol_list64_t *
  2594. (64-bit pointer) */
  2595. uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
  2596. uint64_t classMethods; /* method_list_t * (64-bit pointer) */
  2597. uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
  2598. uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
  2599. uint64_t instanceProperties; /* struct objc_property_list *
  2600. (64-bit pointer) */
  2601. };
  2602. struct protocol32_t {
  2603. uint32_t isa; /* id * (32-bit pointer) */
  2604. uint32_t name; /* const char * (32-bit pointer) */
  2605. uint32_t protocols; /* struct protocol_list_t *
  2606. (32-bit pointer) */
  2607. uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
  2608. uint32_t classMethods; /* method_list_t * (32-bit pointer) */
  2609. uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
  2610. uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
  2611. uint32_t instanceProperties; /* struct objc_property_list *
  2612. (32-bit pointer) */
  2613. };
  2614. struct ivar_list64_t {
  2615. uint32_t entsize;
  2616. uint32_t count;
  2617. /* struct ivar64_t first; These structures follow inline */
  2618. };
  2619. struct ivar_list32_t {
  2620. uint32_t entsize;
  2621. uint32_t count;
  2622. /* struct ivar32_t first; These structures follow inline */
  2623. };
  2624. struct ivar64_t {
  2625. uint64_t offset; /* uintptr_t * (64-bit pointer) */
  2626. uint64_t name; /* const char * (64-bit pointer) */
  2627. uint64_t type; /* const char * (64-bit pointer) */
  2628. uint32_t alignment;
  2629. uint32_t size;
  2630. };
  2631. struct ivar32_t {
  2632. uint32_t offset; /* uintptr_t * (32-bit pointer) */
  2633. uint32_t name; /* const char * (32-bit pointer) */
  2634. uint32_t type; /* const char * (32-bit pointer) */
  2635. uint32_t alignment;
  2636. uint32_t size;
  2637. };
  2638. struct objc_property_list64 {
  2639. uint32_t entsize;
  2640. uint32_t count;
  2641. /* struct objc_property64 first; These structures follow inline */
  2642. };
  2643. struct objc_property_list32 {
  2644. uint32_t entsize;
  2645. uint32_t count;
  2646. /* struct objc_property32 first; These structures follow inline */
  2647. };
  2648. struct objc_property64 {
  2649. uint64_t name; /* const char * (64-bit pointer) */
  2650. uint64_t attributes; /* const char * (64-bit pointer) */
  2651. };
  2652. struct objc_property32 {
  2653. uint32_t name; /* const char * (32-bit pointer) */
  2654. uint32_t attributes; /* const char * (32-bit pointer) */
  2655. };
  2656. struct category64_t {
  2657. uint64_t name; /* const char * (64-bit pointer) */
  2658. uint64_t cls; /* struct class_t * (64-bit pointer) */
  2659. uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
  2660. uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
  2661. uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
  2662. uint64_t instanceProperties; /* struct objc_property_list *
  2663. (64-bit pointer) */
  2664. };
  2665. struct category32_t {
  2666. uint32_t name; /* const char * (32-bit pointer) */
  2667. uint32_t cls; /* struct class_t * (32-bit pointer) */
  2668. uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
  2669. uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
  2670. uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
  2671. uint32_t instanceProperties; /* struct objc_property_list *
  2672. (32-bit pointer) */
  2673. };
  2674. struct objc_image_info64 {
  2675. uint32_t version;
  2676. uint32_t flags;
  2677. };
  2678. struct objc_image_info32 {
  2679. uint32_t version;
  2680. uint32_t flags;
  2681. };
  2682. struct imageInfo_t {
  2683. uint32_t version;
  2684. uint32_t flags;
  2685. };
  2686. /* masks for objc_image_info.flags */
  2687. #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
  2688. #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
  2689. struct message_ref64 {
  2690. uint64_t imp; /* IMP (64-bit pointer) */
  2691. uint64_t sel; /* SEL (64-bit pointer) */
  2692. };
  2693. struct message_ref32 {
  2694. uint32_t imp; /* IMP (32-bit pointer) */
  2695. uint32_t sel; /* SEL (32-bit pointer) */
  2696. };
  2697. // Objective-C 1 (32-bit only) meta data structs.
  2698. struct objc_module_t {
  2699. uint32_t version;
  2700. uint32_t size;
  2701. uint32_t name; /* char * (32-bit pointer) */
  2702. uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
  2703. };
  2704. struct objc_symtab_t {
  2705. uint32_t sel_ref_cnt;
  2706. uint32_t refs; /* SEL * (32-bit pointer) */
  2707. uint16_t cls_def_cnt;
  2708. uint16_t cat_def_cnt;
  2709. // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
  2710. };
  2711. struct objc_class_t {
  2712. uint32_t isa; /* struct objc_class * (32-bit pointer) */
  2713. uint32_t super_class; /* struct objc_class * (32-bit pointer) */
  2714. uint32_t name; /* const char * (32-bit pointer) */
  2715. int32_t version;
  2716. int32_t info;
  2717. int32_t instance_size;
  2718. uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
  2719. uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
  2720. uint32_t cache; /* struct objc_cache * (32-bit pointer) */
  2721. uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
  2722. };
  2723. #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
  2724. // class is not a metaclass
  2725. #define CLS_CLASS 0x1
  2726. // class is a metaclass
  2727. #define CLS_META 0x2
  2728. struct objc_category_t {
  2729. uint32_t category_name; /* char * (32-bit pointer) */
  2730. uint32_t class_name; /* char * (32-bit pointer) */
  2731. uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
  2732. uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
  2733. uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
  2734. };
  2735. struct objc_ivar_t {
  2736. uint32_t ivar_name; /* char * (32-bit pointer) */
  2737. uint32_t ivar_type; /* char * (32-bit pointer) */
  2738. int32_t ivar_offset;
  2739. };
  2740. struct objc_ivar_list_t {
  2741. int32_t ivar_count;
  2742. // struct objc_ivar_t ivar_list[1]; /* variable length structure */
  2743. };
  2744. struct objc_method_list_t {
  2745. uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
  2746. int32_t method_count;
  2747. // struct objc_method_t method_list[1]; /* variable length structure */
  2748. };
  2749. struct objc_method_t {
  2750. uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
  2751. uint32_t method_types; /* char * (32-bit pointer) */
  2752. uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
  2753. (32-bit pointer) */
  2754. };
  2755. struct objc_protocol_list_t {
  2756. uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
  2757. int32_t count;
  2758. // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
  2759. // (32-bit pointer) */
  2760. };
  2761. struct objc_protocol_t {
  2762. uint32_t isa; /* struct objc_class * (32-bit pointer) */
  2763. uint32_t protocol_name; /* char * (32-bit pointer) */
  2764. uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
  2765. uint32_t instance_methods; /* struct objc_method_description_list *
  2766. (32-bit pointer) */
  2767. uint32_t class_methods; /* struct objc_method_description_list *
  2768. (32-bit pointer) */
  2769. };
  2770. struct objc_method_description_list_t {
  2771. int32_t count;
  2772. // struct objc_method_description_t list[1];
  2773. };
  2774. struct objc_method_description_t {
  2775. uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
  2776. uint32_t types; /* char * (32-bit pointer) */
  2777. };
  2778. inline void swapStruct(struct cfstring64_t &cfs) {
  2779. sys::swapByteOrder(cfs.isa);
  2780. sys::swapByteOrder(cfs.flags);
  2781. sys::swapByteOrder(cfs.characters);
  2782. sys::swapByteOrder(cfs.length);
  2783. }
  2784. inline void swapStruct(struct class64_t &c) {
  2785. sys::swapByteOrder(c.isa);
  2786. sys::swapByteOrder(c.superclass);
  2787. sys::swapByteOrder(c.cache);
  2788. sys::swapByteOrder(c.vtable);
  2789. sys::swapByteOrder(c.data);
  2790. }
  2791. inline void swapStruct(struct class32_t &c) {
  2792. sys::swapByteOrder(c.isa);
  2793. sys::swapByteOrder(c.superclass);
  2794. sys::swapByteOrder(c.cache);
  2795. sys::swapByteOrder(c.vtable);
  2796. sys::swapByteOrder(c.data);
  2797. }
  2798. inline void swapStruct(struct class_ro64_t &cro) {
  2799. sys::swapByteOrder(cro.flags);
  2800. sys::swapByteOrder(cro.instanceStart);
  2801. sys::swapByteOrder(cro.instanceSize);
  2802. sys::swapByteOrder(cro.reserved);
  2803. sys::swapByteOrder(cro.ivarLayout);
  2804. sys::swapByteOrder(cro.name);
  2805. sys::swapByteOrder(cro.baseMethods);
  2806. sys::swapByteOrder(cro.baseProtocols);
  2807. sys::swapByteOrder(cro.ivars);
  2808. sys::swapByteOrder(cro.weakIvarLayout);
  2809. sys::swapByteOrder(cro.baseProperties);
  2810. }
  2811. inline void swapStruct(struct class_ro32_t &cro) {
  2812. sys::swapByteOrder(cro.flags);
  2813. sys::swapByteOrder(cro.instanceStart);
  2814. sys::swapByteOrder(cro.instanceSize);
  2815. sys::swapByteOrder(cro.ivarLayout);
  2816. sys::swapByteOrder(cro.name);
  2817. sys::swapByteOrder(cro.baseMethods);
  2818. sys::swapByteOrder(cro.baseProtocols);
  2819. sys::swapByteOrder(cro.ivars);
  2820. sys::swapByteOrder(cro.weakIvarLayout);
  2821. sys::swapByteOrder(cro.baseProperties);
  2822. }
  2823. inline void swapStruct(struct method_list64_t &ml) {
  2824. sys::swapByteOrder(ml.entsize);
  2825. sys::swapByteOrder(ml.count);
  2826. }
  2827. inline void swapStruct(struct method_list32_t &ml) {
  2828. sys::swapByteOrder(ml.entsize);
  2829. sys::swapByteOrder(ml.count);
  2830. }
  2831. inline void swapStruct(struct method64_t &m) {
  2832. sys::swapByteOrder(m.name);
  2833. sys::swapByteOrder(m.types);
  2834. sys::swapByteOrder(m.imp);
  2835. }
  2836. inline void swapStruct(struct method32_t &m) {
  2837. sys::swapByteOrder(m.name);
  2838. sys::swapByteOrder(m.types);
  2839. sys::swapByteOrder(m.imp);
  2840. }
  2841. inline void swapStruct(struct protocol_list64_t &pl) {
  2842. sys::swapByteOrder(pl.count);
  2843. }
  2844. inline void swapStruct(struct protocol_list32_t &pl) {
  2845. sys::swapByteOrder(pl.count);
  2846. }
  2847. inline void swapStruct(struct protocol64_t &p) {
  2848. sys::swapByteOrder(p.isa);
  2849. sys::swapByteOrder(p.name);
  2850. sys::swapByteOrder(p.protocols);
  2851. sys::swapByteOrder(p.instanceMethods);
  2852. sys::swapByteOrder(p.classMethods);
  2853. sys::swapByteOrder(p.optionalInstanceMethods);
  2854. sys::swapByteOrder(p.optionalClassMethods);
  2855. sys::swapByteOrder(p.instanceProperties);
  2856. }
  2857. inline void swapStruct(struct protocol32_t &p) {
  2858. sys::swapByteOrder(p.isa);
  2859. sys::swapByteOrder(p.name);
  2860. sys::swapByteOrder(p.protocols);
  2861. sys::swapByteOrder(p.instanceMethods);
  2862. sys::swapByteOrder(p.classMethods);
  2863. sys::swapByteOrder(p.optionalInstanceMethods);
  2864. sys::swapByteOrder(p.optionalClassMethods);
  2865. sys::swapByteOrder(p.instanceProperties);
  2866. }
  2867. inline void swapStruct(struct ivar_list64_t &il) {
  2868. sys::swapByteOrder(il.entsize);
  2869. sys::swapByteOrder(il.count);
  2870. }
  2871. inline void swapStruct(struct ivar_list32_t &il) {
  2872. sys::swapByteOrder(il.entsize);
  2873. sys::swapByteOrder(il.count);
  2874. }
  2875. inline void swapStruct(struct ivar64_t &i) {
  2876. sys::swapByteOrder(i.offset);
  2877. sys::swapByteOrder(i.name);
  2878. sys::swapByteOrder(i.type);
  2879. sys::swapByteOrder(i.alignment);
  2880. sys::swapByteOrder(i.size);
  2881. }
  2882. inline void swapStruct(struct ivar32_t &i) {
  2883. sys::swapByteOrder(i.offset);
  2884. sys::swapByteOrder(i.name);
  2885. sys::swapByteOrder(i.type);
  2886. sys::swapByteOrder(i.alignment);
  2887. sys::swapByteOrder(i.size);
  2888. }
  2889. inline void swapStruct(struct objc_property_list64 &pl) {
  2890. sys::swapByteOrder(pl.entsize);
  2891. sys::swapByteOrder(pl.count);
  2892. }
  2893. inline void swapStruct(struct objc_property_list32 &pl) {
  2894. sys::swapByteOrder(pl.entsize);
  2895. sys::swapByteOrder(pl.count);
  2896. }
  2897. inline void swapStruct(struct objc_property64 &op) {
  2898. sys::swapByteOrder(op.name);
  2899. sys::swapByteOrder(op.attributes);
  2900. }
  2901. inline void swapStruct(struct objc_property32 &op) {
  2902. sys::swapByteOrder(op.name);
  2903. sys::swapByteOrder(op.attributes);
  2904. }
  2905. inline void swapStruct(struct category64_t &c) {
  2906. sys::swapByteOrder(c.name);
  2907. sys::swapByteOrder(c.cls);
  2908. sys::swapByteOrder(c.instanceMethods);
  2909. sys::swapByteOrder(c.classMethods);
  2910. sys::swapByteOrder(c.protocols);
  2911. sys::swapByteOrder(c.instanceProperties);
  2912. }
  2913. inline void swapStruct(struct category32_t &c) {
  2914. sys::swapByteOrder(c.name);
  2915. sys::swapByteOrder(c.cls);
  2916. sys::swapByteOrder(c.instanceMethods);
  2917. sys::swapByteOrder(c.classMethods);
  2918. sys::swapByteOrder(c.protocols);
  2919. sys::swapByteOrder(c.instanceProperties);
  2920. }
  2921. inline void swapStruct(struct objc_image_info64 &o) {
  2922. sys::swapByteOrder(o.version);
  2923. sys::swapByteOrder(o.flags);
  2924. }
  2925. inline void swapStruct(struct objc_image_info32 &o) {
  2926. sys::swapByteOrder(o.version);
  2927. sys::swapByteOrder(o.flags);
  2928. }
  2929. inline void swapStruct(struct imageInfo_t &o) {
  2930. sys::swapByteOrder(o.version);
  2931. sys::swapByteOrder(o.flags);
  2932. }
  2933. inline void swapStruct(struct message_ref64 &mr) {
  2934. sys::swapByteOrder(mr.imp);
  2935. sys::swapByteOrder(mr.sel);
  2936. }
  2937. inline void swapStruct(struct message_ref32 &mr) {
  2938. sys::swapByteOrder(mr.imp);
  2939. sys::swapByteOrder(mr.sel);
  2940. }
  2941. inline void swapStruct(struct objc_module_t &module) {
  2942. sys::swapByteOrder(module.version);
  2943. sys::swapByteOrder(module.size);
  2944. sys::swapByteOrder(module.name);
  2945. sys::swapByteOrder(module.symtab);
  2946. }
  2947. inline void swapStruct(struct objc_symtab_t &symtab) {
  2948. sys::swapByteOrder(symtab.sel_ref_cnt);
  2949. sys::swapByteOrder(symtab.refs);
  2950. sys::swapByteOrder(symtab.cls_def_cnt);
  2951. sys::swapByteOrder(symtab.cat_def_cnt);
  2952. }
  2953. inline void swapStruct(struct objc_class_t &objc_class) {
  2954. sys::swapByteOrder(objc_class.isa);
  2955. sys::swapByteOrder(objc_class.super_class);
  2956. sys::swapByteOrder(objc_class.name);
  2957. sys::swapByteOrder(objc_class.version);
  2958. sys::swapByteOrder(objc_class.info);
  2959. sys::swapByteOrder(objc_class.instance_size);
  2960. sys::swapByteOrder(objc_class.ivars);
  2961. sys::swapByteOrder(objc_class.methodLists);
  2962. sys::swapByteOrder(objc_class.cache);
  2963. sys::swapByteOrder(objc_class.protocols);
  2964. }
  2965. inline void swapStruct(struct objc_category_t &objc_category) {
  2966. sys::swapByteOrder(objc_category.category_name);
  2967. sys::swapByteOrder(objc_category.class_name);
  2968. sys::swapByteOrder(objc_category.instance_methods);
  2969. sys::swapByteOrder(objc_category.class_methods);
  2970. sys::swapByteOrder(objc_category.protocols);
  2971. }
  2972. inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
  2973. sys::swapByteOrder(objc_ivar_list.ivar_count);
  2974. }
  2975. inline void swapStruct(struct objc_ivar_t &objc_ivar) {
  2976. sys::swapByteOrder(objc_ivar.ivar_name);
  2977. sys::swapByteOrder(objc_ivar.ivar_type);
  2978. sys::swapByteOrder(objc_ivar.ivar_offset);
  2979. }
  2980. inline void swapStruct(struct objc_method_list_t &method_list) {
  2981. sys::swapByteOrder(method_list.obsolete);
  2982. sys::swapByteOrder(method_list.method_count);
  2983. }
  2984. inline void swapStruct(struct objc_method_t &method) {
  2985. sys::swapByteOrder(method.method_name);
  2986. sys::swapByteOrder(method.method_types);
  2987. sys::swapByteOrder(method.method_imp);
  2988. }
  2989. inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
  2990. sys::swapByteOrder(protocol_list.next);
  2991. sys::swapByteOrder(protocol_list.count);
  2992. }
  2993. inline void swapStruct(struct objc_protocol_t &protocol) {
  2994. sys::swapByteOrder(protocol.isa);
  2995. sys::swapByteOrder(protocol.protocol_name);
  2996. sys::swapByteOrder(protocol.protocol_list);
  2997. sys::swapByteOrder(protocol.instance_methods);
  2998. sys::swapByteOrder(protocol.class_methods);
  2999. }
  3000. inline void swapStruct(struct objc_method_description_list_t &mdl) {
  3001. sys::swapByteOrder(mdl.count);
  3002. }
  3003. inline void swapStruct(struct objc_method_description_t &md) {
  3004. sys::swapByteOrder(md.name);
  3005. sys::swapByteOrder(md.types);
  3006. }
  3007. static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
  3008. struct DisassembleInfo *info);
  3009. // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
  3010. // to an Objective-C class and returns the class name. It is also passed the
  3011. // address of the pointer, so when the pointer is zero as it can be in an .o
  3012. // file, that is used to look for an external relocation entry with a symbol
  3013. // name.
  3014. static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
  3015. uint64_t ReferenceValue,
  3016. struct DisassembleInfo *info) {
  3017. const char *r;
  3018. uint32_t offset, left;
  3019. SectionRef S;
  3020. // The pointer_value can be 0 in an object file and have a relocation
  3021. // entry for the class symbol at the ReferenceValue (the address of the
  3022. // pointer).
  3023. if (pointer_value == 0) {
  3024. r = get_pointer_64(ReferenceValue, offset, left, S, info);
  3025. if (r == nullptr || left < sizeof(uint64_t))
  3026. return nullptr;
  3027. uint64_t n_value;
  3028. const char *symbol_name = get_symbol_64(offset, S, info, n_value);
  3029. if (symbol_name == nullptr)
  3030. return nullptr;
  3031. const char *class_name = strrchr(symbol_name, '$');
  3032. if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
  3033. return class_name + 2;
  3034. else
  3035. return nullptr;
  3036. }
  3037. // The case were the pointer_value is non-zero and points to a class defined
  3038. // in this Mach-O file.
  3039. r = get_pointer_64(pointer_value, offset, left, S, info);
  3040. if (r == nullptr || left < sizeof(struct class64_t))
  3041. return nullptr;
  3042. struct class64_t c;
  3043. memcpy(&c, r, sizeof(struct class64_t));
  3044. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3045. swapStruct(c);
  3046. if (c.data == 0)
  3047. return nullptr;
  3048. r = get_pointer_64(c.data, offset, left, S, info);
  3049. if (r == nullptr || left < sizeof(struct class_ro64_t))
  3050. return nullptr;
  3051. struct class_ro64_t cro;
  3052. memcpy(&cro, r, sizeof(struct class_ro64_t));
  3053. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3054. swapStruct(cro);
  3055. if (cro.name == 0)
  3056. return nullptr;
  3057. const char *name = get_pointer_64(cro.name, offset, left, S, info);
  3058. return name;
  3059. }
  3060. // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
  3061. // pointer to a cfstring and returns its name or nullptr.
  3062. static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
  3063. struct DisassembleInfo *info) {
  3064. const char *r, *name;
  3065. uint32_t offset, left;
  3066. SectionRef S;
  3067. struct cfstring64_t cfs;
  3068. uint64_t cfs_characters;
  3069. r = get_pointer_64(ReferenceValue, offset, left, S, info);
  3070. if (r == nullptr || left < sizeof(struct cfstring64_t))
  3071. return nullptr;
  3072. memcpy(&cfs, r, sizeof(struct cfstring64_t));
  3073. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3074. swapStruct(cfs);
  3075. if (cfs.characters == 0) {
  3076. uint64_t n_value;
  3077. const char *symbol_name = get_symbol_64(
  3078. offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
  3079. if (symbol_name == nullptr)
  3080. return nullptr;
  3081. cfs_characters = n_value;
  3082. } else
  3083. cfs_characters = cfs.characters;
  3084. name = get_pointer_64(cfs_characters, offset, left, S, info);
  3085. return name;
  3086. }
  3087. // get_objc2_64bit_selref() is used for disassembly and is passed a the address
  3088. // of a pointer to an Objective-C selector reference when the pointer value is
  3089. // zero as in a .o file and is likely to have a external relocation entry with
  3090. // who's symbol's n_value is the real pointer to the selector name. If that is
  3091. // the case the real pointer to the selector name is returned else 0 is
  3092. // returned
  3093. static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
  3094. struct DisassembleInfo *info) {
  3095. uint32_t offset, left;
  3096. SectionRef S;
  3097. const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
  3098. if (r == nullptr || left < sizeof(uint64_t))
  3099. return 0;
  3100. uint64_t n_value;
  3101. const char *symbol_name = get_symbol_64(offset, S, info, n_value);
  3102. if (symbol_name == nullptr)
  3103. return 0;
  3104. return n_value;
  3105. }
  3106. static const SectionRef get_section(MachOObjectFile *O, const char *segname,
  3107. const char *sectname) {
  3108. for (const SectionRef &Section : O->sections()) {
  3109. StringRef SectName;
  3110. Section.getName(SectName);
  3111. DataRefImpl Ref = Section.getRawDataRefImpl();
  3112. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  3113. if (SegName == segname && SectName == sectname)
  3114. return Section;
  3115. }
  3116. return SectionRef();
  3117. }
  3118. static void
  3119. walk_pointer_list_64(const char *listname, const SectionRef S,
  3120. MachOObjectFile *O, struct DisassembleInfo *info,
  3121. void (*func)(uint64_t, struct DisassembleInfo *info)) {
  3122. if (S == SectionRef())
  3123. return;
  3124. StringRef SectName;
  3125. S.getName(SectName);
  3126. DataRefImpl Ref = S.getRawDataRefImpl();
  3127. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  3128. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  3129. StringRef BytesStr;
  3130. S.getContents(BytesStr);
  3131. const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
  3132. for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
  3133. uint32_t left = S.getSize() - i;
  3134. uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
  3135. uint64_t p = 0;
  3136. memcpy(&p, Contents + i, size);
  3137. if (i + sizeof(uint64_t) > S.getSize())
  3138. outs() << listname << " list pointer extends past end of (" << SegName
  3139. << "," << SectName << ") section\n";
  3140. outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
  3141. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  3142. sys::swapByteOrder(p);
  3143. uint64_t n_value = 0;
  3144. const char *name = get_symbol_64(i, S, info, n_value, p);
  3145. if (name == nullptr)
  3146. name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
  3147. if (n_value != 0) {
  3148. outs() << format("0x%" PRIx64, n_value);
  3149. if (p != 0)
  3150. outs() << " + " << format("0x%" PRIx64, p);
  3151. } else
  3152. outs() << format("0x%" PRIx64, p);
  3153. if (name != nullptr)
  3154. outs() << " " << name;
  3155. outs() << "\n";
  3156. p += n_value;
  3157. if (func)
  3158. func(p, info);
  3159. }
  3160. }
  3161. static void
  3162. walk_pointer_list_32(const char *listname, const SectionRef S,
  3163. MachOObjectFile *O, struct DisassembleInfo *info,
  3164. void (*func)(uint32_t, struct DisassembleInfo *info)) {
  3165. if (S == SectionRef())
  3166. return;
  3167. StringRef SectName;
  3168. S.getName(SectName);
  3169. DataRefImpl Ref = S.getRawDataRefImpl();
  3170. StringRef SegName = O->getSectionFinalSegmentName(Ref);
  3171. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  3172. StringRef BytesStr;
  3173. S.getContents(BytesStr);
  3174. const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
  3175. for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
  3176. uint32_t left = S.getSize() - i;
  3177. uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
  3178. uint32_t p = 0;
  3179. memcpy(&p, Contents + i, size);
  3180. if (i + sizeof(uint32_t) > S.getSize())
  3181. outs() << listname << " list pointer extends past end of (" << SegName
  3182. << "," << SectName << ") section\n";
  3183. uint32_t Address = S.getAddress() + i;
  3184. outs() << format("%08" PRIx32, Address) << " ";
  3185. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  3186. sys::swapByteOrder(p);
  3187. outs() << format("0x%" PRIx32, p);
  3188. const char *name = get_symbol_32(i, S, info, p);
  3189. if (name != nullptr)
  3190. outs() << " " << name;
  3191. outs() << "\n";
  3192. if (func)
  3193. func(p, info);
  3194. }
  3195. }
  3196. static void print_layout_map(const char *layout_map, uint32_t left) {
  3197. if (layout_map == nullptr)
  3198. return;
  3199. outs() << " layout map: ";
  3200. do {
  3201. outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
  3202. left--;
  3203. layout_map++;
  3204. } while (*layout_map != '\0' && left != 0);
  3205. outs() << "\n";
  3206. }
  3207. static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
  3208. uint32_t offset, left;
  3209. SectionRef S;
  3210. const char *layout_map;
  3211. if (p == 0)
  3212. return;
  3213. layout_map = get_pointer_64(p, offset, left, S, info);
  3214. print_layout_map(layout_map, left);
  3215. }
  3216. static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
  3217. uint32_t offset, left;
  3218. SectionRef S;
  3219. const char *layout_map;
  3220. if (p == 0)
  3221. return;
  3222. layout_map = get_pointer_32(p, offset, left, S, info);
  3223. print_layout_map(layout_map, left);
  3224. }
  3225. static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
  3226. const char *indent) {
  3227. struct method_list64_t ml;
  3228. struct method64_t m;
  3229. const char *r;
  3230. uint32_t offset, xoffset, left, i;
  3231. SectionRef S, xS;
  3232. const char *name, *sym_name;
  3233. uint64_t n_value;
  3234. r = get_pointer_64(p, offset, left, S, info);
  3235. if (r == nullptr)
  3236. return;
  3237. memset(&ml, '\0', sizeof(struct method_list64_t));
  3238. if (left < sizeof(struct method_list64_t)) {
  3239. memcpy(&ml, r, left);
  3240. outs() << " (method_list_t entends past the end of the section)\n";
  3241. } else
  3242. memcpy(&ml, r, sizeof(struct method_list64_t));
  3243. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3244. swapStruct(ml);
  3245. outs() << indent << "\t\t entsize " << ml.entsize << "\n";
  3246. outs() << indent << "\t\t count " << ml.count << "\n";
  3247. p += sizeof(struct method_list64_t);
  3248. offset += sizeof(struct method_list64_t);
  3249. for (i = 0; i < ml.count; i++) {
  3250. r = get_pointer_64(p, offset, left, S, info);
  3251. if (r == nullptr)
  3252. return;
  3253. memset(&m, '\0', sizeof(struct method64_t));
  3254. if (left < sizeof(struct method64_t)) {
  3255. memcpy(&m, r, left);
  3256. outs() << indent << " (method_t extends past the end of the section)\n";
  3257. } else
  3258. memcpy(&m, r, sizeof(struct method64_t));
  3259. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3260. swapStruct(m);
  3261. outs() << indent << "\t\t name ";
  3262. sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
  3263. info, n_value, m.name);
  3264. if (n_value != 0) {
  3265. if (info->verbose && sym_name != nullptr)
  3266. outs() << sym_name;
  3267. else
  3268. outs() << format("0x%" PRIx64, n_value);
  3269. if (m.name != 0)
  3270. outs() << " + " << format("0x%" PRIx64, m.name);
  3271. } else
  3272. outs() << format("0x%" PRIx64, m.name);
  3273. name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
  3274. if (name != nullptr)
  3275. outs() << format(" %.*s", left, name);
  3276. outs() << "\n";
  3277. outs() << indent << "\t\t types ";
  3278. sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
  3279. info, n_value, m.types);
  3280. if (n_value != 0) {
  3281. if (info->verbose && sym_name != nullptr)
  3282. outs() << sym_name;
  3283. else
  3284. outs() << format("0x%" PRIx64, n_value);
  3285. if (m.types != 0)
  3286. outs() << " + " << format("0x%" PRIx64, m.types);
  3287. } else
  3288. outs() << format("0x%" PRIx64, m.types);
  3289. name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
  3290. if (name != nullptr)
  3291. outs() << format(" %.*s", left, name);
  3292. outs() << "\n";
  3293. outs() << indent << "\t\t imp ";
  3294. name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
  3295. n_value, m.imp);
  3296. if (info->verbose && name == nullptr) {
  3297. if (n_value != 0) {
  3298. outs() << format("0x%" PRIx64, n_value) << " ";
  3299. if (m.imp != 0)
  3300. outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
  3301. } else
  3302. outs() << format("0x%" PRIx64, m.imp) << " ";
  3303. }
  3304. if (name != nullptr)
  3305. outs() << name;
  3306. outs() << "\n";
  3307. p += sizeof(struct method64_t);
  3308. offset += sizeof(struct method64_t);
  3309. }
  3310. }
  3311. static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
  3312. const char *indent) {
  3313. struct method_list32_t ml;
  3314. struct method32_t m;
  3315. const char *r, *name;
  3316. uint32_t offset, xoffset, left, i;
  3317. SectionRef S, xS;
  3318. r = get_pointer_32(p, offset, left, S, info);
  3319. if (r == nullptr)
  3320. return;
  3321. memset(&ml, '\0', sizeof(struct method_list32_t));
  3322. if (left < sizeof(struct method_list32_t)) {
  3323. memcpy(&ml, r, left);
  3324. outs() << " (method_list_t entends past the end of the section)\n";
  3325. } else
  3326. memcpy(&ml, r, sizeof(struct method_list32_t));
  3327. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3328. swapStruct(ml);
  3329. outs() << indent << "\t\t entsize " << ml.entsize << "\n";
  3330. outs() << indent << "\t\t count " << ml.count << "\n";
  3331. p += sizeof(struct method_list32_t);
  3332. offset += sizeof(struct method_list32_t);
  3333. for (i = 0; i < ml.count; i++) {
  3334. r = get_pointer_32(p, offset, left, S, info);
  3335. if (r == nullptr)
  3336. return;
  3337. memset(&m, '\0', sizeof(struct method32_t));
  3338. if (left < sizeof(struct method32_t)) {
  3339. memcpy(&ml, r, left);
  3340. outs() << indent << " (method_t entends past the end of the section)\n";
  3341. } else
  3342. memcpy(&m, r, sizeof(struct method32_t));
  3343. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3344. swapStruct(m);
  3345. outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
  3346. name = get_pointer_32(m.name, xoffset, left, xS, info);
  3347. if (name != nullptr)
  3348. outs() << format(" %.*s", left, name);
  3349. outs() << "\n";
  3350. outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
  3351. name = get_pointer_32(m.types, xoffset, left, xS, info);
  3352. if (name != nullptr)
  3353. outs() << format(" %.*s", left, name);
  3354. outs() << "\n";
  3355. outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
  3356. name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
  3357. m.imp);
  3358. if (name != nullptr)
  3359. outs() << " " << name;
  3360. outs() << "\n";
  3361. p += sizeof(struct method32_t);
  3362. offset += sizeof(struct method32_t);
  3363. }
  3364. }
  3365. static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
  3366. uint32_t offset, left, xleft;
  3367. SectionRef S;
  3368. struct objc_method_list_t method_list;
  3369. struct objc_method_t method;
  3370. const char *r, *methods, *name, *SymbolName;
  3371. int32_t i;
  3372. r = get_pointer_32(p, offset, left, S, info, true);
  3373. if (r == nullptr)
  3374. return true;
  3375. outs() << "\n";
  3376. if (left > sizeof(struct objc_method_list_t)) {
  3377. memcpy(&method_list, r, sizeof(struct objc_method_list_t));
  3378. } else {
  3379. outs() << "\t\t objc_method_list extends past end of the section\n";
  3380. memset(&method_list, '\0', sizeof(struct objc_method_list_t));
  3381. memcpy(&method_list, r, left);
  3382. }
  3383. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3384. swapStruct(method_list);
  3385. outs() << "\t\t obsolete "
  3386. << format("0x%08" PRIx32, method_list.obsolete) << "\n";
  3387. outs() << "\t\t method_count " << method_list.method_count << "\n";
  3388. methods = r + sizeof(struct objc_method_list_t);
  3389. for (i = 0; i < method_list.method_count; i++) {
  3390. if ((i + 1) * sizeof(struct objc_method_t) > left) {
  3391. outs() << "\t\t remaining method's extend past the of the section\n";
  3392. break;
  3393. }
  3394. memcpy(&method, methods + i * sizeof(struct objc_method_t),
  3395. sizeof(struct objc_method_t));
  3396. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3397. swapStruct(method);
  3398. outs() << "\t\t method_name "
  3399. << format("0x%08" PRIx32, method.method_name);
  3400. if (info->verbose) {
  3401. name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
  3402. if (name != nullptr)
  3403. outs() << format(" %.*s", xleft, name);
  3404. else
  3405. outs() << " (not in an __OBJC section)";
  3406. }
  3407. outs() << "\n";
  3408. outs() << "\t\t method_types "
  3409. << format("0x%08" PRIx32, method.method_types);
  3410. if (info->verbose) {
  3411. name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
  3412. if (name != nullptr)
  3413. outs() << format(" %.*s", xleft, name);
  3414. else
  3415. outs() << " (not in an __OBJC section)";
  3416. }
  3417. outs() << "\n";
  3418. outs() << "\t\t method_imp "
  3419. << format("0x%08" PRIx32, method.method_imp) << " ";
  3420. if (info->verbose) {
  3421. SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
  3422. if (SymbolName != nullptr)
  3423. outs() << SymbolName;
  3424. }
  3425. outs() << "\n";
  3426. }
  3427. return false;
  3428. }
  3429. static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
  3430. struct protocol_list64_t pl;
  3431. uint64_t q, n_value;
  3432. struct protocol64_t pc;
  3433. const char *r;
  3434. uint32_t offset, xoffset, left, i;
  3435. SectionRef S, xS;
  3436. const char *name, *sym_name;
  3437. r = get_pointer_64(p, offset, left, S, info);
  3438. if (r == nullptr)
  3439. return;
  3440. memset(&pl, '\0', sizeof(struct protocol_list64_t));
  3441. if (left < sizeof(struct protocol_list64_t)) {
  3442. memcpy(&pl, r, left);
  3443. outs() << " (protocol_list_t entends past the end of the section)\n";
  3444. } else
  3445. memcpy(&pl, r, sizeof(struct protocol_list64_t));
  3446. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3447. swapStruct(pl);
  3448. outs() << " count " << pl.count << "\n";
  3449. p += sizeof(struct protocol_list64_t);
  3450. offset += sizeof(struct protocol_list64_t);
  3451. for (i = 0; i < pl.count; i++) {
  3452. r = get_pointer_64(p, offset, left, S, info);
  3453. if (r == nullptr)
  3454. return;
  3455. q = 0;
  3456. if (left < sizeof(uint64_t)) {
  3457. memcpy(&q, r, left);
  3458. outs() << " (protocol_t * entends past the end of the section)\n";
  3459. } else
  3460. memcpy(&q, r, sizeof(uint64_t));
  3461. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3462. sys::swapByteOrder(q);
  3463. outs() << "\t\t list[" << i << "] ";
  3464. sym_name = get_symbol_64(offset, S, info, n_value, q);
  3465. if (n_value != 0) {
  3466. if (info->verbose && sym_name != nullptr)
  3467. outs() << sym_name;
  3468. else
  3469. outs() << format("0x%" PRIx64, n_value);
  3470. if (q != 0)
  3471. outs() << " + " << format("0x%" PRIx64, q);
  3472. } else
  3473. outs() << format("0x%" PRIx64, q);
  3474. outs() << " (struct protocol_t *)\n";
  3475. r = get_pointer_64(q + n_value, offset, left, S, info);
  3476. if (r == nullptr)
  3477. return;
  3478. memset(&pc, '\0', sizeof(struct protocol64_t));
  3479. if (left < sizeof(struct protocol64_t)) {
  3480. memcpy(&pc, r, left);
  3481. outs() << " (protocol_t entends past the end of the section)\n";
  3482. } else
  3483. memcpy(&pc, r, sizeof(struct protocol64_t));
  3484. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3485. swapStruct(pc);
  3486. outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
  3487. outs() << "\t\t\t name ";
  3488. sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
  3489. info, n_value, pc.name);
  3490. if (n_value != 0) {
  3491. if (info->verbose && sym_name != nullptr)
  3492. outs() << sym_name;
  3493. else
  3494. outs() << format("0x%" PRIx64, n_value);
  3495. if (pc.name != 0)
  3496. outs() << " + " << format("0x%" PRIx64, pc.name);
  3497. } else
  3498. outs() << format("0x%" PRIx64, pc.name);
  3499. name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
  3500. if (name != nullptr)
  3501. outs() << format(" %.*s", left, name);
  3502. outs() << "\n";
  3503. outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
  3504. outs() << "\t\t instanceMethods ";
  3505. sym_name =
  3506. get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
  3507. S, info, n_value, pc.instanceMethods);
  3508. if (n_value != 0) {
  3509. if (info->verbose && sym_name != nullptr)
  3510. outs() << sym_name;
  3511. else
  3512. outs() << format("0x%" PRIx64, n_value);
  3513. if (pc.instanceMethods != 0)
  3514. outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
  3515. } else
  3516. outs() << format("0x%" PRIx64, pc.instanceMethods);
  3517. outs() << " (struct method_list_t *)\n";
  3518. if (pc.instanceMethods + n_value != 0)
  3519. print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
  3520. outs() << "\t\t classMethods ";
  3521. sym_name =
  3522. get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
  3523. info, n_value, pc.classMethods);
  3524. if (n_value != 0) {
  3525. if (info->verbose && sym_name != nullptr)
  3526. outs() << sym_name;
  3527. else
  3528. outs() << format("0x%" PRIx64, n_value);
  3529. if (pc.classMethods != 0)
  3530. outs() << " + " << format("0x%" PRIx64, pc.classMethods);
  3531. } else
  3532. outs() << format("0x%" PRIx64, pc.classMethods);
  3533. outs() << " (struct method_list_t *)\n";
  3534. if (pc.classMethods + n_value != 0)
  3535. print_method_list64_t(pc.classMethods + n_value, info, "\t");
  3536. outs() << "\t optionalInstanceMethods "
  3537. << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
  3538. outs() << "\t optionalClassMethods "
  3539. << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
  3540. outs() << "\t instanceProperties "
  3541. << format("0x%" PRIx64, pc.instanceProperties) << "\n";
  3542. p += sizeof(uint64_t);
  3543. offset += sizeof(uint64_t);
  3544. }
  3545. }
  3546. static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
  3547. struct protocol_list32_t pl;
  3548. uint32_t q;
  3549. struct protocol32_t pc;
  3550. const char *r;
  3551. uint32_t offset, xoffset, left, i;
  3552. SectionRef S, xS;
  3553. const char *name;
  3554. r = get_pointer_32(p, offset, left, S, info);
  3555. if (r == nullptr)
  3556. return;
  3557. memset(&pl, '\0', sizeof(struct protocol_list32_t));
  3558. if (left < sizeof(struct protocol_list32_t)) {
  3559. memcpy(&pl, r, left);
  3560. outs() << " (protocol_list_t entends past the end of the section)\n";
  3561. } else
  3562. memcpy(&pl, r, sizeof(struct protocol_list32_t));
  3563. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3564. swapStruct(pl);
  3565. outs() << " count " << pl.count << "\n";
  3566. p += sizeof(struct protocol_list32_t);
  3567. offset += sizeof(struct protocol_list32_t);
  3568. for (i = 0; i < pl.count; i++) {
  3569. r = get_pointer_32(p, offset, left, S, info);
  3570. if (r == nullptr)
  3571. return;
  3572. q = 0;
  3573. if (left < sizeof(uint32_t)) {
  3574. memcpy(&q, r, left);
  3575. outs() << " (protocol_t * entends past the end of the section)\n";
  3576. } else
  3577. memcpy(&q, r, sizeof(uint32_t));
  3578. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3579. sys::swapByteOrder(q);
  3580. outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
  3581. << " (struct protocol_t *)\n";
  3582. r = get_pointer_32(q, offset, left, S, info);
  3583. if (r == nullptr)
  3584. return;
  3585. memset(&pc, '\0', sizeof(struct protocol32_t));
  3586. if (left < sizeof(struct protocol32_t)) {
  3587. memcpy(&pc, r, left);
  3588. outs() << " (protocol_t entends past the end of the section)\n";
  3589. } else
  3590. memcpy(&pc, r, sizeof(struct protocol32_t));
  3591. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3592. swapStruct(pc);
  3593. outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
  3594. outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
  3595. name = get_pointer_32(pc.name, xoffset, left, xS, info);
  3596. if (name != nullptr)
  3597. outs() << format(" %.*s", left, name);
  3598. outs() << "\n";
  3599. outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
  3600. outs() << "\t\t instanceMethods "
  3601. << format("0x%" PRIx32, pc.instanceMethods)
  3602. << " (struct method_list_t *)\n";
  3603. if (pc.instanceMethods != 0)
  3604. print_method_list32_t(pc.instanceMethods, info, "\t");
  3605. outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
  3606. << " (struct method_list_t *)\n";
  3607. if (pc.classMethods != 0)
  3608. print_method_list32_t(pc.classMethods, info, "\t");
  3609. outs() << "\t optionalInstanceMethods "
  3610. << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
  3611. outs() << "\t optionalClassMethods "
  3612. << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
  3613. outs() << "\t instanceProperties "
  3614. << format("0x%" PRIx32, pc.instanceProperties) << "\n";
  3615. p += sizeof(uint32_t);
  3616. offset += sizeof(uint32_t);
  3617. }
  3618. }
  3619. static void print_indent(uint32_t indent) {
  3620. for (uint32_t i = 0; i < indent;) {
  3621. if (indent - i >= 8) {
  3622. outs() << "\t";
  3623. i += 8;
  3624. } else {
  3625. for (uint32_t j = i; j < indent; j++)
  3626. outs() << " ";
  3627. return;
  3628. }
  3629. }
  3630. }
  3631. static bool print_method_description_list(uint32_t p, uint32_t indent,
  3632. struct DisassembleInfo *info) {
  3633. uint32_t offset, left, xleft;
  3634. SectionRef S;
  3635. struct objc_method_description_list_t mdl;
  3636. struct objc_method_description_t md;
  3637. const char *r, *list, *name;
  3638. int32_t i;
  3639. r = get_pointer_32(p, offset, left, S, info, true);
  3640. if (r == nullptr)
  3641. return true;
  3642. outs() << "\n";
  3643. if (left > sizeof(struct objc_method_description_list_t)) {
  3644. memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
  3645. } else {
  3646. print_indent(indent);
  3647. outs() << " objc_method_description_list extends past end of the section\n";
  3648. memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
  3649. memcpy(&mdl, r, left);
  3650. }
  3651. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3652. swapStruct(mdl);
  3653. print_indent(indent);
  3654. outs() << " count " << mdl.count << "\n";
  3655. list = r + sizeof(struct objc_method_description_list_t);
  3656. for (i = 0; i < mdl.count; i++) {
  3657. if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
  3658. print_indent(indent);
  3659. outs() << " remaining list entries extend past the of the section\n";
  3660. break;
  3661. }
  3662. print_indent(indent);
  3663. outs() << " list[" << i << "]\n";
  3664. memcpy(&md, list + i * sizeof(struct objc_method_description_t),
  3665. sizeof(struct objc_method_description_t));
  3666. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3667. swapStruct(md);
  3668. print_indent(indent);
  3669. outs() << " name " << format("0x%08" PRIx32, md.name);
  3670. if (info->verbose) {
  3671. name = get_pointer_32(md.name, offset, xleft, S, info, true);
  3672. if (name != nullptr)
  3673. outs() << format(" %.*s", xleft, name);
  3674. else
  3675. outs() << " (not in an __OBJC section)";
  3676. }
  3677. outs() << "\n";
  3678. print_indent(indent);
  3679. outs() << " types " << format("0x%08" PRIx32, md.types);
  3680. if (info->verbose) {
  3681. name = get_pointer_32(md.types, offset, xleft, S, info, true);
  3682. if (name != nullptr)
  3683. outs() << format(" %.*s", xleft, name);
  3684. else
  3685. outs() << " (not in an __OBJC section)";
  3686. }
  3687. outs() << "\n";
  3688. }
  3689. return false;
  3690. }
  3691. static bool print_protocol_list(uint32_t p, uint32_t indent,
  3692. struct DisassembleInfo *info);
  3693. static bool print_protocol(uint32_t p, uint32_t indent,
  3694. struct DisassembleInfo *info) {
  3695. uint32_t offset, left;
  3696. SectionRef S;
  3697. struct objc_protocol_t protocol;
  3698. const char *r, *name;
  3699. r = get_pointer_32(p, offset, left, S, info, true);
  3700. if (r == nullptr)
  3701. return true;
  3702. outs() << "\n";
  3703. if (left >= sizeof(struct objc_protocol_t)) {
  3704. memcpy(&protocol, r, sizeof(struct objc_protocol_t));
  3705. } else {
  3706. print_indent(indent);
  3707. outs() << " Protocol extends past end of the section\n";
  3708. memset(&protocol, '\0', sizeof(struct objc_protocol_t));
  3709. memcpy(&protocol, r, left);
  3710. }
  3711. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3712. swapStruct(protocol);
  3713. print_indent(indent);
  3714. outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
  3715. << "\n";
  3716. print_indent(indent);
  3717. outs() << " protocol_name "
  3718. << format("0x%08" PRIx32, protocol.protocol_name);
  3719. if (info->verbose) {
  3720. name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
  3721. if (name != nullptr)
  3722. outs() << format(" %.*s", left, name);
  3723. else
  3724. outs() << " (not in an __OBJC section)";
  3725. }
  3726. outs() << "\n";
  3727. print_indent(indent);
  3728. outs() << " protocol_list "
  3729. << format("0x%08" PRIx32, protocol.protocol_list);
  3730. if (print_protocol_list(protocol.protocol_list, indent + 4, info))
  3731. outs() << " (not in an __OBJC section)\n";
  3732. print_indent(indent);
  3733. outs() << " instance_methods "
  3734. << format("0x%08" PRIx32, protocol.instance_methods);
  3735. if (print_method_description_list(protocol.instance_methods, indent, info))
  3736. outs() << " (not in an __OBJC section)\n";
  3737. print_indent(indent);
  3738. outs() << " class_methods "
  3739. << format("0x%08" PRIx32, protocol.class_methods);
  3740. if (print_method_description_list(protocol.class_methods, indent, info))
  3741. outs() << " (not in an __OBJC section)\n";
  3742. return false;
  3743. }
  3744. static bool print_protocol_list(uint32_t p, uint32_t indent,
  3745. struct DisassembleInfo *info) {
  3746. uint32_t offset, left, l;
  3747. SectionRef S;
  3748. struct objc_protocol_list_t protocol_list;
  3749. const char *r, *list;
  3750. int32_t i;
  3751. r = get_pointer_32(p, offset, left, S, info, true);
  3752. if (r == nullptr)
  3753. return true;
  3754. outs() << "\n";
  3755. if (left > sizeof(struct objc_protocol_list_t)) {
  3756. memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
  3757. } else {
  3758. outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
  3759. memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
  3760. memcpy(&protocol_list, r, left);
  3761. }
  3762. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3763. swapStruct(protocol_list);
  3764. print_indent(indent);
  3765. outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
  3766. << "\n";
  3767. print_indent(indent);
  3768. outs() << " count " << protocol_list.count << "\n";
  3769. list = r + sizeof(struct objc_protocol_list_t);
  3770. for (i = 0; i < protocol_list.count; i++) {
  3771. if ((i + 1) * sizeof(uint32_t) > left) {
  3772. outs() << "\t\t remaining list entries extend past the of the section\n";
  3773. break;
  3774. }
  3775. memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
  3776. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3777. sys::swapByteOrder(l);
  3778. print_indent(indent);
  3779. outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
  3780. if (print_protocol(l, indent, info))
  3781. outs() << "(not in an __OBJC section)\n";
  3782. }
  3783. return false;
  3784. }
  3785. static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
  3786. struct ivar_list64_t il;
  3787. struct ivar64_t i;
  3788. const char *r;
  3789. uint32_t offset, xoffset, left, j;
  3790. SectionRef S, xS;
  3791. const char *name, *sym_name, *ivar_offset_p;
  3792. uint64_t ivar_offset, n_value;
  3793. r = get_pointer_64(p, offset, left, S, info);
  3794. if (r == nullptr)
  3795. return;
  3796. memset(&il, '\0', sizeof(struct ivar_list64_t));
  3797. if (left < sizeof(struct ivar_list64_t)) {
  3798. memcpy(&il, r, left);
  3799. outs() << " (ivar_list_t entends past the end of the section)\n";
  3800. } else
  3801. memcpy(&il, r, sizeof(struct ivar_list64_t));
  3802. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3803. swapStruct(il);
  3804. outs() << " entsize " << il.entsize << "\n";
  3805. outs() << " count " << il.count << "\n";
  3806. p += sizeof(struct ivar_list64_t);
  3807. offset += sizeof(struct ivar_list64_t);
  3808. for (j = 0; j < il.count; j++) {
  3809. r = get_pointer_64(p, offset, left, S, info);
  3810. if (r == nullptr)
  3811. return;
  3812. memset(&i, '\0', sizeof(struct ivar64_t));
  3813. if (left < sizeof(struct ivar64_t)) {
  3814. memcpy(&i, r, left);
  3815. outs() << " (ivar_t entends past the end of the section)\n";
  3816. } else
  3817. memcpy(&i, r, sizeof(struct ivar64_t));
  3818. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3819. swapStruct(i);
  3820. outs() << "\t\t\t offset ";
  3821. sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
  3822. info, n_value, i.offset);
  3823. if (n_value != 0) {
  3824. if (info->verbose && sym_name != nullptr)
  3825. outs() << sym_name;
  3826. else
  3827. outs() << format("0x%" PRIx64, n_value);
  3828. if (i.offset != 0)
  3829. outs() << " + " << format("0x%" PRIx64, i.offset);
  3830. } else
  3831. outs() << format("0x%" PRIx64, i.offset);
  3832. ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
  3833. if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
  3834. memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
  3835. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3836. sys::swapByteOrder(ivar_offset);
  3837. outs() << " " << ivar_offset << "\n";
  3838. } else
  3839. outs() << "\n";
  3840. outs() << "\t\t\t name ";
  3841. sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
  3842. n_value, i.name);
  3843. if (n_value != 0) {
  3844. if (info->verbose && sym_name != nullptr)
  3845. outs() << sym_name;
  3846. else
  3847. outs() << format("0x%" PRIx64, n_value);
  3848. if (i.name != 0)
  3849. outs() << " + " << format("0x%" PRIx64, i.name);
  3850. } else
  3851. outs() << format("0x%" PRIx64, i.name);
  3852. name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
  3853. if (name != nullptr)
  3854. outs() << format(" %.*s", left, name);
  3855. outs() << "\n";
  3856. outs() << "\t\t\t type ";
  3857. sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
  3858. n_value, i.name);
  3859. name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
  3860. if (n_value != 0) {
  3861. if (info->verbose && sym_name != nullptr)
  3862. outs() << sym_name;
  3863. else
  3864. outs() << format("0x%" PRIx64, n_value);
  3865. if (i.type != 0)
  3866. outs() << " + " << format("0x%" PRIx64, i.type);
  3867. } else
  3868. outs() << format("0x%" PRIx64, i.type);
  3869. if (name != nullptr)
  3870. outs() << format(" %.*s", left, name);
  3871. outs() << "\n";
  3872. outs() << "\t\t\talignment " << i.alignment << "\n";
  3873. outs() << "\t\t\t size " << i.size << "\n";
  3874. p += sizeof(struct ivar64_t);
  3875. offset += sizeof(struct ivar64_t);
  3876. }
  3877. }
  3878. static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
  3879. struct ivar_list32_t il;
  3880. struct ivar32_t i;
  3881. const char *r;
  3882. uint32_t offset, xoffset, left, j;
  3883. SectionRef S, xS;
  3884. const char *name, *ivar_offset_p;
  3885. uint32_t ivar_offset;
  3886. r = get_pointer_32(p, offset, left, S, info);
  3887. if (r == nullptr)
  3888. return;
  3889. memset(&il, '\0', sizeof(struct ivar_list32_t));
  3890. if (left < sizeof(struct ivar_list32_t)) {
  3891. memcpy(&il, r, left);
  3892. outs() << " (ivar_list_t entends past the end of the section)\n";
  3893. } else
  3894. memcpy(&il, r, sizeof(struct ivar_list32_t));
  3895. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3896. swapStruct(il);
  3897. outs() << " entsize " << il.entsize << "\n";
  3898. outs() << " count " << il.count << "\n";
  3899. p += sizeof(struct ivar_list32_t);
  3900. offset += sizeof(struct ivar_list32_t);
  3901. for (j = 0; j < il.count; j++) {
  3902. r = get_pointer_32(p, offset, left, S, info);
  3903. if (r == nullptr)
  3904. return;
  3905. memset(&i, '\0', sizeof(struct ivar32_t));
  3906. if (left < sizeof(struct ivar32_t)) {
  3907. memcpy(&i, r, left);
  3908. outs() << " (ivar_t entends past the end of the section)\n";
  3909. } else
  3910. memcpy(&i, r, sizeof(struct ivar32_t));
  3911. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3912. swapStruct(i);
  3913. outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
  3914. ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
  3915. if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
  3916. memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
  3917. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3918. sys::swapByteOrder(ivar_offset);
  3919. outs() << " " << ivar_offset << "\n";
  3920. } else
  3921. outs() << "\n";
  3922. outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
  3923. name = get_pointer_32(i.name, xoffset, left, xS, info);
  3924. if (name != nullptr)
  3925. outs() << format(" %.*s", left, name);
  3926. outs() << "\n";
  3927. outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
  3928. name = get_pointer_32(i.type, xoffset, left, xS, info);
  3929. if (name != nullptr)
  3930. outs() << format(" %.*s", left, name);
  3931. outs() << "\n";
  3932. outs() << "\t\t\talignment " << i.alignment << "\n";
  3933. outs() << "\t\t\t size " << i.size << "\n";
  3934. p += sizeof(struct ivar32_t);
  3935. offset += sizeof(struct ivar32_t);
  3936. }
  3937. }
  3938. static void print_objc_property_list64(uint64_t p,
  3939. struct DisassembleInfo *info) {
  3940. struct objc_property_list64 opl;
  3941. struct objc_property64 op;
  3942. const char *r;
  3943. uint32_t offset, xoffset, left, j;
  3944. SectionRef S, xS;
  3945. const char *name, *sym_name;
  3946. uint64_t n_value;
  3947. r = get_pointer_64(p, offset, left, S, info);
  3948. if (r == nullptr)
  3949. return;
  3950. memset(&opl, '\0', sizeof(struct objc_property_list64));
  3951. if (left < sizeof(struct objc_property_list64)) {
  3952. memcpy(&opl, r, left);
  3953. outs() << " (objc_property_list entends past the end of the section)\n";
  3954. } else
  3955. memcpy(&opl, r, sizeof(struct objc_property_list64));
  3956. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3957. swapStruct(opl);
  3958. outs() << " entsize " << opl.entsize << "\n";
  3959. outs() << " count " << opl.count << "\n";
  3960. p += sizeof(struct objc_property_list64);
  3961. offset += sizeof(struct objc_property_list64);
  3962. for (j = 0; j < opl.count; j++) {
  3963. r = get_pointer_64(p, offset, left, S, info);
  3964. if (r == nullptr)
  3965. return;
  3966. memset(&op, '\0', sizeof(struct objc_property64));
  3967. if (left < sizeof(struct objc_property64)) {
  3968. memcpy(&op, r, left);
  3969. outs() << " (objc_property entends past the end of the section)\n";
  3970. } else
  3971. memcpy(&op, r, sizeof(struct objc_property64));
  3972. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  3973. swapStruct(op);
  3974. outs() << "\t\t\t name ";
  3975. sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
  3976. info, n_value, op.name);
  3977. if (n_value != 0) {
  3978. if (info->verbose && sym_name != nullptr)
  3979. outs() << sym_name;
  3980. else
  3981. outs() << format("0x%" PRIx64, n_value);
  3982. if (op.name != 0)
  3983. outs() << " + " << format("0x%" PRIx64, op.name);
  3984. } else
  3985. outs() << format("0x%" PRIx64, op.name);
  3986. name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
  3987. if (name != nullptr)
  3988. outs() << format(" %.*s", left, name);
  3989. outs() << "\n";
  3990. outs() << "\t\t\tattributes ";
  3991. sym_name =
  3992. get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
  3993. info, n_value, op.attributes);
  3994. if (n_value != 0) {
  3995. if (info->verbose && sym_name != nullptr)
  3996. outs() << sym_name;
  3997. else
  3998. outs() << format("0x%" PRIx64, n_value);
  3999. if (op.attributes != 0)
  4000. outs() << " + " << format("0x%" PRIx64, op.attributes);
  4001. } else
  4002. outs() << format("0x%" PRIx64, op.attributes);
  4003. name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
  4004. if (name != nullptr)
  4005. outs() << format(" %.*s", left, name);
  4006. outs() << "\n";
  4007. p += sizeof(struct objc_property64);
  4008. offset += sizeof(struct objc_property64);
  4009. }
  4010. }
  4011. static void print_objc_property_list32(uint32_t p,
  4012. struct DisassembleInfo *info) {
  4013. struct objc_property_list32 opl;
  4014. struct objc_property32 op;
  4015. const char *r;
  4016. uint32_t offset, xoffset, left, j;
  4017. SectionRef S, xS;
  4018. const char *name;
  4019. r = get_pointer_32(p, offset, left, S, info);
  4020. if (r == nullptr)
  4021. return;
  4022. memset(&opl, '\0', sizeof(struct objc_property_list32));
  4023. if (left < sizeof(struct objc_property_list32)) {
  4024. memcpy(&opl, r, left);
  4025. outs() << " (objc_property_list entends past the end of the section)\n";
  4026. } else
  4027. memcpy(&opl, r, sizeof(struct objc_property_list32));
  4028. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4029. swapStruct(opl);
  4030. outs() << " entsize " << opl.entsize << "\n";
  4031. outs() << " count " << opl.count << "\n";
  4032. p += sizeof(struct objc_property_list32);
  4033. offset += sizeof(struct objc_property_list32);
  4034. for (j = 0; j < opl.count; j++) {
  4035. r = get_pointer_32(p, offset, left, S, info);
  4036. if (r == nullptr)
  4037. return;
  4038. memset(&op, '\0', sizeof(struct objc_property32));
  4039. if (left < sizeof(struct objc_property32)) {
  4040. memcpy(&op, r, left);
  4041. outs() << " (objc_property entends past the end of the section)\n";
  4042. } else
  4043. memcpy(&op, r, sizeof(struct objc_property32));
  4044. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4045. swapStruct(op);
  4046. outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
  4047. name = get_pointer_32(op.name, xoffset, left, xS, info);
  4048. if (name != nullptr)
  4049. outs() << format(" %.*s", left, name);
  4050. outs() << "\n";
  4051. outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
  4052. name = get_pointer_32(op.attributes, xoffset, left, xS, info);
  4053. if (name != nullptr)
  4054. outs() << format(" %.*s", left, name);
  4055. outs() << "\n";
  4056. p += sizeof(struct objc_property32);
  4057. offset += sizeof(struct objc_property32);
  4058. }
  4059. }
  4060. static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
  4061. bool &is_meta_class) {
  4062. struct class_ro64_t cro;
  4063. const char *r;
  4064. uint32_t offset, xoffset, left;
  4065. SectionRef S, xS;
  4066. const char *name, *sym_name;
  4067. uint64_t n_value;
  4068. r = get_pointer_64(p, offset, left, S, info);
  4069. if (r == nullptr || left < sizeof(struct class_ro64_t))
  4070. return false;
  4071. memcpy(&cro, r, sizeof(struct class_ro64_t));
  4072. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4073. swapStruct(cro);
  4074. outs() << " flags " << format("0x%" PRIx32, cro.flags);
  4075. if (cro.flags & RO_META)
  4076. outs() << " RO_META";
  4077. if (cro.flags & RO_ROOT)
  4078. outs() << " RO_ROOT";
  4079. if (cro.flags & RO_HAS_CXX_STRUCTORS)
  4080. outs() << " RO_HAS_CXX_STRUCTORS";
  4081. outs() << "\n";
  4082. outs() << " instanceStart " << cro.instanceStart << "\n";
  4083. outs() << " instanceSize " << cro.instanceSize << "\n";
  4084. outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
  4085. << "\n";
  4086. outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
  4087. << "\n";
  4088. print_layout_map64(cro.ivarLayout, info);
  4089. outs() << " name ";
  4090. sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
  4091. info, n_value, cro.name);
  4092. if (n_value != 0) {
  4093. if (info->verbose && sym_name != nullptr)
  4094. outs() << sym_name;
  4095. else
  4096. outs() << format("0x%" PRIx64, n_value);
  4097. if (cro.name != 0)
  4098. outs() << " + " << format("0x%" PRIx64, cro.name);
  4099. } else
  4100. outs() << format("0x%" PRIx64, cro.name);
  4101. name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
  4102. if (name != nullptr)
  4103. outs() << format(" %.*s", left, name);
  4104. outs() << "\n";
  4105. outs() << " baseMethods ";
  4106. sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
  4107. S, info, n_value, cro.baseMethods);
  4108. if (n_value != 0) {
  4109. if (info->verbose && sym_name != nullptr)
  4110. outs() << sym_name;
  4111. else
  4112. outs() << format("0x%" PRIx64, n_value);
  4113. if (cro.baseMethods != 0)
  4114. outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
  4115. } else
  4116. outs() << format("0x%" PRIx64, cro.baseMethods);
  4117. outs() << " (struct method_list_t *)\n";
  4118. if (cro.baseMethods + n_value != 0)
  4119. print_method_list64_t(cro.baseMethods + n_value, info, "");
  4120. outs() << " baseProtocols ";
  4121. sym_name =
  4122. get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
  4123. info, n_value, cro.baseProtocols);
  4124. if (n_value != 0) {
  4125. if (info->verbose && sym_name != nullptr)
  4126. outs() << sym_name;
  4127. else
  4128. outs() << format("0x%" PRIx64, n_value);
  4129. if (cro.baseProtocols != 0)
  4130. outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
  4131. } else
  4132. outs() << format("0x%" PRIx64, cro.baseProtocols);
  4133. outs() << "\n";
  4134. if (cro.baseProtocols + n_value != 0)
  4135. print_protocol_list64_t(cro.baseProtocols + n_value, info);
  4136. outs() << " ivars ";
  4137. sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
  4138. info, n_value, cro.ivars);
  4139. if (n_value != 0) {
  4140. if (info->verbose && sym_name != nullptr)
  4141. outs() << sym_name;
  4142. else
  4143. outs() << format("0x%" PRIx64, n_value);
  4144. if (cro.ivars != 0)
  4145. outs() << " + " << format("0x%" PRIx64, cro.ivars);
  4146. } else
  4147. outs() << format("0x%" PRIx64, cro.ivars);
  4148. outs() << "\n";
  4149. if (cro.ivars + n_value != 0)
  4150. print_ivar_list64_t(cro.ivars + n_value, info);
  4151. outs() << " weakIvarLayout ";
  4152. sym_name =
  4153. get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
  4154. info, n_value, cro.weakIvarLayout);
  4155. if (n_value != 0) {
  4156. if (info->verbose && sym_name != nullptr)
  4157. outs() << sym_name;
  4158. else
  4159. outs() << format("0x%" PRIx64, n_value);
  4160. if (cro.weakIvarLayout != 0)
  4161. outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
  4162. } else
  4163. outs() << format("0x%" PRIx64, cro.weakIvarLayout);
  4164. outs() << "\n";
  4165. print_layout_map64(cro.weakIvarLayout + n_value, info);
  4166. outs() << " baseProperties ";
  4167. sym_name =
  4168. get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
  4169. info, n_value, cro.baseProperties);
  4170. if (n_value != 0) {
  4171. if (info->verbose && sym_name != nullptr)
  4172. outs() << sym_name;
  4173. else
  4174. outs() << format("0x%" PRIx64, n_value);
  4175. if (cro.baseProperties != 0)
  4176. outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
  4177. } else
  4178. outs() << format("0x%" PRIx64, cro.baseProperties);
  4179. outs() << "\n";
  4180. if (cro.baseProperties + n_value != 0)
  4181. print_objc_property_list64(cro.baseProperties + n_value, info);
  4182. is_meta_class = (cro.flags & RO_META) != 0;
  4183. return true;
  4184. }
  4185. static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
  4186. bool &is_meta_class) {
  4187. struct class_ro32_t cro;
  4188. const char *r;
  4189. uint32_t offset, xoffset, left;
  4190. SectionRef S, xS;
  4191. const char *name;
  4192. r = get_pointer_32(p, offset, left, S, info);
  4193. if (r == nullptr)
  4194. return false;
  4195. memset(&cro, '\0', sizeof(struct class_ro32_t));
  4196. if (left < sizeof(struct class_ro32_t)) {
  4197. memcpy(&cro, r, left);
  4198. outs() << " (class_ro_t entends past the end of the section)\n";
  4199. } else
  4200. memcpy(&cro, r, sizeof(struct class_ro32_t));
  4201. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4202. swapStruct(cro);
  4203. outs() << " flags " << format("0x%" PRIx32, cro.flags);
  4204. if (cro.flags & RO_META)
  4205. outs() << " RO_META";
  4206. if (cro.flags & RO_ROOT)
  4207. outs() << " RO_ROOT";
  4208. if (cro.flags & RO_HAS_CXX_STRUCTORS)
  4209. outs() << " RO_HAS_CXX_STRUCTORS";
  4210. outs() << "\n";
  4211. outs() << " instanceStart " << cro.instanceStart << "\n";
  4212. outs() << " instanceSize " << cro.instanceSize << "\n";
  4213. outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
  4214. << "\n";
  4215. print_layout_map32(cro.ivarLayout, info);
  4216. outs() << " name " << format("0x%" PRIx32, cro.name);
  4217. name = get_pointer_32(cro.name, xoffset, left, xS, info);
  4218. if (name != nullptr)
  4219. outs() << format(" %.*s", left, name);
  4220. outs() << "\n";
  4221. outs() << " baseMethods "
  4222. << format("0x%" PRIx32, cro.baseMethods)
  4223. << " (struct method_list_t *)\n";
  4224. if (cro.baseMethods != 0)
  4225. print_method_list32_t(cro.baseMethods, info, "");
  4226. outs() << " baseProtocols "
  4227. << format("0x%" PRIx32, cro.baseProtocols) << "\n";
  4228. if (cro.baseProtocols != 0)
  4229. print_protocol_list32_t(cro.baseProtocols, info);
  4230. outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
  4231. << "\n";
  4232. if (cro.ivars != 0)
  4233. print_ivar_list32_t(cro.ivars, info);
  4234. outs() << " weakIvarLayout "
  4235. << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
  4236. print_layout_map32(cro.weakIvarLayout, info);
  4237. outs() << " baseProperties "
  4238. << format("0x%" PRIx32, cro.baseProperties) << "\n";
  4239. if (cro.baseProperties != 0)
  4240. print_objc_property_list32(cro.baseProperties, info);
  4241. is_meta_class = (cro.flags & RO_META) != 0;
  4242. return true;
  4243. }
  4244. static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
  4245. struct class64_t c;
  4246. const char *r;
  4247. uint32_t offset, left;
  4248. SectionRef S;
  4249. const char *name;
  4250. uint64_t isa_n_value, n_value;
  4251. r = get_pointer_64(p, offset, left, S, info);
  4252. if (r == nullptr || left < sizeof(struct class64_t))
  4253. return;
  4254. memcpy(&c, r, sizeof(struct class64_t));
  4255. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4256. swapStruct(c);
  4257. outs() << " isa " << format("0x%" PRIx64, c.isa);
  4258. name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
  4259. isa_n_value, c.isa);
  4260. if (name != nullptr)
  4261. outs() << " " << name;
  4262. outs() << "\n";
  4263. outs() << " superclass " << format("0x%" PRIx64, c.superclass);
  4264. name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
  4265. n_value, c.superclass);
  4266. if (name != nullptr)
  4267. outs() << " " << name;
  4268. else {
  4269. name = get_dyld_bind_info_symbolname(S.getAddress() +
  4270. offset + offsetof(struct class64_t, superclass), info);
  4271. if (name != nullptr)
  4272. outs() << " " << name;
  4273. }
  4274. outs() << "\n";
  4275. outs() << " cache " << format("0x%" PRIx64, c.cache);
  4276. name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
  4277. n_value, c.cache);
  4278. if (name != nullptr)
  4279. outs() << " " << name;
  4280. outs() << "\n";
  4281. outs() << " vtable " << format("0x%" PRIx64, c.vtable);
  4282. name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
  4283. n_value, c.vtable);
  4284. if (name != nullptr)
  4285. outs() << " " << name;
  4286. outs() << "\n";
  4287. name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
  4288. n_value, c.data);
  4289. outs() << " data ";
  4290. if (n_value != 0) {
  4291. if (info->verbose && name != nullptr)
  4292. outs() << name;
  4293. else
  4294. outs() << format("0x%" PRIx64, n_value);
  4295. if (c.data != 0)
  4296. outs() << " + " << format("0x%" PRIx64, c.data);
  4297. } else
  4298. outs() << format("0x%" PRIx64, c.data);
  4299. outs() << " (struct class_ro_t *)";
  4300. // This is a Swift class if some of the low bits of the pointer are set.
  4301. if ((c.data + n_value) & 0x7)
  4302. outs() << " Swift class";
  4303. outs() << "\n";
  4304. bool is_meta_class;
  4305. if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
  4306. return;
  4307. if (!is_meta_class &&
  4308. c.isa + isa_n_value != p &&
  4309. c.isa + isa_n_value != 0 &&
  4310. info->depth < 100) {
  4311. info->depth++;
  4312. outs() << "Meta Class\n";
  4313. print_class64_t(c.isa + isa_n_value, info);
  4314. }
  4315. }
  4316. static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
  4317. struct class32_t c;
  4318. const char *r;
  4319. uint32_t offset, left;
  4320. SectionRef S;
  4321. const char *name;
  4322. r = get_pointer_32(p, offset, left, S, info);
  4323. if (r == nullptr)
  4324. return;
  4325. memset(&c, '\0', sizeof(struct class32_t));
  4326. if (left < sizeof(struct class32_t)) {
  4327. memcpy(&c, r, left);
  4328. outs() << " (class_t entends past the end of the section)\n";
  4329. } else
  4330. memcpy(&c, r, sizeof(struct class32_t));
  4331. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4332. swapStruct(c);
  4333. outs() << " isa " << format("0x%" PRIx32, c.isa);
  4334. name =
  4335. get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
  4336. if (name != nullptr)
  4337. outs() << " " << name;
  4338. outs() << "\n";
  4339. outs() << " superclass " << format("0x%" PRIx32, c.superclass);
  4340. name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
  4341. c.superclass);
  4342. if (name != nullptr)
  4343. outs() << " " << name;
  4344. outs() << "\n";
  4345. outs() << " cache " << format("0x%" PRIx32, c.cache);
  4346. name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
  4347. c.cache);
  4348. if (name != nullptr)
  4349. outs() << " " << name;
  4350. outs() << "\n";
  4351. outs() << " vtable " << format("0x%" PRIx32, c.vtable);
  4352. name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
  4353. c.vtable);
  4354. if (name != nullptr)
  4355. outs() << " " << name;
  4356. outs() << "\n";
  4357. name =
  4358. get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
  4359. outs() << " data " << format("0x%" PRIx32, c.data)
  4360. << " (struct class_ro_t *)";
  4361. // This is a Swift class if some of the low bits of the pointer are set.
  4362. if (c.data & 0x3)
  4363. outs() << " Swift class";
  4364. outs() << "\n";
  4365. bool is_meta_class;
  4366. if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
  4367. return;
  4368. if (!is_meta_class) {
  4369. outs() << "Meta Class\n";
  4370. print_class32_t(c.isa, info);
  4371. }
  4372. }
  4373. static void print_objc_class_t(struct objc_class_t *objc_class,
  4374. struct DisassembleInfo *info) {
  4375. uint32_t offset, left, xleft;
  4376. const char *name, *p, *ivar_list;
  4377. SectionRef S;
  4378. int32_t i;
  4379. struct objc_ivar_list_t objc_ivar_list;
  4380. struct objc_ivar_t ivar;
  4381. outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
  4382. if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
  4383. name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
  4384. if (name != nullptr)
  4385. outs() << format(" %.*s", left, name);
  4386. else
  4387. outs() << " (not in an __OBJC section)";
  4388. }
  4389. outs() << "\n";
  4390. outs() << "\t super_class "
  4391. << format("0x%08" PRIx32, objc_class->super_class);
  4392. if (info->verbose) {
  4393. name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
  4394. if (name != nullptr)
  4395. outs() << format(" %.*s", left, name);
  4396. else
  4397. outs() << " (not in an __OBJC section)";
  4398. }
  4399. outs() << "\n";
  4400. outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
  4401. if (info->verbose) {
  4402. name = get_pointer_32(objc_class->name, offset, left, S, info, true);
  4403. if (name != nullptr)
  4404. outs() << format(" %.*s", left, name);
  4405. else
  4406. outs() << " (not in an __OBJC section)";
  4407. }
  4408. outs() << "\n";
  4409. outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
  4410. << "\n";
  4411. outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
  4412. if (info->verbose) {
  4413. if (CLS_GETINFO(objc_class, CLS_CLASS))
  4414. outs() << " CLS_CLASS";
  4415. else if (CLS_GETINFO(objc_class, CLS_META))
  4416. outs() << " CLS_META";
  4417. }
  4418. outs() << "\n";
  4419. outs() << "\t instance_size "
  4420. << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
  4421. p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
  4422. outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
  4423. if (p != nullptr) {
  4424. if (left > sizeof(struct objc_ivar_list_t)) {
  4425. outs() << "\n";
  4426. memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
  4427. } else {
  4428. outs() << " (entends past the end of the section)\n";
  4429. memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
  4430. memcpy(&objc_ivar_list, p, left);
  4431. }
  4432. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4433. swapStruct(objc_ivar_list);
  4434. outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
  4435. ivar_list = p + sizeof(struct objc_ivar_list_t);
  4436. for (i = 0; i < objc_ivar_list.ivar_count; i++) {
  4437. if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
  4438. outs() << "\t\t remaining ivar's extend past the of the section\n";
  4439. break;
  4440. }
  4441. memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
  4442. sizeof(struct objc_ivar_t));
  4443. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4444. swapStruct(ivar);
  4445. outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
  4446. if (info->verbose) {
  4447. name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
  4448. if (name != nullptr)
  4449. outs() << format(" %.*s", xleft, name);
  4450. else
  4451. outs() << " (not in an __OBJC section)";
  4452. }
  4453. outs() << "\n";
  4454. outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
  4455. if (info->verbose) {
  4456. name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
  4457. if (name != nullptr)
  4458. outs() << format(" %.*s", xleft, name);
  4459. else
  4460. outs() << " (not in an __OBJC section)";
  4461. }
  4462. outs() << "\n";
  4463. outs() << "\t\t ivar_offset "
  4464. << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
  4465. }
  4466. } else {
  4467. outs() << " (not in an __OBJC section)\n";
  4468. }
  4469. outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
  4470. if (print_method_list(objc_class->methodLists, info))
  4471. outs() << " (not in an __OBJC section)\n";
  4472. outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
  4473. << "\n";
  4474. outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
  4475. if (print_protocol_list(objc_class->protocols, 16, info))
  4476. outs() << " (not in an __OBJC section)\n";
  4477. }
  4478. static void print_objc_objc_category_t(struct objc_category_t *objc_category,
  4479. struct DisassembleInfo *info) {
  4480. uint32_t offset, left;
  4481. const char *name;
  4482. SectionRef S;
  4483. outs() << "\t category name "
  4484. << format("0x%08" PRIx32, objc_category->category_name);
  4485. if (info->verbose) {
  4486. name = get_pointer_32(objc_category->category_name, offset, left, S, info,
  4487. true);
  4488. if (name != nullptr)
  4489. outs() << format(" %.*s", left, name);
  4490. else
  4491. outs() << " (not in an __OBJC section)";
  4492. }
  4493. outs() << "\n";
  4494. outs() << "\t\t class name "
  4495. << format("0x%08" PRIx32, objc_category->class_name);
  4496. if (info->verbose) {
  4497. name =
  4498. get_pointer_32(objc_category->class_name, offset, left, S, info, true);
  4499. if (name != nullptr)
  4500. outs() << format(" %.*s", left, name);
  4501. else
  4502. outs() << " (not in an __OBJC section)";
  4503. }
  4504. outs() << "\n";
  4505. outs() << "\t instance methods "
  4506. << format("0x%08" PRIx32, objc_category->instance_methods);
  4507. if (print_method_list(objc_category->instance_methods, info))
  4508. outs() << " (not in an __OBJC section)\n";
  4509. outs() << "\t class methods "
  4510. << format("0x%08" PRIx32, objc_category->class_methods);
  4511. if (print_method_list(objc_category->class_methods, info))
  4512. outs() << " (not in an __OBJC section)\n";
  4513. }
  4514. static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
  4515. struct category64_t c;
  4516. const char *r;
  4517. uint32_t offset, xoffset, left;
  4518. SectionRef S, xS;
  4519. const char *name, *sym_name;
  4520. uint64_t n_value;
  4521. r = get_pointer_64(p, offset, left, S, info);
  4522. if (r == nullptr)
  4523. return;
  4524. memset(&c, '\0', sizeof(struct category64_t));
  4525. if (left < sizeof(struct category64_t)) {
  4526. memcpy(&c, r, left);
  4527. outs() << " (category_t entends past the end of the section)\n";
  4528. } else
  4529. memcpy(&c, r, sizeof(struct category64_t));
  4530. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4531. swapStruct(c);
  4532. outs() << " name ";
  4533. sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
  4534. info, n_value, c.name);
  4535. if (n_value != 0) {
  4536. if (info->verbose && sym_name != nullptr)
  4537. outs() << sym_name;
  4538. else
  4539. outs() << format("0x%" PRIx64, n_value);
  4540. if (c.name != 0)
  4541. outs() << " + " << format("0x%" PRIx64, c.name);
  4542. } else
  4543. outs() << format("0x%" PRIx64, c.name);
  4544. name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
  4545. if (name != nullptr)
  4546. outs() << format(" %.*s", left, name);
  4547. outs() << "\n";
  4548. outs() << " cls ";
  4549. sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
  4550. n_value, c.cls);
  4551. if (n_value != 0) {
  4552. if (info->verbose && sym_name != nullptr)
  4553. outs() << sym_name;
  4554. else
  4555. outs() << format("0x%" PRIx64, n_value);
  4556. if (c.cls != 0)
  4557. outs() << " + " << format("0x%" PRIx64, c.cls);
  4558. } else
  4559. outs() << format("0x%" PRIx64, c.cls);
  4560. outs() << "\n";
  4561. if (c.cls + n_value != 0)
  4562. print_class64_t(c.cls + n_value, info);
  4563. outs() << " instanceMethods ";
  4564. sym_name =
  4565. get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
  4566. info, n_value, c.instanceMethods);
  4567. if (n_value != 0) {
  4568. if (info->verbose && sym_name != nullptr)
  4569. outs() << sym_name;
  4570. else
  4571. outs() << format("0x%" PRIx64, n_value);
  4572. if (c.instanceMethods != 0)
  4573. outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
  4574. } else
  4575. outs() << format("0x%" PRIx64, c.instanceMethods);
  4576. outs() << "\n";
  4577. if (c.instanceMethods + n_value != 0)
  4578. print_method_list64_t(c.instanceMethods + n_value, info, "");
  4579. outs() << " classMethods ";
  4580. sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
  4581. S, info, n_value, c.classMethods);
  4582. if (n_value != 0) {
  4583. if (info->verbose && sym_name != nullptr)
  4584. outs() << sym_name;
  4585. else
  4586. outs() << format("0x%" PRIx64, n_value);
  4587. if (c.classMethods != 0)
  4588. outs() << " + " << format("0x%" PRIx64, c.classMethods);
  4589. } else
  4590. outs() << format("0x%" PRIx64, c.classMethods);
  4591. outs() << "\n";
  4592. if (c.classMethods + n_value != 0)
  4593. print_method_list64_t(c.classMethods + n_value, info, "");
  4594. outs() << " protocols ";
  4595. sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
  4596. info, n_value, c.protocols);
  4597. if (n_value != 0) {
  4598. if (info->verbose && sym_name != nullptr)
  4599. outs() << sym_name;
  4600. else
  4601. outs() << format("0x%" PRIx64, n_value);
  4602. if (c.protocols != 0)
  4603. outs() << " + " << format("0x%" PRIx64, c.protocols);
  4604. } else
  4605. outs() << format("0x%" PRIx64, c.protocols);
  4606. outs() << "\n";
  4607. if (c.protocols + n_value != 0)
  4608. print_protocol_list64_t(c.protocols + n_value, info);
  4609. outs() << "instanceProperties ";
  4610. sym_name =
  4611. get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
  4612. S, info, n_value, c.instanceProperties);
  4613. if (n_value != 0) {
  4614. if (info->verbose && sym_name != nullptr)
  4615. outs() << sym_name;
  4616. else
  4617. outs() << format("0x%" PRIx64, n_value);
  4618. if (c.instanceProperties != 0)
  4619. outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
  4620. } else
  4621. outs() << format("0x%" PRIx64, c.instanceProperties);
  4622. outs() << "\n";
  4623. if (c.instanceProperties + n_value != 0)
  4624. print_objc_property_list64(c.instanceProperties + n_value, info);
  4625. }
  4626. static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
  4627. struct category32_t c;
  4628. const char *r;
  4629. uint32_t offset, left;
  4630. SectionRef S, xS;
  4631. const char *name;
  4632. r = get_pointer_32(p, offset, left, S, info);
  4633. if (r == nullptr)
  4634. return;
  4635. memset(&c, '\0', sizeof(struct category32_t));
  4636. if (left < sizeof(struct category32_t)) {
  4637. memcpy(&c, r, left);
  4638. outs() << " (category_t entends past the end of the section)\n";
  4639. } else
  4640. memcpy(&c, r, sizeof(struct category32_t));
  4641. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4642. swapStruct(c);
  4643. outs() << " name " << format("0x%" PRIx32, c.name);
  4644. name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
  4645. c.name);
  4646. if (name)
  4647. outs() << " " << name;
  4648. outs() << "\n";
  4649. outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
  4650. if (c.cls != 0)
  4651. print_class32_t(c.cls, info);
  4652. outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
  4653. << "\n";
  4654. if (c.instanceMethods != 0)
  4655. print_method_list32_t(c.instanceMethods, info, "");
  4656. outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
  4657. << "\n";
  4658. if (c.classMethods != 0)
  4659. print_method_list32_t(c.classMethods, info, "");
  4660. outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
  4661. if (c.protocols != 0)
  4662. print_protocol_list32_t(c.protocols, info);
  4663. outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
  4664. << "\n";
  4665. if (c.instanceProperties != 0)
  4666. print_objc_property_list32(c.instanceProperties, info);
  4667. }
  4668. static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
  4669. uint32_t i, left, offset, xoffset;
  4670. uint64_t p, n_value;
  4671. struct message_ref64 mr;
  4672. const char *name, *sym_name;
  4673. const char *r;
  4674. SectionRef xS;
  4675. if (S == SectionRef())
  4676. return;
  4677. StringRef SectName;
  4678. S.getName(SectName);
  4679. DataRefImpl Ref = S.getRawDataRefImpl();
  4680. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  4681. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  4682. offset = 0;
  4683. for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
  4684. p = S.getAddress() + i;
  4685. r = get_pointer_64(p, offset, left, S, info);
  4686. if (r == nullptr)
  4687. return;
  4688. memset(&mr, '\0', sizeof(struct message_ref64));
  4689. if (left < sizeof(struct message_ref64)) {
  4690. memcpy(&mr, r, left);
  4691. outs() << " (message_ref entends past the end of the section)\n";
  4692. } else
  4693. memcpy(&mr, r, sizeof(struct message_ref64));
  4694. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4695. swapStruct(mr);
  4696. outs() << " imp ";
  4697. name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
  4698. n_value, mr.imp);
  4699. if (n_value != 0) {
  4700. outs() << format("0x%" PRIx64, n_value) << " ";
  4701. if (mr.imp != 0)
  4702. outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
  4703. } else
  4704. outs() << format("0x%" PRIx64, mr.imp) << " ";
  4705. if (name != nullptr)
  4706. outs() << " " << name;
  4707. outs() << "\n";
  4708. outs() << " sel ";
  4709. sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
  4710. info, n_value, mr.sel);
  4711. if (n_value != 0) {
  4712. if (info->verbose && sym_name != nullptr)
  4713. outs() << sym_name;
  4714. else
  4715. outs() << format("0x%" PRIx64, n_value);
  4716. if (mr.sel != 0)
  4717. outs() << " + " << format("0x%" PRIx64, mr.sel);
  4718. } else
  4719. outs() << format("0x%" PRIx64, mr.sel);
  4720. name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
  4721. if (name != nullptr)
  4722. outs() << format(" %.*s", left, name);
  4723. outs() << "\n";
  4724. offset += sizeof(struct message_ref64);
  4725. }
  4726. }
  4727. static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
  4728. uint32_t i, left, offset, xoffset, p;
  4729. struct message_ref32 mr;
  4730. const char *name, *r;
  4731. SectionRef xS;
  4732. if (S == SectionRef())
  4733. return;
  4734. StringRef SectName;
  4735. S.getName(SectName);
  4736. DataRefImpl Ref = S.getRawDataRefImpl();
  4737. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  4738. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  4739. offset = 0;
  4740. for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
  4741. p = S.getAddress() + i;
  4742. r = get_pointer_32(p, offset, left, S, info);
  4743. if (r == nullptr)
  4744. return;
  4745. memset(&mr, '\0', sizeof(struct message_ref32));
  4746. if (left < sizeof(struct message_ref32)) {
  4747. memcpy(&mr, r, left);
  4748. outs() << " (message_ref entends past the end of the section)\n";
  4749. } else
  4750. memcpy(&mr, r, sizeof(struct message_ref32));
  4751. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4752. swapStruct(mr);
  4753. outs() << " imp " << format("0x%" PRIx32, mr.imp);
  4754. name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
  4755. mr.imp);
  4756. if (name != nullptr)
  4757. outs() << " " << name;
  4758. outs() << "\n";
  4759. outs() << " sel " << format("0x%" PRIx32, mr.sel);
  4760. name = get_pointer_32(mr.sel, xoffset, left, xS, info);
  4761. if (name != nullptr)
  4762. outs() << " " << name;
  4763. outs() << "\n";
  4764. offset += sizeof(struct message_ref32);
  4765. }
  4766. }
  4767. static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
  4768. uint32_t left, offset, swift_version;
  4769. uint64_t p;
  4770. struct objc_image_info64 o;
  4771. const char *r;
  4772. if (S == SectionRef())
  4773. return;
  4774. StringRef SectName;
  4775. S.getName(SectName);
  4776. DataRefImpl Ref = S.getRawDataRefImpl();
  4777. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  4778. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  4779. p = S.getAddress();
  4780. r = get_pointer_64(p, offset, left, S, info);
  4781. if (r == nullptr)
  4782. return;
  4783. memset(&o, '\0', sizeof(struct objc_image_info64));
  4784. if (left < sizeof(struct objc_image_info64)) {
  4785. memcpy(&o, r, left);
  4786. outs() << " (objc_image_info entends past the end of the section)\n";
  4787. } else
  4788. memcpy(&o, r, sizeof(struct objc_image_info64));
  4789. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4790. swapStruct(o);
  4791. outs() << " version " << o.version << "\n";
  4792. outs() << " flags " << format("0x%" PRIx32, o.flags);
  4793. if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
  4794. outs() << " OBJC_IMAGE_IS_REPLACEMENT";
  4795. if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
  4796. outs() << " OBJC_IMAGE_SUPPORTS_GC";
  4797. swift_version = (o.flags >> 8) & 0xff;
  4798. if (swift_version != 0) {
  4799. if (swift_version == 1)
  4800. outs() << " Swift 1.0";
  4801. else if (swift_version == 2)
  4802. outs() << " Swift 1.1";
  4803. else
  4804. outs() << " unknown future Swift version (" << swift_version << ")";
  4805. }
  4806. outs() << "\n";
  4807. }
  4808. static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
  4809. uint32_t left, offset, swift_version, p;
  4810. struct objc_image_info32 o;
  4811. const char *r;
  4812. if (S == SectionRef())
  4813. return;
  4814. StringRef SectName;
  4815. S.getName(SectName);
  4816. DataRefImpl Ref = S.getRawDataRefImpl();
  4817. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  4818. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  4819. p = S.getAddress();
  4820. r = get_pointer_32(p, offset, left, S, info);
  4821. if (r == nullptr)
  4822. return;
  4823. memset(&o, '\0', sizeof(struct objc_image_info32));
  4824. if (left < sizeof(struct objc_image_info32)) {
  4825. memcpy(&o, r, left);
  4826. outs() << " (objc_image_info entends past the end of the section)\n";
  4827. } else
  4828. memcpy(&o, r, sizeof(struct objc_image_info32));
  4829. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4830. swapStruct(o);
  4831. outs() << " version " << o.version << "\n";
  4832. outs() << " flags " << format("0x%" PRIx32, o.flags);
  4833. if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
  4834. outs() << " OBJC_IMAGE_IS_REPLACEMENT";
  4835. if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
  4836. outs() << " OBJC_IMAGE_SUPPORTS_GC";
  4837. swift_version = (o.flags >> 8) & 0xff;
  4838. if (swift_version != 0) {
  4839. if (swift_version == 1)
  4840. outs() << " Swift 1.0";
  4841. else if (swift_version == 2)
  4842. outs() << " Swift 1.1";
  4843. else
  4844. outs() << " unknown future Swift version (" << swift_version << ")";
  4845. }
  4846. outs() << "\n";
  4847. }
  4848. static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
  4849. uint32_t left, offset, p;
  4850. struct imageInfo_t o;
  4851. const char *r;
  4852. StringRef SectName;
  4853. S.getName(SectName);
  4854. DataRefImpl Ref = S.getRawDataRefImpl();
  4855. StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
  4856. outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
  4857. p = S.getAddress();
  4858. r = get_pointer_32(p, offset, left, S, info);
  4859. if (r == nullptr)
  4860. return;
  4861. memset(&o, '\0', sizeof(struct imageInfo_t));
  4862. if (left < sizeof(struct imageInfo_t)) {
  4863. memcpy(&o, r, left);
  4864. outs() << " (imageInfo entends past the end of the section)\n";
  4865. } else
  4866. memcpy(&o, r, sizeof(struct imageInfo_t));
  4867. if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
  4868. swapStruct(o);
  4869. outs() << " version " << o.version << "\n";
  4870. outs() << " flags " << format("0x%" PRIx32, o.flags);
  4871. if (o.flags & 0x1)
  4872. outs() << " F&C";
  4873. if (o.flags & 0x2)
  4874. outs() << " GC";
  4875. if (o.flags & 0x4)
  4876. outs() << " GC-only";
  4877. else
  4878. outs() << " RR";
  4879. outs() << "\n";
  4880. }
  4881. static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
  4882. SymbolAddressMap AddrMap;
  4883. if (verbose)
  4884. CreateSymbolAddressMap(O, &AddrMap);
  4885. std::vector<SectionRef> Sections;
  4886. for (const SectionRef &Section : O->sections()) {
  4887. StringRef SectName;
  4888. Section.getName(SectName);
  4889. Sections.push_back(Section);
  4890. }
  4891. struct DisassembleInfo info;
  4892. // Set up the block of info used by the Symbolizer call backs.
  4893. info.verbose = verbose;
  4894. info.O = O;
  4895. info.AddrMap = &AddrMap;
  4896. info.Sections = &Sections;
  4897. info.class_name = nullptr;
  4898. info.selector_name = nullptr;
  4899. info.method = nullptr;
  4900. info.demangled_name = nullptr;
  4901. info.bindtable = nullptr;
  4902. info.adrp_addr = 0;
  4903. info.adrp_inst = 0;
  4904. info.depth = 0;
  4905. SectionRef CL = get_section(O, "__OBJC2", "__class_list");
  4906. if (CL == SectionRef())
  4907. CL = get_section(O, "__DATA", "__objc_classlist");
  4908. if (CL == SectionRef())
  4909. CL = get_section(O, "__DATA_CONST", "__objc_classlist");
  4910. if (CL == SectionRef())
  4911. CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
  4912. info.S = CL;
  4913. walk_pointer_list_64("class", CL, O, &info, print_class64_t);
  4914. SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
  4915. if (CR == SectionRef())
  4916. CR = get_section(O, "__DATA", "__objc_classrefs");
  4917. if (CR == SectionRef())
  4918. CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
  4919. if (CR == SectionRef())
  4920. CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
  4921. info.S = CR;
  4922. walk_pointer_list_64("class refs", CR, O, &info, nullptr);
  4923. SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
  4924. if (SR == SectionRef())
  4925. SR = get_section(O, "__DATA", "__objc_superrefs");
  4926. if (SR == SectionRef())
  4927. SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
  4928. if (SR == SectionRef())
  4929. SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
  4930. info.S = SR;
  4931. walk_pointer_list_64("super refs", SR, O, &info, nullptr);
  4932. SectionRef CA = get_section(O, "__OBJC2", "__category_list");
  4933. if (CA == SectionRef())
  4934. CA = get_section(O, "__DATA", "__objc_catlist");
  4935. if (CA == SectionRef())
  4936. CA = get_section(O, "__DATA_CONST", "__objc_catlist");
  4937. if (CA == SectionRef())
  4938. CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
  4939. info.S = CA;
  4940. walk_pointer_list_64("category", CA, O, &info, print_category64_t);
  4941. SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
  4942. if (PL == SectionRef())
  4943. PL = get_section(O, "__DATA", "__objc_protolist");
  4944. if (PL == SectionRef())
  4945. PL = get_section(O, "__DATA_CONST", "__objc_protolist");
  4946. if (PL == SectionRef())
  4947. PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
  4948. info.S = PL;
  4949. walk_pointer_list_64("protocol", PL, O, &info, nullptr);
  4950. SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
  4951. if (MR == SectionRef())
  4952. MR = get_section(O, "__DATA", "__objc_msgrefs");
  4953. if (MR == SectionRef())
  4954. MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
  4955. if (MR == SectionRef())
  4956. MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
  4957. info.S = MR;
  4958. print_message_refs64(MR, &info);
  4959. SectionRef II = get_section(O, "__OBJC2", "__image_info");
  4960. if (II == SectionRef())
  4961. II = get_section(O, "__DATA", "__objc_imageinfo");
  4962. if (II == SectionRef())
  4963. II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
  4964. if (II == SectionRef())
  4965. II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
  4966. info.S = II;
  4967. print_image_info64(II, &info);
  4968. }
  4969. static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
  4970. SymbolAddressMap AddrMap;
  4971. if (verbose)
  4972. CreateSymbolAddressMap(O, &AddrMap);
  4973. std::vector<SectionRef> Sections;
  4974. for (const SectionRef &Section : O->sections()) {
  4975. StringRef SectName;
  4976. Section.getName(SectName);
  4977. Sections.push_back(Section);
  4978. }
  4979. struct DisassembleInfo info;
  4980. // Set up the block of info used by the Symbolizer call backs.
  4981. info.verbose = verbose;
  4982. info.O = O;
  4983. info.AddrMap = &AddrMap;
  4984. info.Sections = &Sections;
  4985. info.class_name = nullptr;
  4986. info.selector_name = nullptr;
  4987. info.method = nullptr;
  4988. info.demangled_name = nullptr;
  4989. info.bindtable = nullptr;
  4990. info.adrp_addr = 0;
  4991. info.adrp_inst = 0;
  4992. SectionRef CL = get_section(O, "__OBJC2", "__class_list");
  4993. if (CL == SectionRef())
  4994. CL = get_section(O, "__DATA", "__objc_classlist");
  4995. if (CL == SectionRef())
  4996. CL = get_section(O, "__DATA_CONST", "__objc_classlist");
  4997. if (CL == SectionRef())
  4998. CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
  4999. info.S = CL;
  5000. walk_pointer_list_32("class", CL, O, &info, print_class32_t);
  5001. SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
  5002. if (CR == SectionRef())
  5003. CR = get_section(O, "__DATA", "__objc_classrefs");
  5004. if (CR == SectionRef())
  5005. CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
  5006. if (CR == SectionRef())
  5007. CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
  5008. info.S = CR;
  5009. walk_pointer_list_32("class refs", CR, O, &info, nullptr);
  5010. SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
  5011. if (SR == SectionRef())
  5012. SR = get_section(O, "__DATA", "__objc_superrefs");
  5013. if (SR == SectionRef())
  5014. SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
  5015. if (SR == SectionRef())
  5016. SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
  5017. info.S = SR;
  5018. walk_pointer_list_32("super refs", SR, O, &info, nullptr);
  5019. SectionRef CA = get_section(O, "__OBJC2", "__category_list");
  5020. if (CA == SectionRef())
  5021. CA = get_section(O, "__DATA", "__objc_catlist");
  5022. if (CA == SectionRef())
  5023. CA = get_section(O, "__DATA_CONST", "__objc_catlist");
  5024. if (CA == SectionRef())
  5025. CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
  5026. info.S = CA;
  5027. walk_pointer_list_32("category", CA, O, &info, print_category32_t);
  5028. SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
  5029. if (PL == SectionRef())
  5030. PL = get_section(O, "__DATA", "__objc_protolist");
  5031. if (PL == SectionRef())
  5032. PL = get_section(O, "__DATA_CONST", "__objc_protolist");
  5033. if (PL == SectionRef())
  5034. PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
  5035. info.S = PL;
  5036. walk_pointer_list_32("protocol", PL, O, &info, nullptr);
  5037. SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
  5038. if (MR == SectionRef())
  5039. MR = get_section(O, "__DATA", "__objc_msgrefs");
  5040. if (MR == SectionRef())
  5041. MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
  5042. if (MR == SectionRef())
  5043. MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
  5044. info.S = MR;
  5045. print_message_refs32(MR, &info);
  5046. SectionRef II = get_section(O, "__OBJC2", "__image_info");
  5047. if (II == SectionRef())
  5048. II = get_section(O, "__DATA", "__objc_imageinfo");
  5049. if (II == SectionRef())
  5050. II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
  5051. if (II == SectionRef())
  5052. II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
  5053. info.S = II;
  5054. print_image_info32(II, &info);
  5055. }
  5056. static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
  5057. uint32_t i, j, p, offset, xoffset, left, defs_left, def;
  5058. const char *r, *name, *defs;
  5059. struct objc_module_t module;
  5060. SectionRef S, xS;
  5061. struct objc_symtab_t symtab;
  5062. struct objc_class_t objc_class;
  5063. struct objc_category_t objc_category;
  5064. outs() << "Objective-C segment\n";
  5065. S = get_section(O, "__OBJC", "__module_info");
  5066. if (S == SectionRef())
  5067. return false;
  5068. SymbolAddressMap AddrMap;
  5069. if (verbose)
  5070. CreateSymbolAddressMap(O, &AddrMap);
  5071. std::vector<SectionRef> Sections;
  5072. for (const SectionRef &Section : O->sections()) {
  5073. StringRef SectName;
  5074. Section.getName(SectName);
  5075. Sections.push_back(Section);
  5076. }
  5077. struct DisassembleInfo info;
  5078. // Set up the block of info used by the Symbolizer call backs.
  5079. info.verbose = verbose;
  5080. info.O = O;
  5081. info.AddrMap = &AddrMap;
  5082. info.Sections = &Sections;
  5083. info.class_name = nullptr;
  5084. info.selector_name = nullptr;
  5085. info.method = nullptr;
  5086. info.demangled_name = nullptr;
  5087. info.bindtable = nullptr;
  5088. info.adrp_addr = 0;
  5089. info.adrp_inst = 0;
  5090. for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
  5091. p = S.getAddress() + i;
  5092. r = get_pointer_32(p, offset, left, S, &info, true);
  5093. if (r == nullptr)
  5094. return true;
  5095. memset(&module, '\0', sizeof(struct objc_module_t));
  5096. if (left < sizeof(struct objc_module_t)) {
  5097. memcpy(&module, r, left);
  5098. outs() << " (module extends past end of __module_info section)\n";
  5099. } else
  5100. memcpy(&module, r, sizeof(struct objc_module_t));
  5101. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5102. swapStruct(module);
  5103. outs() << "Module " << format("0x%" PRIx32, p) << "\n";
  5104. outs() << " version " << module.version << "\n";
  5105. outs() << " size " << module.size << "\n";
  5106. outs() << " name ";
  5107. name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
  5108. if (name != nullptr)
  5109. outs() << format("%.*s", left, name);
  5110. else
  5111. outs() << format("0x%08" PRIx32, module.name)
  5112. << "(not in an __OBJC section)";
  5113. outs() << "\n";
  5114. r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
  5115. if (module.symtab == 0 || r == nullptr) {
  5116. outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
  5117. << " (not in an __OBJC section)\n";
  5118. continue;
  5119. }
  5120. outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
  5121. memset(&symtab, '\0', sizeof(struct objc_symtab_t));
  5122. defs_left = 0;
  5123. defs = nullptr;
  5124. if (left < sizeof(struct objc_symtab_t)) {
  5125. memcpy(&symtab, r, left);
  5126. outs() << "\tsymtab extends past end of an __OBJC section)\n";
  5127. } else {
  5128. memcpy(&symtab, r, sizeof(struct objc_symtab_t));
  5129. if (left > sizeof(struct objc_symtab_t)) {
  5130. defs_left = left - sizeof(struct objc_symtab_t);
  5131. defs = r + sizeof(struct objc_symtab_t);
  5132. }
  5133. }
  5134. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5135. swapStruct(symtab);
  5136. outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
  5137. r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
  5138. outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
  5139. if (r == nullptr)
  5140. outs() << " (not in an __OBJC section)";
  5141. outs() << "\n";
  5142. outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
  5143. outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
  5144. if (symtab.cls_def_cnt > 0)
  5145. outs() << "\tClass Definitions\n";
  5146. for (j = 0; j < symtab.cls_def_cnt; j++) {
  5147. if ((j + 1) * sizeof(uint32_t) > defs_left) {
  5148. outs() << "\t(remaining class defs entries entends past the end of the "
  5149. << "section)\n";
  5150. break;
  5151. }
  5152. memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
  5153. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5154. sys::swapByteOrder(def);
  5155. r = get_pointer_32(def, xoffset, left, xS, &info, true);
  5156. outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
  5157. if (r != nullptr) {
  5158. if (left > sizeof(struct objc_class_t)) {
  5159. outs() << "\n";
  5160. memcpy(&objc_class, r, sizeof(struct objc_class_t));
  5161. } else {
  5162. outs() << " (entends past the end of the section)\n";
  5163. memset(&objc_class, '\0', sizeof(struct objc_class_t));
  5164. memcpy(&objc_class, r, left);
  5165. }
  5166. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5167. swapStruct(objc_class);
  5168. print_objc_class_t(&objc_class, &info);
  5169. } else {
  5170. outs() << "(not in an __OBJC section)\n";
  5171. }
  5172. if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
  5173. outs() << "\tMeta Class";
  5174. r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
  5175. if (r != nullptr) {
  5176. if (left > sizeof(struct objc_class_t)) {
  5177. outs() << "\n";
  5178. memcpy(&objc_class, r, sizeof(struct objc_class_t));
  5179. } else {
  5180. outs() << " (entends past the end of the section)\n";
  5181. memset(&objc_class, '\0', sizeof(struct objc_class_t));
  5182. memcpy(&objc_class, r, left);
  5183. }
  5184. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5185. swapStruct(objc_class);
  5186. print_objc_class_t(&objc_class, &info);
  5187. } else {
  5188. outs() << "(not in an __OBJC section)\n";
  5189. }
  5190. }
  5191. }
  5192. if (symtab.cat_def_cnt > 0)
  5193. outs() << "\tCategory Definitions\n";
  5194. for (j = 0; j < symtab.cat_def_cnt; j++) {
  5195. if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
  5196. outs() << "\t(remaining category defs entries entends past the end of "
  5197. << "the section)\n";
  5198. break;
  5199. }
  5200. memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
  5201. sizeof(uint32_t));
  5202. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5203. sys::swapByteOrder(def);
  5204. r = get_pointer_32(def, xoffset, left, xS, &info, true);
  5205. outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
  5206. << format("0x%08" PRIx32, def);
  5207. if (r != nullptr) {
  5208. if (left > sizeof(struct objc_category_t)) {
  5209. outs() << "\n";
  5210. memcpy(&objc_category, r, sizeof(struct objc_category_t));
  5211. } else {
  5212. outs() << " (entends past the end of the section)\n";
  5213. memset(&objc_category, '\0', sizeof(struct objc_category_t));
  5214. memcpy(&objc_category, r, left);
  5215. }
  5216. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5217. swapStruct(objc_category);
  5218. print_objc_objc_category_t(&objc_category, &info);
  5219. } else {
  5220. outs() << "(not in an __OBJC section)\n";
  5221. }
  5222. }
  5223. }
  5224. const SectionRef II = get_section(O, "__OBJC", "__image_info");
  5225. if (II != SectionRef())
  5226. print_image_info(II, &info);
  5227. return true;
  5228. }
  5229. static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
  5230. uint32_t size, uint32_t addr) {
  5231. SymbolAddressMap AddrMap;
  5232. CreateSymbolAddressMap(O, &AddrMap);
  5233. std::vector<SectionRef> Sections;
  5234. for (const SectionRef &Section : O->sections()) {
  5235. StringRef SectName;
  5236. Section.getName(SectName);
  5237. Sections.push_back(Section);
  5238. }
  5239. struct DisassembleInfo info;
  5240. // Set up the block of info used by the Symbolizer call backs.
  5241. info.verbose = true;
  5242. info.O = O;
  5243. info.AddrMap = &AddrMap;
  5244. info.Sections = &Sections;
  5245. info.class_name = nullptr;
  5246. info.selector_name = nullptr;
  5247. info.method = nullptr;
  5248. info.demangled_name = nullptr;
  5249. info.bindtable = nullptr;
  5250. info.adrp_addr = 0;
  5251. info.adrp_inst = 0;
  5252. const char *p;
  5253. struct objc_protocol_t protocol;
  5254. uint32_t left, paddr;
  5255. for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
  5256. memset(&protocol, '\0', sizeof(struct objc_protocol_t));
  5257. left = size - (p - sect);
  5258. if (left < sizeof(struct objc_protocol_t)) {
  5259. outs() << "Protocol extends past end of __protocol section\n";
  5260. memcpy(&protocol, p, left);
  5261. } else
  5262. memcpy(&protocol, p, sizeof(struct objc_protocol_t));
  5263. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  5264. swapStruct(protocol);
  5265. paddr = addr + (p - sect);
  5266. outs() << "Protocol " << format("0x%" PRIx32, paddr);
  5267. if (print_protocol(paddr, 0, &info))
  5268. outs() << "(not in an __OBJC section)\n";
  5269. }
  5270. }
  5271. #ifdef HAVE_LIBXAR
  5272. inline void swapStruct(struct xar_header &xar) {
  5273. sys::swapByteOrder(xar.magic);
  5274. sys::swapByteOrder(xar.size);
  5275. sys::swapByteOrder(xar.version);
  5276. sys::swapByteOrder(xar.toc_length_compressed);
  5277. sys::swapByteOrder(xar.toc_length_uncompressed);
  5278. sys::swapByteOrder(xar.cksum_alg);
  5279. }
  5280. static void PrintModeVerbose(uint32_t mode) {
  5281. switch(mode & S_IFMT){
  5282. case S_IFDIR:
  5283. outs() << "d";
  5284. break;
  5285. case S_IFCHR:
  5286. outs() << "c";
  5287. break;
  5288. case S_IFBLK:
  5289. outs() << "b";
  5290. break;
  5291. case S_IFREG:
  5292. outs() << "-";
  5293. break;
  5294. case S_IFLNK:
  5295. outs() << "l";
  5296. break;
  5297. case S_IFSOCK:
  5298. outs() << "s";
  5299. break;
  5300. default:
  5301. outs() << "?";
  5302. break;
  5303. }
  5304. /* owner permissions */
  5305. if(mode & S_IREAD)
  5306. outs() << "r";
  5307. else
  5308. outs() << "-";
  5309. if(mode & S_IWRITE)
  5310. outs() << "w";
  5311. else
  5312. outs() << "-";
  5313. if(mode & S_ISUID)
  5314. outs() << "s";
  5315. else if(mode & S_IEXEC)
  5316. outs() << "x";
  5317. else
  5318. outs() << "-";
  5319. /* group permissions */
  5320. if(mode & (S_IREAD >> 3))
  5321. outs() << "r";
  5322. else
  5323. outs() << "-";
  5324. if(mode & (S_IWRITE >> 3))
  5325. outs() << "w";
  5326. else
  5327. outs() << "-";
  5328. if(mode & S_ISGID)
  5329. outs() << "s";
  5330. else if(mode & (S_IEXEC >> 3))
  5331. outs() << "x";
  5332. else
  5333. outs() << "-";
  5334. /* other permissions */
  5335. if(mode & (S_IREAD >> 6))
  5336. outs() << "r";
  5337. else
  5338. outs() << "-";
  5339. if(mode & (S_IWRITE >> 6))
  5340. outs() << "w";
  5341. else
  5342. outs() << "-";
  5343. if(mode & S_ISVTX)
  5344. outs() << "t";
  5345. else if(mode & (S_IEXEC >> 6))
  5346. outs() << "x";
  5347. else
  5348. outs() << "-";
  5349. }
  5350. static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
  5351. xar_iter_t xi;
  5352. xar_file_t xf;
  5353. xar_iter_t xp;
  5354. const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
  5355. char *endp;
  5356. uint32_t mode_value;
  5357. xi = xar_iter_new();
  5358. if (!xi) {
  5359. errs() << "Can't obtain an xar iterator for xar archive "
  5360. << XarFilename << "\n";
  5361. return;
  5362. }
  5363. // Go through the xar's files.
  5364. for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
  5365. xp = xar_iter_new();
  5366. if(!xp){
  5367. errs() << "Can't obtain an xar iterator for xar archive "
  5368. << XarFilename << "\n";
  5369. return;
  5370. }
  5371. type = nullptr;
  5372. mode = nullptr;
  5373. user = nullptr;
  5374. group = nullptr;
  5375. size = nullptr;
  5376. mtime = nullptr;
  5377. name = nullptr;
  5378. for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
  5379. const char *val = nullptr;
  5380. xar_prop_get(xf, key, &val);
  5381. #if 0 // Useful for debugging.
  5382. outs() << "key: " << key << " value: " << val << "\n";
  5383. #endif
  5384. if(strcmp(key, "type") == 0)
  5385. type = val;
  5386. if(strcmp(key, "mode") == 0)
  5387. mode = val;
  5388. if(strcmp(key, "user") == 0)
  5389. user = val;
  5390. if(strcmp(key, "group") == 0)
  5391. group = val;
  5392. if(strcmp(key, "data/size") == 0)
  5393. size = val;
  5394. if(strcmp(key, "mtime") == 0)
  5395. mtime = val;
  5396. if(strcmp(key, "name") == 0)
  5397. name = val;
  5398. }
  5399. if(mode != nullptr){
  5400. mode_value = strtoul(mode, &endp, 8);
  5401. if(*endp != '\0')
  5402. outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
  5403. if(strcmp(type, "file") == 0)
  5404. mode_value |= S_IFREG;
  5405. PrintModeVerbose(mode_value);
  5406. outs() << " ";
  5407. }
  5408. if(user != nullptr)
  5409. outs() << format("%10s/", user);
  5410. if(group != nullptr)
  5411. outs() << format("%-10s ", group);
  5412. if(size != nullptr)
  5413. outs() << format("%7s ", size);
  5414. if(mtime != nullptr){
  5415. for(m = mtime; *m != 'T' && *m != '\0'; m++)
  5416. outs() << *m;
  5417. if(*m == 'T')
  5418. m++;
  5419. outs() << " ";
  5420. for( ; *m != 'Z' && *m != '\0'; m++)
  5421. outs() << *m;
  5422. outs() << " ";
  5423. }
  5424. if(name != nullptr)
  5425. outs() << name;
  5426. outs() << "\n";
  5427. xar_iter_free(xp);
  5428. }
  5429. xar_iter_free(xi);
  5430. }
  5431. static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
  5432. uint32_t size, bool verbose,
  5433. bool PrintXarHeader, bool PrintXarFileHeaders,
  5434. std::string XarMemberName) {
  5435. if(size < sizeof(struct xar_header)) {
  5436. outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
  5437. "of struct xar_header)\n";
  5438. return;
  5439. }
  5440. struct xar_header XarHeader;
  5441. memcpy(&XarHeader, sect, sizeof(struct xar_header));
  5442. if (sys::IsLittleEndianHost)
  5443. swapStruct(XarHeader);
  5444. if (PrintXarHeader) {
  5445. if (!XarMemberName.empty())
  5446. outs() << "In xar member " << XarMemberName << ": ";
  5447. else
  5448. outs() << "For (__LLVM,__bundle) section: ";
  5449. outs() << "xar header\n";
  5450. if (XarHeader.magic == XAR_HEADER_MAGIC)
  5451. outs() << " magic XAR_HEADER_MAGIC\n";
  5452. else
  5453. outs() << " magic "
  5454. << format_hex(XarHeader.magic, 10, true)
  5455. << " (not XAR_HEADER_MAGIC)\n";
  5456. outs() << " size " << XarHeader.size << "\n";
  5457. outs() << " version " << XarHeader.version << "\n";
  5458. outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
  5459. << "\n";
  5460. outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
  5461. << "\n";
  5462. outs() << " cksum_alg ";
  5463. switch (XarHeader.cksum_alg) {
  5464. case XAR_CKSUM_NONE:
  5465. outs() << "XAR_CKSUM_NONE\n";
  5466. break;
  5467. case XAR_CKSUM_SHA1:
  5468. outs() << "XAR_CKSUM_SHA1\n";
  5469. break;
  5470. case XAR_CKSUM_MD5:
  5471. outs() << "XAR_CKSUM_MD5\n";
  5472. break;
  5473. #ifdef XAR_CKSUM_SHA256
  5474. case XAR_CKSUM_SHA256:
  5475. outs() << "XAR_CKSUM_SHA256\n";
  5476. break;
  5477. #endif
  5478. #ifdef XAR_CKSUM_SHA512
  5479. case XAR_CKSUM_SHA512:
  5480. outs() << "XAR_CKSUM_SHA512\n";
  5481. break;
  5482. #endif
  5483. default:
  5484. outs() << XarHeader.cksum_alg << "\n";
  5485. }
  5486. }
  5487. SmallString<128> XarFilename;
  5488. int FD;
  5489. std::error_code XarEC =
  5490. sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
  5491. if (XarEC) {
  5492. errs() << XarEC.message() << "\n";
  5493. return;
  5494. }
  5495. ToolOutputFile XarFile(XarFilename, FD);
  5496. raw_fd_ostream &XarOut = XarFile.os();
  5497. StringRef XarContents(sect, size);
  5498. XarOut << XarContents;
  5499. XarOut.close();
  5500. if (XarOut.has_error())
  5501. return;
  5502. xar_t xar = xar_open(XarFilename.c_str(), READ);
  5503. if (!xar) {
  5504. errs() << "Can't create temporary xar archive " << XarFilename << "\n";
  5505. return;
  5506. }
  5507. SmallString<128> TocFilename;
  5508. std::error_code TocEC =
  5509. sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
  5510. if (TocEC) {
  5511. errs() << TocEC.message() << "\n";
  5512. return;
  5513. }
  5514. xar_serialize(xar, TocFilename.c_str());
  5515. if (PrintXarFileHeaders) {
  5516. if (!XarMemberName.empty())
  5517. outs() << "In xar member " << XarMemberName << ": ";
  5518. else
  5519. outs() << "For (__LLVM,__bundle) section: ";
  5520. outs() << "xar archive files:\n";
  5521. PrintXarFilesSummary(XarFilename.c_str(), xar);
  5522. }
  5523. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  5524. MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
  5525. if (std::error_code EC = FileOrErr.getError()) {
  5526. errs() << EC.message() << "\n";
  5527. return;
  5528. }
  5529. std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
  5530. if (!XarMemberName.empty())
  5531. outs() << "In xar member " << XarMemberName << ": ";
  5532. else
  5533. outs() << "For (__LLVM,__bundle) section: ";
  5534. outs() << "xar table of contents:\n";
  5535. outs() << Buffer->getBuffer() << "\n";
  5536. // TODO: Go through the xar's files.
  5537. xar_iter_t xi = xar_iter_new();
  5538. if(!xi){
  5539. errs() << "Can't obtain an xar iterator for xar archive "
  5540. << XarFilename.c_str() << "\n";
  5541. xar_close(xar);
  5542. return;
  5543. }
  5544. for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
  5545. const char *key;
  5546. xar_iter_t xp;
  5547. const char *member_name, *member_type, *member_size_string;
  5548. size_t member_size;
  5549. xp = xar_iter_new();
  5550. if(!xp){
  5551. errs() << "Can't obtain an xar iterator for xar archive "
  5552. << XarFilename.c_str() << "\n";
  5553. xar_close(xar);
  5554. return;
  5555. }
  5556. member_name = NULL;
  5557. member_type = NULL;
  5558. member_size_string = NULL;
  5559. for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
  5560. const char *val = nullptr;
  5561. xar_prop_get(xf, key, &val);
  5562. #if 0 // Useful for debugging.
  5563. outs() << "key: " << key << " value: " << val << "\n";
  5564. #endif
  5565. if (strcmp(key, "name") == 0)
  5566. member_name = val;
  5567. if (strcmp(key, "type") == 0)
  5568. member_type = val;
  5569. if (strcmp(key, "data/size") == 0)
  5570. member_size_string = val;
  5571. }
  5572. /*
  5573. * If we find a file with a name, date/size and type properties
  5574. * and with the type being "file" see if that is a xar file.
  5575. */
  5576. if (member_name != NULL && member_type != NULL &&
  5577. strcmp(member_type, "file") == 0 &&
  5578. member_size_string != NULL){
  5579. // Extract the file into a buffer.
  5580. char *endptr;
  5581. member_size = strtoul(member_size_string, &endptr, 10);
  5582. if (*endptr == '\0' && member_size != 0) {
  5583. char *buffer;
  5584. if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
  5585. #if 0 // Useful for debugging.
  5586. outs() << "xar member: " << member_name << " extracted\n";
  5587. #endif
  5588. // Set the XarMemberName we want to see printed in the header.
  5589. std::string OldXarMemberName;
  5590. // If XarMemberName is already set this is nested. So
  5591. // save the old name and create the nested name.
  5592. if (!XarMemberName.empty()) {
  5593. OldXarMemberName = XarMemberName;
  5594. XarMemberName =
  5595. (Twine("[") + XarMemberName + "]" + member_name).str();
  5596. } else {
  5597. OldXarMemberName = "";
  5598. XarMemberName = member_name;
  5599. }
  5600. // See if this is could be a xar file (nested).
  5601. if (member_size >= sizeof(struct xar_header)) {
  5602. #if 0 // Useful for debugging.
  5603. outs() << "could be a xar file: " << member_name << "\n";
  5604. #endif
  5605. memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
  5606. if (sys::IsLittleEndianHost)
  5607. swapStruct(XarHeader);
  5608. if (XarHeader.magic == XAR_HEADER_MAGIC)
  5609. DumpBitcodeSection(O, buffer, member_size, verbose,
  5610. PrintXarHeader, PrintXarFileHeaders,
  5611. XarMemberName);
  5612. }
  5613. XarMemberName = OldXarMemberName;
  5614. delete buffer;
  5615. }
  5616. }
  5617. }
  5618. xar_iter_free(xp);
  5619. }
  5620. xar_iter_free(xi);
  5621. xar_close(xar);
  5622. }
  5623. #endif // defined(HAVE_LIBXAR)
  5624. static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
  5625. if (O->is64Bit())
  5626. printObjc2_64bit_MetaData(O, verbose);
  5627. else {
  5628. MachO::mach_header H;
  5629. H = O->getHeader();
  5630. if (H.cputype == MachO::CPU_TYPE_ARM)
  5631. printObjc2_32bit_MetaData(O, verbose);
  5632. else {
  5633. // This is the 32-bit non-arm cputype case. Which is normally
  5634. // the first Objective-C ABI. But it may be the case of a
  5635. // binary for the iOS simulator which is the second Objective-C
  5636. // ABI. In that case printObjc1_32bit_MetaData() will determine that
  5637. // and return false.
  5638. if (!printObjc1_32bit_MetaData(O, verbose))
  5639. printObjc2_32bit_MetaData(O, verbose);
  5640. }
  5641. }
  5642. }
  5643. // GuessLiteralPointer returns a string which for the item in the Mach-O file
  5644. // for the address passed in as ReferenceValue for printing as a comment with
  5645. // the instruction and also returns the corresponding type of that item
  5646. // indirectly through ReferenceType.
  5647. //
  5648. // If ReferenceValue is an address of literal cstring then a pointer to the
  5649. // cstring is returned and ReferenceType is set to
  5650. // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
  5651. //
  5652. // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
  5653. // Class ref that name is returned and the ReferenceType is set accordingly.
  5654. //
  5655. // Lastly, literals which are Symbol address in a literal pool are looked for
  5656. // and if found the symbol name is returned and ReferenceType is set to
  5657. // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
  5658. //
  5659. // If there is no item in the Mach-O file for the address passed in as
  5660. // ReferenceValue nullptr is returned and ReferenceType is unchanged.
  5661. static const char *GuessLiteralPointer(uint64_t ReferenceValue,
  5662. uint64_t ReferencePC,
  5663. uint64_t *ReferenceType,
  5664. struct DisassembleInfo *info) {
  5665. // First see if there is an external relocation entry at the ReferencePC.
  5666. if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
  5667. uint64_t sect_addr = info->S.getAddress();
  5668. uint64_t sect_offset = ReferencePC - sect_addr;
  5669. bool reloc_found = false;
  5670. DataRefImpl Rel;
  5671. MachO::any_relocation_info RE;
  5672. bool isExtern = false;
  5673. SymbolRef Symbol;
  5674. for (const RelocationRef &Reloc : info->S.relocations()) {
  5675. uint64_t RelocOffset = Reloc.getOffset();
  5676. if (RelocOffset == sect_offset) {
  5677. Rel = Reloc.getRawDataRefImpl();
  5678. RE = info->O->getRelocation(Rel);
  5679. if (info->O->isRelocationScattered(RE))
  5680. continue;
  5681. isExtern = info->O->getPlainRelocationExternal(RE);
  5682. if (isExtern) {
  5683. symbol_iterator RelocSym = Reloc.getSymbol();
  5684. Symbol = *RelocSym;
  5685. }
  5686. reloc_found = true;
  5687. break;
  5688. }
  5689. }
  5690. // If there is an external relocation entry for a symbol in a section
  5691. // then used that symbol's value for the value of the reference.
  5692. if (reloc_found && isExtern) {
  5693. if (info->O->getAnyRelocationPCRel(RE)) {
  5694. unsigned Type = info->O->getAnyRelocationType(RE);
  5695. if (Type == MachO::X86_64_RELOC_SIGNED) {
  5696. ReferenceValue = Symbol.getValue();
  5697. }
  5698. }
  5699. }
  5700. }
  5701. // Look for literals such as Objective-C CFStrings refs, Selector refs,
  5702. // Message refs and Class refs.
  5703. bool classref, selref, msgref, cfstring;
  5704. uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
  5705. selref, msgref, cfstring);
  5706. if (classref && pointer_value == 0) {
  5707. // Note the ReferenceValue is a pointer into the __objc_classrefs section.
  5708. // And the pointer_value in that section is typically zero as it will be
  5709. // set by dyld as part of the "bind information".
  5710. const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
  5711. if (name != nullptr) {
  5712. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
  5713. const char *class_name = strrchr(name, '$');
  5714. if (class_name != nullptr && class_name[1] == '_' &&
  5715. class_name[2] != '\0') {
  5716. info->class_name = class_name + 2;
  5717. return name;
  5718. }
  5719. }
  5720. }
  5721. if (classref) {
  5722. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
  5723. const char *name =
  5724. get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
  5725. if (name != nullptr)
  5726. info->class_name = name;
  5727. else
  5728. name = "bad class ref";
  5729. return name;
  5730. }
  5731. if (cfstring) {
  5732. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
  5733. const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
  5734. return name;
  5735. }
  5736. if (selref && pointer_value == 0)
  5737. pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
  5738. if (pointer_value != 0)
  5739. ReferenceValue = pointer_value;
  5740. const char *name = GuessCstringPointer(ReferenceValue, info);
  5741. if (name) {
  5742. if (pointer_value != 0 && selref) {
  5743. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
  5744. info->selector_name = name;
  5745. } else if (pointer_value != 0 && msgref) {
  5746. info->class_name = nullptr;
  5747. *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
  5748. info->selector_name = name;
  5749. } else
  5750. *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
  5751. return name;
  5752. }
  5753. // Lastly look for an indirect symbol with this ReferenceValue which is in
  5754. // a literal pool. If found return that symbol name.
  5755. name = GuessIndirectSymbol(ReferenceValue, info);
  5756. if (name) {
  5757. *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
  5758. return name;
  5759. }
  5760. return nullptr;
  5761. }
  5762. // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
  5763. // the Symbolizer. It looks up the ReferenceValue using the info passed via the
  5764. // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
  5765. // is created and returns the symbol name that matches the ReferenceValue or
  5766. // nullptr if none. The ReferenceType is passed in for the IN type of
  5767. // reference the instruction is making from the values in defined in the header
  5768. // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
  5769. // Out type and the ReferenceName will also be set which is added as a comment
  5770. // to the disassembled instruction.
  5771. //
  5772. // If the symbol name is a C++ mangled name then the demangled name is
  5773. // returned through ReferenceName and ReferenceType is set to
  5774. // LLVMDisassembler_ReferenceType_DeMangled_Name .
  5775. //
  5776. // When this is called to get a symbol name for a branch target then the
  5777. // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
  5778. // SymbolValue will be looked for in the indirect symbol table to determine if
  5779. // it is an address for a symbol stub. If so then the symbol name for that
  5780. // stub is returned indirectly through ReferenceName and then ReferenceType is
  5781. // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
  5782. //
  5783. // When this is called with an value loaded via a PC relative load then
  5784. // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
  5785. // SymbolValue is checked to be an address of literal pointer, symbol pointer,
  5786. // or an Objective-C meta data reference. If so the output ReferenceType is
  5787. // set to correspond to that as well as setting the ReferenceName.
  5788. static const char *SymbolizerSymbolLookUp(void *DisInfo,
  5789. uint64_t ReferenceValue,
  5790. uint64_t *ReferenceType,
  5791. uint64_t ReferencePC,
  5792. const char **ReferenceName) {
  5793. struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
  5794. // If no verbose symbolic information is wanted then just return nullptr.
  5795. if (!info->verbose) {
  5796. *ReferenceName = nullptr;
  5797. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5798. return nullptr;
  5799. }
  5800. const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
  5801. if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
  5802. *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
  5803. if (*ReferenceName != nullptr) {
  5804. method_reference(info, ReferenceType, ReferenceName);
  5805. if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
  5806. *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
  5807. } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
  5808. if (info->demangled_name != nullptr)
  5809. free(info->demangled_name);
  5810. int status;
  5811. info->demangled_name =
  5812. itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
  5813. if (info->demangled_name != nullptr) {
  5814. *ReferenceName = info->demangled_name;
  5815. *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
  5816. } else
  5817. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5818. } else
  5819. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5820. } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
  5821. *ReferenceName =
  5822. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  5823. if (*ReferenceName)
  5824. method_reference(info, ReferenceType, ReferenceName);
  5825. else
  5826. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5827. // If this is arm64 and the reference is an adrp instruction save the
  5828. // instruction, passed in ReferenceValue and the address of the instruction
  5829. // for use later if we see and add immediate instruction.
  5830. } else if (info->O->getArch() == Triple::aarch64 &&
  5831. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
  5832. info->adrp_inst = ReferenceValue;
  5833. info->adrp_addr = ReferencePC;
  5834. SymbolName = nullptr;
  5835. *ReferenceName = nullptr;
  5836. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5837. // If this is arm64 and reference is an add immediate instruction and we
  5838. // have
  5839. // seen an adrp instruction just before it and the adrp's Xd register
  5840. // matches
  5841. // this add's Xn register reconstruct the value being referenced and look to
  5842. // see if it is a literal pointer. Note the add immediate instruction is
  5843. // passed in ReferenceValue.
  5844. } else if (info->O->getArch() == Triple::aarch64 &&
  5845. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
  5846. ReferencePC - 4 == info->adrp_addr &&
  5847. (info->adrp_inst & 0x9f000000) == 0x90000000 &&
  5848. (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
  5849. uint32_t addxri_inst;
  5850. uint64_t adrp_imm, addxri_imm;
  5851. adrp_imm =
  5852. ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
  5853. if (info->adrp_inst & 0x0200000)
  5854. adrp_imm |= 0xfffffffffc000000LL;
  5855. addxri_inst = ReferenceValue;
  5856. addxri_imm = (addxri_inst >> 10) & 0xfff;
  5857. if (((addxri_inst >> 22) & 0x3) == 1)
  5858. addxri_imm <<= 12;
  5859. ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
  5860. (adrp_imm << 12) + addxri_imm;
  5861. *ReferenceName =
  5862. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  5863. if (*ReferenceName == nullptr)
  5864. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5865. // If this is arm64 and the reference is a load register instruction and we
  5866. // have seen an adrp instruction just before it and the adrp's Xd register
  5867. // matches this add's Xn register reconstruct the value being referenced and
  5868. // look to see if it is a literal pointer. Note the load register
  5869. // instruction is passed in ReferenceValue.
  5870. } else if (info->O->getArch() == Triple::aarch64 &&
  5871. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
  5872. ReferencePC - 4 == info->adrp_addr &&
  5873. (info->adrp_inst & 0x9f000000) == 0x90000000 &&
  5874. (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
  5875. uint32_t ldrxui_inst;
  5876. uint64_t adrp_imm, ldrxui_imm;
  5877. adrp_imm =
  5878. ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
  5879. if (info->adrp_inst & 0x0200000)
  5880. adrp_imm |= 0xfffffffffc000000LL;
  5881. ldrxui_inst = ReferenceValue;
  5882. ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
  5883. ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
  5884. (adrp_imm << 12) + (ldrxui_imm << 3);
  5885. *ReferenceName =
  5886. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  5887. if (*ReferenceName == nullptr)
  5888. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5889. }
  5890. // If this arm64 and is an load register (PC-relative) instruction the
  5891. // ReferenceValue is the PC plus the immediate value.
  5892. else if (info->O->getArch() == Triple::aarch64 &&
  5893. (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
  5894. *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
  5895. *ReferenceName =
  5896. GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
  5897. if (*ReferenceName == nullptr)
  5898. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5899. } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
  5900. if (info->demangled_name != nullptr)
  5901. free(info->demangled_name);
  5902. int status;
  5903. info->demangled_name =
  5904. itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
  5905. if (info->demangled_name != nullptr) {
  5906. *ReferenceName = info->demangled_name;
  5907. *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
  5908. }
  5909. }
  5910. else {
  5911. *ReferenceName = nullptr;
  5912. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  5913. }
  5914. return SymbolName;
  5915. }
  5916. /// \brief Emits the comments that are stored in the CommentStream.
  5917. /// Each comment in the CommentStream must end with a newline.
  5918. static void emitComments(raw_svector_ostream &CommentStream,
  5919. SmallString<128> &CommentsToEmit,
  5920. formatted_raw_ostream &FormattedOS,
  5921. const MCAsmInfo &MAI) {
  5922. // Flush the stream before taking its content.
  5923. StringRef Comments = CommentsToEmit.str();
  5924. // Get the default information for printing a comment.
  5925. StringRef CommentBegin = MAI.getCommentString();
  5926. unsigned CommentColumn = MAI.getCommentColumn();
  5927. bool IsFirst = true;
  5928. while (!Comments.empty()) {
  5929. if (!IsFirst)
  5930. FormattedOS << '\n';
  5931. // Emit a line of comments.
  5932. FormattedOS.PadToColumn(CommentColumn);
  5933. size_t Position = Comments.find('\n');
  5934. FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
  5935. // Move after the newline character.
  5936. Comments = Comments.substr(Position + 1);
  5937. IsFirst = false;
  5938. }
  5939. FormattedOS.flush();
  5940. // Tell the comment stream that the vector changed underneath it.
  5941. CommentsToEmit.clear();
  5942. }
  5943. static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
  5944. StringRef DisSegName, StringRef DisSectName) {
  5945. const char *McpuDefault = nullptr;
  5946. const Target *ThumbTarget = nullptr;
  5947. const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
  5948. if (!TheTarget) {
  5949. // GetTarget prints out stuff.
  5950. return;
  5951. }
  5952. std::string MachOMCPU;
  5953. if (MCPU.empty() && McpuDefault)
  5954. MachOMCPU = McpuDefault;
  5955. else
  5956. MachOMCPU = MCPU;
  5957. std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
  5958. std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
  5959. if (ThumbTarget)
  5960. ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
  5961. // Package up features to be passed to target/subtarget
  5962. std::string FeaturesStr;
  5963. if (MAttrs.size()) {
  5964. SubtargetFeatures Features;
  5965. for (unsigned i = 0; i != MAttrs.size(); ++i)
  5966. Features.AddFeature(MAttrs[i]);
  5967. FeaturesStr = Features.getString();
  5968. }
  5969. // Set up disassembler.
  5970. std::unique_ptr<const MCRegisterInfo> MRI(
  5971. TheTarget->createMCRegInfo(TripleName));
  5972. std::unique_ptr<const MCAsmInfo> AsmInfo(
  5973. TheTarget->createMCAsmInfo(*MRI, TripleName));
  5974. std::unique_ptr<const MCSubtargetInfo> STI(
  5975. TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr));
  5976. MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
  5977. std::unique_ptr<MCDisassembler> DisAsm(
  5978. TheTarget->createMCDisassembler(*STI, Ctx));
  5979. std::unique_ptr<MCSymbolizer> Symbolizer;
  5980. struct DisassembleInfo SymbolizerInfo;
  5981. std::unique_ptr<MCRelocationInfo> RelInfo(
  5982. TheTarget->createMCRelocationInfo(TripleName, Ctx));
  5983. if (RelInfo) {
  5984. Symbolizer.reset(TheTarget->createMCSymbolizer(
  5985. TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
  5986. &SymbolizerInfo, &Ctx, std::move(RelInfo)));
  5987. DisAsm->setSymbolizer(std::move(Symbolizer));
  5988. }
  5989. int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
  5990. std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
  5991. Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
  5992. // Set the display preference for hex vs. decimal immediates.
  5993. IP->setPrintImmHex(PrintImmHex);
  5994. // Comment stream and backing vector.
  5995. SmallString<128> CommentsToEmit;
  5996. raw_svector_ostream CommentStream(CommentsToEmit);
  5997. // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
  5998. // if it is done then arm64 comments for string literals don't get printed
  5999. // and some constant get printed instead and not setting it causes intel
  6000. // (32-bit and 64-bit) comments printed with different spacing before the
  6001. // comment causing different diffs with the 'C' disassembler library API.
  6002. // IP->setCommentStream(CommentStream);
  6003. if (!AsmInfo || !STI || !DisAsm || !IP) {
  6004. errs() << "error: couldn't initialize disassembler for target "
  6005. << TripleName << '\n';
  6006. return;
  6007. }
  6008. // Set up separate thumb disassembler if needed.
  6009. std::unique_ptr<const MCRegisterInfo> ThumbMRI;
  6010. std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
  6011. std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
  6012. std::unique_ptr<MCDisassembler> ThumbDisAsm;
  6013. std::unique_ptr<MCInstPrinter> ThumbIP;
  6014. std::unique_ptr<MCContext> ThumbCtx;
  6015. std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
  6016. struct DisassembleInfo ThumbSymbolizerInfo;
  6017. std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
  6018. if (ThumbTarget) {
  6019. ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
  6020. ThumbAsmInfo.reset(
  6021. ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
  6022. ThumbSTI.reset(
  6023. ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU,
  6024. FeaturesStr));
  6025. ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
  6026. ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
  6027. MCContext *PtrThumbCtx = ThumbCtx.get();
  6028. ThumbRelInfo.reset(
  6029. ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
  6030. if (ThumbRelInfo) {
  6031. ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
  6032. ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
  6033. &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
  6034. ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
  6035. }
  6036. int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
  6037. ThumbIP.reset(ThumbTarget->createMCInstPrinter(
  6038. Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
  6039. *ThumbInstrInfo, *ThumbMRI));
  6040. // Set the display preference for hex vs. decimal immediates.
  6041. ThumbIP->setPrintImmHex(PrintImmHex);
  6042. }
  6043. if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
  6044. errs() << "error: couldn't initialize disassembler for target "
  6045. << ThumbTripleName << '\n';
  6046. return;
  6047. }
  6048. MachO::mach_header Header = MachOOF->getHeader();
  6049. // FIXME: Using the -cfg command line option, this code used to be able to
  6050. // annotate relocations with the referenced symbol's name, and if this was
  6051. // inside a __[cf]string section, the data it points to. This is now replaced
  6052. // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
  6053. std::vector<SectionRef> Sections;
  6054. std::vector<SymbolRef> Symbols;
  6055. SmallVector<uint64_t, 8> FoundFns;
  6056. uint64_t BaseSegmentAddress;
  6057. getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
  6058. BaseSegmentAddress);
  6059. // Sort the symbols by address, just in case they didn't come in that way.
  6060. std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
  6061. // Build a data in code table that is sorted on by the address of each entry.
  6062. uint64_t BaseAddress = 0;
  6063. if (Header.filetype == MachO::MH_OBJECT)
  6064. BaseAddress = Sections[0].getAddress();
  6065. else
  6066. BaseAddress = BaseSegmentAddress;
  6067. DiceTable Dices;
  6068. for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
  6069. DI != DE; ++DI) {
  6070. uint32_t Offset;
  6071. DI->getOffset(Offset);
  6072. Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
  6073. }
  6074. array_pod_sort(Dices.begin(), Dices.end());
  6075. #ifndef NDEBUG
  6076. raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
  6077. #else
  6078. raw_ostream &DebugOut = nulls();
  6079. #endif
  6080. std::unique_ptr<DIContext> diContext;
  6081. ObjectFile *DbgObj = MachOOF;
  6082. // Try to find debug info and set up the DIContext for it.
  6083. if (UseDbg) {
  6084. // A separate DSym file path was specified, parse it as a macho file,
  6085. // get the sections and supply it to the section name parsing machinery.
  6086. if (!DSYMFile.empty()) {
  6087. ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
  6088. MemoryBuffer::getFileOrSTDIN(DSYMFile);
  6089. if (std::error_code EC = BufOrErr.getError()) {
  6090. errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
  6091. return;
  6092. }
  6093. DbgObj =
  6094. ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
  6095. .get()
  6096. .release();
  6097. }
  6098. // Setup the DIContext
  6099. diContext = DWARFContext::create(*DbgObj);
  6100. }
  6101. if (FilterSections.size() == 0)
  6102. outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
  6103. for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
  6104. StringRef SectName;
  6105. if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
  6106. continue;
  6107. DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
  6108. StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
  6109. if (SegmentName != DisSegName)
  6110. continue;
  6111. StringRef BytesStr;
  6112. Sections[SectIdx].getContents(BytesStr);
  6113. ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
  6114. BytesStr.size());
  6115. uint64_t SectAddress = Sections[SectIdx].getAddress();
  6116. bool symbolTableWorked = false;
  6117. // Create a map of symbol addresses to symbol names for use by
  6118. // the SymbolizerSymbolLookUp() routine.
  6119. SymbolAddressMap AddrMap;
  6120. bool DisSymNameFound = false;
  6121. for (const SymbolRef &Symbol : MachOOF->symbols()) {
  6122. Expected<SymbolRef::Type> STOrErr = Symbol.getType();
  6123. if (!STOrErr)
  6124. report_error(MachOOF->getFileName(), STOrErr.takeError());
  6125. SymbolRef::Type ST = *STOrErr;
  6126. if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
  6127. ST == SymbolRef::ST_Other) {
  6128. uint64_t Address = Symbol.getValue();
  6129. Expected<StringRef> SymNameOrErr = Symbol.getName();
  6130. if (!SymNameOrErr)
  6131. report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
  6132. StringRef SymName = *SymNameOrErr;
  6133. AddrMap[Address] = SymName;
  6134. if (!DisSymName.empty() && DisSymName == SymName)
  6135. DisSymNameFound = true;
  6136. }
  6137. }
  6138. if (!DisSymName.empty() && !DisSymNameFound) {
  6139. outs() << "Can't find -dis-symname: " << DisSymName << "\n";
  6140. return;
  6141. }
  6142. // Set up the block of info used by the Symbolizer call backs.
  6143. SymbolizerInfo.verbose = !NoSymbolicOperands;
  6144. SymbolizerInfo.O = MachOOF;
  6145. SymbolizerInfo.S = Sections[SectIdx];
  6146. SymbolizerInfo.AddrMap = &AddrMap;
  6147. SymbolizerInfo.Sections = &Sections;
  6148. SymbolizerInfo.class_name = nullptr;
  6149. SymbolizerInfo.selector_name = nullptr;
  6150. SymbolizerInfo.method = nullptr;
  6151. SymbolizerInfo.demangled_name = nullptr;
  6152. SymbolizerInfo.bindtable = nullptr;
  6153. SymbolizerInfo.adrp_addr = 0;
  6154. SymbolizerInfo.adrp_inst = 0;
  6155. // Same for the ThumbSymbolizer
  6156. ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
  6157. ThumbSymbolizerInfo.O = MachOOF;
  6158. ThumbSymbolizerInfo.S = Sections[SectIdx];
  6159. ThumbSymbolizerInfo.AddrMap = &AddrMap;
  6160. ThumbSymbolizerInfo.Sections = &Sections;
  6161. ThumbSymbolizerInfo.class_name = nullptr;
  6162. ThumbSymbolizerInfo.selector_name = nullptr;
  6163. ThumbSymbolizerInfo.method = nullptr;
  6164. ThumbSymbolizerInfo.demangled_name = nullptr;
  6165. ThumbSymbolizerInfo.bindtable = nullptr;
  6166. ThumbSymbolizerInfo.adrp_addr = 0;
  6167. ThumbSymbolizerInfo.adrp_inst = 0;
  6168. unsigned int Arch = MachOOF->getArch();
  6169. // Skip all symbols if this is a stubs file.
  6170. if (Bytes.size() == 0)
  6171. return;
  6172. // If the section has symbols but no symbol at the start of the section
  6173. // these are used to make sure the bytes before the first symbol are
  6174. // disassembled.
  6175. bool FirstSymbol = true;
  6176. bool FirstSymbolAtSectionStart = true;
  6177. // Disassemble symbol by symbol.
  6178. for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
  6179. Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
  6180. if (!SymNameOrErr)
  6181. report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
  6182. StringRef SymName = *SymNameOrErr;
  6183. Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
  6184. if (!STOrErr)
  6185. report_error(MachOOF->getFileName(), STOrErr.takeError());
  6186. SymbolRef::Type ST = *STOrErr;
  6187. if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
  6188. continue;
  6189. // Make sure the symbol is defined in this section.
  6190. bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
  6191. if (!containsSym) {
  6192. if (!DisSymName.empty() && DisSymName == SymName) {
  6193. outs() << "-dis-symname: " << DisSymName << " not in the section\n";
  6194. return;
  6195. }
  6196. continue;
  6197. }
  6198. // The __mh_execute_header is special and we need to deal with that fact
  6199. // this symbol is before the start of the (__TEXT,__text) section and at the
  6200. // address of the start of the __TEXT segment. This is because this symbol
  6201. // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
  6202. // start of the section in a standard MH_EXECUTE filetype.
  6203. if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
  6204. outs() << "-dis-symname: __mh_execute_header not in any section\n";
  6205. return;
  6206. }
  6207. // When this code is trying to disassemble a symbol at a time and in the
  6208. // case there is only the __mh_execute_header symbol left as in a stripped
  6209. // executable, we need to deal with this by ignoring this symbol so the
  6210. // whole section is disassembled and this symbol is then not displayed.
  6211. if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
  6212. SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
  6213. SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
  6214. continue;
  6215. // If we are only disassembling one symbol see if this is that symbol.
  6216. if (!DisSymName.empty() && DisSymName != SymName)
  6217. continue;
  6218. // Start at the address of the symbol relative to the section's address.
  6219. uint64_t SectSize = Sections[SectIdx].getSize();
  6220. uint64_t Start = Symbols[SymIdx].getValue();
  6221. uint64_t SectionAddress = Sections[SectIdx].getAddress();
  6222. Start -= SectionAddress;
  6223. if (Start > SectSize) {
  6224. outs() << "section data ends, " << SymName
  6225. << " lies outside valid range\n";
  6226. return;
  6227. }
  6228. // Stop disassembling either at the beginning of the next symbol or at
  6229. // the end of the section.
  6230. bool containsNextSym = false;
  6231. uint64_t NextSym = 0;
  6232. uint64_t NextSymIdx = SymIdx + 1;
  6233. while (Symbols.size() > NextSymIdx) {
  6234. Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
  6235. if (!STOrErr)
  6236. report_error(MachOOF->getFileName(), STOrErr.takeError());
  6237. SymbolRef::Type NextSymType = *STOrErr;
  6238. if (NextSymType == SymbolRef::ST_Function) {
  6239. containsNextSym =
  6240. Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
  6241. NextSym = Symbols[NextSymIdx].getValue();
  6242. NextSym -= SectionAddress;
  6243. break;
  6244. }
  6245. ++NextSymIdx;
  6246. }
  6247. uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
  6248. uint64_t Size;
  6249. symbolTableWorked = true;
  6250. DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
  6251. bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
  6252. // We only need the dedicated Thumb target if there's a real choice
  6253. // (i.e. we're not targeting M-class) and the function is Thumb.
  6254. bool UseThumbTarget = IsThumb && ThumbTarget;
  6255. // If we are not specifying a symbol to start disassembly with and this
  6256. // is the first symbol in the section but not at the start of the section
  6257. // then move the disassembly index to the start of the section and
  6258. // don't print the symbol name just yet. This is so the bytes before the
  6259. // first symbol are disassembled.
  6260. uint64_t SymbolStart = Start;
  6261. if (DisSymName.empty() && FirstSymbol && Start != 0) {
  6262. FirstSymbolAtSectionStart = false;
  6263. Start = 0;
  6264. }
  6265. else
  6266. outs() << SymName << ":\n";
  6267. DILineInfo lastLine;
  6268. for (uint64_t Index = Start; Index < End; Index += Size) {
  6269. MCInst Inst;
  6270. // If this is the first symbol in the section and it was not at the
  6271. // start of the section, see if we are at its Index now and if so print
  6272. // the symbol name.
  6273. if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
  6274. outs() << SymName << ":\n";
  6275. uint64_t PC = SectAddress + Index;
  6276. if (!NoLeadingAddr) {
  6277. if (FullLeadingAddr) {
  6278. if (MachOOF->is64Bit())
  6279. outs() << format("%016" PRIx64, PC);
  6280. else
  6281. outs() << format("%08" PRIx64, PC);
  6282. } else {
  6283. outs() << format("%8" PRIx64 ":", PC);
  6284. }
  6285. }
  6286. if (!NoShowRawInsn || Arch == Triple::arm)
  6287. outs() << "\t";
  6288. // Check the data in code table here to see if this is data not an
  6289. // instruction to be disassembled.
  6290. DiceTable Dice;
  6291. Dice.push_back(std::make_pair(PC, DiceRef()));
  6292. dice_table_iterator DTI =
  6293. std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
  6294. compareDiceTableEntries);
  6295. if (DTI != Dices.end()) {
  6296. uint16_t Length;
  6297. DTI->second.getLength(Length);
  6298. uint16_t Kind;
  6299. DTI->second.getKind(Kind);
  6300. Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
  6301. if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
  6302. (PC == (DTI->first + Length - 1)) && (Length & 1))
  6303. Size++;
  6304. continue;
  6305. }
  6306. SmallVector<char, 64> AnnotationsBytes;
  6307. raw_svector_ostream Annotations(AnnotationsBytes);
  6308. bool gotInst;
  6309. if (UseThumbTarget)
  6310. gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
  6311. PC, DebugOut, Annotations);
  6312. else
  6313. gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
  6314. DebugOut, Annotations);
  6315. if (gotInst) {
  6316. if (!NoShowRawInsn || Arch == Triple::arm) {
  6317. dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
  6318. }
  6319. formatted_raw_ostream FormattedOS(outs());
  6320. StringRef AnnotationsStr = Annotations.str();
  6321. if (UseThumbTarget)
  6322. ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
  6323. else
  6324. IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
  6325. emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
  6326. // Print debug info.
  6327. if (diContext) {
  6328. DILineInfo dli = diContext->getLineInfoForAddress(PC);
  6329. // Print valid line info if it changed.
  6330. if (dli != lastLine && dli.Line != 0)
  6331. outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
  6332. << dli.Column;
  6333. lastLine = dli;
  6334. }
  6335. outs() << "\n";
  6336. } else {
  6337. unsigned int Arch = MachOOF->getArch();
  6338. if (Arch == Triple::x86_64 || Arch == Triple::x86) {
  6339. outs() << format("\t.byte 0x%02x #bad opcode\n",
  6340. *(Bytes.data() + Index) & 0xff);
  6341. Size = 1; // skip exactly one illegible byte and move on.
  6342. } else if (Arch == Triple::aarch64 ||
  6343. (Arch == Triple::arm && !IsThumb)) {
  6344. uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
  6345. (*(Bytes.data() + Index + 1) & 0xff) << 8 |
  6346. (*(Bytes.data() + Index + 2) & 0xff) << 16 |
  6347. (*(Bytes.data() + Index + 3) & 0xff) << 24;
  6348. outs() << format("\t.long\t0x%08x\n", opcode);
  6349. Size = 4;
  6350. } else if (Arch == Triple::arm) {
  6351. assert(IsThumb && "ARM mode should have been dealt with above");
  6352. uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
  6353. (*(Bytes.data() + Index + 1) & 0xff) << 8;
  6354. outs() << format("\t.short\t0x%04x\n", opcode);
  6355. Size = 2;
  6356. } else{
  6357. errs() << "llvm-objdump: warning: invalid instruction encoding\n";
  6358. if (Size == 0)
  6359. Size = 1; // skip illegible bytes
  6360. }
  6361. }
  6362. }
  6363. // Now that we are done disassembled the first symbol set the bool that
  6364. // were doing this to false.
  6365. FirstSymbol = false;
  6366. }
  6367. if (!symbolTableWorked) {
  6368. // Reading the symbol table didn't work, disassemble the whole section.
  6369. uint64_t SectAddress = Sections[SectIdx].getAddress();
  6370. uint64_t SectSize = Sections[SectIdx].getSize();
  6371. uint64_t InstSize;
  6372. for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
  6373. MCInst Inst;
  6374. uint64_t PC = SectAddress + Index;
  6375. SmallVector<char, 64> AnnotationsBytes;
  6376. raw_svector_ostream Annotations(AnnotationsBytes);
  6377. if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
  6378. DebugOut, Annotations)) {
  6379. if (!NoLeadingAddr) {
  6380. if (FullLeadingAddr) {
  6381. if (MachOOF->is64Bit())
  6382. outs() << format("%016" PRIx64, PC);
  6383. else
  6384. outs() << format("%08" PRIx64, PC);
  6385. } else {
  6386. outs() << format("%8" PRIx64 ":", PC);
  6387. }
  6388. }
  6389. if (!NoShowRawInsn || Arch == Triple::arm) {
  6390. outs() << "\t";
  6391. dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
  6392. }
  6393. StringRef AnnotationsStr = Annotations.str();
  6394. IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
  6395. outs() << "\n";
  6396. } else {
  6397. unsigned int Arch = MachOOF->getArch();
  6398. if (Arch == Triple::x86_64 || Arch == Triple::x86) {
  6399. outs() << format("\t.byte 0x%02x #bad opcode\n",
  6400. *(Bytes.data() + Index) & 0xff);
  6401. InstSize = 1; // skip exactly one illegible byte and move on.
  6402. } else {
  6403. errs() << "llvm-objdump: warning: invalid instruction encoding\n";
  6404. if (InstSize == 0)
  6405. InstSize = 1; // skip illegible bytes
  6406. }
  6407. }
  6408. }
  6409. }
  6410. // The TripleName's need to be reset if we are called again for a different
  6411. // archtecture.
  6412. TripleName = "";
  6413. ThumbTripleName = "";
  6414. if (SymbolizerInfo.method != nullptr)
  6415. free(SymbolizerInfo.method);
  6416. if (SymbolizerInfo.demangled_name != nullptr)
  6417. free(SymbolizerInfo.demangled_name);
  6418. if (ThumbSymbolizerInfo.method != nullptr)
  6419. free(ThumbSymbolizerInfo.method);
  6420. if (ThumbSymbolizerInfo.demangled_name != nullptr)
  6421. free(ThumbSymbolizerInfo.demangled_name);
  6422. }
  6423. }
  6424. //===----------------------------------------------------------------------===//
  6425. // __compact_unwind section dumping
  6426. //===----------------------------------------------------------------------===//
  6427. namespace {
  6428. template <typename T> static uint64_t readNext(const char *&Buf) {
  6429. using llvm::support::little;
  6430. using llvm::support::unaligned;
  6431. uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
  6432. Buf += sizeof(T);
  6433. return Val;
  6434. }
  6435. struct CompactUnwindEntry {
  6436. uint32_t OffsetInSection;
  6437. uint64_t FunctionAddr;
  6438. uint32_t Length;
  6439. uint32_t CompactEncoding;
  6440. uint64_t PersonalityAddr;
  6441. uint64_t LSDAAddr;
  6442. RelocationRef FunctionReloc;
  6443. RelocationRef PersonalityReloc;
  6444. RelocationRef LSDAReloc;
  6445. CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
  6446. : OffsetInSection(Offset) {
  6447. if (Is64)
  6448. read<uint64_t>(Contents.data() + Offset);
  6449. else
  6450. read<uint32_t>(Contents.data() + Offset);
  6451. }
  6452. private:
  6453. template <typename UIntPtr> void read(const char *Buf) {
  6454. FunctionAddr = readNext<UIntPtr>(Buf);
  6455. Length = readNext<uint32_t>(Buf);
  6456. CompactEncoding = readNext<uint32_t>(Buf);
  6457. PersonalityAddr = readNext<UIntPtr>(Buf);
  6458. LSDAAddr = readNext<UIntPtr>(Buf);
  6459. }
  6460. };
  6461. }
  6462. /// Given a relocation from __compact_unwind, consisting of the RelocationRef
  6463. /// and data being relocated, determine the best base Name and Addend to use for
  6464. /// display purposes.
  6465. ///
  6466. /// 1. An Extern relocation will directly reference a symbol (and the data is
  6467. /// then already an addend), so use that.
  6468. /// 2. Otherwise the data is an offset in the object file's layout; try to find
  6469. // a symbol before it in the same section, and use the offset from there.
  6470. /// 3. Finally, if all that fails, fall back to an offset from the start of the
  6471. /// referenced section.
  6472. static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
  6473. std::map<uint64_t, SymbolRef> &Symbols,
  6474. const RelocationRef &Reloc, uint64_t Addr,
  6475. StringRef &Name, uint64_t &Addend) {
  6476. if (Reloc.getSymbol() != Obj->symbol_end()) {
  6477. Expected<StringRef> NameOrErr = Reloc.getSymbol()->getName();
  6478. if (!NameOrErr)
  6479. report_error(Obj->getFileName(), NameOrErr.takeError());
  6480. Name = *NameOrErr;
  6481. Addend = Addr;
  6482. return;
  6483. }
  6484. auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
  6485. SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
  6486. uint64_t SectionAddr = RelocSection.getAddress();
  6487. auto Sym = Symbols.upper_bound(Addr);
  6488. if (Sym == Symbols.begin()) {
  6489. // The first symbol in the object is after this reference, the best we can
  6490. // do is section-relative notation.
  6491. RelocSection.getName(Name);
  6492. Addend = Addr - SectionAddr;
  6493. return;
  6494. }
  6495. // Go back one so that SymbolAddress <= Addr.
  6496. --Sym;
  6497. auto SectOrErr = Sym->second.getSection();
  6498. if (!SectOrErr)
  6499. report_error(Obj->getFileName(), SectOrErr.takeError());
  6500. section_iterator SymSection = *SectOrErr;
  6501. if (RelocSection == *SymSection) {
  6502. // There's a valid symbol in the same section before this reference.
  6503. Expected<StringRef> NameOrErr = Sym->second.getName();
  6504. if (!NameOrErr)
  6505. report_error(Obj->getFileName(), NameOrErr.takeError());
  6506. Name = *NameOrErr;
  6507. Addend = Addr - Sym->first;
  6508. return;
  6509. }
  6510. // There is a symbol before this reference, but it's in a different
  6511. // section. Probably not helpful to mention it, so use the section name.
  6512. RelocSection.getName(Name);
  6513. Addend = Addr - SectionAddr;
  6514. }
  6515. static void printUnwindRelocDest(const MachOObjectFile *Obj,
  6516. std::map<uint64_t, SymbolRef> &Symbols,
  6517. const RelocationRef &Reloc, uint64_t Addr) {
  6518. StringRef Name;
  6519. uint64_t Addend;
  6520. if (!Reloc.getObject())
  6521. return;
  6522. findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
  6523. outs() << Name;
  6524. if (Addend)
  6525. outs() << " + " << format("0x%" PRIx64, Addend);
  6526. }
  6527. static void
  6528. printMachOCompactUnwindSection(const MachOObjectFile *Obj,
  6529. std::map<uint64_t, SymbolRef> &Symbols,
  6530. const SectionRef &CompactUnwind) {
  6531. if (!Obj->isLittleEndian()) {
  6532. outs() << "Skipping big-endian __compact_unwind section\n";
  6533. return;
  6534. }
  6535. bool Is64 = Obj->is64Bit();
  6536. uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
  6537. uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
  6538. StringRef Contents;
  6539. CompactUnwind.getContents(Contents);
  6540. SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
  6541. // First populate the initial raw offsets, encodings and so on from the entry.
  6542. for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
  6543. CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
  6544. CompactUnwinds.push_back(Entry);
  6545. }
  6546. // Next we need to look at the relocations to find out what objects are
  6547. // actually being referred to.
  6548. for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
  6549. uint64_t RelocAddress = Reloc.getOffset();
  6550. uint32_t EntryIdx = RelocAddress / EntrySize;
  6551. uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
  6552. CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
  6553. if (OffsetInEntry == 0)
  6554. Entry.FunctionReloc = Reloc;
  6555. else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
  6556. Entry.PersonalityReloc = Reloc;
  6557. else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
  6558. Entry.LSDAReloc = Reloc;
  6559. else {
  6560. outs() << "Invalid relocation in __compact_unwind section\n";
  6561. return;
  6562. }
  6563. }
  6564. // Finally, we're ready to print the data we've gathered.
  6565. outs() << "Contents of __compact_unwind section:\n";
  6566. for (auto &Entry : CompactUnwinds) {
  6567. outs() << " Entry at offset "
  6568. << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
  6569. // 1. Start of the region this entry applies to.
  6570. outs() << " start: " << format("0x%" PRIx64,
  6571. Entry.FunctionAddr) << ' ';
  6572. printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
  6573. outs() << '\n';
  6574. // 2. Length of the region this entry applies to.
  6575. outs() << " length: " << format("0x%" PRIx32, Entry.Length)
  6576. << '\n';
  6577. // 3. The 32-bit compact encoding.
  6578. outs() << " compact encoding: "
  6579. << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
  6580. // 4. The personality function, if present.
  6581. if (Entry.PersonalityReloc.getObject()) {
  6582. outs() << " personality function: "
  6583. << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
  6584. printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
  6585. Entry.PersonalityAddr);
  6586. outs() << '\n';
  6587. }
  6588. // 5. This entry's language-specific data area.
  6589. if (Entry.LSDAReloc.getObject()) {
  6590. outs() << " LSDA: " << format("0x%" PRIx64,
  6591. Entry.LSDAAddr) << ' ';
  6592. printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
  6593. outs() << '\n';
  6594. }
  6595. }
  6596. }
  6597. //===----------------------------------------------------------------------===//
  6598. // __unwind_info section dumping
  6599. //===----------------------------------------------------------------------===//
  6600. static void printRegularSecondLevelUnwindPage(const char *PageStart) {
  6601. const char *Pos = PageStart;
  6602. uint32_t Kind = readNext<uint32_t>(Pos);
  6603. (void)Kind;
  6604. assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
  6605. uint16_t EntriesStart = readNext<uint16_t>(Pos);
  6606. uint16_t NumEntries = readNext<uint16_t>(Pos);
  6607. Pos = PageStart + EntriesStart;
  6608. for (unsigned i = 0; i < NumEntries; ++i) {
  6609. uint32_t FunctionOffset = readNext<uint32_t>(Pos);
  6610. uint32_t Encoding = readNext<uint32_t>(Pos);
  6611. outs() << " [" << i << "]: "
  6612. << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
  6613. << ", "
  6614. << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
  6615. }
  6616. }
  6617. static void printCompressedSecondLevelUnwindPage(
  6618. const char *PageStart, uint32_t FunctionBase,
  6619. const SmallVectorImpl<uint32_t> &CommonEncodings) {
  6620. const char *Pos = PageStart;
  6621. uint32_t Kind = readNext<uint32_t>(Pos);
  6622. (void)Kind;
  6623. assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
  6624. uint16_t EntriesStart = readNext<uint16_t>(Pos);
  6625. uint16_t NumEntries = readNext<uint16_t>(Pos);
  6626. uint16_t EncodingsStart = readNext<uint16_t>(Pos);
  6627. readNext<uint16_t>(Pos);
  6628. const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
  6629. PageStart + EncodingsStart);
  6630. Pos = PageStart + EntriesStart;
  6631. for (unsigned i = 0; i < NumEntries; ++i) {
  6632. uint32_t Entry = readNext<uint32_t>(Pos);
  6633. uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
  6634. uint32_t EncodingIdx = Entry >> 24;
  6635. uint32_t Encoding;
  6636. if (EncodingIdx < CommonEncodings.size())
  6637. Encoding = CommonEncodings[EncodingIdx];
  6638. else
  6639. Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
  6640. outs() << " [" << i << "]: "
  6641. << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
  6642. << ", "
  6643. << "encoding[" << EncodingIdx
  6644. << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
  6645. }
  6646. }
  6647. static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
  6648. std::map<uint64_t, SymbolRef> &Symbols,
  6649. const SectionRef &UnwindInfo) {
  6650. if (!Obj->isLittleEndian()) {
  6651. outs() << "Skipping big-endian __unwind_info section\n";
  6652. return;
  6653. }
  6654. outs() << "Contents of __unwind_info section:\n";
  6655. StringRef Contents;
  6656. UnwindInfo.getContents(Contents);
  6657. const char *Pos = Contents.data();
  6658. //===----------------------------------
  6659. // Section header
  6660. //===----------------------------------
  6661. uint32_t Version = readNext<uint32_t>(Pos);
  6662. outs() << " Version: "
  6663. << format("0x%" PRIx32, Version) << '\n';
  6664. if (Version != 1) {
  6665. outs() << " Skipping section with unknown version\n";
  6666. return;
  6667. }
  6668. uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
  6669. outs() << " Common encodings array section offset: "
  6670. << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
  6671. uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
  6672. outs() << " Number of common encodings in array: "
  6673. << format("0x%" PRIx32, NumCommonEncodings) << '\n';
  6674. uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
  6675. outs() << " Personality function array section offset: "
  6676. << format("0x%" PRIx32, PersonalitiesStart) << '\n';
  6677. uint32_t NumPersonalities = readNext<uint32_t>(Pos);
  6678. outs() << " Number of personality functions in array: "
  6679. << format("0x%" PRIx32, NumPersonalities) << '\n';
  6680. uint32_t IndicesStart = readNext<uint32_t>(Pos);
  6681. outs() << " Index array section offset: "
  6682. << format("0x%" PRIx32, IndicesStart) << '\n';
  6683. uint32_t NumIndices = readNext<uint32_t>(Pos);
  6684. outs() << " Number of indices in array: "
  6685. << format("0x%" PRIx32, NumIndices) << '\n';
  6686. //===----------------------------------
  6687. // A shared list of common encodings
  6688. //===----------------------------------
  6689. // These occupy indices in the range [0, N] whenever an encoding is referenced
  6690. // from a compressed 2nd level index table. In practice the linker only
  6691. // creates ~128 of these, so that indices are available to embed encodings in
  6692. // the 2nd level index.
  6693. SmallVector<uint32_t, 64> CommonEncodings;
  6694. outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
  6695. Pos = Contents.data() + CommonEncodingsStart;
  6696. for (unsigned i = 0; i < NumCommonEncodings; ++i) {
  6697. uint32_t Encoding = readNext<uint32_t>(Pos);
  6698. CommonEncodings.push_back(Encoding);
  6699. outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
  6700. << '\n';
  6701. }
  6702. //===----------------------------------
  6703. // Personality functions used in this executable
  6704. //===----------------------------------
  6705. // There should be only a handful of these (one per source language,
  6706. // roughly). Particularly since they only get 2 bits in the compact encoding.
  6707. outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
  6708. Pos = Contents.data() + PersonalitiesStart;
  6709. for (unsigned i = 0; i < NumPersonalities; ++i) {
  6710. uint32_t PersonalityFn = readNext<uint32_t>(Pos);
  6711. outs() << " personality[" << i + 1
  6712. << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
  6713. }
  6714. //===----------------------------------
  6715. // The level 1 index entries
  6716. //===----------------------------------
  6717. // These specify an approximate place to start searching for the more detailed
  6718. // information, sorted by PC.
  6719. struct IndexEntry {
  6720. uint32_t FunctionOffset;
  6721. uint32_t SecondLevelPageStart;
  6722. uint32_t LSDAStart;
  6723. };
  6724. SmallVector<IndexEntry, 4> IndexEntries;
  6725. outs() << " Top level indices: (count = " << NumIndices << ")\n";
  6726. Pos = Contents.data() + IndicesStart;
  6727. for (unsigned i = 0; i < NumIndices; ++i) {
  6728. IndexEntry Entry;
  6729. Entry.FunctionOffset = readNext<uint32_t>(Pos);
  6730. Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
  6731. Entry.LSDAStart = readNext<uint32_t>(Pos);
  6732. IndexEntries.push_back(Entry);
  6733. outs() << " [" << i << "]: "
  6734. << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
  6735. << ", "
  6736. << "2nd level page offset="
  6737. << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
  6738. << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
  6739. }
  6740. //===----------------------------------
  6741. // Next come the LSDA tables
  6742. //===----------------------------------
  6743. // The LSDA layout is rather implicit: it's a contiguous array of entries from
  6744. // the first top-level index's LSDAOffset to the last (sentinel).
  6745. outs() << " LSDA descriptors:\n";
  6746. Pos = Contents.data() + IndexEntries[0].LSDAStart;
  6747. int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
  6748. (2 * sizeof(uint32_t));
  6749. for (int i = 0; i < NumLSDAs; ++i) {
  6750. uint32_t FunctionOffset = readNext<uint32_t>(Pos);
  6751. uint32_t LSDAOffset = readNext<uint32_t>(Pos);
  6752. outs() << " [" << i << "]: "
  6753. << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
  6754. << ", "
  6755. << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
  6756. }
  6757. //===----------------------------------
  6758. // Finally, the 2nd level indices
  6759. //===----------------------------------
  6760. // Generally these are 4K in size, and have 2 possible forms:
  6761. // + Regular stores up to 511 entries with disparate encodings
  6762. // + Compressed stores up to 1021 entries if few enough compact encoding
  6763. // values are used.
  6764. outs() << " Second level indices:\n";
  6765. for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
  6766. // The final sentinel top-level index has no associated 2nd level page
  6767. if (IndexEntries[i].SecondLevelPageStart == 0)
  6768. break;
  6769. outs() << " Second level index[" << i << "]: "
  6770. << "offset in section="
  6771. << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
  6772. << ", "
  6773. << "base function offset="
  6774. << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
  6775. Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
  6776. uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
  6777. if (Kind == 2)
  6778. printRegularSecondLevelUnwindPage(Pos);
  6779. else if (Kind == 3)
  6780. printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
  6781. CommonEncodings);
  6782. else
  6783. outs() << " Skipping 2nd level page with unknown kind " << Kind
  6784. << '\n';
  6785. }
  6786. }
  6787. void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
  6788. std::map<uint64_t, SymbolRef> Symbols;
  6789. for (const SymbolRef &SymRef : Obj->symbols()) {
  6790. // Discard any undefined or absolute symbols. They're not going to take part
  6791. // in the convenience lookup for unwind info and just take up resources.
  6792. auto SectOrErr = SymRef.getSection();
  6793. if (!SectOrErr) {
  6794. // TODO: Actually report errors helpfully.
  6795. consumeError(SectOrErr.takeError());
  6796. continue;
  6797. }
  6798. section_iterator Section = *SectOrErr;
  6799. if (Section == Obj->section_end())
  6800. continue;
  6801. uint64_t Addr = SymRef.getValue();
  6802. Symbols.insert(std::make_pair(Addr, SymRef));
  6803. }
  6804. for (const SectionRef &Section : Obj->sections()) {
  6805. StringRef SectName;
  6806. Section.getName(SectName);
  6807. if (SectName == "__compact_unwind")
  6808. printMachOCompactUnwindSection(Obj, Symbols, Section);
  6809. else if (SectName == "__unwind_info")
  6810. printMachOUnwindInfoSection(Obj, Symbols, Section);
  6811. }
  6812. }
  6813. static void PrintMachHeader(uint32_t magic, uint32_t cputype,
  6814. uint32_t cpusubtype, uint32_t filetype,
  6815. uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
  6816. bool verbose) {
  6817. outs() << "Mach header\n";
  6818. outs() << " magic cputype cpusubtype caps filetype ncmds "
  6819. "sizeofcmds flags\n";
  6820. if (verbose) {
  6821. if (magic == MachO::MH_MAGIC)
  6822. outs() << " MH_MAGIC";
  6823. else if (magic == MachO::MH_MAGIC_64)
  6824. outs() << "MH_MAGIC_64";
  6825. else
  6826. outs() << format(" 0x%08" PRIx32, magic);
  6827. switch (cputype) {
  6828. case MachO::CPU_TYPE_I386:
  6829. outs() << " I386";
  6830. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6831. case MachO::CPU_SUBTYPE_I386_ALL:
  6832. outs() << " ALL";
  6833. break;
  6834. default:
  6835. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6836. break;
  6837. }
  6838. break;
  6839. case MachO::CPU_TYPE_X86_64:
  6840. outs() << " X86_64";
  6841. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6842. case MachO::CPU_SUBTYPE_X86_64_ALL:
  6843. outs() << " ALL";
  6844. break;
  6845. case MachO::CPU_SUBTYPE_X86_64_H:
  6846. outs() << " Haswell";
  6847. break;
  6848. default:
  6849. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6850. break;
  6851. }
  6852. break;
  6853. case MachO::CPU_TYPE_ARM:
  6854. outs() << " ARM";
  6855. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6856. case MachO::CPU_SUBTYPE_ARM_ALL:
  6857. outs() << " ALL";
  6858. break;
  6859. case MachO::CPU_SUBTYPE_ARM_V4T:
  6860. outs() << " V4T";
  6861. break;
  6862. case MachO::CPU_SUBTYPE_ARM_V5TEJ:
  6863. outs() << " V5TEJ";
  6864. break;
  6865. case MachO::CPU_SUBTYPE_ARM_XSCALE:
  6866. outs() << " XSCALE";
  6867. break;
  6868. case MachO::CPU_SUBTYPE_ARM_V6:
  6869. outs() << " V6";
  6870. break;
  6871. case MachO::CPU_SUBTYPE_ARM_V6M:
  6872. outs() << " V6M";
  6873. break;
  6874. case MachO::CPU_SUBTYPE_ARM_V7:
  6875. outs() << " V7";
  6876. break;
  6877. case MachO::CPU_SUBTYPE_ARM_V7EM:
  6878. outs() << " V7EM";
  6879. break;
  6880. case MachO::CPU_SUBTYPE_ARM_V7K:
  6881. outs() << " V7K";
  6882. break;
  6883. case MachO::CPU_SUBTYPE_ARM_V7M:
  6884. outs() << " V7M";
  6885. break;
  6886. case MachO::CPU_SUBTYPE_ARM_V7S:
  6887. outs() << " V7S";
  6888. break;
  6889. default:
  6890. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6891. break;
  6892. }
  6893. break;
  6894. case MachO::CPU_TYPE_ARM64:
  6895. outs() << " ARM64";
  6896. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6897. case MachO::CPU_SUBTYPE_ARM64_ALL:
  6898. outs() << " ALL";
  6899. break;
  6900. default:
  6901. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6902. break;
  6903. }
  6904. break;
  6905. case MachO::CPU_TYPE_POWERPC:
  6906. outs() << " PPC";
  6907. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6908. case MachO::CPU_SUBTYPE_POWERPC_ALL:
  6909. outs() << " ALL";
  6910. break;
  6911. default:
  6912. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6913. break;
  6914. }
  6915. break;
  6916. case MachO::CPU_TYPE_POWERPC64:
  6917. outs() << " PPC64";
  6918. switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
  6919. case MachO::CPU_SUBTYPE_POWERPC_ALL:
  6920. outs() << " ALL";
  6921. break;
  6922. default:
  6923. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6924. break;
  6925. }
  6926. break;
  6927. default:
  6928. outs() << format(" %7d", cputype);
  6929. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  6930. break;
  6931. }
  6932. if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
  6933. outs() << " LIB64";
  6934. } else {
  6935. outs() << format(" 0x%02" PRIx32,
  6936. (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
  6937. }
  6938. switch (filetype) {
  6939. case MachO::MH_OBJECT:
  6940. outs() << " OBJECT";
  6941. break;
  6942. case MachO::MH_EXECUTE:
  6943. outs() << " EXECUTE";
  6944. break;
  6945. case MachO::MH_FVMLIB:
  6946. outs() << " FVMLIB";
  6947. break;
  6948. case MachO::MH_CORE:
  6949. outs() << " CORE";
  6950. break;
  6951. case MachO::MH_PRELOAD:
  6952. outs() << " PRELOAD";
  6953. break;
  6954. case MachO::MH_DYLIB:
  6955. outs() << " DYLIB";
  6956. break;
  6957. case MachO::MH_DYLIB_STUB:
  6958. outs() << " DYLIB_STUB";
  6959. break;
  6960. case MachO::MH_DYLINKER:
  6961. outs() << " DYLINKER";
  6962. break;
  6963. case MachO::MH_BUNDLE:
  6964. outs() << " BUNDLE";
  6965. break;
  6966. case MachO::MH_DSYM:
  6967. outs() << " DSYM";
  6968. break;
  6969. case MachO::MH_KEXT_BUNDLE:
  6970. outs() << " KEXTBUNDLE";
  6971. break;
  6972. default:
  6973. outs() << format(" %10u", filetype);
  6974. break;
  6975. }
  6976. outs() << format(" %5u", ncmds);
  6977. outs() << format(" %10u", sizeofcmds);
  6978. uint32_t f = flags;
  6979. if (f & MachO::MH_NOUNDEFS) {
  6980. outs() << " NOUNDEFS";
  6981. f &= ~MachO::MH_NOUNDEFS;
  6982. }
  6983. if (f & MachO::MH_INCRLINK) {
  6984. outs() << " INCRLINK";
  6985. f &= ~MachO::MH_INCRLINK;
  6986. }
  6987. if (f & MachO::MH_DYLDLINK) {
  6988. outs() << " DYLDLINK";
  6989. f &= ~MachO::MH_DYLDLINK;
  6990. }
  6991. if (f & MachO::MH_BINDATLOAD) {
  6992. outs() << " BINDATLOAD";
  6993. f &= ~MachO::MH_BINDATLOAD;
  6994. }
  6995. if (f & MachO::MH_PREBOUND) {
  6996. outs() << " PREBOUND";
  6997. f &= ~MachO::MH_PREBOUND;
  6998. }
  6999. if (f & MachO::MH_SPLIT_SEGS) {
  7000. outs() << " SPLIT_SEGS";
  7001. f &= ~MachO::MH_SPLIT_SEGS;
  7002. }
  7003. if (f & MachO::MH_LAZY_INIT) {
  7004. outs() << " LAZY_INIT";
  7005. f &= ~MachO::MH_LAZY_INIT;
  7006. }
  7007. if (f & MachO::MH_TWOLEVEL) {
  7008. outs() << " TWOLEVEL";
  7009. f &= ~MachO::MH_TWOLEVEL;
  7010. }
  7011. if (f & MachO::MH_FORCE_FLAT) {
  7012. outs() << " FORCE_FLAT";
  7013. f &= ~MachO::MH_FORCE_FLAT;
  7014. }
  7015. if (f & MachO::MH_NOMULTIDEFS) {
  7016. outs() << " NOMULTIDEFS";
  7017. f &= ~MachO::MH_NOMULTIDEFS;
  7018. }
  7019. if (f & MachO::MH_NOFIXPREBINDING) {
  7020. outs() << " NOFIXPREBINDING";
  7021. f &= ~MachO::MH_NOFIXPREBINDING;
  7022. }
  7023. if (f & MachO::MH_PREBINDABLE) {
  7024. outs() << " PREBINDABLE";
  7025. f &= ~MachO::MH_PREBINDABLE;
  7026. }
  7027. if (f & MachO::MH_ALLMODSBOUND) {
  7028. outs() << " ALLMODSBOUND";
  7029. f &= ~MachO::MH_ALLMODSBOUND;
  7030. }
  7031. if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
  7032. outs() << " SUBSECTIONS_VIA_SYMBOLS";
  7033. f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
  7034. }
  7035. if (f & MachO::MH_CANONICAL) {
  7036. outs() << " CANONICAL";
  7037. f &= ~MachO::MH_CANONICAL;
  7038. }
  7039. if (f & MachO::MH_WEAK_DEFINES) {
  7040. outs() << " WEAK_DEFINES";
  7041. f &= ~MachO::MH_WEAK_DEFINES;
  7042. }
  7043. if (f & MachO::MH_BINDS_TO_WEAK) {
  7044. outs() << " BINDS_TO_WEAK";
  7045. f &= ~MachO::MH_BINDS_TO_WEAK;
  7046. }
  7047. if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
  7048. outs() << " ALLOW_STACK_EXECUTION";
  7049. f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
  7050. }
  7051. if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
  7052. outs() << " DEAD_STRIPPABLE_DYLIB";
  7053. f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
  7054. }
  7055. if (f & MachO::MH_PIE) {
  7056. outs() << " PIE";
  7057. f &= ~MachO::MH_PIE;
  7058. }
  7059. if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
  7060. outs() << " NO_REEXPORTED_DYLIBS";
  7061. f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
  7062. }
  7063. if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
  7064. outs() << " MH_HAS_TLV_DESCRIPTORS";
  7065. f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
  7066. }
  7067. if (f & MachO::MH_NO_HEAP_EXECUTION) {
  7068. outs() << " MH_NO_HEAP_EXECUTION";
  7069. f &= ~MachO::MH_NO_HEAP_EXECUTION;
  7070. }
  7071. if (f & MachO::MH_APP_EXTENSION_SAFE) {
  7072. outs() << " APP_EXTENSION_SAFE";
  7073. f &= ~MachO::MH_APP_EXTENSION_SAFE;
  7074. }
  7075. if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
  7076. outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
  7077. f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
  7078. }
  7079. if (f != 0 || flags == 0)
  7080. outs() << format(" 0x%08" PRIx32, f);
  7081. } else {
  7082. outs() << format(" 0x%08" PRIx32, magic);
  7083. outs() << format(" %7d", cputype);
  7084. outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
  7085. outs() << format(" 0x%02" PRIx32,
  7086. (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
  7087. outs() << format(" %10u", filetype);
  7088. outs() << format(" %5u", ncmds);
  7089. outs() << format(" %10u", sizeofcmds);
  7090. outs() << format(" 0x%08" PRIx32, flags);
  7091. }
  7092. outs() << "\n";
  7093. }
  7094. static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
  7095. StringRef SegName, uint64_t vmaddr,
  7096. uint64_t vmsize, uint64_t fileoff,
  7097. uint64_t filesize, uint32_t maxprot,
  7098. uint32_t initprot, uint32_t nsects,
  7099. uint32_t flags, uint32_t object_size,
  7100. bool verbose) {
  7101. uint64_t expected_cmdsize;
  7102. if (cmd == MachO::LC_SEGMENT) {
  7103. outs() << " cmd LC_SEGMENT\n";
  7104. expected_cmdsize = nsects;
  7105. expected_cmdsize *= sizeof(struct MachO::section);
  7106. expected_cmdsize += sizeof(struct MachO::segment_command);
  7107. } else {
  7108. outs() << " cmd LC_SEGMENT_64\n";
  7109. expected_cmdsize = nsects;
  7110. expected_cmdsize *= sizeof(struct MachO::section_64);
  7111. expected_cmdsize += sizeof(struct MachO::segment_command_64);
  7112. }
  7113. outs() << " cmdsize " << cmdsize;
  7114. if (cmdsize != expected_cmdsize)
  7115. outs() << " Inconsistent size\n";
  7116. else
  7117. outs() << "\n";
  7118. outs() << " segname " << SegName << "\n";
  7119. if (cmd == MachO::LC_SEGMENT_64) {
  7120. outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
  7121. outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
  7122. } else {
  7123. outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
  7124. outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
  7125. }
  7126. outs() << " fileoff " << fileoff;
  7127. if (fileoff > object_size)
  7128. outs() << " (past end of file)\n";
  7129. else
  7130. outs() << "\n";
  7131. outs() << " filesize " << filesize;
  7132. if (fileoff + filesize > object_size)
  7133. outs() << " (past end of file)\n";
  7134. else
  7135. outs() << "\n";
  7136. if (verbose) {
  7137. if ((maxprot &
  7138. ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
  7139. MachO::VM_PROT_EXECUTE)) != 0)
  7140. outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
  7141. else {
  7142. outs() << " maxprot ";
  7143. outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
  7144. outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
  7145. outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
  7146. }
  7147. if ((initprot &
  7148. ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
  7149. MachO::VM_PROT_EXECUTE)) != 0)
  7150. outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
  7151. else {
  7152. outs() << " initprot ";
  7153. outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
  7154. outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
  7155. outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
  7156. }
  7157. } else {
  7158. outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
  7159. outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
  7160. }
  7161. outs() << " nsects " << nsects << "\n";
  7162. if (verbose) {
  7163. outs() << " flags";
  7164. if (flags == 0)
  7165. outs() << " (none)\n";
  7166. else {
  7167. if (flags & MachO::SG_HIGHVM) {
  7168. outs() << " HIGHVM";
  7169. flags &= ~MachO::SG_HIGHVM;
  7170. }
  7171. if (flags & MachO::SG_FVMLIB) {
  7172. outs() << " FVMLIB";
  7173. flags &= ~MachO::SG_FVMLIB;
  7174. }
  7175. if (flags & MachO::SG_NORELOC) {
  7176. outs() << " NORELOC";
  7177. flags &= ~MachO::SG_NORELOC;
  7178. }
  7179. if (flags & MachO::SG_PROTECTED_VERSION_1) {
  7180. outs() << " PROTECTED_VERSION_1";
  7181. flags &= ~MachO::SG_PROTECTED_VERSION_1;
  7182. }
  7183. if (flags)
  7184. outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
  7185. else
  7186. outs() << "\n";
  7187. }
  7188. } else {
  7189. outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
  7190. }
  7191. }
  7192. static void PrintSection(const char *sectname, const char *segname,
  7193. uint64_t addr, uint64_t size, uint32_t offset,
  7194. uint32_t align, uint32_t reloff, uint32_t nreloc,
  7195. uint32_t flags, uint32_t reserved1, uint32_t reserved2,
  7196. uint32_t cmd, const char *sg_segname,
  7197. uint32_t filetype, uint32_t object_size,
  7198. bool verbose) {
  7199. outs() << "Section\n";
  7200. outs() << " sectname " << format("%.16s\n", sectname);
  7201. outs() << " segname " << format("%.16s", segname);
  7202. if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
  7203. outs() << " (does not match segment)\n";
  7204. else
  7205. outs() << "\n";
  7206. if (cmd == MachO::LC_SEGMENT_64) {
  7207. outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
  7208. outs() << " size " << format("0x%016" PRIx64, size);
  7209. } else {
  7210. outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
  7211. outs() << " size " << format("0x%08" PRIx64, size);
  7212. }
  7213. if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
  7214. outs() << " (past end of file)\n";
  7215. else
  7216. outs() << "\n";
  7217. outs() << " offset " << offset;
  7218. if (offset > object_size)
  7219. outs() << " (past end of file)\n";
  7220. else
  7221. outs() << "\n";
  7222. uint32_t align_shifted = 1 << align;
  7223. outs() << " align 2^" << align << " (" << align_shifted << ")\n";
  7224. outs() << " reloff " << reloff;
  7225. if (reloff > object_size)
  7226. outs() << " (past end of file)\n";
  7227. else
  7228. outs() << "\n";
  7229. outs() << " nreloc " << nreloc;
  7230. if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
  7231. outs() << " (past end of file)\n";
  7232. else
  7233. outs() << "\n";
  7234. uint32_t section_type = flags & MachO::SECTION_TYPE;
  7235. if (verbose) {
  7236. outs() << " type";
  7237. if (section_type == MachO::S_REGULAR)
  7238. outs() << " S_REGULAR\n";
  7239. else if (section_type == MachO::S_ZEROFILL)
  7240. outs() << " S_ZEROFILL\n";
  7241. else if (section_type == MachO::S_CSTRING_LITERALS)
  7242. outs() << " S_CSTRING_LITERALS\n";
  7243. else if (section_type == MachO::S_4BYTE_LITERALS)
  7244. outs() << " S_4BYTE_LITERALS\n";
  7245. else if (section_type == MachO::S_8BYTE_LITERALS)
  7246. outs() << " S_8BYTE_LITERALS\n";
  7247. else if (section_type == MachO::S_16BYTE_LITERALS)
  7248. outs() << " S_16BYTE_LITERALS\n";
  7249. else if (section_type == MachO::S_LITERAL_POINTERS)
  7250. outs() << " S_LITERAL_POINTERS\n";
  7251. else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
  7252. outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
  7253. else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
  7254. outs() << " S_LAZY_SYMBOL_POINTERS\n";
  7255. else if (section_type == MachO::S_SYMBOL_STUBS)
  7256. outs() << " S_SYMBOL_STUBS\n";
  7257. else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
  7258. outs() << " S_MOD_INIT_FUNC_POINTERS\n";
  7259. else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
  7260. outs() << " S_MOD_TERM_FUNC_POINTERS\n";
  7261. else if (section_type == MachO::S_COALESCED)
  7262. outs() << " S_COALESCED\n";
  7263. else if (section_type == MachO::S_INTERPOSING)
  7264. outs() << " S_INTERPOSING\n";
  7265. else if (section_type == MachO::S_DTRACE_DOF)
  7266. outs() << " S_DTRACE_DOF\n";
  7267. else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
  7268. outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
  7269. else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
  7270. outs() << " S_THREAD_LOCAL_REGULAR\n";
  7271. else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
  7272. outs() << " S_THREAD_LOCAL_ZEROFILL\n";
  7273. else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
  7274. outs() << " S_THREAD_LOCAL_VARIABLES\n";
  7275. else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
  7276. outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
  7277. else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
  7278. outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
  7279. else
  7280. outs() << format("0x%08" PRIx32, section_type) << "\n";
  7281. outs() << "attributes";
  7282. uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
  7283. if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
  7284. outs() << " PURE_INSTRUCTIONS";
  7285. if (section_attributes & MachO::S_ATTR_NO_TOC)
  7286. outs() << " NO_TOC";
  7287. if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
  7288. outs() << " STRIP_STATIC_SYMS";
  7289. if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
  7290. outs() << " NO_DEAD_STRIP";
  7291. if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
  7292. outs() << " LIVE_SUPPORT";
  7293. if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
  7294. outs() << " SELF_MODIFYING_CODE";
  7295. if (section_attributes & MachO::S_ATTR_DEBUG)
  7296. outs() << " DEBUG";
  7297. if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
  7298. outs() << " SOME_INSTRUCTIONS";
  7299. if (section_attributes & MachO::S_ATTR_EXT_RELOC)
  7300. outs() << " EXT_RELOC";
  7301. if (section_attributes & MachO::S_ATTR_LOC_RELOC)
  7302. outs() << " LOC_RELOC";
  7303. if (section_attributes == 0)
  7304. outs() << " (none)";
  7305. outs() << "\n";
  7306. } else
  7307. outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
  7308. outs() << " reserved1 " << reserved1;
  7309. if (section_type == MachO::S_SYMBOL_STUBS ||
  7310. section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
  7311. section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
  7312. section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
  7313. section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
  7314. outs() << " (index into indirect symbol table)\n";
  7315. else
  7316. outs() << "\n";
  7317. outs() << " reserved2 " << reserved2;
  7318. if (section_type == MachO::S_SYMBOL_STUBS)
  7319. outs() << " (size of stubs)\n";
  7320. else
  7321. outs() << "\n";
  7322. }
  7323. static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
  7324. uint32_t object_size) {
  7325. outs() << " cmd LC_SYMTAB\n";
  7326. outs() << " cmdsize " << st.cmdsize;
  7327. if (st.cmdsize != sizeof(struct MachO::symtab_command))
  7328. outs() << " Incorrect size\n";
  7329. else
  7330. outs() << "\n";
  7331. outs() << " symoff " << st.symoff;
  7332. if (st.symoff > object_size)
  7333. outs() << " (past end of file)\n";
  7334. else
  7335. outs() << "\n";
  7336. outs() << " nsyms " << st.nsyms;
  7337. uint64_t big_size;
  7338. if (Is64Bit) {
  7339. big_size = st.nsyms;
  7340. big_size *= sizeof(struct MachO::nlist_64);
  7341. big_size += st.symoff;
  7342. if (big_size > object_size)
  7343. outs() << " (past end of file)\n";
  7344. else
  7345. outs() << "\n";
  7346. } else {
  7347. big_size = st.nsyms;
  7348. big_size *= sizeof(struct MachO::nlist);
  7349. big_size += st.symoff;
  7350. if (big_size > object_size)
  7351. outs() << " (past end of file)\n";
  7352. else
  7353. outs() << "\n";
  7354. }
  7355. outs() << " stroff " << st.stroff;
  7356. if (st.stroff > object_size)
  7357. outs() << " (past end of file)\n";
  7358. else
  7359. outs() << "\n";
  7360. outs() << " strsize " << st.strsize;
  7361. big_size = st.stroff;
  7362. big_size += st.strsize;
  7363. if (big_size > object_size)
  7364. outs() << " (past end of file)\n";
  7365. else
  7366. outs() << "\n";
  7367. }
  7368. static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
  7369. uint32_t nsyms, uint32_t object_size,
  7370. bool Is64Bit) {
  7371. outs() << " cmd LC_DYSYMTAB\n";
  7372. outs() << " cmdsize " << dyst.cmdsize;
  7373. if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
  7374. outs() << " Incorrect size\n";
  7375. else
  7376. outs() << "\n";
  7377. outs() << " ilocalsym " << dyst.ilocalsym;
  7378. if (dyst.ilocalsym > nsyms)
  7379. outs() << " (greater than the number of symbols)\n";
  7380. else
  7381. outs() << "\n";
  7382. outs() << " nlocalsym " << dyst.nlocalsym;
  7383. uint64_t big_size;
  7384. big_size = dyst.ilocalsym;
  7385. big_size += dyst.nlocalsym;
  7386. if (big_size > nsyms)
  7387. outs() << " (past the end of the symbol table)\n";
  7388. else
  7389. outs() << "\n";
  7390. outs() << " iextdefsym " << dyst.iextdefsym;
  7391. if (dyst.iextdefsym > nsyms)
  7392. outs() << " (greater than the number of symbols)\n";
  7393. else
  7394. outs() << "\n";
  7395. outs() << " nextdefsym " << dyst.nextdefsym;
  7396. big_size = dyst.iextdefsym;
  7397. big_size += dyst.nextdefsym;
  7398. if (big_size > nsyms)
  7399. outs() << " (past the end of the symbol table)\n";
  7400. else
  7401. outs() << "\n";
  7402. outs() << " iundefsym " << dyst.iundefsym;
  7403. if (dyst.iundefsym > nsyms)
  7404. outs() << " (greater than the number of symbols)\n";
  7405. else
  7406. outs() << "\n";
  7407. outs() << " nundefsym " << dyst.nundefsym;
  7408. big_size = dyst.iundefsym;
  7409. big_size += dyst.nundefsym;
  7410. if (big_size > nsyms)
  7411. outs() << " (past the end of the symbol table)\n";
  7412. else
  7413. outs() << "\n";
  7414. outs() << " tocoff " << dyst.tocoff;
  7415. if (dyst.tocoff > object_size)
  7416. outs() << " (past end of file)\n";
  7417. else
  7418. outs() << "\n";
  7419. outs() << " ntoc " << dyst.ntoc;
  7420. big_size = dyst.ntoc;
  7421. big_size *= sizeof(struct MachO::dylib_table_of_contents);
  7422. big_size += dyst.tocoff;
  7423. if (big_size > object_size)
  7424. outs() << " (past end of file)\n";
  7425. else
  7426. outs() << "\n";
  7427. outs() << " modtaboff " << dyst.modtaboff;
  7428. if (dyst.modtaboff > object_size)
  7429. outs() << " (past end of file)\n";
  7430. else
  7431. outs() << "\n";
  7432. outs() << " nmodtab " << dyst.nmodtab;
  7433. uint64_t modtabend;
  7434. if (Is64Bit) {
  7435. modtabend = dyst.nmodtab;
  7436. modtabend *= sizeof(struct MachO::dylib_module_64);
  7437. modtabend += dyst.modtaboff;
  7438. } else {
  7439. modtabend = dyst.nmodtab;
  7440. modtabend *= sizeof(struct MachO::dylib_module);
  7441. modtabend += dyst.modtaboff;
  7442. }
  7443. if (modtabend > object_size)
  7444. outs() << " (past end of file)\n";
  7445. else
  7446. outs() << "\n";
  7447. outs() << " extrefsymoff " << dyst.extrefsymoff;
  7448. if (dyst.extrefsymoff > object_size)
  7449. outs() << " (past end of file)\n";
  7450. else
  7451. outs() << "\n";
  7452. outs() << " nextrefsyms " << dyst.nextrefsyms;
  7453. big_size = dyst.nextrefsyms;
  7454. big_size *= sizeof(struct MachO::dylib_reference);
  7455. big_size += dyst.extrefsymoff;
  7456. if (big_size > object_size)
  7457. outs() << " (past end of file)\n";
  7458. else
  7459. outs() << "\n";
  7460. outs() << " indirectsymoff " << dyst.indirectsymoff;
  7461. if (dyst.indirectsymoff > object_size)
  7462. outs() << " (past end of file)\n";
  7463. else
  7464. outs() << "\n";
  7465. outs() << " nindirectsyms " << dyst.nindirectsyms;
  7466. big_size = dyst.nindirectsyms;
  7467. big_size *= sizeof(uint32_t);
  7468. big_size += dyst.indirectsymoff;
  7469. if (big_size > object_size)
  7470. outs() << " (past end of file)\n";
  7471. else
  7472. outs() << "\n";
  7473. outs() << " extreloff " << dyst.extreloff;
  7474. if (dyst.extreloff > object_size)
  7475. outs() << " (past end of file)\n";
  7476. else
  7477. outs() << "\n";
  7478. outs() << " nextrel " << dyst.nextrel;
  7479. big_size = dyst.nextrel;
  7480. big_size *= sizeof(struct MachO::relocation_info);
  7481. big_size += dyst.extreloff;
  7482. if (big_size > object_size)
  7483. outs() << " (past end of file)\n";
  7484. else
  7485. outs() << "\n";
  7486. outs() << " locreloff " << dyst.locreloff;
  7487. if (dyst.locreloff > object_size)
  7488. outs() << " (past end of file)\n";
  7489. else
  7490. outs() << "\n";
  7491. outs() << " nlocrel " << dyst.nlocrel;
  7492. big_size = dyst.nlocrel;
  7493. big_size *= sizeof(struct MachO::relocation_info);
  7494. big_size += dyst.locreloff;
  7495. if (big_size > object_size)
  7496. outs() << " (past end of file)\n";
  7497. else
  7498. outs() << "\n";
  7499. }
  7500. static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
  7501. uint32_t object_size) {
  7502. if (dc.cmd == MachO::LC_DYLD_INFO)
  7503. outs() << " cmd LC_DYLD_INFO\n";
  7504. else
  7505. outs() << " cmd LC_DYLD_INFO_ONLY\n";
  7506. outs() << " cmdsize " << dc.cmdsize;
  7507. if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
  7508. outs() << " Incorrect size\n";
  7509. else
  7510. outs() << "\n";
  7511. outs() << " rebase_off " << dc.rebase_off;
  7512. if (dc.rebase_off > object_size)
  7513. outs() << " (past end of file)\n";
  7514. else
  7515. outs() << "\n";
  7516. outs() << " rebase_size " << dc.rebase_size;
  7517. uint64_t big_size;
  7518. big_size = dc.rebase_off;
  7519. big_size += dc.rebase_size;
  7520. if (big_size > object_size)
  7521. outs() << " (past end of file)\n";
  7522. else
  7523. outs() << "\n";
  7524. outs() << " bind_off " << dc.bind_off;
  7525. if (dc.bind_off > object_size)
  7526. outs() << " (past end of file)\n";
  7527. else
  7528. outs() << "\n";
  7529. outs() << " bind_size " << dc.bind_size;
  7530. big_size = dc.bind_off;
  7531. big_size += dc.bind_size;
  7532. if (big_size > object_size)
  7533. outs() << " (past end of file)\n";
  7534. else
  7535. outs() << "\n";
  7536. outs() << " weak_bind_off " << dc.weak_bind_off;
  7537. if (dc.weak_bind_off > object_size)
  7538. outs() << " (past end of file)\n";
  7539. else
  7540. outs() << "\n";
  7541. outs() << " weak_bind_size " << dc.weak_bind_size;
  7542. big_size = dc.weak_bind_off;
  7543. big_size += dc.weak_bind_size;
  7544. if (big_size > object_size)
  7545. outs() << " (past end of file)\n";
  7546. else
  7547. outs() << "\n";
  7548. outs() << " lazy_bind_off " << dc.lazy_bind_off;
  7549. if (dc.lazy_bind_off > object_size)
  7550. outs() << " (past end of file)\n";
  7551. else
  7552. outs() << "\n";
  7553. outs() << " lazy_bind_size " << dc.lazy_bind_size;
  7554. big_size = dc.lazy_bind_off;
  7555. big_size += dc.lazy_bind_size;
  7556. if (big_size > object_size)
  7557. outs() << " (past end of file)\n";
  7558. else
  7559. outs() << "\n";
  7560. outs() << " export_off " << dc.export_off;
  7561. if (dc.export_off > object_size)
  7562. outs() << " (past end of file)\n";
  7563. else
  7564. outs() << "\n";
  7565. outs() << " export_size " << dc.export_size;
  7566. big_size = dc.export_off;
  7567. big_size += dc.export_size;
  7568. if (big_size > object_size)
  7569. outs() << " (past end of file)\n";
  7570. else
  7571. outs() << "\n";
  7572. }
  7573. static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
  7574. const char *Ptr) {
  7575. if (dyld.cmd == MachO::LC_ID_DYLINKER)
  7576. outs() << " cmd LC_ID_DYLINKER\n";
  7577. else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
  7578. outs() << " cmd LC_LOAD_DYLINKER\n";
  7579. else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
  7580. outs() << " cmd LC_DYLD_ENVIRONMENT\n";
  7581. else
  7582. outs() << " cmd ?(" << dyld.cmd << ")\n";
  7583. outs() << " cmdsize " << dyld.cmdsize;
  7584. if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
  7585. outs() << " Incorrect size\n";
  7586. else
  7587. outs() << "\n";
  7588. if (dyld.name >= dyld.cmdsize)
  7589. outs() << " name ?(bad offset " << dyld.name << ")\n";
  7590. else {
  7591. const char *P = (const char *)(Ptr) + dyld.name;
  7592. outs() << " name " << P << " (offset " << dyld.name << ")\n";
  7593. }
  7594. }
  7595. static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
  7596. outs() << " cmd LC_UUID\n";
  7597. outs() << " cmdsize " << uuid.cmdsize;
  7598. if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
  7599. outs() << " Incorrect size\n";
  7600. else
  7601. outs() << "\n";
  7602. outs() << " uuid ";
  7603. for (int i = 0; i < 16; ++i) {
  7604. outs() << format("%02" PRIX32, uuid.uuid[i]);
  7605. if (i == 3 || i == 5 || i == 7 || i == 9)
  7606. outs() << "-";
  7607. }
  7608. outs() << "\n";
  7609. }
  7610. static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
  7611. outs() << " cmd LC_RPATH\n";
  7612. outs() << " cmdsize " << rpath.cmdsize;
  7613. if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
  7614. outs() << " Incorrect size\n";
  7615. else
  7616. outs() << "\n";
  7617. if (rpath.path >= rpath.cmdsize)
  7618. outs() << " path ?(bad offset " << rpath.path << ")\n";
  7619. else {
  7620. const char *P = (const char *)(Ptr) + rpath.path;
  7621. outs() << " path " << P << " (offset " << rpath.path << ")\n";
  7622. }
  7623. }
  7624. static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
  7625. StringRef LoadCmdName;
  7626. switch (vd.cmd) {
  7627. case MachO::LC_VERSION_MIN_MACOSX:
  7628. LoadCmdName = "LC_VERSION_MIN_MACOSX";
  7629. break;
  7630. case MachO::LC_VERSION_MIN_IPHONEOS:
  7631. LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
  7632. break;
  7633. case MachO::LC_VERSION_MIN_TVOS:
  7634. LoadCmdName = "LC_VERSION_MIN_TVOS";
  7635. break;
  7636. case MachO::LC_VERSION_MIN_WATCHOS:
  7637. LoadCmdName = "LC_VERSION_MIN_WATCHOS";
  7638. break;
  7639. default:
  7640. llvm_unreachable("Unknown version min load command");
  7641. }
  7642. outs() << " cmd " << LoadCmdName << '\n';
  7643. outs() << " cmdsize " << vd.cmdsize;
  7644. if (vd.cmdsize != sizeof(struct MachO::version_min_command))
  7645. outs() << " Incorrect size\n";
  7646. else
  7647. outs() << "\n";
  7648. outs() << " version "
  7649. << MachOObjectFile::getVersionMinMajor(vd, false) << "."
  7650. << MachOObjectFile::getVersionMinMinor(vd, false);
  7651. uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
  7652. if (Update != 0)
  7653. outs() << "." << Update;
  7654. outs() << "\n";
  7655. if (vd.sdk == 0)
  7656. outs() << " sdk n/a";
  7657. else {
  7658. outs() << " sdk "
  7659. << MachOObjectFile::getVersionMinMajor(vd, true) << "."
  7660. << MachOObjectFile::getVersionMinMinor(vd, true);
  7661. }
  7662. Update = MachOObjectFile::getVersionMinUpdate(vd, true);
  7663. if (Update != 0)
  7664. outs() << "." << Update;
  7665. outs() << "\n";
  7666. }
  7667. static void PrintNoteLoadCommand(MachO::note_command Nt) {
  7668. outs() << " cmd LC_NOTE\n";
  7669. outs() << " cmdsize " << Nt.cmdsize;
  7670. if (Nt.cmdsize != sizeof(struct MachO::note_command))
  7671. outs() << " Incorrect size\n";
  7672. else
  7673. outs() << "\n";
  7674. const char *d = Nt.data_owner;
  7675. outs() << "data_owner " << format("%.16s\n", d);
  7676. outs() << " offset " << Nt.offset << "\n";
  7677. outs() << " size " << Nt.size << "\n";
  7678. }
  7679. static void PrintBuildToolVersion(MachO::build_tool_version bv) {
  7680. outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n";
  7681. outs() << " version " << MachOObjectFile::getVersionString(bv.version)
  7682. << "\n";
  7683. }
  7684. static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
  7685. MachO::build_version_command bd) {
  7686. outs() << " cmd LC_BUILD_VERSION\n";
  7687. outs() << " cmdsize " << bd.cmdsize;
  7688. if (bd.cmdsize !=
  7689. sizeof(struct MachO::build_version_command) +
  7690. bd.ntools * sizeof(struct MachO::build_tool_version))
  7691. outs() << " Incorrect size\n";
  7692. else
  7693. outs() << "\n";
  7694. outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform)
  7695. << "\n";
  7696. if (bd.sdk)
  7697. outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk)
  7698. << "\n";
  7699. else
  7700. outs() << " sdk n/a\n";
  7701. outs() << " minos " << MachOObjectFile::getVersionString(bd.minos)
  7702. << "\n";
  7703. outs() << " ntools " << bd.ntools << "\n";
  7704. for (unsigned i = 0; i < bd.ntools; ++i) {
  7705. MachO::build_tool_version bv = obj->getBuildToolVersion(i);
  7706. PrintBuildToolVersion(bv);
  7707. }
  7708. }
  7709. static void PrintSourceVersionCommand(MachO::source_version_command sd) {
  7710. outs() << " cmd LC_SOURCE_VERSION\n";
  7711. outs() << " cmdsize " << sd.cmdsize;
  7712. if (sd.cmdsize != sizeof(struct MachO::source_version_command))
  7713. outs() << " Incorrect size\n";
  7714. else
  7715. outs() << "\n";
  7716. uint64_t a = (sd.version >> 40) & 0xffffff;
  7717. uint64_t b = (sd.version >> 30) & 0x3ff;
  7718. uint64_t c = (sd.version >> 20) & 0x3ff;
  7719. uint64_t d = (sd.version >> 10) & 0x3ff;
  7720. uint64_t e = sd.version & 0x3ff;
  7721. outs() << " version " << a << "." << b;
  7722. if (e != 0)
  7723. outs() << "." << c << "." << d << "." << e;
  7724. else if (d != 0)
  7725. outs() << "." << c << "." << d;
  7726. else if (c != 0)
  7727. outs() << "." << c;
  7728. outs() << "\n";
  7729. }
  7730. static void PrintEntryPointCommand(MachO::entry_point_command ep) {
  7731. outs() << " cmd LC_MAIN\n";
  7732. outs() << " cmdsize " << ep.cmdsize;
  7733. if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
  7734. outs() << " Incorrect size\n";
  7735. else
  7736. outs() << "\n";
  7737. outs() << " entryoff " << ep.entryoff << "\n";
  7738. outs() << " stacksize " << ep.stacksize << "\n";
  7739. }
  7740. static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
  7741. uint32_t object_size) {
  7742. outs() << " cmd LC_ENCRYPTION_INFO\n";
  7743. outs() << " cmdsize " << ec.cmdsize;
  7744. if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
  7745. outs() << " Incorrect size\n";
  7746. else
  7747. outs() << "\n";
  7748. outs() << " cryptoff " << ec.cryptoff;
  7749. if (ec.cryptoff > object_size)
  7750. outs() << " (past end of file)\n";
  7751. else
  7752. outs() << "\n";
  7753. outs() << " cryptsize " << ec.cryptsize;
  7754. if (ec.cryptsize > object_size)
  7755. outs() << " (past end of file)\n";
  7756. else
  7757. outs() << "\n";
  7758. outs() << " cryptid " << ec.cryptid << "\n";
  7759. }
  7760. static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
  7761. uint32_t object_size) {
  7762. outs() << " cmd LC_ENCRYPTION_INFO_64\n";
  7763. outs() << " cmdsize " << ec.cmdsize;
  7764. if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
  7765. outs() << " Incorrect size\n";
  7766. else
  7767. outs() << "\n";
  7768. outs() << " cryptoff " << ec.cryptoff;
  7769. if (ec.cryptoff > object_size)
  7770. outs() << " (past end of file)\n";
  7771. else
  7772. outs() << "\n";
  7773. outs() << " cryptsize " << ec.cryptsize;
  7774. if (ec.cryptsize > object_size)
  7775. outs() << " (past end of file)\n";
  7776. else
  7777. outs() << "\n";
  7778. outs() << " cryptid " << ec.cryptid << "\n";
  7779. outs() << " pad " << ec.pad << "\n";
  7780. }
  7781. static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
  7782. const char *Ptr) {
  7783. outs() << " cmd LC_LINKER_OPTION\n";
  7784. outs() << " cmdsize " << lo.cmdsize;
  7785. if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
  7786. outs() << " Incorrect size\n";
  7787. else
  7788. outs() << "\n";
  7789. outs() << " count " << lo.count << "\n";
  7790. const char *string = Ptr + sizeof(struct MachO::linker_option_command);
  7791. uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
  7792. uint32_t i = 0;
  7793. while (left > 0) {
  7794. while (*string == '\0' && left > 0) {
  7795. string++;
  7796. left--;
  7797. }
  7798. if (left > 0) {
  7799. i++;
  7800. outs() << " string #" << i << " " << format("%.*s\n", left, string);
  7801. uint32_t NullPos = StringRef(string, left).find('\0');
  7802. uint32_t len = std::min(NullPos, left) + 1;
  7803. string += len;
  7804. left -= len;
  7805. }
  7806. }
  7807. if (lo.count != i)
  7808. outs() << " count " << lo.count << " does not match number of strings "
  7809. << i << "\n";
  7810. }
  7811. static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
  7812. const char *Ptr) {
  7813. outs() << " cmd LC_SUB_FRAMEWORK\n";
  7814. outs() << " cmdsize " << sub.cmdsize;
  7815. if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
  7816. outs() << " Incorrect size\n";
  7817. else
  7818. outs() << "\n";
  7819. if (sub.umbrella < sub.cmdsize) {
  7820. const char *P = Ptr + sub.umbrella;
  7821. outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
  7822. } else {
  7823. outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
  7824. }
  7825. }
  7826. static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
  7827. const char *Ptr) {
  7828. outs() << " cmd LC_SUB_UMBRELLA\n";
  7829. outs() << " cmdsize " << sub.cmdsize;
  7830. if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
  7831. outs() << " Incorrect size\n";
  7832. else
  7833. outs() << "\n";
  7834. if (sub.sub_umbrella < sub.cmdsize) {
  7835. const char *P = Ptr + sub.sub_umbrella;
  7836. outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
  7837. } else {
  7838. outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
  7839. }
  7840. }
  7841. static void PrintSubLibraryCommand(MachO::sub_library_command sub,
  7842. const char *Ptr) {
  7843. outs() << " cmd LC_SUB_LIBRARY\n";
  7844. outs() << " cmdsize " << sub.cmdsize;
  7845. if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
  7846. outs() << " Incorrect size\n";
  7847. else
  7848. outs() << "\n";
  7849. if (sub.sub_library < sub.cmdsize) {
  7850. const char *P = Ptr + sub.sub_library;
  7851. outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
  7852. } else {
  7853. outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
  7854. }
  7855. }
  7856. static void PrintSubClientCommand(MachO::sub_client_command sub,
  7857. const char *Ptr) {
  7858. outs() << " cmd LC_SUB_CLIENT\n";
  7859. outs() << " cmdsize " << sub.cmdsize;
  7860. if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
  7861. outs() << " Incorrect size\n";
  7862. else
  7863. outs() << "\n";
  7864. if (sub.client < sub.cmdsize) {
  7865. const char *P = Ptr + sub.client;
  7866. outs() << " client " << P << " (offset " << sub.client << ")\n";
  7867. } else {
  7868. outs() << " client ?(bad offset " << sub.client << ")\n";
  7869. }
  7870. }
  7871. static void PrintRoutinesCommand(MachO::routines_command r) {
  7872. outs() << " cmd LC_ROUTINES\n";
  7873. outs() << " cmdsize " << r.cmdsize;
  7874. if (r.cmdsize != sizeof(struct MachO::routines_command))
  7875. outs() << " Incorrect size\n";
  7876. else
  7877. outs() << "\n";
  7878. outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
  7879. outs() << " init_module " << r.init_module << "\n";
  7880. outs() << " reserved1 " << r.reserved1 << "\n";
  7881. outs() << " reserved2 " << r.reserved2 << "\n";
  7882. outs() << " reserved3 " << r.reserved3 << "\n";
  7883. outs() << " reserved4 " << r.reserved4 << "\n";
  7884. outs() << " reserved5 " << r.reserved5 << "\n";
  7885. outs() << " reserved6 " << r.reserved6 << "\n";
  7886. }
  7887. static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
  7888. outs() << " cmd LC_ROUTINES_64\n";
  7889. outs() << " cmdsize " << r.cmdsize;
  7890. if (r.cmdsize != sizeof(struct MachO::routines_command_64))
  7891. outs() << " Incorrect size\n";
  7892. else
  7893. outs() << "\n";
  7894. outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
  7895. outs() << " init_module " << r.init_module << "\n";
  7896. outs() << " reserved1 " << r.reserved1 << "\n";
  7897. outs() << " reserved2 " << r.reserved2 << "\n";
  7898. outs() << " reserved3 " << r.reserved3 << "\n";
  7899. outs() << " reserved4 " << r.reserved4 << "\n";
  7900. outs() << " reserved5 " << r.reserved5 << "\n";
  7901. outs() << " reserved6 " << r.reserved6 << "\n";
  7902. }
  7903. static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
  7904. outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax);
  7905. outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx);
  7906. outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
  7907. outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
  7908. outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi);
  7909. outs() << " esi " << format("0x%08" PRIx32, cpu32.esi);
  7910. outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
  7911. outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
  7912. outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss);
  7913. outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
  7914. outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
  7915. outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n";
  7916. outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds);
  7917. outs() << " es " << format("0x%08" PRIx32, cpu32.es);
  7918. outs() << " fs " << format("0x%08" PRIx32, cpu32.fs);
  7919. outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n";
  7920. }
  7921. static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
  7922. outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
  7923. outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
  7924. outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
  7925. outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
  7926. outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
  7927. outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
  7928. outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
  7929. outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
  7930. outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
  7931. outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
  7932. outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
  7933. outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
  7934. outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
  7935. outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
  7936. outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
  7937. outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
  7938. outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
  7939. outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
  7940. outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
  7941. outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
  7942. outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
  7943. }
  7944. static void Print_mmst_reg(MachO::mmst_reg_t &r) {
  7945. uint32_t f;
  7946. outs() << "\t mmst_reg ";
  7947. for (f = 0; f < 10; f++)
  7948. outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
  7949. outs() << "\n";
  7950. outs() << "\t mmst_rsrv ";
  7951. for (f = 0; f < 6; f++)
  7952. outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
  7953. outs() << "\n";
  7954. }
  7955. static void Print_xmm_reg(MachO::xmm_reg_t &r) {
  7956. uint32_t f;
  7957. outs() << "\t xmm_reg ";
  7958. for (f = 0; f < 16; f++)
  7959. outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
  7960. outs() << "\n";
  7961. }
  7962. static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
  7963. outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
  7964. outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
  7965. outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
  7966. outs() << " denorm " << fpu.fpu_fcw.denorm;
  7967. outs() << " zdiv " << fpu.fpu_fcw.zdiv;
  7968. outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
  7969. outs() << " undfl " << fpu.fpu_fcw.undfl;
  7970. outs() << " precis " << fpu.fpu_fcw.precis << "\n";
  7971. outs() << "\t\t pc ";
  7972. if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
  7973. outs() << "FP_PREC_24B ";
  7974. else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
  7975. outs() << "FP_PREC_53B ";
  7976. else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
  7977. outs() << "FP_PREC_64B ";
  7978. else
  7979. outs() << fpu.fpu_fcw.pc << " ";
  7980. outs() << "rc ";
  7981. if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
  7982. outs() << "FP_RND_NEAR ";
  7983. else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
  7984. outs() << "FP_RND_DOWN ";
  7985. else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
  7986. outs() << "FP_RND_UP ";
  7987. else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
  7988. outs() << "FP_CHOP ";
  7989. outs() << "\n";
  7990. outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
  7991. outs() << " denorm " << fpu.fpu_fsw.denorm;
  7992. outs() << " zdiv " << fpu.fpu_fsw.zdiv;
  7993. outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
  7994. outs() << " undfl " << fpu.fpu_fsw.undfl;
  7995. outs() << " precis " << fpu.fpu_fsw.precis;
  7996. outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
  7997. outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
  7998. outs() << " c0 " << fpu.fpu_fsw.c0;
  7999. outs() << " c1 " << fpu.fpu_fsw.c1;
  8000. outs() << " c2 " << fpu.fpu_fsw.c2;
  8001. outs() << " tos " << fpu.fpu_fsw.tos;
  8002. outs() << " c3 " << fpu.fpu_fsw.c3;
  8003. outs() << " busy " << fpu.fpu_fsw.busy << "\n";
  8004. outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
  8005. outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
  8006. outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
  8007. outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
  8008. outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
  8009. outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
  8010. outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
  8011. outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
  8012. outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
  8013. outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
  8014. outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
  8015. outs() << "\n";
  8016. outs() << "\t fpu_stmm0:\n";
  8017. Print_mmst_reg(fpu.fpu_stmm0);
  8018. outs() << "\t fpu_stmm1:\n";
  8019. Print_mmst_reg(fpu.fpu_stmm1);
  8020. outs() << "\t fpu_stmm2:\n";
  8021. Print_mmst_reg(fpu.fpu_stmm2);
  8022. outs() << "\t fpu_stmm3:\n";
  8023. Print_mmst_reg(fpu.fpu_stmm3);
  8024. outs() << "\t fpu_stmm4:\n";
  8025. Print_mmst_reg(fpu.fpu_stmm4);
  8026. outs() << "\t fpu_stmm5:\n";
  8027. Print_mmst_reg(fpu.fpu_stmm5);
  8028. outs() << "\t fpu_stmm6:\n";
  8029. Print_mmst_reg(fpu.fpu_stmm6);
  8030. outs() << "\t fpu_stmm7:\n";
  8031. Print_mmst_reg(fpu.fpu_stmm7);
  8032. outs() << "\t fpu_xmm0:\n";
  8033. Print_xmm_reg(fpu.fpu_xmm0);
  8034. outs() << "\t fpu_xmm1:\n";
  8035. Print_xmm_reg(fpu.fpu_xmm1);
  8036. outs() << "\t fpu_xmm2:\n";
  8037. Print_xmm_reg(fpu.fpu_xmm2);
  8038. outs() << "\t fpu_xmm3:\n";
  8039. Print_xmm_reg(fpu.fpu_xmm3);
  8040. outs() << "\t fpu_xmm4:\n";
  8041. Print_xmm_reg(fpu.fpu_xmm4);
  8042. outs() << "\t fpu_xmm5:\n";
  8043. Print_xmm_reg(fpu.fpu_xmm5);
  8044. outs() << "\t fpu_xmm6:\n";
  8045. Print_xmm_reg(fpu.fpu_xmm6);
  8046. outs() << "\t fpu_xmm7:\n";
  8047. Print_xmm_reg(fpu.fpu_xmm7);
  8048. outs() << "\t fpu_xmm8:\n";
  8049. Print_xmm_reg(fpu.fpu_xmm8);
  8050. outs() << "\t fpu_xmm9:\n";
  8051. Print_xmm_reg(fpu.fpu_xmm9);
  8052. outs() << "\t fpu_xmm10:\n";
  8053. Print_xmm_reg(fpu.fpu_xmm10);
  8054. outs() << "\t fpu_xmm11:\n";
  8055. Print_xmm_reg(fpu.fpu_xmm11);
  8056. outs() << "\t fpu_xmm12:\n";
  8057. Print_xmm_reg(fpu.fpu_xmm12);
  8058. outs() << "\t fpu_xmm13:\n";
  8059. Print_xmm_reg(fpu.fpu_xmm13);
  8060. outs() << "\t fpu_xmm14:\n";
  8061. Print_xmm_reg(fpu.fpu_xmm14);
  8062. outs() << "\t fpu_xmm15:\n";
  8063. Print_xmm_reg(fpu.fpu_xmm15);
  8064. outs() << "\t fpu_rsrv4:\n";
  8065. for (uint32_t f = 0; f < 6; f++) {
  8066. outs() << "\t ";
  8067. for (uint32_t g = 0; g < 16; g++)
  8068. outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
  8069. outs() << "\n";
  8070. }
  8071. outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
  8072. outs() << "\n";
  8073. }
  8074. static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
  8075. outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
  8076. outs() << " err " << format("0x%08" PRIx32, exc64.err);
  8077. outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
  8078. }
  8079. static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
  8080. outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]);
  8081. outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]);
  8082. outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]);
  8083. outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
  8084. outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]);
  8085. outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]);
  8086. outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]);
  8087. outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
  8088. outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]);
  8089. outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]);
  8090. outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]);
  8091. outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
  8092. outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]);
  8093. outs() << " sp " << format("0x%08" PRIx32, cpu32.sp);
  8094. outs() << " lr " << format("0x%08" PRIx32, cpu32.lr);
  8095. outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n";
  8096. outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
  8097. }
  8098. static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
  8099. outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]);
  8100. outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]);
  8101. outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
  8102. outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]);
  8103. outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]);
  8104. outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
  8105. outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]);
  8106. outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]);
  8107. outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
  8108. outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]);
  8109. outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]);
  8110. outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
  8111. outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]);
  8112. outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]);
  8113. outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
  8114. outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]);
  8115. outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]);
  8116. outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
  8117. outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]);
  8118. outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]);
  8119. outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
  8120. outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]);
  8121. outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]);
  8122. outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
  8123. outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]);
  8124. outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]);
  8125. outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
  8126. outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]);
  8127. outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]);
  8128. outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n";
  8129. outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr);
  8130. outs() << " sp " << format("0x%016" PRIx64, cpu64.sp);
  8131. outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n";
  8132. outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n";
  8133. }
  8134. static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
  8135. bool isLittleEndian, uint32_t cputype) {
  8136. if (t.cmd == MachO::LC_THREAD)
  8137. outs() << " cmd LC_THREAD\n";
  8138. else if (t.cmd == MachO::LC_UNIXTHREAD)
  8139. outs() << " cmd LC_UNIXTHREAD\n";
  8140. else
  8141. outs() << " cmd " << t.cmd << " (unknown)\n";
  8142. outs() << " cmdsize " << t.cmdsize;
  8143. if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
  8144. outs() << " Incorrect size\n";
  8145. else
  8146. outs() << "\n";
  8147. const char *begin = Ptr + sizeof(struct MachO::thread_command);
  8148. const char *end = Ptr + t.cmdsize;
  8149. uint32_t flavor, count, left;
  8150. if (cputype == MachO::CPU_TYPE_I386) {
  8151. while (begin < end) {
  8152. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8153. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  8154. begin += sizeof(uint32_t);
  8155. } else {
  8156. flavor = 0;
  8157. begin = end;
  8158. }
  8159. if (isLittleEndian != sys::IsLittleEndianHost)
  8160. sys::swapByteOrder(flavor);
  8161. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8162. memcpy((char *)&count, begin, sizeof(uint32_t));
  8163. begin += sizeof(uint32_t);
  8164. } else {
  8165. count = 0;
  8166. begin = end;
  8167. }
  8168. if (isLittleEndian != sys::IsLittleEndianHost)
  8169. sys::swapByteOrder(count);
  8170. if (flavor == MachO::x86_THREAD_STATE32) {
  8171. outs() << " flavor i386_THREAD_STATE\n";
  8172. if (count == MachO::x86_THREAD_STATE32_COUNT)
  8173. outs() << " count i386_THREAD_STATE_COUNT\n";
  8174. else
  8175. outs() << " count " << count
  8176. << " (not x86_THREAD_STATE32_COUNT)\n";
  8177. MachO::x86_thread_state32_t cpu32;
  8178. left = end - begin;
  8179. if (left >= sizeof(MachO::x86_thread_state32_t)) {
  8180. memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
  8181. begin += sizeof(MachO::x86_thread_state32_t);
  8182. } else {
  8183. memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
  8184. memcpy(&cpu32, begin, left);
  8185. begin += left;
  8186. }
  8187. if (isLittleEndian != sys::IsLittleEndianHost)
  8188. swapStruct(cpu32);
  8189. Print_x86_thread_state32_t(cpu32);
  8190. } else if (flavor == MachO::x86_THREAD_STATE) {
  8191. outs() << " flavor x86_THREAD_STATE\n";
  8192. if (count == MachO::x86_THREAD_STATE_COUNT)
  8193. outs() << " count x86_THREAD_STATE_COUNT\n";
  8194. else
  8195. outs() << " count " << count
  8196. << " (not x86_THREAD_STATE_COUNT)\n";
  8197. struct MachO::x86_thread_state_t ts;
  8198. left = end - begin;
  8199. if (left >= sizeof(MachO::x86_thread_state_t)) {
  8200. memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
  8201. begin += sizeof(MachO::x86_thread_state_t);
  8202. } else {
  8203. memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
  8204. memcpy(&ts, begin, left);
  8205. begin += left;
  8206. }
  8207. if (isLittleEndian != sys::IsLittleEndianHost)
  8208. swapStruct(ts);
  8209. if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
  8210. outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
  8211. if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
  8212. outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
  8213. else
  8214. outs() << "tsh.count " << ts.tsh.count
  8215. << " (not x86_THREAD_STATE32_COUNT\n";
  8216. Print_x86_thread_state32_t(ts.uts.ts32);
  8217. } else {
  8218. outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
  8219. << ts.tsh.count << "\n";
  8220. }
  8221. } else {
  8222. outs() << " flavor " << flavor << " (unknown)\n";
  8223. outs() << " count " << count << "\n";
  8224. outs() << " state (unknown)\n";
  8225. begin += count * sizeof(uint32_t);
  8226. }
  8227. }
  8228. } else if (cputype == MachO::CPU_TYPE_X86_64) {
  8229. while (begin < end) {
  8230. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8231. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  8232. begin += sizeof(uint32_t);
  8233. } else {
  8234. flavor = 0;
  8235. begin = end;
  8236. }
  8237. if (isLittleEndian != sys::IsLittleEndianHost)
  8238. sys::swapByteOrder(flavor);
  8239. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8240. memcpy((char *)&count, begin, sizeof(uint32_t));
  8241. begin += sizeof(uint32_t);
  8242. } else {
  8243. count = 0;
  8244. begin = end;
  8245. }
  8246. if (isLittleEndian != sys::IsLittleEndianHost)
  8247. sys::swapByteOrder(count);
  8248. if (flavor == MachO::x86_THREAD_STATE64) {
  8249. outs() << " flavor x86_THREAD_STATE64\n";
  8250. if (count == MachO::x86_THREAD_STATE64_COUNT)
  8251. outs() << " count x86_THREAD_STATE64_COUNT\n";
  8252. else
  8253. outs() << " count " << count
  8254. << " (not x86_THREAD_STATE64_COUNT)\n";
  8255. MachO::x86_thread_state64_t cpu64;
  8256. left = end - begin;
  8257. if (left >= sizeof(MachO::x86_thread_state64_t)) {
  8258. memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
  8259. begin += sizeof(MachO::x86_thread_state64_t);
  8260. } else {
  8261. memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
  8262. memcpy(&cpu64, begin, left);
  8263. begin += left;
  8264. }
  8265. if (isLittleEndian != sys::IsLittleEndianHost)
  8266. swapStruct(cpu64);
  8267. Print_x86_thread_state64_t(cpu64);
  8268. } else if (flavor == MachO::x86_THREAD_STATE) {
  8269. outs() << " flavor x86_THREAD_STATE\n";
  8270. if (count == MachO::x86_THREAD_STATE_COUNT)
  8271. outs() << " count x86_THREAD_STATE_COUNT\n";
  8272. else
  8273. outs() << " count " << count
  8274. << " (not x86_THREAD_STATE_COUNT)\n";
  8275. struct MachO::x86_thread_state_t ts;
  8276. left = end - begin;
  8277. if (left >= sizeof(MachO::x86_thread_state_t)) {
  8278. memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
  8279. begin += sizeof(MachO::x86_thread_state_t);
  8280. } else {
  8281. memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
  8282. memcpy(&ts, begin, left);
  8283. begin += left;
  8284. }
  8285. if (isLittleEndian != sys::IsLittleEndianHost)
  8286. swapStruct(ts);
  8287. if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
  8288. outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
  8289. if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
  8290. outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
  8291. else
  8292. outs() << "tsh.count " << ts.tsh.count
  8293. << " (not x86_THREAD_STATE64_COUNT\n";
  8294. Print_x86_thread_state64_t(ts.uts.ts64);
  8295. } else {
  8296. outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
  8297. << ts.tsh.count << "\n";
  8298. }
  8299. } else if (flavor == MachO::x86_FLOAT_STATE) {
  8300. outs() << " flavor x86_FLOAT_STATE\n";
  8301. if (count == MachO::x86_FLOAT_STATE_COUNT)
  8302. outs() << " count x86_FLOAT_STATE_COUNT\n";
  8303. else
  8304. outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
  8305. struct MachO::x86_float_state_t fs;
  8306. left = end - begin;
  8307. if (left >= sizeof(MachO::x86_float_state_t)) {
  8308. memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
  8309. begin += sizeof(MachO::x86_float_state_t);
  8310. } else {
  8311. memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
  8312. memcpy(&fs, begin, left);
  8313. begin += left;
  8314. }
  8315. if (isLittleEndian != sys::IsLittleEndianHost)
  8316. swapStruct(fs);
  8317. if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
  8318. outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
  8319. if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
  8320. outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
  8321. else
  8322. outs() << "fsh.count " << fs.fsh.count
  8323. << " (not x86_FLOAT_STATE64_COUNT\n";
  8324. Print_x86_float_state_t(fs.ufs.fs64);
  8325. } else {
  8326. outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
  8327. << fs.fsh.count << "\n";
  8328. }
  8329. } else if (flavor == MachO::x86_EXCEPTION_STATE) {
  8330. outs() << " flavor x86_EXCEPTION_STATE\n";
  8331. if (count == MachO::x86_EXCEPTION_STATE_COUNT)
  8332. outs() << " count x86_EXCEPTION_STATE_COUNT\n";
  8333. else
  8334. outs() << " count " << count
  8335. << " (not x86_EXCEPTION_STATE_COUNT)\n";
  8336. struct MachO::x86_exception_state_t es;
  8337. left = end - begin;
  8338. if (left >= sizeof(MachO::x86_exception_state_t)) {
  8339. memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
  8340. begin += sizeof(MachO::x86_exception_state_t);
  8341. } else {
  8342. memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
  8343. memcpy(&es, begin, left);
  8344. begin += left;
  8345. }
  8346. if (isLittleEndian != sys::IsLittleEndianHost)
  8347. swapStruct(es);
  8348. if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
  8349. outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
  8350. if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
  8351. outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
  8352. else
  8353. outs() << "\t esh.count " << es.esh.count
  8354. << " (not x86_EXCEPTION_STATE64_COUNT\n";
  8355. Print_x86_exception_state_t(es.ues.es64);
  8356. } else {
  8357. outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
  8358. << es.esh.count << "\n";
  8359. }
  8360. } else {
  8361. outs() << " flavor " << flavor << " (unknown)\n";
  8362. outs() << " count " << count << "\n";
  8363. outs() << " state (unknown)\n";
  8364. begin += count * sizeof(uint32_t);
  8365. }
  8366. }
  8367. } else if (cputype == MachO::CPU_TYPE_ARM) {
  8368. while (begin < end) {
  8369. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8370. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  8371. begin += sizeof(uint32_t);
  8372. } else {
  8373. flavor = 0;
  8374. begin = end;
  8375. }
  8376. if (isLittleEndian != sys::IsLittleEndianHost)
  8377. sys::swapByteOrder(flavor);
  8378. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8379. memcpy((char *)&count, begin, sizeof(uint32_t));
  8380. begin += sizeof(uint32_t);
  8381. } else {
  8382. count = 0;
  8383. begin = end;
  8384. }
  8385. if (isLittleEndian != sys::IsLittleEndianHost)
  8386. sys::swapByteOrder(count);
  8387. if (flavor == MachO::ARM_THREAD_STATE) {
  8388. outs() << " flavor ARM_THREAD_STATE\n";
  8389. if (count == MachO::ARM_THREAD_STATE_COUNT)
  8390. outs() << " count ARM_THREAD_STATE_COUNT\n";
  8391. else
  8392. outs() << " count " << count
  8393. << " (not ARM_THREAD_STATE_COUNT)\n";
  8394. MachO::arm_thread_state32_t cpu32;
  8395. left = end - begin;
  8396. if (left >= sizeof(MachO::arm_thread_state32_t)) {
  8397. memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
  8398. begin += sizeof(MachO::arm_thread_state32_t);
  8399. } else {
  8400. memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
  8401. memcpy(&cpu32, begin, left);
  8402. begin += left;
  8403. }
  8404. if (isLittleEndian != sys::IsLittleEndianHost)
  8405. swapStruct(cpu32);
  8406. Print_arm_thread_state32_t(cpu32);
  8407. } else {
  8408. outs() << " flavor " << flavor << " (unknown)\n";
  8409. outs() << " count " << count << "\n";
  8410. outs() << " state (unknown)\n";
  8411. begin += count * sizeof(uint32_t);
  8412. }
  8413. }
  8414. } else if (cputype == MachO::CPU_TYPE_ARM64) {
  8415. while (begin < end) {
  8416. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8417. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  8418. begin += sizeof(uint32_t);
  8419. } else {
  8420. flavor = 0;
  8421. begin = end;
  8422. }
  8423. if (isLittleEndian != sys::IsLittleEndianHost)
  8424. sys::swapByteOrder(flavor);
  8425. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8426. memcpy((char *)&count, begin, sizeof(uint32_t));
  8427. begin += sizeof(uint32_t);
  8428. } else {
  8429. count = 0;
  8430. begin = end;
  8431. }
  8432. if (isLittleEndian != sys::IsLittleEndianHost)
  8433. sys::swapByteOrder(count);
  8434. if (flavor == MachO::ARM_THREAD_STATE64) {
  8435. outs() << " flavor ARM_THREAD_STATE64\n";
  8436. if (count == MachO::ARM_THREAD_STATE64_COUNT)
  8437. outs() << " count ARM_THREAD_STATE64_COUNT\n";
  8438. else
  8439. outs() << " count " << count
  8440. << " (not ARM_THREAD_STATE64_COUNT)\n";
  8441. MachO::arm_thread_state64_t cpu64;
  8442. left = end - begin;
  8443. if (left >= sizeof(MachO::arm_thread_state64_t)) {
  8444. memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
  8445. begin += sizeof(MachO::arm_thread_state64_t);
  8446. } else {
  8447. memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
  8448. memcpy(&cpu64, begin, left);
  8449. begin += left;
  8450. }
  8451. if (isLittleEndian != sys::IsLittleEndianHost)
  8452. swapStruct(cpu64);
  8453. Print_arm_thread_state64_t(cpu64);
  8454. } else {
  8455. outs() << " flavor " << flavor << " (unknown)\n";
  8456. outs() << " count " << count << "\n";
  8457. outs() << " state (unknown)\n";
  8458. begin += count * sizeof(uint32_t);
  8459. }
  8460. }
  8461. } else {
  8462. while (begin < end) {
  8463. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8464. memcpy((char *)&flavor, begin, sizeof(uint32_t));
  8465. begin += sizeof(uint32_t);
  8466. } else {
  8467. flavor = 0;
  8468. begin = end;
  8469. }
  8470. if (isLittleEndian != sys::IsLittleEndianHost)
  8471. sys::swapByteOrder(flavor);
  8472. if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
  8473. memcpy((char *)&count, begin, sizeof(uint32_t));
  8474. begin += sizeof(uint32_t);
  8475. } else {
  8476. count = 0;
  8477. begin = end;
  8478. }
  8479. if (isLittleEndian != sys::IsLittleEndianHost)
  8480. sys::swapByteOrder(count);
  8481. outs() << " flavor " << flavor << "\n";
  8482. outs() << " count " << count << "\n";
  8483. outs() << " state (Unknown cputype/cpusubtype)\n";
  8484. begin += count * sizeof(uint32_t);
  8485. }
  8486. }
  8487. }
  8488. static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
  8489. if (dl.cmd == MachO::LC_ID_DYLIB)
  8490. outs() << " cmd LC_ID_DYLIB\n";
  8491. else if (dl.cmd == MachO::LC_LOAD_DYLIB)
  8492. outs() << " cmd LC_LOAD_DYLIB\n";
  8493. else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
  8494. outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
  8495. else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
  8496. outs() << " cmd LC_REEXPORT_DYLIB\n";
  8497. else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
  8498. outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
  8499. else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
  8500. outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
  8501. else
  8502. outs() << " cmd " << dl.cmd << " (unknown)\n";
  8503. outs() << " cmdsize " << dl.cmdsize;
  8504. if (dl.cmdsize < sizeof(struct MachO::dylib_command))
  8505. outs() << " Incorrect size\n";
  8506. else
  8507. outs() << "\n";
  8508. if (dl.dylib.name < dl.cmdsize) {
  8509. const char *P = (const char *)(Ptr) + dl.dylib.name;
  8510. outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
  8511. } else {
  8512. outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
  8513. }
  8514. outs() << " time stamp " << dl.dylib.timestamp << " ";
  8515. time_t t = dl.dylib.timestamp;
  8516. outs() << ctime(&t);
  8517. outs() << " current version ";
  8518. if (dl.dylib.current_version == 0xffffffff)
  8519. outs() << "n/a\n";
  8520. else
  8521. outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
  8522. << ((dl.dylib.current_version >> 8) & 0xff) << "."
  8523. << (dl.dylib.current_version & 0xff) << "\n";
  8524. outs() << "compatibility version ";
  8525. if (dl.dylib.compatibility_version == 0xffffffff)
  8526. outs() << "n/a\n";
  8527. else
  8528. outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
  8529. << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
  8530. << (dl.dylib.compatibility_version & 0xff) << "\n";
  8531. }
  8532. static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
  8533. uint32_t object_size) {
  8534. if (ld.cmd == MachO::LC_CODE_SIGNATURE)
  8535. outs() << " cmd LC_CODE_SIGNATURE\n";
  8536. else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
  8537. outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
  8538. else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
  8539. outs() << " cmd LC_FUNCTION_STARTS\n";
  8540. else if (ld.cmd == MachO::LC_DATA_IN_CODE)
  8541. outs() << " cmd LC_DATA_IN_CODE\n";
  8542. else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
  8543. outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
  8544. else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
  8545. outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
  8546. else
  8547. outs() << " cmd " << ld.cmd << " (?)\n";
  8548. outs() << " cmdsize " << ld.cmdsize;
  8549. if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
  8550. outs() << " Incorrect size\n";
  8551. else
  8552. outs() << "\n";
  8553. outs() << " dataoff " << ld.dataoff;
  8554. if (ld.dataoff > object_size)
  8555. outs() << " (past end of file)\n";
  8556. else
  8557. outs() << "\n";
  8558. outs() << " datasize " << ld.datasize;
  8559. uint64_t big_size = ld.dataoff;
  8560. big_size += ld.datasize;
  8561. if (big_size > object_size)
  8562. outs() << " (past end of file)\n";
  8563. else
  8564. outs() << "\n";
  8565. }
  8566. static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
  8567. uint32_t cputype, bool verbose) {
  8568. StringRef Buf = Obj->getData();
  8569. unsigned Index = 0;
  8570. for (const auto &Command : Obj->load_commands()) {
  8571. outs() << "Load command " << Index++ << "\n";
  8572. if (Command.C.cmd == MachO::LC_SEGMENT) {
  8573. MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
  8574. const char *sg_segname = SLC.segname;
  8575. PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
  8576. SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
  8577. SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
  8578. verbose);
  8579. for (unsigned j = 0; j < SLC.nsects; j++) {
  8580. MachO::section S = Obj->getSection(Command, j);
  8581. PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
  8582. S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
  8583. SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
  8584. }
  8585. } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
  8586. MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
  8587. const char *sg_segname = SLC_64.segname;
  8588. PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
  8589. SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
  8590. SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
  8591. SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
  8592. for (unsigned j = 0; j < SLC_64.nsects; j++) {
  8593. MachO::section_64 S_64 = Obj->getSection64(Command, j);
  8594. PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
  8595. S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
  8596. S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
  8597. sg_segname, filetype, Buf.size(), verbose);
  8598. }
  8599. } else if (Command.C.cmd == MachO::LC_SYMTAB) {
  8600. MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
  8601. PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
  8602. } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
  8603. MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
  8604. MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
  8605. PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
  8606. Obj->is64Bit());
  8607. } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
  8608. Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
  8609. MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
  8610. PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
  8611. } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
  8612. Command.C.cmd == MachO::LC_ID_DYLINKER ||
  8613. Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
  8614. MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
  8615. PrintDyldLoadCommand(Dyld, Command.Ptr);
  8616. } else if (Command.C.cmd == MachO::LC_UUID) {
  8617. MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
  8618. PrintUuidLoadCommand(Uuid);
  8619. } else if (Command.C.cmd == MachO::LC_RPATH) {
  8620. MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
  8621. PrintRpathLoadCommand(Rpath, Command.Ptr);
  8622. } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
  8623. Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
  8624. Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
  8625. Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
  8626. MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
  8627. PrintVersionMinLoadCommand(Vd);
  8628. } else if (Command.C.cmd == MachO::LC_NOTE) {
  8629. MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
  8630. PrintNoteLoadCommand(Nt);
  8631. } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
  8632. MachO::build_version_command Bv =
  8633. Obj->getBuildVersionLoadCommand(Command);
  8634. PrintBuildVersionLoadCommand(Obj, Bv);
  8635. } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
  8636. MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
  8637. PrintSourceVersionCommand(Sd);
  8638. } else if (Command.C.cmd == MachO::LC_MAIN) {
  8639. MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
  8640. PrintEntryPointCommand(Ep);
  8641. } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
  8642. MachO::encryption_info_command Ei =
  8643. Obj->getEncryptionInfoCommand(Command);
  8644. PrintEncryptionInfoCommand(Ei, Buf.size());
  8645. } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
  8646. MachO::encryption_info_command_64 Ei =
  8647. Obj->getEncryptionInfoCommand64(Command);
  8648. PrintEncryptionInfoCommand64(Ei, Buf.size());
  8649. } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
  8650. MachO::linker_option_command Lo =
  8651. Obj->getLinkerOptionLoadCommand(Command);
  8652. PrintLinkerOptionCommand(Lo, Command.Ptr);
  8653. } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
  8654. MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
  8655. PrintSubFrameworkCommand(Sf, Command.Ptr);
  8656. } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
  8657. MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
  8658. PrintSubUmbrellaCommand(Sf, Command.Ptr);
  8659. } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
  8660. MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
  8661. PrintSubLibraryCommand(Sl, Command.Ptr);
  8662. } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
  8663. MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
  8664. PrintSubClientCommand(Sc, Command.Ptr);
  8665. } else if (Command.C.cmd == MachO::LC_ROUTINES) {
  8666. MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
  8667. PrintRoutinesCommand(Rc);
  8668. } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
  8669. MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
  8670. PrintRoutinesCommand64(Rc);
  8671. } else if (Command.C.cmd == MachO::LC_THREAD ||
  8672. Command.C.cmd == MachO::LC_UNIXTHREAD) {
  8673. MachO::thread_command Tc = Obj->getThreadCommand(Command);
  8674. PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
  8675. } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
  8676. Command.C.cmd == MachO::LC_ID_DYLIB ||
  8677. Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
  8678. Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
  8679. Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
  8680. Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
  8681. MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
  8682. PrintDylibCommand(Dl, Command.Ptr);
  8683. } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
  8684. Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
  8685. Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
  8686. Command.C.cmd == MachO::LC_DATA_IN_CODE ||
  8687. Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
  8688. Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
  8689. MachO::linkedit_data_command Ld =
  8690. Obj->getLinkeditDataLoadCommand(Command);
  8691. PrintLinkEditDataCommand(Ld, Buf.size());
  8692. } else {
  8693. outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
  8694. << ")\n";
  8695. outs() << " cmdsize " << Command.C.cmdsize << "\n";
  8696. // TODO: get and print the raw bytes of the load command.
  8697. }
  8698. // TODO: print all the other kinds of load commands.
  8699. }
  8700. }
  8701. static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
  8702. if (Obj->is64Bit()) {
  8703. MachO::mach_header_64 H_64;
  8704. H_64 = Obj->getHeader64();
  8705. PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
  8706. H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
  8707. } else {
  8708. MachO::mach_header H;
  8709. H = Obj->getHeader();
  8710. PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
  8711. H.sizeofcmds, H.flags, verbose);
  8712. }
  8713. }
  8714. void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
  8715. const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
  8716. PrintMachHeader(file, !NonVerbose);
  8717. }
  8718. void llvm::printMachOLoadCommands(const object::ObjectFile *Obj) {
  8719. const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
  8720. uint32_t filetype = 0;
  8721. uint32_t cputype = 0;
  8722. if (file->is64Bit()) {
  8723. MachO::mach_header_64 H_64;
  8724. H_64 = file->getHeader64();
  8725. filetype = H_64.filetype;
  8726. cputype = H_64.cputype;
  8727. } else {
  8728. MachO::mach_header H;
  8729. H = file->getHeader();
  8730. filetype = H.filetype;
  8731. cputype = H.cputype;
  8732. }
  8733. PrintLoadCommands(file, filetype, cputype, !NonVerbose);
  8734. }
  8735. //===----------------------------------------------------------------------===//
  8736. // export trie dumping
  8737. //===----------------------------------------------------------------------===//
  8738. void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
  8739. uint64_t BaseSegmentAddress = 0;
  8740. for (const auto &Command : Obj->load_commands()) {
  8741. if (Command.C.cmd == MachO::LC_SEGMENT) {
  8742. MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
  8743. if (Seg.fileoff == 0 && Seg.filesize != 0) {
  8744. BaseSegmentAddress = Seg.vmaddr;
  8745. break;
  8746. }
  8747. } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
  8748. MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
  8749. if (Seg.fileoff == 0 && Seg.filesize != 0) {
  8750. BaseSegmentAddress = Seg.vmaddr;
  8751. break;
  8752. }
  8753. }
  8754. }
  8755. Error Err = Error::success();
  8756. for (const llvm::object::ExportEntry &Entry : Obj->exports(Err)) {
  8757. uint64_t Flags = Entry.flags();
  8758. bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
  8759. bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
  8760. bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
  8761. MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
  8762. bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
  8763. MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
  8764. bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
  8765. if (ReExport)
  8766. outs() << "[re-export] ";
  8767. else
  8768. outs() << format("0x%08llX ",
  8769. Entry.address() + BaseSegmentAddress);
  8770. outs() << Entry.name();
  8771. if (WeakDef || ThreadLocal || Resolver || Abs) {
  8772. bool NeedsComma = false;
  8773. outs() << " [";
  8774. if (WeakDef) {
  8775. outs() << "weak_def";
  8776. NeedsComma = true;
  8777. }
  8778. if (ThreadLocal) {
  8779. if (NeedsComma)
  8780. outs() << ", ";
  8781. outs() << "per-thread";
  8782. NeedsComma = true;
  8783. }
  8784. if (Abs) {
  8785. if (NeedsComma)
  8786. outs() << ", ";
  8787. outs() << "absolute";
  8788. NeedsComma = true;
  8789. }
  8790. if (Resolver) {
  8791. if (NeedsComma)
  8792. outs() << ", ";
  8793. outs() << format("resolver=0x%08llX", Entry.other());
  8794. NeedsComma = true;
  8795. }
  8796. outs() << "]";
  8797. }
  8798. if (ReExport) {
  8799. StringRef DylibName = "unknown";
  8800. int Ordinal = Entry.other() - 1;
  8801. Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
  8802. if (Entry.otherName().empty())
  8803. outs() << " (from " << DylibName << ")";
  8804. else
  8805. outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
  8806. }
  8807. outs() << "\n";
  8808. }
  8809. if (Err)
  8810. report_error(Obj->getFileName(), std::move(Err));
  8811. }
  8812. //===----------------------------------------------------------------------===//
  8813. // rebase table dumping
  8814. //===----------------------------------------------------------------------===//
  8815. void llvm::printMachORebaseTable(object::MachOObjectFile *Obj) {
  8816. outs() << "segment section address type\n";
  8817. Error Err = Error::success();
  8818. for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
  8819. StringRef SegmentName = Entry.segmentName();
  8820. StringRef SectionName = Entry.sectionName();
  8821. uint64_t Address = Entry.address();
  8822. // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
  8823. outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
  8824. SegmentName.str().c_str(), SectionName.str().c_str(),
  8825. Address, Entry.typeName().str().c_str());
  8826. }
  8827. if (Err)
  8828. report_error(Obj->getFileName(), std::move(Err));
  8829. }
  8830. static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
  8831. StringRef DylibName;
  8832. switch (Ordinal) {
  8833. case MachO::BIND_SPECIAL_DYLIB_SELF:
  8834. return "this-image";
  8835. case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
  8836. return "main-executable";
  8837. case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
  8838. return "flat-namespace";
  8839. default:
  8840. if (Ordinal > 0) {
  8841. std::error_code EC =
  8842. Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
  8843. if (EC)
  8844. return "<<bad library ordinal>>";
  8845. return DylibName;
  8846. }
  8847. }
  8848. return "<<unknown special ordinal>>";
  8849. }
  8850. //===----------------------------------------------------------------------===//
  8851. // bind table dumping
  8852. //===----------------------------------------------------------------------===//
  8853. void llvm::printMachOBindTable(object::MachOObjectFile *Obj) {
  8854. // Build table of sections so names can used in final output.
  8855. outs() << "segment section address type "
  8856. "addend dylib symbol\n";
  8857. Error Err = Error::success();
  8858. for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
  8859. StringRef SegmentName = Entry.segmentName();
  8860. StringRef SectionName = Entry.sectionName();
  8861. uint64_t Address = Entry.address();
  8862. // Table lines look like:
  8863. // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
  8864. StringRef Attr;
  8865. if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
  8866. Attr = " (weak_import)";
  8867. outs() << left_justify(SegmentName, 8) << " "
  8868. << left_justify(SectionName, 18) << " "
  8869. << format_hex(Address, 10, true) << " "
  8870. << left_justify(Entry.typeName(), 8) << " "
  8871. << format_decimal(Entry.addend(), 8) << " "
  8872. << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
  8873. << Entry.symbolName() << Attr << "\n";
  8874. }
  8875. if (Err)
  8876. report_error(Obj->getFileName(), std::move(Err));
  8877. }
  8878. //===----------------------------------------------------------------------===//
  8879. // lazy bind table dumping
  8880. //===----------------------------------------------------------------------===//
  8881. void llvm::printMachOLazyBindTable(object::MachOObjectFile *Obj) {
  8882. outs() << "segment section address "
  8883. "dylib symbol\n";
  8884. Error Err = Error::success();
  8885. for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
  8886. StringRef SegmentName = Entry.segmentName();
  8887. StringRef SectionName = Entry.sectionName();
  8888. uint64_t Address = Entry.address();
  8889. // Table lines look like:
  8890. // __DATA __got 0x00012010 libSystem ___stack_chk_guard
  8891. outs() << left_justify(SegmentName, 8) << " "
  8892. << left_justify(SectionName, 18) << " "
  8893. << format_hex(Address, 10, true) << " "
  8894. << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
  8895. << Entry.symbolName() << "\n";
  8896. }
  8897. if (Err)
  8898. report_error(Obj->getFileName(), std::move(Err));
  8899. }
  8900. //===----------------------------------------------------------------------===//
  8901. // weak bind table dumping
  8902. //===----------------------------------------------------------------------===//
  8903. void llvm::printMachOWeakBindTable(object::MachOObjectFile *Obj) {
  8904. outs() << "segment section address "
  8905. "type addend symbol\n";
  8906. Error Err = Error::success();
  8907. for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
  8908. // Strong symbols don't have a location to update.
  8909. if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
  8910. outs() << " strong "
  8911. << Entry.symbolName() << "\n";
  8912. continue;
  8913. }
  8914. StringRef SegmentName = Entry.segmentName();
  8915. StringRef SectionName = Entry.sectionName();
  8916. uint64_t Address = Entry.address();
  8917. // Table lines look like:
  8918. // __DATA __data 0x00001000 pointer 0 _foo
  8919. outs() << left_justify(SegmentName, 8) << " "
  8920. << left_justify(SectionName, 18) << " "
  8921. << format_hex(Address, 10, true) << " "
  8922. << left_justify(Entry.typeName(), 8) << " "
  8923. << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
  8924. << "\n";
  8925. }
  8926. if (Err)
  8927. report_error(Obj->getFileName(), std::move(Err));
  8928. }
  8929. // get_dyld_bind_info_symbolname() is used for disassembly and passed an
  8930. // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
  8931. // information for that address. If the address is found its binding symbol
  8932. // name is returned. If not nullptr is returned.
  8933. static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
  8934. struct DisassembleInfo *info) {
  8935. if (info->bindtable == nullptr) {
  8936. info->bindtable = llvm::make_unique<SymbolAddressMap>();
  8937. Error Err = Error::success();
  8938. for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
  8939. uint64_t Address = Entry.address();
  8940. StringRef name = Entry.symbolName();
  8941. if (!name.empty())
  8942. (*info->bindtable)[Address] = name;
  8943. }
  8944. if (Err)
  8945. report_error(info->O->getFileName(), std::move(Err));
  8946. }
  8947. auto name = info->bindtable->lookup(ReferenceValue);
  8948. return !name.empty() ? name.data() : nullptr;
  8949. }