debug.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372
  1. /* debug.c -- Handle generic debugging information.
  2. Copyright (C) 1995-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <ian@cygnus.com>.
  4. This file is part of GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. /* This file implements a generic debugging format. We may eventually
  18. have readers which convert different formats into this generic
  19. format, and writers which write it out. The initial impetus for
  20. this was writing a converter from stabs to HP IEEE-695 debugging
  21. format. */
  22. #include "sysdep.h"
  23. #include <assert.h>
  24. #include "bfd.h"
  25. #include "libiberty.h"
  26. #include "filenames.h"
  27. #include "debug.h"
  28. /* Global information we keep for debugging. A pointer to this
  29. structure is the debugging handle passed to all the routines. */
  30. struct debug_handle
  31. {
  32. /* A linked list of compilation units. */
  33. struct debug_unit *units;
  34. /* The current compilation unit. */
  35. struct debug_unit *current_unit;
  36. /* The current source file. */
  37. struct debug_file *current_file;
  38. /* The current function. */
  39. struct debug_function *current_function;
  40. /* The current block. */
  41. struct debug_block *current_block;
  42. /* The current line number information for the current unit. */
  43. struct debug_lineno *current_lineno;
  44. /* Mark. This is used by debug_write. */
  45. unsigned int mark;
  46. /* A struct/class ID used by debug_write. */
  47. unsigned int class_id;
  48. /* The base for class_id for this call to debug_write. */
  49. unsigned int base_id;
  50. /* The current line number in debug_write. */
  51. struct debug_lineno *current_write_lineno;
  52. unsigned int current_write_lineno_index;
  53. /* A list of classes which have assigned ID's during debug_write.
  54. This is linked through the next_id field of debug_class_type. */
  55. struct debug_class_id *id_list;
  56. /* A list used to avoid recursion during debug_type_samep. */
  57. struct debug_type_compare_list *compare_list;
  58. };
  59. /* Information we keep for a single compilation unit. */
  60. struct debug_unit
  61. {
  62. /* The next compilation unit. */
  63. struct debug_unit *next;
  64. /* A list of files included in this compilation unit. The first
  65. file is always the main one, and that is where the main file name
  66. is stored. */
  67. struct debug_file *files;
  68. /* Line number information for this compilation unit. This is not
  69. stored by function, because assembler code may have line number
  70. information without function information. */
  71. struct debug_lineno *linenos;
  72. };
  73. /* Information kept for a single source file. */
  74. struct debug_file
  75. {
  76. /* The next source file in this compilation unit. */
  77. struct debug_file *next;
  78. /* The name of the source file. */
  79. const char *filename;
  80. /* Global functions, variables, types, etc. */
  81. struct debug_namespace *globals;
  82. };
  83. /* A type. */
  84. struct debug_type_s
  85. {
  86. /* Kind of type. */
  87. enum debug_type_kind kind;
  88. /* Size of type (0 if not known). */
  89. unsigned int size;
  90. /* Type which is a pointer to this type. */
  91. debug_type pointer;
  92. /* Tagged union with additional information about the type. */
  93. union
  94. {
  95. /* DEBUG_KIND_INDIRECT. */
  96. struct debug_indirect_type *kindirect;
  97. /* DEBUG_KIND_INT. */
  98. /* Whether the integer is unsigned. */
  99. bfd_boolean kint;
  100. /* DEBUG_KIND_STRUCT, DEBUG_KIND_UNION, DEBUG_KIND_CLASS,
  101. DEBUG_KIND_UNION_CLASS. */
  102. struct debug_class_type *kclass;
  103. /* DEBUG_KIND_ENUM. */
  104. struct debug_enum_type *kenum;
  105. /* DEBUG_KIND_POINTER. */
  106. struct debug_type_s *kpointer;
  107. /* DEBUG_KIND_FUNCTION. */
  108. struct debug_function_type *kfunction;
  109. /* DEBUG_KIND_REFERENCE. */
  110. struct debug_type_s *kreference;
  111. /* DEBUG_KIND_RANGE. */
  112. struct debug_range_type *krange;
  113. /* DEBUG_KIND_ARRAY. */
  114. struct debug_array_type *karray;
  115. /* DEBUG_KIND_SET. */
  116. struct debug_set_type *kset;
  117. /* DEBUG_KIND_OFFSET. */
  118. struct debug_offset_type *koffset;
  119. /* DEBUG_KIND_METHOD. */
  120. struct debug_method_type *kmethod;
  121. /* DEBUG_KIND_CONST. */
  122. struct debug_type_s *kconst;
  123. /* DEBUG_KIND_VOLATILE. */
  124. struct debug_type_s *kvolatile;
  125. /* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED. */
  126. struct debug_named_type *knamed;
  127. } u;
  128. };
  129. /* Information kept for an indirect type. */
  130. struct debug_indirect_type
  131. {
  132. /* Slot where the final type will appear. */
  133. debug_type *slot;
  134. /* Tag. */
  135. const char *tag;
  136. };
  137. /* Information kept for a struct, union, or class. */
  138. struct debug_class_type
  139. {
  140. /* NULL terminated array of fields. */
  141. debug_field *fields;
  142. /* A mark field which indicates whether the struct has already been
  143. printed. */
  144. unsigned int mark;
  145. /* This is used to uniquely identify unnamed structs when printing. */
  146. unsigned int id;
  147. /* The remaining fields are only used for DEBUG_KIND_CLASS and
  148. DEBUG_KIND_UNION_CLASS. */
  149. /* NULL terminated array of base classes. */
  150. debug_baseclass *baseclasses;
  151. /* NULL terminated array of methods. */
  152. debug_method *methods;
  153. /* The type of the class providing the virtual function table for
  154. this class. This may point to the type itself. */
  155. debug_type vptrbase;
  156. };
  157. /* Information kept for an enum. */
  158. struct debug_enum_type
  159. {
  160. /* NULL terminated array of names. */
  161. const char **names;
  162. /* Array of corresponding values. */
  163. bfd_signed_vma *values;
  164. };
  165. /* Information kept for a function. FIXME: We should be able to
  166. record the parameter types. */
  167. struct debug_function_type
  168. {
  169. /* Return type. */
  170. debug_type return_type;
  171. /* NULL terminated array of argument types. */
  172. debug_type *arg_types;
  173. /* Whether the function takes a variable number of arguments. */
  174. bfd_boolean varargs;
  175. };
  176. /* Information kept for a range. */
  177. struct debug_range_type
  178. {
  179. /* Range base type. */
  180. debug_type type;
  181. /* Lower bound. */
  182. bfd_signed_vma lower;
  183. /* Upper bound. */
  184. bfd_signed_vma upper;
  185. };
  186. /* Information kept for an array. */
  187. struct debug_array_type
  188. {
  189. /* Element type. */
  190. debug_type element_type;
  191. /* Range type. */
  192. debug_type range_type;
  193. /* Lower bound. */
  194. bfd_signed_vma lower;
  195. /* Upper bound. */
  196. bfd_signed_vma upper;
  197. /* Whether this array is really a string. */
  198. bfd_boolean stringp;
  199. };
  200. /* Information kept for a set. */
  201. struct debug_set_type
  202. {
  203. /* Base type. */
  204. debug_type type;
  205. /* Whether this set is really a bitstring. */
  206. bfd_boolean bitstringp;
  207. };
  208. /* Information kept for an offset type (a based pointer). */
  209. struct debug_offset_type
  210. {
  211. /* The type the pointer is an offset from. */
  212. debug_type base_type;
  213. /* The type the pointer points to. */
  214. debug_type target_type;
  215. };
  216. /* Information kept for a method type. */
  217. struct debug_method_type
  218. {
  219. /* The return type. */
  220. debug_type return_type;
  221. /* The object type which this method is for. */
  222. debug_type domain_type;
  223. /* A NULL terminated array of argument types. */
  224. debug_type *arg_types;
  225. /* Whether the method takes a variable number of arguments. */
  226. bfd_boolean varargs;
  227. };
  228. /* Information kept for a named type. */
  229. struct debug_named_type
  230. {
  231. /* Name. */
  232. struct debug_name *name;
  233. /* Real type. */
  234. debug_type type;
  235. };
  236. /* A field in a struct or union. */
  237. struct debug_field_s
  238. {
  239. /* Name of the field. */
  240. const char *name;
  241. /* Type of the field. */
  242. struct debug_type_s *type;
  243. /* Visibility of the field. */
  244. enum debug_visibility visibility;
  245. /* Whether this is a static member. */
  246. bfd_boolean static_member;
  247. union
  248. {
  249. /* If static_member is false. */
  250. struct
  251. {
  252. /* Bit position of the field in the struct. */
  253. unsigned int bitpos;
  254. /* Size of the field in bits. */
  255. unsigned int bitsize;
  256. } f;
  257. /* If static_member is true. */
  258. struct
  259. {
  260. const char *physname;
  261. } s;
  262. } u;
  263. };
  264. /* A base class for an object. */
  265. struct debug_baseclass_s
  266. {
  267. /* Type of the base class. */
  268. struct debug_type_s *type;
  269. /* Bit position of the base class in the object. */
  270. unsigned int bitpos;
  271. /* Whether the base class is virtual. */
  272. bfd_boolean is_virtual;
  273. /* Visibility of the base class. */
  274. enum debug_visibility visibility;
  275. };
  276. /* A method of an object. */
  277. struct debug_method_s
  278. {
  279. /* The name of the method. */
  280. const char *name;
  281. /* A NULL terminated array of different types of variants. */
  282. struct debug_method_variant_s **variants;
  283. };
  284. /* The variants of a method function of an object. These indicate
  285. which method to run. */
  286. struct debug_method_variant_s
  287. {
  288. /* The physical name of the function. */
  289. const char *physname;
  290. /* The type of the function. */
  291. struct debug_type_s *type;
  292. /* The visibility of the function. */
  293. enum debug_visibility visibility;
  294. /* Whether the function is const. */
  295. bfd_boolean constp;
  296. /* Whether the function is volatile. */
  297. bfd_boolean volatilep;
  298. /* The offset to the function in the virtual function table. */
  299. bfd_vma voffset;
  300. /* If voffset is VOFFSET_STATIC_METHOD, this is a static method. */
  301. #define VOFFSET_STATIC_METHOD ((bfd_vma) -1)
  302. /* Context of a virtual method function. */
  303. struct debug_type_s *context;
  304. };
  305. /* A variable. This is the information we keep for a variable object.
  306. This has no name; a name is associated with a variable in a
  307. debug_name structure. */
  308. struct debug_variable
  309. {
  310. /* Kind of variable. */
  311. enum debug_var_kind kind;
  312. /* Type. */
  313. debug_type type;
  314. /* Value. The interpretation of the value depends upon kind. */
  315. bfd_vma val;
  316. };
  317. /* A function. This has no name; a name is associated with a function
  318. in a debug_name structure. */
  319. struct debug_function
  320. {
  321. /* Return type. */
  322. debug_type return_type;
  323. /* Parameter information. */
  324. struct debug_parameter *parameters;
  325. /* Block information. The first structure on the list is the main
  326. block of the function, and describes function local variables. */
  327. struct debug_block *blocks;
  328. };
  329. /* A function parameter. */
  330. struct debug_parameter
  331. {
  332. /* Next parameter. */
  333. struct debug_parameter *next;
  334. /* Name. */
  335. const char *name;
  336. /* Type. */
  337. debug_type type;
  338. /* Kind. */
  339. enum debug_parm_kind kind;
  340. /* Value (meaning depends upon kind). */
  341. bfd_vma val;
  342. };
  343. /* A typed constant. */
  344. struct debug_typed_constant
  345. {
  346. /* Type. */
  347. debug_type type;
  348. /* Value. FIXME: We may eventually need to support non-integral
  349. values. */
  350. bfd_vma val;
  351. };
  352. /* Information about a block within a function. */
  353. struct debug_block
  354. {
  355. /* Next block with the same parent. */
  356. struct debug_block *next;
  357. /* Parent block. */
  358. struct debug_block *parent;
  359. /* List of child blocks. */
  360. struct debug_block *children;
  361. /* Start address of the block. */
  362. bfd_vma start;
  363. /* End address of the block. */
  364. bfd_vma end;
  365. /* Local variables. */
  366. struct debug_namespace *locals;
  367. };
  368. /* Line number information we keep for a compilation unit. FIXME:
  369. This structure is easy to create, but can be very space
  370. inefficient. */
  371. struct debug_lineno
  372. {
  373. /* More line number information for this block. */
  374. struct debug_lineno *next;
  375. /* Source file. */
  376. struct debug_file *file;
  377. /* Line numbers, terminated by a -1 or the end of the array. */
  378. #define DEBUG_LINENO_COUNT 10
  379. unsigned long linenos[DEBUG_LINENO_COUNT];
  380. /* Addresses for the line numbers. */
  381. bfd_vma addrs[DEBUG_LINENO_COUNT];
  382. };
  383. /* A namespace. This is a mapping from names to objects. FIXME: This
  384. should be implemented as a hash table. */
  385. struct debug_namespace
  386. {
  387. /* List of items in this namespace. */
  388. struct debug_name *list;
  389. /* Pointer to where the next item in this namespace should go. */
  390. struct debug_name **tail;
  391. };
  392. /* Kinds of objects that appear in a namespace. */
  393. enum debug_object_kind
  394. {
  395. /* A type. */
  396. DEBUG_OBJECT_TYPE,
  397. /* A tagged type (really a different sort of namespace). */
  398. DEBUG_OBJECT_TAG,
  399. /* A variable. */
  400. DEBUG_OBJECT_VARIABLE,
  401. /* A function. */
  402. DEBUG_OBJECT_FUNCTION,
  403. /* An integer constant. */
  404. DEBUG_OBJECT_INT_CONSTANT,
  405. /* A floating point constant. */
  406. DEBUG_OBJECT_FLOAT_CONSTANT,
  407. /* A typed constant. */
  408. DEBUG_OBJECT_TYPED_CONSTANT
  409. };
  410. /* Linkage of an object that appears in a namespace. */
  411. enum debug_object_linkage
  412. {
  413. /* Local variable. */
  414. DEBUG_LINKAGE_AUTOMATIC,
  415. /* Static--either file static or function static, depending upon the
  416. namespace is. */
  417. DEBUG_LINKAGE_STATIC,
  418. /* Global. */
  419. DEBUG_LINKAGE_GLOBAL,
  420. /* No linkage. */
  421. DEBUG_LINKAGE_NONE
  422. };
  423. /* A name in a namespace. */
  424. struct debug_name
  425. {
  426. /* Next name in this namespace. */
  427. struct debug_name *next;
  428. /* Name. */
  429. const char *name;
  430. /* Mark. This is used by debug_write. */
  431. unsigned int mark;
  432. /* Kind of object. */
  433. enum debug_object_kind kind;
  434. /* Linkage of object. */
  435. enum debug_object_linkage linkage;
  436. /* Tagged union with additional information about the object. */
  437. union
  438. {
  439. /* DEBUG_OBJECT_TYPE. */
  440. struct debug_type_s *type;
  441. /* DEBUG_OBJECT_TAG. */
  442. struct debug_type_s *tag;
  443. /* DEBUG_OBJECT_VARIABLE. */
  444. struct debug_variable *variable;
  445. /* DEBUG_OBJECT_FUNCTION. */
  446. struct debug_function *function;
  447. /* DEBUG_OBJECT_INT_CONSTANT. */
  448. bfd_vma int_constant;
  449. /* DEBUG_OBJECT_FLOAT_CONSTANT. */
  450. double float_constant;
  451. /* DEBUG_OBJECT_TYPED_CONSTANT. */
  452. struct debug_typed_constant *typed_constant;
  453. } u;
  454. };
  455. /* During debug_write, a linked list of these structures is used to
  456. keep track of ID numbers that have been assigned to classes. */
  457. struct debug_class_id
  458. {
  459. /* Next ID number. */
  460. struct debug_class_id *next;
  461. /* The type with the ID. */
  462. struct debug_type_s *type;
  463. /* The tag; NULL if no tag. */
  464. const char *tag;
  465. };
  466. /* During debug_type_samep, a linked list of these structures is kept
  467. on the stack to avoid infinite recursion. */
  468. struct debug_type_compare_list
  469. {
  470. /* Next type on list. */
  471. struct debug_type_compare_list *next;
  472. /* The types we are comparing. */
  473. struct debug_type_s *t1;
  474. struct debug_type_s *t2;
  475. };
  476. /* During debug_get_real_type, a linked list of these structures is
  477. kept on the stack to avoid infinite recursion. */
  478. struct debug_type_real_list
  479. {
  480. /* Next type on list. */
  481. struct debug_type_real_list *next;
  482. /* The type we are checking. */
  483. struct debug_type_s *t;
  484. };
  485. /* Local functions. */
  486. static void debug_error (const char *);
  487. static struct debug_name *debug_add_to_namespace
  488. (struct debug_handle *, struct debug_namespace **, const char *,
  489. enum debug_object_kind, enum debug_object_linkage);
  490. static struct debug_name *debug_add_to_current_namespace
  491. (struct debug_handle *, const char *, enum debug_object_kind,
  492. enum debug_object_linkage);
  493. static struct debug_type_s *debug_make_type
  494. (struct debug_handle *, enum debug_type_kind, unsigned int);
  495. static struct debug_type_s *debug_get_real_type
  496. (void *, debug_type, struct debug_type_real_list *);
  497. static bfd_boolean debug_write_name
  498. (struct debug_handle *, const struct debug_write_fns *, void *,
  499. struct debug_name *);
  500. static bfd_boolean debug_write_type
  501. (struct debug_handle *, const struct debug_write_fns *, void *,
  502. struct debug_type_s *, struct debug_name *);
  503. static bfd_boolean debug_write_class_type
  504. (struct debug_handle *, const struct debug_write_fns *, void *,
  505. struct debug_type_s *, const char *);
  506. static bfd_boolean debug_write_function
  507. (struct debug_handle *, const struct debug_write_fns *, void *,
  508. const char *, enum debug_object_linkage, struct debug_function *);
  509. static bfd_boolean debug_write_block
  510. (struct debug_handle *, const struct debug_write_fns *, void *,
  511. struct debug_block *);
  512. static bfd_boolean debug_write_linenos
  513. (struct debug_handle *, const struct debug_write_fns *, void *, bfd_vma);
  514. static bfd_boolean debug_set_class_id
  515. (struct debug_handle *, const char *, struct debug_type_s *);
  516. static bfd_boolean debug_type_samep
  517. (struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
  518. static bfd_boolean debug_class_type_samep
  519. (struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
  520. /* Issue an error message. */
  521. static void
  522. debug_error (const char *message)
  523. {
  524. fprintf (stderr, "%s\n", message);
  525. }
  526. /* Add an object to a namespace. */
  527. static struct debug_name *
  528. debug_add_to_namespace (struct debug_handle *info ATTRIBUTE_UNUSED,
  529. struct debug_namespace **nsp, const char *name,
  530. enum debug_object_kind kind,
  531. enum debug_object_linkage linkage)
  532. {
  533. struct debug_name *n;
  534. struct debug_namespace *ns;
  535. n = (struct debug_name *) xmalloc (sizeof *n);
  536. memset (n, 0, sizeof *n);
  537. n->name = name;
  538. n->kind = kind;
  539. n->linkage = linkage;
  540. ns = *nsp;
  541. if (ns == NULL)
  542. {
  543. ns = (struct debug_namespace *) xmalloc (sizeof *ns);
  544. memset (ns, 0, sizeof *ns);
  545. ns->tail = &ns->list;
  546. *nsp = ns;
  547. }
  548. *ns->tail = n;
  549. ns->tail = &n->next;
  550. return n;
  551. }
  552. /* Add an object to the current namespace. */
  553. static struct debug_name *
  554. debug_add_to_current_namespace (struct debug_handle *info, const char *name,
  555. enum debug_object_kind kind,
  556. enum debug_object_linkage linkage)
  557. {
  558. struct debug_namespace **nsp;
  559. if (info->current_unit == NULL
  560. || info->current_file == NULL)
  561. {
  562. debug_error (_("debug_add_to_current_namespace: no current file"));
  563. return NULL;
  564. }
  565. if (info->current_block != NULL)
  566. nsp = &info->current_block->locals;
  567. else
  568. nsp = &info->current_file->globals;
  569. return debug_add_to_namespace (info, nsp, name, kind, linkage);
  570. }
  571. /* Return a handle for debugging information. */
  572. void *
  573. debug_init (void)
  574. {
  575. struct debug_handle *ret;
  576. ret = (struct debug_handle *) xmalloc (sizeof *ret);
  577. memset (ret, 0, sizeof *ret);
  578. return (void *) ret;
  579. }
  580. /* Set the source filename. This implicitly starts a new compilation
  581. unit. */
  582. bfd_boolean
  583. debug_set_filename (void *handle, const char *name)
  584. {
  585. struct debug_handle *info = (struct debug_handle *) handle;
  586. struct debug_file *nfile;
  587. struct debug_unit *nunit;
  588. if (name == NULL)
  589. name = "";
  590. nfile = (struct debug_file *) xmalloc (sizeof *nfile);
  591. memset (nfile, 0, sizeof *nfile);
  592. nfile->filename = name;
  593. nunit = (struct debug_unit *) xmalloc (sizeof *nunit);
  594. memset (nunit, 0, sizeof *nunit);
  595. nunit->files = nfile;
  596. info->current_file = nfile;
  597. if (info->current_unit != NULL)
  598. info->current_unit->next = nunit;
  599. else
  600. {
  601. assert (info->units == NULL);
  602. info->units = nunit;
  603. }
  604. info->current_unit = nunit;
  605. info->current_function = NULL;
  606. info->current_block = NULL;
  607. info->current_lineno = NULL;
  608. return TRUE;
  609. }
  610. /* Change source files to the given file name. This is used for
  611. include files in a single compilation unit. */
  612. bfd_boolean
  613. debug_start_source (void *handle, const char *name)
  614. {
  615. struct debug_handle *info = (struct debug_handle *) handle;
  616. struct debug_file *f, **pf;
  617. if (name == NULL)
  618. name = "";
  619. if (info->current_unit == NULL)
  620. {
  621. debug_error (_("debug_start_source: no debug_set_filename call"));
  622. return FALSE;
  623. }
  624. for (f = info->current_unit->files; f != NULL; f = f->next)
  625. {
  626. if (filename_cmp (f->filename, name) == 0)
  627. {
  628. info->current_file = f;
  629. return TRUE;
  630. }
  631. }
  632. f = (struct debug_file *) xmalloc (sizeof *f);
  633. memset (f, 0, sizeof *f);
  634. f->filename = name;
  635. for (pf = &info->current_file->next;
  636. *pf != NULL;
  637. pf = &(*pf)->next)
  638. ;
  639. *pf = f;
  640. info->current_file = f;
  641. return TRUE;
  642. }
  643. /* Record a function definition. This implicitly starts a function
  644. block. The debug_type argument is the type of the return value.
  645. The boolean indicates whether the function is globally visible.
  646. The bfd_vma is the address of the start of the function. Currently
  647. the parameter types are specified by calls to
  648. debug_record_parameter. FIXME: There is no way to specify nested
  649. functions. */
  650. bfd_boolean
  651. debug_record_function (void *handle, const char *name,
  652. debug_type return_type, bfd_boolean global,
  653. bfd_vma addr)
  654. {
  655. struct debug_handle *info = (struct debug_handle *) handle;
  656. struct debug_function *f;
  657. struct debug_block *b;
  658. struct debug_name *n;
  659. if (name == NULL)
  660. name = "";
  661. if (return_type == NULL)
  662. return FALSE;
  663. if (info->current_unit == NULL)
  664. {
  665. debug_error (_("debug_record_function: no debug_set_filename call"));
  666. return FALSE;
  667. }
  668. f = (struct debug_function *) xmalloc (sizeof *f);
  669. memset (f, 0, sizeof *f);
  670. f->return_type = return_type;
  671. b = (struct debug_block *) xmalloc (sizeof *b);
  672. memset (b, 0, sizeof *b);
  673. b->start = addr;
  674. b->end = (bfd_vma) -1;
  675. f->blocks = b;
  676. info->current_function = f;
  677. info->current_block = b;
  678. /* FIXME: If we could handle nested functions, this would be the
  679. place: we would want to use a different namespace. */
  680. n = debug_add_to_namespace (info,
  681. &info->current_file->globals,
  682. name,
  683. DEBUG_OBJECT_FUNCTION,
  684. (global
  685. ? DEBUG_LINKAGE_GLOBAL
  686. : DEBUG_LINKAGE_STATIC));
  687. if (n == NULL)
  688. return FALSE;
  689. n->u.function = f;
  690. return TRUE;
  691. }
  692. /* Record a parameter for the current function. */
  693. bfd_boolean
  694. debug_record_parameter (void *handle, const char *name, debug_type type,
  695. enum debug_parm_kind kind, bfd_vma val)
  696. {
  697. struct debug_handle *info = (struct debug_handle *) handle;
  698. struct debug_parameter *p, **pp;
  699. if (name == NULL || type == NULL)
  700. return FALSE;
  701. if (info->current_unit == NULL
  702. || info->current_function == NULL)
  703. {
  704. debug_error (_("debug_record_parameter: no current function"));
  705. return FALSE;
  706. }
  707. p = (struct debug_parameter *) xmalloc (sizeof *p);
  708. memset (p, 0, sizeof *p);
  709. p->name = name;
  710. p->type = type;
  711. p->kind = kind;
  712. p->val = val;
  713. for (pp = &info->current_function->parameters;
  714. *pp != NULL;
  715. pp = &(*pp)->next)
  716. ;
  717. *pp = p;
  718. return TRUE;
  719. }
  720. /* End a function. FIXME: This should handle function nesting. */
  721. bfd_boolean
  722. debug_end_function (void *handle, bfd_vma addr)
  723. {
  724. struct debug_handle *info = (struct debug_handle *) handle;
  725. if (info->current_unit == NULL
  726. || info->current_block == NULL
  727. || info->current_function == NULL)
  728. {
  729. debug_error (_("debug_end_function: no current function"));
  730. return FALSE;
  731. }
  732. if (info->current_block->parent != NULL)
  733. {
  734. debug_error (_("debug_end_function: some blocks were not closed"));
  735. return FALSE;
  736. }
  737. info->current_block->end = addr;
  738. info->current_function = NULL;
  739. info->current_block = NULL;
  740. return TRUE;
  741. }
  742. /* Start a block in a function. All local information will be
  743. recorded in this block, until the matching call to debug_end_block.
  744. debug_start_block and debug_end_block may be nested. The bfd_vma
  745. argument is the address at which this block starts. */
  746. bfd_boolean
  747. debug_start_block (void *handle, bfd_vma addr)
  748. {
  749. struct debug_handle *info = (struct debug_handle *) handle;
  750. struct debug_block *b, **pb;
  751. /* We must always have a current block: debug_record_function sets
  752. one up. */
  753. if (info->current_unit == NULL
  754. || info->current_block == NULL)
  755. {
  756. debug_error (_("debug_start_block: no current block"));
  757. return FALSE;
  758. }
  759. b = (struct debug_block *) xmalloc (sizeof *b);
  760. memset (b, 0, sizeof *b);
  761. b->parent = info->current_block;
  762. b->start = addr;
  763. b->end = (bfd_vma) -1;
  764. /* This new block is a child of the current block. */
  765. for (pb = &info->current_block->children;
  766. *pb != NULL;
  767. pb = &(*pb)->next)
  768. ;
  769. *pb = b;
  770. info->current_block = b;
  771. return TRUE;
  772. }
  773. /* Finish a block in a function. This matches the call to
  774. debug_start_block. The argument is the address at which this block
  775. ends. */
  776. bfd_boolean
  777. debug_end_block (void *handle, bfd_vma addr)
  778. {
  779. struct debug_handle *info = (struct debug_handle *) handle;
  780. struct debug_block *parent;
  781. if (info->current_unit == NULL
  782. || info->current_block == NULL)
  783. {
  784. debug_error (_("debug_end_block: no current block"));
  785. return FALSE;
  786. }
  787. parent = info->current_block->parent;
  788. if (parent == NULL)
  789. {
  790. debug_error (_("debug_end_block: attempt to close top level block"));
  791. return FALSE;
  792. }
  793. info->current_block->end = addr;
  794. info->current_block = parent;
  795. return TRUE;
  796. }
  797. /* Associate a line number in the current source file and function
  798. with a given address. */
  799. bfd_boolean
  800. debug_record_line (void *handle, unsigned long lineno, bfd_vma addr)
  801. {
  802. struct debug_handle *info = (struct debug_handle *) handle;
  803. struct debug_lineno *l;
  804. unsigned int i;
  805. if (info->current_unit == NULL)
  806. {
  807. debug_error (_("debug_record_line: no current unit"));
  808. return FALSE;
  809. }
  810. l = info->current_lineno;
  811. if (l != NULL && l->file == info->current_file)
  812. {
  813. for (i = 0; i < DEBUG_LINENO_COUNT; i++)
  814. {
  815. if (l->linenos[i] == (unsigned long) -1)
  816. {
  817. l->linenos[i] = lineno;
  818. l->addrs[i] = addr;
  819. return TRUE;
  820. }
  821. }
  822. }
  823. /* If we get here, then either 1) there is no current_lineno
  824. structure, which means this is the first line number in this
  825. compilation unit, 2) the current_lineno structure is for a
  826. different file, or 3) the current_lineno structure is full.
  827. Regardless, we want to allocate a new debug_lineno structure, put
  828. it in the right place, and make it the new current_lineno
  829. structure. */
  830. l = (struct debug_lineno *) xmalloc (sizeof *l);
  831. memset (l, 0, sizeof *l);
  832. l->file = info->current_file;
  833. l->linenos[0] = lineno;
  834. l->addrs[0] = addr;
  835. for (i = 1; i < DEBUG_LINENO_COUNT; i++)
  836. l->linenos[i] = (unsigned long) -1;
  837. if (info->current_lineno != NULL)
  838. info->current_lineno->next = l;
  839. else
  840. info->current_unit->linenos = l;
  841. info->current_lineno = l;
  842. return TRUE;
  843. }
  844. /* Start a named common block. This is a block of variables that may
  845. move in memory. */
  846. bfd_boolean
  847. debug_start_common_block (void *handle ATTRIBUTE_UNUSED,
  848. const char *name ATTRIBUTE_UNUSED)
  849. {
  850. /* FIXME */
  851. debug_error (_("debug_start_common_block: not implemented"));
  852. return FALSE;
  853. }
  854. /* End a named common block. */
  855. bfd_boolean
  856. debug_end_common_block (void *handle ATTRIBUTE_UNUSED,
  857. const char *name ATTRIBUTE_UNUSED)
  858. {
  859. /* FIXME */
  860. debug_error (_("debug_end_common_block: not implemented"));
  861. return FALSE;
  862. }
  863. /* Record a named integer constant. */
  864. bfd_boolean
  865. debug_record_int_const (void *handle, const char *name, bfd_vma val)
  866. {
  867. struct debug_handle *info = (struct debug_handle *) handle;
  868. struct debug_name *n;
  869. if (name == NULL)
  870. return FALSE;
  871. n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_INT_CONSTANT,
  872. DEBUG_LINKAGE_NONE);
  873. if (n == NULL)
  874. return FALSE;
  875. n->u.int_constant = val;
  876. return TRUE;
  877. }
  878. /* Record a named floating point constant. */
  879. bfd_boolean
  880. debug_record_float_const (void *handle, const char *name, double val)
  881. {
  882. struct debug_handle *info = (struct debug_handle *) handle;
  883. struct debug_name *n;
  884. if (name == NULL)
  885. return FALSE;
  886. n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_FLOAT_CONSTANT,
  887. DEBUG_LINKAGE_NONE);
  888. if (n == NULL)
  889. return FALSE;
  890. n->u.float_constant = val;
  891. return TRUE;
  892. }
  893. /* Record a typed constant with an integral value. */
  894. bfd_boolean
  895. debug_record_typed_const (void *handle, const char *name, debug_type type,
  896. bfd_vma val)
  897. {
  898. struct debug_handle *info = (struct debug_handle *) handle;
  899. struct debug_name *n;
  900. struct debug_typed_constant *tc;
  901. if (name == NULL || type == NULL)
  902. return FALSE;
  903. n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_TYPED_CONSTANT,
  904. DEBUG_LINKAGE_NONE);
  905. if (n == NULL)
  906. return FALSE;
  907. tc = (struct debug_typed_constant *) xmalloc (sizeof *tc);
  908. memset (tc, 0, sizeof *tc);
  909. tc->type = type;
  910. tc->val = val;
  911. n->u.typed_constant = tc;
  912. return TRUE;
  913. }
  914. /* Record a label. */
  915. bfd_boolean
  916. debug_record_label (void *handle ATTRIBUTE_UNUSED,
  917. const char *name ATTRIBUTE_UNUSED,
  918. debug_type type ATTRIBUTE_UNUSED,
  919. bfd_vma addr ATTRIBUTE_UNUSED)
  920. {
  921. /* FIXME. */
  922. debug_error (_("debug_record_label: not implemented"));
  923. return FALSE;
  924. }
  925. /* Record a variable. */
  926. bfd_boolean
  927. debug_record_variable (void *handle, const char *name, debug_type type,
  928. enum debug_var_kind kind, bfd_vma val)
  929. {
  930. struct debug_handle *info = (struct debug_handle *) handle;
  931. struct debug_namespace **nsp;
  932. enum debug_object_linkage linkage;
  933. struct debug_name *n;
  934. struct debug_variable *v;
  935. if (name == NULL || type == NULL)
  936. return FALSE;
  937. if (info->current_unit == NULL
  938. || info->current_file == NULL)
  939. {
  940. debug_error (_("debug_record_variable: no current file"));
  941. return FALSE;
  942. }
  943. if (kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
  944. {
  945. nsp = &info->current_file->globals;
  946. if (kind == DEBUG_GLOBAL)
  947. linkage = DEBUG_LINKAGE_GLOBAL;
  948. else
  949. linkage = DEBUG_LINKAGE_STATIC;
  950. }
  951. else
  952. {
  953. if (info->current_block == NULL)
  954. nsp = &info->current_file->globals;
  955. else
  956. nsp = &info->current_block->locals;
  957. linkage = DEBUG_LINKAGE_AUTOMATIC;
  958. }
  959. n = debug_add_to_namespace (info, nsp, name, DEBUG_OBJECT_VARIABLE, linkage);
  960. if (n == NULL)
  961. return FALSE;
  962. v = (struct debug_variable *) xmalloc (sizeof *v);
  963. memset (v, 0, sizeof *v);
  964. v->kind = kind;
  965. v->type = type;
  966. v->val = val;
  967. n->u.variable = v;
  968. return TRUE;
  969. }
  970. /* Make a type with a given kind and size. */
  971. static struct debug_type_s *
  972. debug_make_type (struct debug_handle *info ATTRIBUTE_UNUSED,
  973. enum debug_type_kind kind, unsigned int size)
  974. {
  975. struct debug_type_s *t;
  976. t = (struct debug_type_s *) xmalloc (sizeof *t);
  977. memset (t, 0, sizeof *t);
  978. t->kind = kind;
  979. t->size = size;
  980. return t;
  981. }
  982. /* Make an indirect type which may be used as a placeholder for a type
  983. which is referenced before it is defined. */
  984. debug_type
  985. debug_make_indirect_type (void *handle, debug_type *slot, const char *tag)
  986. {
  987. struct debug_handle *info = (struct debug_handle *) handle;
  988. struct debug_type_s *t;
  989. struct debug_indirect_type *i;
  990. t = debug_make_type (info, DEBUG_KIND_INDIRECT, 0);
  991. if (t == NULL)
  992. return DEBUG_TYPE_NULL;
  993. i = (struct debug_indirect_type *) xmalloc (sizeof *i);
  994. memset (i, 0, sizeof *i);
  995. i->slot = slot;
  996. i->tag = tag;
  997. t->u.kindirect = i;
  998. return t;
  999. }
  1000. /* Make a void type. There is only one of these. */
  1001. debug_type
  1002. debug_make_void_type (void *handle)
  1003. {
  1004. struct debug_handle *info = (struct debug_handle *) handle;
  1005. return debug_make_type (info, DEBUG_KIND_VOID, 0);
  1006. }
  1007. /* Make an integer type of a given size. The boolean argument is true
  1008. if the integer is unsigned. */
  1009. debug_type
  1010. debug_make_int_type (void *handle, unsigned int size, bfd_boolean unsignedp)
  1011. {
  1012. struct debug_handle *info = (struct debug_handle *) handle;
  1013. struct debug_type_s *t;
  1014. t = debug_make_type (info, DEBUG_KIND_INT, size);
  1015. if (t == NULL)
  1016. return DEBUG_TYPE_NULL;
  1017. t->u.kint = unsignedp;
  1018. return t;
  1019. }
  1020. /* Make a floating point type of a given size. FIXME: On some
  1021. platforms, like an Alpha, you probably need to be able to specify
  1022. the format. */
  1023. debug_type
  1024. debug_make_float_type (void *handle, unsigned int size)
  1025. {
  1026. struct debug_handle *info = (struct debug_handle *) handle;
  1027. return debug_make_type (info, DEBUG_KIND_FLOAT, size);
  1028. }
  1029. /* Make a boolean type of a given size. */
  1030. debug_type
  1031. debug_make_bool_type (void *handle, unsigned int size)
  1032. {
  1033. struct debug_handle *info = (struct debug_handle *) handle;
  1034. return debug_make_type (info, DEBUG_KIND_BOOL, size);
  1035. }
  1036. /* Make a complex type of a given size. */
  1037. debug_type
  1038. debug_make_complex_type (void *handle, unsigned int size)
  1039. {
  1040. struct debug_handle *info = (struct debug_handle *) handle;
  1041. return debug_make_type (info, DEBUG_KIND_COMPLEX, size);
  1042. }
  1043. /* Make a structure type. The second argument is true for a struct,
  1044. false for a union. The third argument is the size of the struct.
  1045. The fourth argument is a NULL terminated array of fields. */
  1046. debug_type
  1047. debug_make_struct_type (void *handle, bfd_boolean structp, bfd_vma size,
  1048. debug_field *fields)
  1049. {
  1050. struct debug_handle *info = (struct debug_handle *) handle;
  1051. struct debug_type_s *t;
  1052. struct debug_class_type *c;
  1053. t = debug_make_type (info,
  1054. structp ? DEBUG_KIND_STRUCT : DEBUG_KIND_UNION,
  1055. size);
  1056. if (t == NULL)
  1057. return DEBUG_TYPE_NULL;
  1058. c = (struct debug_class_type *) xmalloc (sizeof *c);
  1059. memset (c, 0, sizeof *c);
  1060. c->fields = fields;
  1061. t->u.kclass = c;
  1062. return t;
  1063. }
  1064. /* Make an object type. The first three arguments after the handle
  1065. are the same as for debug_make_struct_type. The next arguments are
  1066. a NULL terminated array of base classes, a NULL terminated array of
  1067. methods, the type of the object holding the virtual function table
  1068. if it is not this object, and a boolean which is true if this
  1069. object has its own virtual function table. */
  1070. debug_type
  1071. debug_make_object_type (void *handle, bfd_boolean structp, bfd_vma size,
  1072. debug_field *fields, debug_baseclass *baseclasses,
  1073. debug_method *methods, debug_type vptrbase,
  1074. bfd_boolean ownvptr)
  1075. {
  1076. struct debug_handle *info = (struct debug_handle *) handle;
  1077. struct debug_type_s *t;
  1078. struct debug_class_type *c;
  1079. t = debug_make_type (info,
  1080. structp ? DEBUG_KIND_CLASS : DEBUG_KIND_UNION_CLASS,
  1081. size);
  1082. if (t == NULL)
  1083. return DEBUG_TYPE_NULL;
  1084. c = (struct debug_class_type *) xmalloc (sizeof *c);
  1085. memset (c, 0, sizeof *c);
  1086. c->fields = fields;
  1087. c->baseclasses = baseclasses;
  1088. c->methods = methods;
  1089. if (ownvptr)
  1090. c->vptrbase = t;
  1091. else
  1092. c->vptrbase = vptrbase;
  1093. t->u.kclass = c;
  1094. return t;
  1095. }
  1096. /* Make an enumeration type. The arguments are a null terminated
  1097. array of strings, and an array of corresponding values. */
  1098. debug_type
  1099. debug_make_enum_type (void *handle, const char **names,
  1100. bfd_signed_vma *values)
  1101. {
  1102. struct debug_handle *info = (struct debug_handle *) handle;
  1103. struct debug_type_s *t;
  1104. struct debug_enum_type *e;
  1105. t = debug_make_type (info, DEBUG_KIND_ENUM, 0);
  1106. if (t == NULL)
  1107. return DEBUG_TYPE_NULL;
  1108. e = (struct debug_enum_type *) xmalloc (sizeof *e);
  1109. memset (e, 0, sizeof *e);
  1110. e->names = names;
  1111. e->values = values;
  1112. t->u.kenum = e;
  1113. return t;
  1114. }
  1115. /* Make a pointer to a given type. */
  1116. debug_type
  1117. debug_make_pointer_type (void *handle, debug_type type)
  1118. {
  1119. struct debug_handle *info = (struct debug_handle *) handle;
  1120. struct debug_type_s *t;
  1121. if (type == NULL)
  1122. return DEBUG_TYPE_NULL;
  1123. if (type->pointer != DEBUG_TYPE_NULL)
  1124. return type->pointer;
  1125. t = debug_make_type (info, DEBUG_KIND_POINTER, 0);
  1126. if (t == NULL)
  1127. return DEBUG_TYPE_NULL;
  1128. t->u.kpointer = type;
  1129. type->pointer = t;
  1130. return t;
  1131. }
  1132. /* Make a function returning a given type. FIXME: We should be able
  1133. to record the parameter types. */
  1134. debug_type
  1135. debug_make_function_type (void *handle, debug_type type,
  1136. debug_type *arg_types, bfd_boolean varargs)
  1137. {
  1138. struct debug_handle *info = (struct debug_handle *) handle;
  1139. struct debug_type_s *t;
  1140. struct debug_function_type *f;
  1141. if (type == NULL)
  1142. return DEBUG_TYPE_NULL;
  1143. t = debug_make_type (info, DEBUG_KIND_FUNCTION, 0);
  1144. if (t == NULL)
  1145. return DEBUG_TYPE_NULL;
  1146. f = (struct debug_function_type *) xmalloc (sizeof *f);
  1147. memset (f, 0, sizeof *f);
  1148. f->return_type = type;
  1149. f->arg_types = arg_types;
  1150. f->varargs = varargs;
  1151. t->u.kfunction = f;
  1152. return t;
  1153. }
  1154. /* Make a reference to a given type. */
  1155. debug_type
  1156. debug_make_reference_type (void *handle, debug_type type)
  1157. {
  1158. struct debug_handle *info = (struct debug_handle *) handle;
  1159. struct debug_type_s *t;
  1160. if (type == NULL)
  1161. return DEBUG_TYPE_NULL;
  1162. t = debug_make_type (info, DEBUG_KIND_REFERENCE, 0);
  1163. if (t == NULL)
  1164. return DEBUG_TYPE_NULL;
  1165. t->u.kreference = type;
  1166. return t;
  1167. }
  1168. /* Make a range of a given type from a lower to an upper bound. */
  1169. debug_type
  1170. debug_make_range_type (void *handle, debug_type type, bfd_signed_vma lower,
  1171. bfd_signed_vma upper)
  1172. {
  1173. struct debug_handle *info = (struct debug_handle *) handle;
  1174. struct debug_type_s *t;
  1175. struct debug_range_type *r;
  1176. if (type == NULL)
  1177. return DEBUG_TYPE_NULL;
  1178. t = debug_make_type (info, DEBUG_KIND_RANGE, 0);
  1179. if (t == NULL)
  1180. return DEBUG_TYPE_NULL;
  1181. r = (struct debug_range_type *) xmalloc (sizeof *r);
  1182. memset (r, 0, sizeof *r);
  1183. r->type = type;
  1184. r->lower = lower;
  1185. r->upper = upper;
  1186. t->u.krange = r;
  1187. return t;
  1188. }
  1189. /* Make an array type. The second argument is the type of an element
  1190. of the array. The third argument is the type of a range of the
  1191. array. The fourth and fifth argument are the lower and upper
  1192. bounds, respectively. The sixth argument is true if this array is
  1193. actually a string, as in C. */
  1194. debug_type
  1195. debug_make_array_type (void *handle, debug_type element_type,
  1196. debug_type range_type, bfd_signed_vma lower,
  1197. bfd_signed_vma upper, bfd_boolean stringp)
  1198. {
  1199. struct debug_handle *info = (struct debug_handle *) handle;
  1200. struct debug_type_s *t;
  1201. struct debug_array_type *a;
  1202. if (element_type == NULL || range_type == NULL)
  1203. return DEBUG_TYPE_NULL;
  1204. t = debug_make_type (info, DEBUG_KIND_ARRAY, 0);
  1205. if (t == NULL)
  1206. return DEBUG_TYPE_NULL;
  1207. a = (struct debug_array_type *) xmalloc (sizeof *a);
  1208. memset (a, 0, sizeof *a);
  1209. a->element_type = element_type;
  1210. a->range_type = range_type;
  1211. a->lower = lower;
  1212. a->upper = upper;
  1213. a->stringp = stringp;
  1214. t->u.karray = a;
  1215. return t;
  1216. }
  1217. /* Make a set of a given type. For example, a Pascal set type. The
  1218. boolean argument is true if this set is actually a bitstring, as in
  1219. CHILL. */
  1220. debug_type
  1221. debug_make_set_type (void *handle, debug_type type, bfd_boolean bitstringp)
  1222. {
  1223. struct debug_handle *info = (struct debug_handle *) handle;
  1224. struct debug_type_s *t;
  1225. struct debug_set_type *s;
  1226. if (type == NULL)
  1227. return DEBUG_TYPE_NULL;
  1228. t = debug_make_type (info, DEBUG_KIND_SET, 0);
  1229. if (t == NULL)
  1230. return DEBUG_TYPE_NULL;
  1231. s = (struct debug_set_type *) xmalloc (sizeof *s);
  1232. memset (s, 0, sizeof *s);
  1233. s->type = type;
  1234. s->bitstringp = bitstringp;
  1235. t->u.kset = s;
  1236. return t;
  1237. }
  1238. /* Make a type for a pointer which is relative to an object. The
  1239. second argument is the type of the object to which the pointer is
  1240. relative. The third argument is the type that the pointer points
  1241. to. */
  1242. debug_type
  1243. debug_make_offset_type (void *handle, debug_type base_type,
  1244. debug_type target_type)
  1245. {
  1246. struct debug_handle *info = (struct debug_handle *) handle;
  1247. struct debug_type_s *t;
  1248. struct debug_offset_type *o;
  1249. if (base_type == NULL || target_type == NULL)
  1250. return DEBUG_TYPE_NULL;
  1251. t = debug_make_type (info, DEBUG_KIND_OFFSET, 0);
  1252. if (t == NULL)
  1253. return DEBUG_TYPE_NULL;
  1254. o = (struct debug_offset_type *) xmalloc (sizeof *o);
  1255. memset (o, 0, sizeof *o);
  1256. o->base_type = base_type;
  1257. o->target_type = target_type;
  1258. t->u.koffset = o;
  1259. return t;
  1260. }
  1261. /* Make a type for a method function. The second argument is the
  1262. return type, the third argument is the domain, and the fourth
  1263. argument is a NULL terminated array of argument types. */
  1264. debug_type
  1265. debug_make_method_type (void *handle, debug_type return_type,
  1266. debug_type domain_type, debug_type *arg_types,
  1267. bfd_boolean varargs)
  1268. {
  1269. struct debug_handle *info = (struct debug_handle *) handle;
  1270. struct debug_type_s *t;
  1271. struct debug_method_type *m;
  1272. if (return_type == NULL)
  1273. return DEBUG_TYPE_NULL;
  1274. t = debug_make_type (info, DEBUG_KIND_METHOD, 0);
  1275. if (t == NULL)
  1276. return DEBUG_TYPE_NULL;
  1277. m = (struct debug_method_type *) xmalloc (sizeof *m);
  1278. memset (m, 0, sizeof *m);
  1279. m->return_type = return_type;
  1280. m->domain_type = domain_type;
  1281. m->arg_types = arg_types;
  1282. m->varargs = varargs;
  1283. t->u.kmethod = m;
  1284. return t;
  1285. }
  1286. /* Make a const qualified version of a given type. */
  1287. debug_type
  1288. debug_make_const_type (void *handle, debug_type type)
  1289. {
  1290. struct debug_handle *info = (struct debug_handle *) handle;
  1291. struct debug_type_s *t;
  1292. if (type == NULL)
  1293. return DEBUG_TYPE_NULL;
  1294. t = debug_make_type (info, DEBUG_KIND_CONST, 0);
  1295. if (t == NULL)
  1296. return DEBUG_TYPE_NULL;
  1297. t->u.kconst = type;
  1298. return t;
  1299. }
  1300. /* Make a volatile qualified version of a given type. */
  1301. debug_type
  1302. debug_make_volatile_type (void *handle, debug_type type)
  1303. {
  1304. struct debug_handle *info = (struct debug_handle *) handle;
  1305. struct debug_type_s *t;
  1306. if (type == NULL)
  1307. return DEBUG_TYPE_NULL;
  1308. t = debug_make_type (info, DEBUG_KIND_VOLATILE, 0);
  1309. if (t == NULL)
  1310. return DEBUG_TYPE_NULL;
  1311. t->u.kvolatile = type;
  1312. return t;
  1313. }
  1314. /* Make an undefined tagged type. For example, a struct which has
  1315. been mentioned, but not defined. */
  1316. debug_type
  1317. debug_make_undefined_tagged_type (void *handle, const char *name,
  1318. enum debug_type_kind kind)
  1319. {
  1320. struct debug_handle *info = (struct debug_handle *) handle;
  1321. struct debug_type_s *t;
  1322. if (name == NULL)
  1323. return DEBUG_TYPE_NULL;
  1324. switch (kind)
  1325. {
  1326. case DEBUG_KIND_STRUCT:
  1327. case DEBUG_KIND_UNION:
  1328. case DEBUG_KIND_CLASS:
  1329. case DEBUG_KIND_UNION_CLASS:
  1330. case DEBUG_KIND_ENUM:
  1331. break;
  1332. default:
  1333. debug_error (_("debug_make_undefined_type: unsupported kind"));
  1334. return DEBUG_TYPE_NULL;
  1335. }
  1336. t = debug_make_type (info, kind, 0);
  1337. if (t == NULL)
  1338. return DEBUG_TYPE_NULL;
  1339. return debug_tag_type (handle, name, t);
  1340. }
  1341. /* Make a base class for an object. The second argument is the base
  1342. class type. The third argument is the bit position of this base
  1343. class in the object (always 0 unless doing multiple inheritance).
  1344. The fourth argument is whether this is a virtual class. The fifth
  1345. argument is the visibility of the base class. */
  1346. debug_baseclass
  1347. debug_make_baseclass (void *handle ATTRIBUTE_UNUSED, debug_type type,
  1348. bfd_vma bitpos, bfd_boolean is_virtual,
  1349. enum debug_visibility visibility)
  1350. {
  1351. struct debug_baseclass_s *b;
  1352. b = (struct debug_baseclass_s *) xmalloc (sizeof *b);
  1353. memset (b, 0, sizeof *b);
  1354. b->type = type;
  1355. b->bitpos = bitpos;
  1356. b->is_virtual = is_virtual;
  1357. b->visibility = visibility;
  1358. return b;
  1359. }
  1360. /* Make a field for a struct. The second argument is the name. The
  1361. third argument is the type of the field. The fourth argument is
  1362. the bit position of the field. The fifth argument is the size of
  1363. the field (it may be zero). The sixth argument is the visibility
  1364. of the field. */
  1365. debug_field
  1366. debug_make_field (void *handle ATTRIBUTE_UNUSED, const char *name,
  1367. debug_type type, bfd_vma bitpos, bfd_vma bitsize,
  1368. enum debug_visibility visibility)
  1369. {
  1370. struct debug_field_s *f;
  1371. f = (struct debug_field_s *) xmalloc (sizeof *f);
  1372. memset (f, 0, sizeof *f);
  1373. f->name = name;
  1374. f->type = type;
  1375. f->static_member = FALSE;
  1376. f->u.f.bitpos = bitpos;
  1377. f->u.f.bitsize = bitsize;
  1378. f->visibility = visibility;
  1379. return f;
  1380. }
  1381. /* Make a static member of an object. The second argument is the
  1382. name. The third argument is the type of the member. The fourth
  1383. argument is the physical name of the member (i.e., the name as a
  1384. global variable). The fifth argument is the visibility of the
  1385. member. */
  1386. debug_field
  1387. debug_make_static_member (void *handle ATTRIBUTE_UNUSED, const char *name,
  1388. debug_type type, const char *physname,
  1389. enum debug_visibility visibility)
  1390. {
  1391. struct debug_field_s *f;
  1392. f = (struct debug_field_s *) xmalloc (sizeof *f);
  1393. memset (f, 0, sizeof *f);
  1394. f->name = name;
  1395. f->type = type;
  1396. f->static_member = TRUE;
  1397. f->u.s.physname = physname;
  1398. f->visibility = visibility;
  1399. return f;
  1400. }
  1401. /* Make a method. The second argument is the name, and the third
  1402. argument is a NULL terminated array of method variants. */
  1403. debug_method
  1404. debug_make_method (void *handle ATTRIBUTE_UNUSED, const char *name,
  1405. debug_method_variant *variants)
  1406. {
  1407. struct debug_method_s *m;
  1408. m = (struct debug_method_s *) xmalloc (sizeof *m);
  1409. memset (m, 0, sizeof *m);
  1410. m->name = name;
  1411. m->variants = variants;
  1412. return m;
  1413. }
  1414. /* Make a method argument. The second argument is the real name of
  1415. the function. The third argument is the type of the function. The
  1416. fourth argument is the visibility. The fifth argument is whether
  1417. this is a const function. The sixth argument is whether this is a
  1418. volatile function. The seventh argument is the offset in the
  1419. virtual function table, if any. The eighth argument is the virtual
  1420. function context. FIXME: Are the const and volatile arguments
  1421. necessary? Could we just use debug_make_const_type? */
  1422. debug_method_variant
  1423. debug_make_method_variant (void *handle ATTRIBUTE_UNUSED,
  1424. const char *physname, debug_type type,
  1425. enum debug_visibility visibility,
  1426. bfd_boolean constp, bfd_boolean volatilep,
  1427. bfd_vma voffset, debug_type context)
  1428. {
  1429. struct debug_method_variant_s *m;
  1430. m = (struct debug_method_variant_s *) xmalloc (sizeof *m);
  1431. memset (m, 0, sizeof *m);
  1432. m->physname = physname;
  1433. m->type = type;
  1434. m->visibility = visibility;
  1435. m->constp = constp;
  1436. m->volatilep = volatilep;
  1437. m->voffset = voffset;
  1438. m->context = context;
  1439. return m;
  1440. }
  1441. /* Make a static method argument. The arguments are the same as for
  1442. debug_make_method_variant, except that the last two are omitted
  1443. since a static method can not also be virtual. */
  1444. debug_method_variant
  1445. debug_make_static_method_variant (void *handle ATTRIBUTE_UNUSED,
  1446. const char *physname, debug_type type,
  1447. enum debug_visibility visibility,
  1448. bfd_boolean constp, bfd_boolean volatilep)
  1449. {
  1450. struct debug_method_variant_s *m;
  1451. m = (struct debug_method_variant_s *) xmalloc (sizeof *m);
  1452. memset (m, 0, sizeof *m);
  1453. m->physname = physname;
  1454. m->type = type;
  1455. m->visibility = visibility;
  1456. m->constp = constp;
  1457. m->volatilep = volatilep;
  1458. m->voffset = VOFFSET_STATIC_METHOD;
  1459. return m;
  1460. }
  1461. /* Name a type. */
  1462. debug_type
  1463. debug_name_type (void *handle, const char *name, debug_type type)
  1464. {
  1465. struct debug_handle *info = (struct debug_handle *) handle;
  1466. struct debug_type_s *t;
  1467. struct debug_named_type *n;
  1468. struct debug_name *nm;
  1469. if (name == NULL || type == NULL)
  1470. return DEBUG_TYPE_NULL;
  1471. if (info->current_unit == NULL
  1472. || info->current_file == NULL)
  1473. {
  1474. debug_error (_("debug_name_type: no current file"));
  1475. return DEBUG_TYPE_NULL;
  1476. }
  1477. t = debug_make_type (info, DEBUG_KIND_NAMED, 0);
  1478. if (t == NULL)
  1479. return DEBUG_TYPE_NULL;
  1480. n = (struct debug_named_type *) xmalloc (sizeof *n);
  1481. memset (n, 0, sizeof *n);
  1482. n->type = type;
  1483. t->u.knamed = n;
  1484. /* We always add the name to the global namespace. This is probably
  1485. wrong in some cases, but it seems to be right for stabs. FIXME. */
  1486. nm = debug_add_to_namespace (info, &info->current_file->globals, name,
  1487. DEBUG_OBJECT_TYPE, DEBUG_LINKAGE_NONE);
  1488. if (nm == NULL)
  1489. return DEBUG_TYPE_NULL;
  1490. nm->u.type = t;
  1491. n->name = nm;
  1492. return t;
  1493. }
  1494. /* Tag a type. */
  1495. debug_type
  1496. debug_tag_type (void *handle, const char *name, debug_type type)
  1497. {
  1498. struct debug_handle *info = (struct debug_handle *) handle;
  1499. struct debug_type_s *t;
  1500. struct debug_named_type *n;
  1501. struct debug_name *nm;
  1502. if (name == NULL || type == NULL)
  1503. return DEBUG_TYPE_NULL;
  1504. if (info->current_file == NULL)
  1505. {
  1506. debug_error (_("debug_tag_type: no current file"));
  1507. return DEBUG_TYPE_NULL;
  1508. }
  1509. if (type->kind == DEBUG_KIND_TAGGED)
  1510. {
  1511. if (strcmp (type->u.knamed->name->name, name) == 0)
  1512. return type;
  1513. debug_error (_("debug_tag_type: extra tag attempted"));
  1514. return DEBUG_TYPE_NULL;
  1515. }
  1516. t = debug_make_type (info, DEBUG_KIND_TAGGED, 0);
  1517. if (t == NULL)
  1518. return DEBUG_TYPE_NULL;
  1519. n = (struct debug_named_type *) xmalloc (sizeof *n);
  1520. memset (n, 0, sizeof *n);
  1521. n->type = type;
  1522. t->u.knamed = n;
  1523. /* We keep a global namespace of tags for each compilation unit. I
  1524. don't know if that is the right thing to do. */
  1525. nm = debug_add_to_namespace (info, &info->current_file->globals, name,
  1526. DEBUG_OBJECT_TAG, DEBUG_LINKAGE_NONE);
  1527. if (nm == NULL)
  1528. return DEBUG_TYPE_NULL;
  1529. nm->u.tag = t;
  1530. n->name = nm;
  1531. return t;
  1532. }
  1533. /* Record the size of a given type. */
  1534. bfd_boolean
  1535. debug_record_type_size (void *handle ATTRIBUTE_UNUSED, debug_type type,
  1536. unsigned int size)
  1537. {
  1538. if (type->size != 0 && type->size != size)
  1539. fprintf (stderr, _("Warning: changing type size from %d to %d\n"),
  1540. type->size, size);
  1541. type->size = size;
  1542. return TRUE;
  1543. }
  1544. /* Find a named type. */
  1545. debug_type
  1546. debug_find_named_type (void *handle, const char *name)
  1547. {
  1548. struct debug_handle *info = (struct debug_handle *) handle;
  1549. struct debug_block *b;
  1550. struct debug_file *f;
  1551. /* We only search the current compilation unit. I don't know if
  1552. this is right or not. */
  1553. if (info->current_unit == NULL)
  1554. {
  1555. debug_error (_("debug_find_named_type: no current compilation unit"));
  1556. return DEBUG_TYPE_NULL;
  1557. }
  1558. for (b = info->current_block; b != NULL; b = b->parent)
  1559. {
  1560. if (b->locals != NULL)
  1561. {
  1562. struct debug_name *n;
  1563. for (n = b->locals->list; n != NULL; n = n->next)
  1564. {
  1565. if (n->kind == DEBUG_OBJECT_TYPE
  1566. && n->name[0] == name[0]
  1567. && strcmp (n->name, name) == 0)
  1568. return n->u.type;
  1569. }
  1570. }
  1571. }
  1572. for (f = info->current_unit->files; f != NULL; f = f->next)
  1573. {
  1574. if (f->globals != NULL)
  1575. {
  1576. struct debug_name *n;
  1577. for (n = f->globals->list; n != NULL; n = n->next)
  1578. {
  1579. if (n->kind == DEBUG_OBJECT_TYPE
  1580. && n->name[0] == name[0]
  1581. && strcmp (n->name, name) == 0)
  1582. return n->u.type;
  1583. }
  1584. }
  1585. }
  1586. return DEBUG_TYPE_NULL;
  1587. }
  1588. /* Find a tagged type. */
  1589. debug_type
  1590. debug_find_tagged_type (void *handle, const char *name,
  1591. enum debug_type_kind kind)
  1592. {
  1593. struct debug_handle *info = (struct debug_handle *) handle;
  1594. struct debug_unit *u;
  1595. /* We search the globals of all the compilation units. I don't know
  1596. if this is correct or not. It would be easy to change. */
  1597. for (u = info->units; u != NULL; u = u->next)
  1598. {
  1599. struct debug_file *f;
  1600. for (f = u->files; f != NULL; f = f->next)
  1601. {
  1602. struct debug_name *n;
  1603. if (f->globals != NULL)
  1604. {
  1605. for (n = f->globals->list; n != NULL; n = n->next)
  1606. {
  1607. if (n->kind == DEBUG_OBJECT_TAG
  1608. && (kind == DEBUG_KIND_ILLEGAL
  1609. || n->u.tag->kind == kind)
  1610. && n->name[0] == name[0]
  1611. && strcmp (n->name, name) == 0)
  1612. return n->u.tag;
  1613. }
  1614. }
  1615. }
  1616. }
  1617. return DEBUG_TYPE_NULL;
  1618. }
  1619. /* Get a base type. We build a linked list on the stack to avoid
  1620. crashing if the type is defined circularly. */
  1621. static struct debug_type_s *
  1622. debug_get_real_type (void *handle, debug_type type,
  1623. struct debug_type_real_list *list)
  1624. {
  1625. struct debug_type_real_list *l;
  1626. struct debug_type_real_list rl;
  1627. switch (type->kind)
  1628. {
  1629. default:
  1630. return type;
  1631. case DEBUG_KIND_INDIRECT:
  1632. case DEBUG_KIND_NAMED:
  1633. case DEBUG_KIND_TAGGED:
  1634. break;
  1635. }
  1636. for (l = list; l != NULL; l = l->next)
  1637. {
  1638. if (l->t == type || l == l->next)
  1639. {
  1640. fprintf (stderr,
  1641. _("debug_get_real_type: circular debug information for %s\n"),
  1642. debug_get_type_name (handle, type));
  1643. return NULL;
  1644. }
  1645. }
  1646. rl.next = list;
  1647. rl.t = type;
  1648. switch (type->kind)
  1649. {
  1650. /* The default case is just here to avoid warnings. */
  1651. default:
  1652. case DEBUG_KIND_INDIRECT:
  1653. if (*type->u.kindirect->slot != NULL)
  1654. return debug_get_real_type (handle, *type->u.kindirect->slot, &rl);
  1655. return type;
  1656. case DEBUG_KIND_NAMED:
  1657. case DEBUG_KIND_TAGGED:
  1658. return debug_get_real_type (handle, type->u.knamed->type, &rl);
  1659. }
  1660. /*NOTREACHED*/
  1661. }
  1662. /* Get the kind of a type. */
  1663. enum debug_type_kind
  1664. debug_get_type_kind (void *handle, debug_type type)
  1665. {
  1666. if (type == NULL)
  1667. return DEBUG_KIND_ILLEGAL;
  1668. type = debug_get_real_type (handle, type, NULL);
  1669. if (type == NULL)
  1670. return DEBUG_KIND_ILLEGAL;
  1671. return type->kind;
  1672. }
  1673. /* Get the name of a type. */
  1674. const char *
  1675. debug_get_type_name (void *handle, debug_type type)
  1676. {
  1677. if (type->kind == DEBUG_KIND_INDIRECT)
  1678. {
  1679. if (*type->u.kindirect->slot != NULL)
  1680. return debug_get_type_name (handle, *type->u.kindirect->slot);
  1681. return type->u.kindirect->tag;
  1682. }
  1683. if (type->kind == DEBUG_KIND_NAMED
  1684. || type->kind == DEBUG_KIND_TAGGED)
  1685. return type->u.knamed->name->name;
  1686. return NULL;
  1687. }
  1688. /* Get the size of a type. */
  1689. bfd_vma
  1690. debug_get_type_size (void *handle, debug_type type)
  1691. {
  1692. if (type == NULL)
  1693. return 0;
  1694. /* We don't call debug_get_real_type, because somebody might have
  1695. called debug_record_type_size on a named or indirect type. */
  1696. if (type->size != 0)
  1697. return type->size;
  1698. switch (type->kind)
  1699. {
  1700. default:
  1701. return 0;
  1702. case DEBUG_KIND_INDIRECT:
  1703. if (*type->u.kindirect->slot != NULL)
  1704. return debug_get_type_size (handle, *type->u.kindirect->slot);
  1705. return 0;
  1706. case DEBUG_KIND_NAMED:
  1707. case DEBUG_KIND_TAGGED:
  1708. return debug_get_type_size (handle, type->u.knamed->type);
  1709. }
  1710. /*NOTREACHED*/
  1711. }
  1712. /* Get the return type of a function or method type. */
  1713. debug_type
  1714. debug_get_return_type (void *handle, debug_type type)
  1715. {
  1716. if (type == NULL)
  1717. return DEBUG_TYPE_NULL;
  1718. type = debug_get_real_type (handle, type, NULL);
  1719. if (type == NULL)
  1720. return DEBUG_TYPE_NULL;
  1721. switch (type->kind)
  1722. {
  1723. default:
  1724. return DEBUG_TYPE_NULL;
  1725. case DEBUG_KIND_FUNCTION:
  1726. return type->u.kfunction->return_type;
  1727. case DEBUG_KIND_METHOD:
  1728. return type->u.kmethod->return_type;
  1729. }
  1730. /*NOTREACHED*/
  1731. }
  1732. /* Get the parameter types of a function or method type (except that
  1733. we don't currently store the parameter types of a function). */
  1734. const debug_type *
  1735. debug_get_parameter_types (void *handle, debug_type type,
  1736. bfd_boolean *pvarargs)
  1737. {
  1738. if (type == NULL)
  1739. return NULL;
  1740. type = debug_get_real_type (handle, type, NULL);
  1741. if (type == NULL)
  1742. return NULL;
  1743. switch (type->kind)
  1744. {
  1745. default:
  1746. return NULL;
  1747. case DEBUG_KIND_FUNCTION:
  1748. *pvarargs = type->u.kfunction->varargs;
  1749. return type->u.kfunction->arg_types;
  1750. case DEBUG_KIND_METHOD:
  1751. *pvarargs = type->u.kmethod->varargs;
  1752. return type->u.kmethod->arg_types;
  1753. }
  1754. /*NOTREACHED*/
  1755. }
  1756. /* Get the target type of a type. */
  1757. debug_type
  1758. debug_get_target_type (void *handle, debug_type type)
  1759. {
  1760. if (type == NULL)
  1761. return NULL;
  1762. type = debug_get_real_type (handle, type, NULL);
  1763. if (type == NULL)
  1764. return NULL;
  1765. switch (type->kind)
  1766. {
  1767. default:
  1768. return NULL;
  1769. case DEBUG_KIND_POINTER:
  1770. return type->u.kpointer;
  1771. case DEBUG_KIND_REFERENCE:
  1772. return type->u.kreference;
  1773. case DEBUG_KIND_CONST:
  1774. return type->u.kconst;
  1775. case DEBUG_KIND_VOLATILE:
  1776. return type->u.kvolatile;
  1777. }
  1778. /*NOTREACHED*/
  1779. }
  1780. /* Get the NULL terminated array of fields for a struct, union, or
  1781. class. */
  1782. const debug_field *
  1783. debug_get_fields (void *handle, debug_type type)
  1784. {
  1785. if (type == NULL)
  1786. return NULL;
  1787. type = debug_get_real_type (handle, type, NULL);
  1788. if (type == NULL)
  1789. return NULL;
  1790. switch (type->kind)
  1791. {
  1792. default:
  1793. return NULL;
  1794. case DEBUG_KIND_STRUCT:
  1795. case DEBUG_KIND_UNION:
  1796. case DEBUG_KIND_CLASS:
  1797. case DEBUG_KIND_UNION_CLASS:
  1798. return type->u.kclass->fields;
  1799. }
  1800. /*NOTREACHED*/
  1801. }
  1802. /* Get the type of a field. */
  1803. debug_type
  1804. debug_get_field_type (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1805. {
  1806. if (field == NULL)
  1807. return NULL;
  1808. return field->type;
  1809. }
  1810. /* Get the name of a field. */
  1811. const char *
  1812. debug_get_field_name (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1813. {
  1814. if (field == NULL)
  1815. return NULL;
  1816. return field->name;
  1817. }
  1818. /* Get the bit position of a field. */
  1819. bfd_vma
  1820. debug_get_field_bitpos (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1821. {
  1822. if (field == NULL || field->static_member)
  1823. return (bfd_vma) -1;
  1824. return field->u.f.bitpos;
  1825. }
  1826. /* Get the bit size of a field. */
  1827. bfd_vma
  1828. debug_get_field_bitsize (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1829. {
  1830. if (field == NULL || field->static_member)
  1831. return (bfd_vma) -1;
  1832. return field->u.f.bitsize;
  1833. }
  1834. /* Get the visibility of a field. */
  1835. enum debug_visibility
  1836. debug_get_field_visibility (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1837. {
  1838. if (field == NULL)
  1839. return DEBUG_VISIBILITY_IGNORE;
  1840. return field->visibility;
  1841. }
  1842. /* Get the physical name of a field. */
  1843. const char *
  1844. debug_get_field_physname (void *handle ATTRIBUTE_UNUSED, debug_field field)
  1845. {
  1846. if (field == NULL || ! field->static_member)
  1847. return NULL;
  1848. return field->u.s.physname;
  1849. }
  1850. /* Write out the debugging information. This is given a handle to
  1851. debugging information, and a set of function pointers to call. */
  1852. bfd_boolean
  1853. debug_write (void *handle, const struct debug_write_fns *fns, void *fhandle)
  1854. {
  1855. struct debug_handle *info = (struct debug_handle *) handle;
  1856. struct debug_unit *u;
  1857. /* We use a mark to tell whether we have already written out a
  1858. particular name. We use an integer, so that we don't have to
  1859. clear the mark fields if we happen to write out the same
  1860. information more than once. */
  1861. ++info->mark;
  1862. /* The base_id field holds an ID value which will never be used, so
  1863. that we can tell whether we have assigned an ID during this call
  1864. to debug_write. */
  1865. info->base_id = info->class_id;
  1866. /* We keep a linked list of classes for which was have assigned ID's
  1867. during this call to debug_write. */
  1868. info->id_list = NULL;
  1869. for (u = info->units; u != NULL; u = u->next)
  1870. {
  1871. struct debug_file *f;
  1872. bfd_boolean first_file;
  1873. info->current_write_lineno = u->linenos;
  1874. info->current_write_lineno_index = 0;
  1875. if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
  1876. return FALSE;
  1877. first_file = TRUE;
  1878. for (f = u->files; f != NULL; f = f->next)
  1879. {
  1880. struct debug_name *n;
  1881. if (first_file)
  1882. first_file = FALSE;
  1883. else if (! (*fns->start_source) (fhandle, f->filename))
  1884. return FALSE;
  1885. if (f->globals != NULL)
  1886. for (n = f->globals->list; n != NULL; n = n->next)
  1887. if (! debug_write_name (info, fns, fhandle, n))
  1888. return FALSE;
  1889. }
  1890. /* Output any line number information which hasn't already been
  1891. handled. */
  1892. if (! debug_write_linenos (info, fns, fhandle, (bfd_vma) -1))
  1893. return FALSE;
  1894. }
  1895. return TRUE;
  1896. }
  1897. /* Write out an element in a namespace. */
  1898. static bfd_boolean
  1899. debug_write_name (struct debug_handle *info,
  1900. const struct debug_write_fns *fns, void *fhandle,
  1901. struct debug_name *n)
  1902. {
  1903. switch (n->kind)
  1904. {
  1905. case DEBUG_OBJECT_TYPE:
  1906. if (! debug_write_type (info, fns, fhandle, n->u.type, n)
  1907. || ! (*fns->typdef) (fhandle, n->name))
  1908. return FALSE;
  1909. return TRUE;
  1910. case DEBUG_OBJECT_TAG:
  1911. if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
  1912. return FALSE;
  1913. return (*fns->tag) (fhandle, n->name);
  1914. case DEBUG_OBJECT_VARIABLE:
  1915. if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
  1916. (struct debug_name *) NULL))
  1917. return FALSE;
  1918. return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
  1919. n->u.variable->val);
  1920. case DEBUG_OBJECT_FUNCTION:
  1921. return debug_write_function (info, fns, fhandle, n->name,
  1922. n->linkage, n->u.function);
  1923. case DEBUG_OBJECT_INT_CONSTANT:
  1924. return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
  1925. case DEBUG_OBJECT_FLOAT_CONSTANT:
  1926. return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
  1927. case DEBUG_OBJECT_TYPED_CONSTANT:
  1928. if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
  1929. (struct debug_name *) NULL))
  1930. return FALSE;
  1931. return (*fns->typed_constant) (fhandle, n->name,
  1932. n->u.typed_constant->val);
  1933. default:
  1934. abort ();
  1935. return FALSE;
  1936. }
  1937. /*NOTREACHED*/
  1938. }
  1939. /* Write out a type. If the type is DEBUG_KIND_NAMED or
  1940. DEBUG_KIND_TAGGED, then the name argument is the name for which we
  1941. are about to call typedef or tag. If the type is anything else,
  1942. then the name argument is a tag from a DEBUG_KIND_TAGGED type which
  1943. points to this one. */
  1944. static bfd_boolean
  1945. debug_write_type (struct debug_handle *info,
  1946. const struct debug_write_fns *fns, void *fhandle,
  1947. struct debug_type_s *type, struct debug_name *name)
  1948. {
  1949. unsigned int i;
  1950. int is;
  1951. const char *tag = NULL;
  1952. /* If we have a name for this type, just output it. We only output
  1953. typedef names after they have been defined. We output type tags
  1954. whenever we are not actually defining them. */
  1955. if ((type->kind == DEBUG_KIND_NAMED
  1956. || type->kind == DEBUG_KIND_TAGGED)
  1957. && (type->u.knamed->name->mark == info->mark
  1958. || (type->kind == DEBUG_KIND_TAGGED
  1959. && type->u.knamed->name != name)))
  1960. {
  1961. if (type->kind == DEBUG_KIND_NAMED)
  1962. return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
  1963. else
  1964. {
  1965. struct debug_type_s *real;
  1966. unsigned int id;
  1967. real = debug_get_real_type ((void *) info, type, NULL);
  1968. if (real == NULL)
  1969. return (*fns->empty_type) (fhandle);
  1970. id = 0;
  1971. if ((real->kind == DEBUG_KIND_STRUCT
  1972. || real->kind == DEBUG_KIND_UNION
  1973. || real->kind == DEBUG_KIND_CLASS
  1974. || real->kind == DEBUG_KIND_UNION_CLASS)
  1975. && real->u.kclass != NULL)
  1976. {
  1977. if (real->u.kclass->id <= info->base_id)
  1978. {
  1979. if (! debug_set_class_id (info,
  1980. type->u.knamed->name->name,
  1981. real))
  1982. return FALSE;
  1983. }
  1984. id = real->u.kclass->id;
  1985. }
  1986. return (*fns->tag_type) (fhandle, type->u.knamed->name->name, id,
  1987. real->kind);
  1988. }
  1989. }
  1990. /* Mark the name after we have already looked for a known name, so
  1991. that we don't just define a type in terms of itself. We need to
  1992. mark the name here so that a struct containing a pointer to
  1993. itself will work. */
  1994. if (name != NULL)
  1995. name->mark = info->mark;
  1996. if (name != NULL
  1997. && type->kind != DEBUG_KIND_NAMED
  1998. && type->kind != DEBUG_KIND_TAGGED)
  1999. {
  2000. assert (name->kind == DEBUG_OBJECT_TAG);
  2001. tag = name->name;
  2002. }
  2003. switch (type->kind)
  2004. {
  2005. case DEBUG_KIND_ILLEGAL:
  2006. debug_error (_("debug_write_type: illegal type encountered"));
  2007. return FALSE;
  2008. case DEBUG_KIND_INDIRECT:
  2009. if (*type->u.kindirect->slot == DEBUG_TYPE_NULL)
  2010. return (*fns->empty_type) (fhandle);
  2011. return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
  2012. name);
  2013. case DEBUG_KIND_VOID:
  2014. return (*fns->void_type) (fhandle);
  2015. case DEBUG_KIND_INT:
  2016. return (*fns->int_type) (fhandle, type->size, type->u.kint);
  2017. case DEBUG_KIND_FLOAT:
  2018. return (*fns->float_type) (fhandle, type->size);
  2019. case DEBUG_KIND_COMPLEX:
  2020. return (*fns->complex_type) (fhandle, type->size);
  2021. case DEBUG_KIND_BOOL:
  2022. return (*fns->bool_type) (fhandle, type->size);
  2023. case DEBUG_KIND_STRUCT:
  2024. case DEBUG_KIND_UNION:
  2025. if (type->u.kclass != NULL)
  2026. {
  2027. if (type->u.kclass->id <= info->base_id)
  2028. {
  2029. if (! debug_set_class_id (info, tag, type))
  2030. return FALSE;
  2031. }
  2032. if (info->mark == type->u.kclass->mark)
  2033. {
  2034. /* We are currently outputting this struct, or we have
  2035. already output it. I don't know if this can happen,
  2036. but it can happen for a class. */
  2037. assert (type->u.kclass->id > info->base_id);
  2038. return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
  2039. type->kind);
  2040. }
  2041. type->u.kclass->mark = info->mark;
  2042. }
  2043. if (! (*fns->start_struct_type) (fhandle, tag,
  2044. (type->u.kclass != NULL
  2045. ? type->u.kclass->id
  2046. : 0),
  2047. type->kind == DEBUG_KIND_STRUCT,
  2048. type->size))
  2049. return FALSE;
  2050. if (type->u.kclass != NULL
  2051. && type->u.kclass->fields != NULL)
  2052. {
  2053. for (i = 0; type->u.kclass->fields[i] != NULL; i++)
  2054. {
  2055. struct debug_field_s *f;
  2056. f = type->u.kclass->fields[i];
  2057. if (! debug_write_type (info, fns, fhandle, f->type,
  2058. (struct debug_name *) NULL)
  2059. || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
  2060. f->u.f.bitsize, f->visibility))
  2061. return FALSE;
  2062. }
  2063. }
  2064. return (*fns->end_struct_type) (fhandle);
  2065. case DEBUG_KIND_CLASS:
  2066. case DEBUG_KIND_UNION_CLASS:
  2067. return debug_write_class_type (info, fns, fhandle, type, tag);
  2068. case DEBUG_KIND_ENUM:
  2069. if (type->u.kenum == NULL)
  2070. return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
  2071. (bfd_signed_vma *) NULL);
  2072. return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
  2073. type->u.kenum->values);
  2074. case DEBUG_KIND_POINTER:
  2075. if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
  2076. (struct debug_name *) NULL))
  2077. return FALSE;
  2078. return (*fns->pointer_type) (fhandle);
  2079. case DEBUG_KIND_FUNCTION:
  2080. if (! debug_write_type (info, fns, fhandle,
  2081. type->u.kfunction->return_type,
  2082. (struct debug_name *) NULL))
  2083. return FALSE;
  2084. if (type->u.kfunction->arg_types == NULL)
  2085. is = -1;
  2086. else
  2087. {
  2088. for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
  2089. if (! debug_write_type (info, fns, fhandle,
  2090. type->u.kfunction->arg_types[is],
  2091. (struct debug_name *) NULL))
  2092. return FALSE;
  2093. }
  2094. return (*fns->function_type) (fhandle, is,
  2095. type->u.kfunction->varargs);
  2096. case DEBUG_KIND_REFERENCE:
  2097. if (! debug_write_type (info, fns, fhandle, type->u.kreference,
  2098. (struct debug_name *) NULL))
  2099. return FALSE;
  2100. return (*fns->reference_type) (fhandle);
  2101. case DEBUG_KIND_RANGE:
  2102. if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
  2103. (struct debug_name *) NULL))
  2104. return FALSE;
  2105. return (*fns->range_type) (fhandle, type->u.krange->lower,
  2106. type->u.krange->upper);
  2107. case DEBUG_KIND_ARRAY:
  2108. if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
  2109. (struct debug_name *) NULL)
  2110. || ! debug_write_type (info, fns, fhandle,
  2111. type->u.karray->range_type,
  2112. (struct debug_name *) NULL))
  2113. return FALSE;
  2114. return (*fns->array_type) (fhandle, type->u.karray->lower,
  2115. type->u.karray->upper,
  2116. type->u.karray->stringp);
  2117. case DEBUG_KIND_SET:
  2118. if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
  2119. (struct debug_name *) NULL))
  2120. return FALSE;
  2121. return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
  2122. case DEBUG_KIND_OFFSET:
  2123. if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
  2124. (struct debug_name *) NULL)
  2125. || ! debug_write_type (info, fns, fhandle,
  2126. type->u.koffset->target_type,
  2127. (struct debug_name *) NULL))
  2128. return FALSE;
  2129. return (*fns->offset_type) (fhandle);
  2130. case DEBUG_KIND_METHOD:
  2131. if (! debug_write_type (info, fns, fhandle,
  2132. type->u.kmethod->return_type,
  2133. (struct debug_name *) NULL))
  2134. return FALSE;
  2135. if (type->u.kmethod->arg_types == NULL)
  2136. is = -1;
  2137. else
  2138. {
  2139. for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
  2140. if (! debug_write_type (info, fns, fhandle,
  2141. type->u.kmethod->arg_types[is],
  2142. (struct debug_name *) NULL))
  2143. return FALSE;
  2144. }
  2145. if (type->u.kmethod->domain_type != NULL)
  2146. {
  2147. if (! debug_write_type (info, fns, fhandle,
  2148. type->u.kmethod->domain_type,
  2149. (struct debug_name *) NULL))
  2150. return FALSE;
  2151. }
  2152. return (*fns->method_type) (fhandle,
  2153. type->u.kmethod->domain_type != NULL,
  2154. is,
  2155. type->u.kmethod->varargs);
  2156. case DEBUG_KIND_CONST:
  2157. if (! debug_write_type (info, fns, fhandle, type->u.kconst,
  2158. (struct debug_name *) NULL))
  2159. return FALSE;
  2160. return (*fns->const_type) (fhandle);
  2161. case DEBUG_KIND_VOLATILE:
  2162. if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
  2163. (struct debug_name *) NULL))
  2164. return FALSE;
  2165. return (*fns->volatile_type) (fhandle);
  2166. case DEBUG_KIND_NAMED:
  2167. return debug_write_type (info, fns, fhandle, type->u.knamed->type,
  2168. (struct debug_name *) NULL);
  2169. case DEBUG_KIND_TAGGED:
  2170. return debug_write_type (info, fns, fhandle, type->u.knamed->type,
  2171. type->u.knamed->name);
  2172. default:
  2173. abort ();
  2174. return FALSE;
  2175. }
  2176. }
  2177. /* Write out a class type. */
  2178. static bfd_boolean
  2179. debug_write_class_type (struct debug_handle *info,
  2180. const struct debug_write_fns *fns, void *fhandle,
  2181. struct debug_type_s *type, const char *tag)
  2182. {
  2183. unsigned int i;
  2184. unsigned int id;
  2185. struct debug_type_s *vptrbase;
  2186. if (type->u.kclass == NULL)
  2187. {
  2188. id = 0;
  2189. vptrbase = NULL;
  2190. }
  2191. else
  2192. {
  2193. if (type->u.kclass->id <= info->base_id)
  2194. {
  2195. if (! debug_set_class_id (info, tag, type))
  2196. return FALSE;
  2197. }
  2198. if (info->mark == type->u.kclass->mark)
  2199. {
  2200. /* We are currently outputting this class, or we have
  2201. already output it. This can happen when there are
  2202. methods for an anonymous class. */
  2203. assert (type->u.kclass->id > info->base_id);
  2204. return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
  2205. type->kind);
  2206. }
  2207. type->u.kclass->mark = info->mark;
  2208. id = type->u.kclass->id;
  2209. vptrbase = type->u.kclass->vptrbase;
  2210. if (vptrbase != NULL && vptrbase != type)
  2211. {
  2212. if (! debug_write_type (info, fns, fhandle, vptrbase,
  2213. (struct debug_name *) NULL))
  2214. return FALSE;
  2215. }
  2216. }
  2217. if (! (*fns->start_class_type) (fhandle, tag, id,
  2218. type->kind == DEBUG_KIND_CLASS,
  2219. type->size,
  2220. vptrbase != NULL,
  2221. vptrbase == type))
  2222. return FALSE;
  2223. if (type->u.kclass != NULL)
  2224. {
  2225. if (type->u.kclass->fields != NULL)
  2226. {
  2227. for (i = 0; type->u.kclass->fields[i] != NULL; i++)
  2228. {
  2229. struct debug_field_s *f;
  2230. f = type->u.kclass->fields[i];
  2231. if (! debug_write_type (info, fns, fhandle, f->type,
  2232. (struct debug_name *) NULL))
  2233. return FALSE;
  2234. if (f->static_member)
  2235. {
  2236. if (! (*fns->class_static_member) (fhandle, f->name,
  2237. f->u.s.physname,
  2238. f->visibility))
  2239. return FALSE;
  2240. }
  2241. else
  2242. {
  2243. if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
  2244. f->u.f.bitsize, f->visibility))
  2245. return FALSE;
  2246. }
  2247. }
  2248. }
  2249. if (type->u.kclass->baseclasses != NULL)
  2250. {
  2251. for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
  2252. {
  2253. struct debug_baseclass_s *b;
  2254. b = type->u.kclass->baseclasses[i];
  2255. if (! debug_write_type (info, fns, fhandle, b->type,
  2256. (struct debug_name *) NULL))
  2257. return FALSE;
  2258. if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->is_virtual,
  2259. b->visibility))
  2260. return FALSE;
  2261. }
  2262. }
  2263. if (type->u.kclass->methods != NULL)
  2264. {
  2265. for (i = 0; type->u.kclass->methods[i] != NULL; i++)
  2266. {
  2267. struct debug_method_s *m;
  2268. unsigned int j;
  2269. m = type->u.kclass->methods[i];
  2270. if (! (*fns->class_start_method) (fhandle, m->name))
  2271. return FALSE;
  2272. for (j = 0; m->variants[j] != NULL; j++)
  2273. {
  2274. struct debug_method_variant_s *v;
  2275. v = m->variants[j];
  2276. if (v->context != NULL)
  2277. {
  2278. if (! debug_write_type (info, fns, fhandle, v->context,
  2279. (struct debug_name *) NULL))
  2280. return FALSE;
  2281. }
  2282. if (! debug_write_type (info, fns, fhandle, v->type,
  2283. (struct debug_name *) NULL))
  2284. return FALSE;
  2285. if (v->voffset != VOFFSET_STATIC_METHOD)
  2286. {
  2287. if (! (*fns->class_method_variant) (fhandle, v->physname,
  2288. v->visibility,
  2289. v->constp,
  2290. v->volatilep,
  2291. v->voffset,
  2292. v->context != NULL))
  2293. return FALSE;
  2294. }
  2295. else
  2296. {
  2297. if (! (*fns->class_static_method_variant) (fhandle,
  2298. v->physname,
  2299. v->visibility,
  2300. v->constp,
  2301. v->volatilep))
  2302. return FALSE;
  2303. }
  2304. }
  2305. if (! (*fns->class_end_method) (fhandle))
  2306. return FALSE;
  2307. }
  2308. }
  2309. }
  2310. return (*fns->end_class_type) (fhandle);
  2311. }
  2312. /* Write out information for a function. */
  2313. static bfd_boolean
  2314. debug_write_function (struct debug_handle *info,
  2315. const struct debug_write_fns *fns, void *fhandle,
  2316. const char *name, enum debug_object_linkage linkage,
  2317. struct debug_function *function)
  2318. {
  2319. struct debug_parameter *p;
  2320. struct debug_block *b;
  2321. if (! debug_write_linenos (info, fns, fhandle, function->blocks->start))
  2322. return FALSE;
  2323. if (! debug_write_type (info, fns, fhandle, function->return_type,
  2324. (struct debug_name *) NULL))
  2325. return FALSE;
  2326. if (! (*fns->start_function) (fhandle, name,
  2327. linkage == DEBUG_LINKAGE_GLOBAL))
  2328. return FALSE;
  2329. for (p = function->parameters; p != NULL; p = p->next)
  2330. {
  2331. if (! debug_write_type (info, fns, fhandle, p->type,
  2332. (struct debug_name *) NULL)
  2333. || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
  2334. return FALSE;
  2335. }
  2336. for (b = function->blocks; b != NULL; b = b->next)
  2337. {
  2338. if (! debug_write_block (info, fns, fhandle, b))
  2339. return FALSE;
  2340. }
  2341. return (*fns->end_function) (fhandle);
  2342. }
  2343. /* Write out information for a block. */
  2344. static bfd_boolean
  2345. debug_write_block (struct debug_handle *info,
  2346. const struct debug_write_fns *fns, void *fhandle,
  2347. struct debug_block *block)
  2348. {
  2349. struct debug_name *n;
  2350. struct debug_block *b;
  2351. if (! debug_write_linenos (info, fns, fhandle, block->start))
  2352. return FALSE;
  2353. /* I can't see any point to writing out a block with no local
  2354. variables, so we don't bother, except for the top level block. */
  2355. if (block->locals != NULL || block->parent == NULL)
  2356. {
  2357. if (! (*fns->start_block) (fhandle, block->start))
  2358. return FALSE;
  2359. }
  2360. if (block->locals != NULL)
  2361. {
  2362. for (n = block->locals->list; n != NULL; n = n->next)
  2363. {
  2364. if (! debug_write_name (info, fns, fhandle, n))
  2365. return FALSE;
  2366. }
  2367. }
  2368. for (b = block->children; b != NULL; b = b->next)
  2369. {
  2370. if (! debug_write_block (info, fns, fhandle, b))
  2371. return FALSE;
  2372. }
  2373. if (! debug_write_linenos (info, fns, fhandle, block->end))
  2374. return FALSE;
  2375. if (block->locals != NULL || block->parent == NULL)
  2376. {
  2377. if (! (*fns->end_block) (fhandle, block->end))
  2378. return FALSE;
  2379. }
  2380. return TRUE;
  2381. }
  2382. /* Write out line number information up to ADDRESS. */
  2383. static bfd_boolean
  2384. debug_write_linenos (struct debug_handle *info,
  2385. const struct debug_write_fns *fns, void *fhandle,
  2386. bfd_vma address)
  2387. {
  2388. while (info->current_write_lineno != NULL)
  2389. {
  2390. struct debug_lineno *l;
  2391. l = info->current_write_lineno;
  2392. while (info->current_write_lineno_index < DEBUG_LINENO_COUNT)
  2393. {
  2394. if (l->linenos[info->current_write_lineno_index]
  2395. == (unsigned long) -1)
  2396. break;
  2397. if (l->addrs[info->current_write_lineno_index] >= address)
  2398. return TRUE;
  2399. if (! (*fns->lineno) (fhandle, l->file->filename,
  2400. l->linenos[info->current_write_lineno_index],
  2401. l->addrs[info->current_write_lineno_index]))
  2402. return FALSE;
  2403. ++info->current_write_lineno_index;
  2404. }
  2405. info->current_write_lineno = l->next;
  2406. info->current_write_lineno_index = 0;
  2407. }
  2408. return TRUE;
  2409. }
  2410. /* Get the ID number for a class. If during the same call to
  2411. debug_write we find a struct with the same definition with the same
  2412. name, we use the same ID. This type of things happens because the
  2413. same struct will be defined by multiple compilation units. */
  2414. static bfd_boolean
  2415. debug_set_class_id (struct debug_handle *info, const char *tag,
  2416. struct debug_type_s *type)
  2417. {
  2418. struct debug_class_type *c;
  2419. struct debug_class_id *l;
  2420. assert (type->kind == DEBUG_KIND_STRUCT
  2421. || type->kind == DEBUG_KIND_UNION
  2422. || type->kind == DEBUG_KIND_CLASS
  2423. || type->kind == DEBUG_KIND_UNION_CLASS);
  2424. c = type->u.kclass;
  2425. if (c->id > info->base_id)
  2426. return TRUE;
  2427. for (l = info->id_list; l != NULL; l = l->next)
  2428. {
  2429. if (l->type->kind != type->kind)
  2430. continue;
  2431. if (tag == NULL)
  2432. {
  2433. if (l->tag != NULL)
  2434. continue;
  2435. }
  2436. else
  2437. {
  2438. if (l->tag == NULL
  2439. || l->tag[0] != tag[0]
  2440. || strcmp (l->tag, tag) != 0)
  2441. continue;
  2442. }
  2443. if (debug_type_samep (info, l->type, type))
  2444. {
  2445. c->id = l->type->u.kclass->id;
  2446. return TRUE;
  2447. }
  2448. }
  2449. /* There are no identical types. Use a new ID, and add it to the
  2450. list. */
  2451. ++info->class_id;
  2452. c->id = info->class_id;
  2453. l = (struct debug_class_id *) xmalloc (sizeof *l);
  2454. memset (l, 0, sizeof *l);
  2455. l->type = type;
  2456. l->tag = tag;
  2457. l->next = info->id_list;
  2458. info->id_list = l;
  2459. return TRUE;
  2460. }
  2461. /* See if two types are the same. At this point, we don't care about
  2462. tags and the like. */
  2463. static bfd_boolean
  2464. debug_type_samep (struct debug_handle *info, struct debug_type_s *t1,
  2465. struct debug_type_s *t2)
  2466. {
  2467. struct debug_type_compare_list *l;
  2468. struct debug_type_compare_list top;
  2469. bfd_boolean ret;
  2470. if (t1 == NULL)
  2471. return t2 == NULL;
  2472. if (t2 == NULL)
  2473. return FALSE;
  2474. while (t1->kind == DEBUG_KIND_INDIRECT)
  2475. {
  2476. t1 = *t1->u.kindirect->slot;
  2477. if (t1 == NULL)
  2478. return FALSE;
  2479. }
  2480. while (t2->kind == DEBUG_KIND_INDIRECT)
  2481. {
  2482. t2 = *t2->u.kindirect->slot;
  2483. if (t2 == NULL)
  2484. return FALSE;
  2485. }
  2486. if (t1 == t2)
  2487. return TRUE;
  2488. /* As a special case, permit a typedef to match a tag, since C++
  2489. debugging output will sometimes add a typedef where C debugging
  2490. output will not. */
  2491. if (t1->kind == DEBUG_KIND_NAMED
  2492. && t2->kind == DEBUG_KIND_TAGGED)
  2493. return debug_type_samep (info, t1->u.knamed->type, t2);
  2494. else if (t1->kind == DEBUG_KIND_TAGGED
  2495. && t2->kind == DEBUG_KIND_NAMED)
  2496. return debug_type_samep (info, t1, t2->u.knamed->type);
  2497. if (t1->kind != t2->kind
  2498. || t1->size != t2->size)
  2499. return FALSE;
  2500. /* Get rid of the trivial cases first. */
  2501. switch (t1->kind)
  2502. {
  2503. default:
  2504. break;
  2505. case DEBUG_KIND_VOID:
  2506. case DEBUG_KIND_FLOAT:
  2507. case DEBUG_KIND_COMPLEX:
  2508. case DEBUG_KIND_BOOL:
  2509. return TRUE;
  2510. case DEBUG_KIND_INT:
  2511. return t1->u.kint == t2->u.kint;
  2512. }
  2513. /* We have to avoid an infinite recursion. We do this by keeping a
  2514. list of types which we are comparing. We just keep the list on
  2515. the stack. If we encounter a pair of types we are currently
  2516. comparing, we just assume that they are equal. */
  2517. for (l = info->compare_list; l != NULL; l = l->next)
  2518. {
  2519. if (l->t1 == t1 && l->t2 == t2)
  2520. return TRUE;
  2521. }
  2522. top.t1 = t1;
  2523. top.t2 = t2;
  2524. top.next = info->compare_list;
  2525. info->compare_list = &top;
  2526. switch (t1->kind)
  2527. {
  2528. default:
  2529. abort ();
  2530. ret = FALSE;
  2531. break;
  2532. case DEBUG_KIND_STRUCT:
  2533. case DEBUG_KIND_UNION:
  2534. case DEBUG_KIND_CLASS:
  2535. case DEBUG_KIND_UNION_CLASS:
  2536. if (t1->u.kclass == NULL)
  2537. ret = t2->u.kclass == NULL;
  2538. else if (t2->u.kclass == NULL)
  2539. ret = FALSE;
  2540. else if (t1->u.kclass->id > info->base_id
  2541. && t1->u.kclass->id == t2->u.kclass->id)
  2542. ret = TRUE;
  2543. else
  2544. ret = debug_class_type_samep (info, t1, t2);
  2545. break;
  2546. case DEBUG_KIND_ENUM:
  2547. if (t1->u.kenum == NULL)
  2548. ret = t2->u.kenum == NULL;
  2549. else if (t2->u.kenum == NULL)
  2550. ret = FALSE;
  2551. else
  2552. {
  2553. const char **pn1, **pn2;
  2554. bfd_signed_vma *pv1, *pv2;
  2555. pn1 = t1->u.kenum->names;
  2556. pn2 = t2->u.kenum->names;
  2557. pv1 = t1->u.kenum->values;
  2558. pv2 = t2->u.kenum->values;
  2559. while (*pn1 != NULL && *pn2 != NULL)
  2560. {
  2561. if (**pn1 != **pn2
  2562. || *pv1 != *pv2
  2563. || strcmp (*pn1, *pn2) != 0)
  2564. break;
  2565. ++pn1;
  2566. ++pn2;
  2567. ++pv1;
  2568. ++pv2;
  2569. }
  2570. ret = *pn1 == NULL && *pn2 == NULL;
  2571. }
  2572. break;
  2573. case DEBUG_KIND_POINTER:
  2574. ret = debug_type_samep (info, t1->u.kpointer, t2->u.kpointer);
  2575. break;
  2576. case DEBUG_KIND_FUNCTION:
  2577. if (t1->u.kfunction->varargs != t2->u.kfunction->varargs
  2578. || ! debug_type_samep (info, t1->u.kfunction->return_type,
  2579. t2->u.kfunction->return_type)
  2580. || ((t1->u.kfunction->arg_types == NULL)
  2581. != (t2->u.kfunction->arg_types == NULL)))
  2582. ret = FALSE;
  2583. else if (t1->u.kfunction->arg_types == NULL)
  2584. ret = TRUE;
  2585. else
  2586. {
  2587. struct debug_type_s **a1, **a2;
  2588. a1 = t1->u.kfunction->arg_types;
  2589. a2 = t2->u.kfunction->arg_types;
  2590. while (*a1 != NULL && *a2 != NULL)
  2591. {
  2592. if (! debug_type_samep (info, *a1, *a2))
  2593. break;
  2594. ++a1;
  2595. ++a2;
  2596. }
  2597. ret = *a1 == NULL && *a2 == NULL;
  2598. }
  2599. break;
  2600. case DEBUG_KIND_REFERENCE:
  2601. ret = debug_type_samep (info, t1->u.kreference, t2->u.kreference);
  2602. break;
  2603. case DEBUG_KIND_RANGE:
  2604. ret = (t1->u.krange->lower == t2->u.krange->lower
  2605. && t1->u.krange->upper == t2->u.krange->upper
  2606. && debug_type_samep (info, t1->u.krange->type,
  2607. t2->u.krange->type));
  2608. break;
  2609. case DEBUG_KIND_ARRAY:
  2610. ret = (t1->u.karray->lower == t2->u.karray->lower
  2611. && t1->u.karray->upper == t2->u.karray->upper
  2612. && t1->u.karray->stringp == t2->u.karray->stringp
  2613. && debug_type_samep (info, t1->u.karray->element_type,
  2614. t2->u.karray->element_type));
  2615. break;
  2616. case DEBUG_KIND_SET:
  2617. ret = (t1->u.kset->bitstringp == t2->u.kset->bitstringp
  2618. && debug_type_samep (info, t1->u.kset->type, t2->u.kset->type));
  2619. break;
  2620. case DEBUG_KIND_OFFSET:
  2621. ret = (debug_type_samep (info, t1->u.koffset->base_type,
  2622. t2->u.koffset->base_type)
  2623. && debug_type_samep (info, t1->u.koffset->target_type,
  2624. t2->u.koffset->target_type));
  2625. break;
  2626. case DEBUG_KIND_METHOD:
  2627. if (t1->u.kmethod->varargs != t2->u.kmethod->varargs
  2628. || ! debug_type_samep (info, t1->u.kmethod->return_type,
  2629. t2->u.kmethod->return_type)
  2630. || ! debug_type_samep (info, t1->u.kmethod->domain_type,
  2631. t2->u.kmethod->domain_type)
  2632. || ((t1->u.kmethod->arg_types == NULL)
  2633. != (t2->u.kmethod->arg_types == NULL)))
  2634. ret = FALSE;
  2635. else if (t1->u.kmethod->arg_types == NULL)
  2636. ret = TRUE;
  2637. else
  2638. {
  2639. struct debug_type_s **a1, **a2;
  2640. a1 = t1->u.kmethod->arg_types;
  2641. a2 = t2->u.kmethod->arg_types;
  2642. while (*a1 != NULL && *a2 != NULL)
  2643. {
  2644. if (! debug_type_samep (info, *a1, *a2))
  2645. break;
  2646. ++a1;
  2647. ++a2;
  2648. }
  2649. ret = *a1 == NULL && *a2 == NULL;
  2650. }
  2651. break;
  2652. case DEBUG_KIND_CONST:
  2653. ret = debug_type_samep (info, t1->u.kconst, t2->u.kconst);
  2654. break;
  2655. case DEBUG_KIND_VOLATILE:
  2656. ret = debug_type_samep (info, t1->u.kvolatile, t2->u.kvolatile);
  2657. break;
  2658. case DEBUG_KIND_NAMED:
  2659. case DEBUG_KIND_TAGGED:
  2660. ret = (strcmp (t1->u.knamed->name->name, t2->u.knamed->name->name) == 0
  2661. && debug_type_samep (info, t1->u.knamed->type,
  2662. t2->u.knamed->type));
  2663. break;
  2664. }
  2665. info->compare_list = top.next;
  2666. return ret;
  2667. }
  2668. /* See if two classes are the same. This is a subroutine of
  2669. debug_type_samep. */
  2670. static bfd_boolean
  2671. debug_class_type_samep (struct debug_handle *info, struct debug_type_s *t1,
  2672. struct debug_type_s *t2)
  2673. {
  2674. struct debug_class_type *c1, *c2;
  2675. c1 = t1->u.kclass;
  2676. c2 = t2->u.kclass;
  2677. if ((c1->fields == NULL) != (c2->fields == NULL)
  2678. || (c1->baseclasses == NULL) != (c2->baseclasses == NULL)
  2679. || (c1->methods == NULL) != (c2->methods == NULL)
  2680. || (c1->vptrbase == NULL) != (c2->vptrbase == NULL))
  2681. return FALSE;
  2682. if (c1->fields != NULL)
  2683. {
  2684. struct debug_field_s **pf1, **pf2;
  2685. for (pf1 = c1->fields, pf2 = c2->fields;
  2686. *pf1 != NULL && *pf2 != NULL;
  2687. pf1++, pf2++)
  2688. {
  2689. struct debug_field_s *f1, *f2;
  2690. f1 = *pf1;
  2691. f2 = *pf2;
  2692. if (f1->name[0] != f2->name[0]
  2693. || f1->visibility != f2->visibility
  2694. || f1->static_member != f2->static_member)
  2695. return FALSE;
  2696. if (f1->static_member)
  2697. {
  2698. if (strcmp (f1->u.s.physname, f2->u.s.physname) != 0)
  2699. return FALSE;
  2700. }
  2701. else
  2702. {
  2703. if (f1->u.f.bitpos != f2->u.f.bitpos
  2704. || f1->u.f.bitsize != f2->u.f.bitsize)
  2705. return FALSE;
  2706. }
  2707. /* We do the checks which require function calls last. We
  2708. don't require that the types of fields have the same
  2709. names, since that sometimes fails in the presence of
  2710. typedefs and we really don't care. */
  2711. if (strcmp (f1->name, f2->name) != 0
  2712. || ! debug_type_samep (info,
  2713. debug_get_real_type ((void *) info,
  2714. f1->type, NULL),
  2715. debug_get_real_type ((void *) info,
  2716. f2->type, NULL)))
  2717. return FALSE;
  2718. }
  2719. if (*pf1 != NULL || *pf2 != NULL)
  2720. return FALSE;
  2721. }
  2722. if (c1->vptrbase != NULL)
  2723. {
  2724. if (! debug_type_samep (info, c1->vptrbase, c2->vptrbase))
  2725. return FALSE;
  2726. }
  2727. if (c1->baseclasses != NULL)
  2728. {
  2729. struct debug_baseclass_s **pb1, **pb2;
  2730. for (pb1 = c1->baseclasses, pb2 = c2->baseclasses;
  2731. *pb1 != NULL && *pb2 != NULL;
  2732. ++pb1, ++pb2)
  2733. {
  2734. struct debug_baseclass_s *b1, *b2;
  2735. b1 = *pb1;
  2736. b2 = *pb2;
  2737. if (b1->bitpos != b2->bitpos
  2738. || b1->is_virtual != b2->is_virtual
  2739. || b1->visibility != b2->visibility
  2740. || ! debug_type_samep (info, b1->type, b2->type))
  2741. return FALSE;
  2742. }
  2743. if (*pb1 != NULL || *pb2 != NULL)
  2744. return FALSE;
  2745. }
  2746. if (c1->methods != NULL)
  2747. {
  2748. struct debug_method_s **pm1, **pm2;
  2749. for (pm1 = c1->methods, pm2 = c2->methods;
  2750. *pm1 != NULL && *pm2 != NULL;
  2751. ++pm1, ++pm2)
  2752. {
  2753. struct debug_method_s *m1, *m2;
  2754. m1 = *pm1;
  2755. m2 = *pm2;
  2756. if (m1->name[0] != m2->name[0]
  2757. || strcmp (m1->name, m2->name) != 0
  2758. || (m1->variants == NULL) != (m2->variants == NULL))
  2759. return FALSE;
  2760. if (m1->variants == NULL)
  2761. {
  2762. struct debug_method_variant_s **pv1, **pv2;
  2763. for (pv1 = m1->variants, pv2 = m2->variants;
  2764. *pv1 != NULL && *pv2 != NULL;
  2765. ++pv1, ++pv2)
  2766. {
  2767. struct debug_method_variant_s *v1, *v2;
  2768. v1 = *pv1;
  2769. v2 = *pv2;
  2770. if (v1->physname[0] != v2->physname[0]
  2771. || v1->visibility != v2->visibility
  2772. || v1->constp != v2->constp
  2773. || v1->volatilep != v2->volatilep
  2774. || v1->voffset != v2->voffset
  2775. || (v1->context == NULL) != (v2->context == NULL)
  2776. || strcmp (v1->physname, v2->physname) != 0
  2777. || ! debug_type_samep (info, v1->type, v2->type))
  2778. return FALSE;
  2779. if (v1->context != NULL)
  2780. {
  2781. if (! debug_type_samep (info, v1->context,
  2782. v2->context))
  2783. return FALSE;
  2784. }
  2785. }
  2786. if (*pv1 != NULL || *pv2 != NULL)
  2787. return FALSE;
  2788. }
  2789. }
  2790. if (*pm1 != NULL || *pm2 != NULL)
  2791. return FALSE;
  2792. }
  2793. return TRUE;
  2794. }