mach-o.c 168 KB

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