prdbg.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838
  1. /* prdbg.c -- Print out generic debugging information.
  2. Copyright (C) 1995-2017 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <ian@cygnus.com>.
  4. Tags style generation written by Salvador E. Tropea <set@computer.org>.
  5. This file is part of GNU Binutils.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. /* This file prints out the generic debugging information, by
  19. supplying a set of routines to debug_write. */
  20. #include "sysdep.h"
  21. #include <assert.h>
  22. #include "bfd.h"
  23. #include "libiberty.h"
  24. #include "demangle.h"
  25. #include "debug.h"
  26. #include "budbg.h"
  27. /* This is the structure we use as a handle for these routines. */
  28. struct pr_handle
  29. {
  30. /* File to print information to. */
  31. FILE *f;
  32. /* Current indentation level. */
  33. unsigned int indent;
  34. /* Type stack. */
  35. struct pr_stack *stack;
  36. /* Parameter number we are about to output. */
  37. int parameter;
  38. /* The following are used only by the tags code (tg_). */
  39. /* Name of the file we are using. */
  40. char *filename;
  41. /* The BFD. */
  42. bfd *abfd;
  43. /* The symbols table for this BFD. */
  44. asymbol **syms;
  45. /* Pointer to a function to demangle symbols. */
  46. char *(*demangler) (bfd *, const char *, int);
  47. };
  48. /* The type stack. */
  49. struct pr_stack
  50. {
  51. /* Next element on the stack. */
  52. struct pr_stack *next;
  53. /* This element. */
  54. char *type;
  55. /* Current visibility of fields if this is a class. */
  56. enum debug_visibility visibility;
  57. /* Name of the current method we are handling. */
  58. const char *method;
  59. /* The following are used only by the tags code (tg_). */
  60. /* Type for the container (struct, union, class, union class). */
  61. const char *flavor;
  62. /* A comma separated list of parent classes. */
  63. char *parents;
  64. /* How many parents contains parents. */
  65. int num_parents;
  66. };
  67. static void indent (struct pr_handle *);
  68. static bfd_boolean push_type (struct pr_handle *, const char *);
  69. static bfd_boolean prepend_type (struct pr_handle *, const char *);
  70. static bfd_boolean append_type (struct pr_handle *, const char *);
  71. static bfd_boolean substitute_type (struct pr_handle *, const char *);
  72. static bfd_boolean indent_type (struct pr_handle *);
  73. static char *pop_type (struct pr_handle *);
  74. static void print_vma (bfd_vma, char *, bfd_boolean, bfd_boolean);
  75. static bfd_boolean pr_fix_visibility
  76. (struct pr_handle *, enum debug_visibility);
  77. static bfd_boolean pr_start_compilation_unit (void *, const char *);
  78. static bfd_boolean pr_start_source (void *, const char *);
  79. static bfd_boolean pr_empty_type (void *);
  80. static bfd_boolean pr_void_type (void *);
  81. static bfd_boolean pr_int_type (void *, unsigned int, bfd_boolean);
  82. static bfd_boolean pr_float_type (void *, unsigned int);
  83. static bfd_boolean pr_complex_type (void *, unsigned int);
  84. static bfd_boolean pr_bool_type (void *, unsigned int);
  85. static bfd_boolean pr_enum_type
  86. (void *, const char *, const char **, bfd_signed_vma *);
  87. static bfd_boolean pr_pointer_type (void *);
  88. static bfd_boolean pr_function_type (void *, int, bfd_boolean);
  89. static bfd_boolean pr_reference_type (void *);
  90. static bfd_boolean pr_range_type (void *, bfd_signed_vma, bfd_signed_vma);
  91. static bfd_boolean pr_array_type
  92. (void *, bfd_signed_vma, bfd_signed_vma, bfd_boolean);
  93. static bfd_boolean pr_set_type (void *, bfd_boolean);
  94. static bfd_boolean pr_offset_type (void *);
  95. static bfd_boolean pr_method_type (void *, bfd_boolean, int, bfd_boolean);
  96. static bfd_boolean pr_const_type (void *);
  97. static bfd_boolean pr_volatile_type (void *);
  98. static bfd_boolean pr_start_struct_type
  99. (void *, const char *, unsigned int, bfd_boolean, unsigned int);
  100. static bfd_boolean pr_struct_field
  101. (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
  102. static bfd_boolean pr_end_struct_type (void *);
  103. static bfd_boolean pr_start_class_type
  104. (void *, const char *, unsigned int, bfd_boolean, unsigned int,
  105. bfd_boolean, bfd_boolean);
  106. static bfd_boolean pr_class_static_member
  107. (void *, const char *, const char *, enum debug_visibility);
  108. static bfd_boolean pr_class_baseclass
  109. (void *, bfd_vma, bfd_boolean, enum debug_visibility);
  110. static bfd_boolean pr_class_start_method (void *, const char *);
  111. static bfd_boolean pr_class_method_variant
  112. (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
  113. bfd_vma, bfd_boolean);
  114. static bfd_boolean pr_class_static_method_variant
  115. (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
  116. static bfd_boolean pr_class_end_method (void *);
  117. static bfd_boolean pr_end_class_type (void *);
  118. static bfd_boolean pr_typedef_type (void *, const char *);
  119. static bfd_boolean pr_tag_type
  120. (void *, const char *, unsigned int, enum debug_type_kind);
  121. static bfd_boolean pr_typdef (void *, const char *);
  122. static bfd_boolean pr_tag (void *, const char *);
  123. static bfd_boolean pr_int_constant (void *, const char *, bfd_vma);
  124. static bfd_boolean pr_float_constant (void *, const char *, double);
  125. static bfd_boolean pr_typed_constant (void *, const char *, bfd_vma);
  126. static bfd_boolean pr_variable
  127. (void *, const char *, enum debug_var_kind, bfd_vma);
  128. static bfd_boolean pr_start_function (void *, const char *, bfd_boolean);
  129. static bfd_boolean pr_function_parameter
  130. (void *, const char *, enum debug_parm_kind, bfd_vma);
  131. static bfd_boolean pr_start_block (void *, bfd_vma);
  132. static bfd_boolean pr_end_block (void *, bfd_vma);
  133. static bfd_boolean pr_end_function (void *);
  134. static bfd_boolean pr_lineno (void *, const char *, unsigned long, bfd_vma);
  135. static bfd_boolean append_parent (struct pr_handle *, const char *);
  136. /* Only used by tg_ code. */
  137. static bfd_boolean tg_fix_visibility
  138. (struct pr_handle *, enum debug_visibility);
  139. static void find_address_in_section (bfd *, asection *, void *);
  140. static void translate_addresses (bfd *, char *, FILE *, asymbol **);
  141. static const char *visibility_name (enum debug_visibility);
  142. /* Tags style replacements. */
  143. static bfd_boolean tg_start_compilation_unit (void *, const char *);
  144. static bfd_boolean tg_start_source (void *, const char *);
  145. static bfd_boolean tg_enum_type
  146. (void *, const char *, const char **, bfd_signed_vma *);
  147. static bfd_boolean tg_start_struct_type
  148. (void *, const char *, unsigned int, bfd_boolean, unsigned int);
  149. static bfd_boolean pr_struct_field
  150. (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
  151. static bfd_boolean tg_struct_field
  152. (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
  153. static bfd_boolean tg_struct_field
  154. (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
  155. static bfd_boolean tg_end_struct_type (void *);
  156. static bfd_boolean tg_start_class_type
  157. (void *, const char *, unsigned int, bfd_boolean, unsigned int, bfd_boolean, bfd_boolean);
  158. static bfd_boolean tg_class_static_member
  159. (void *, const char *, const char *, enum debug_visibility);
  160. static bfd_boolean tg_class_baseclass
  161. (void *, bfd_vma, bfd_boolean, enum debug_visibility);
  162. static bfd_boolean tg_class_method_variant
  163. (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean);
  164. static bfd_boolean tg_class_static_method_variant
  165. (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
  166. static bfd_boolean tg_end_class_type (void *);
  167. static bfd_boolean tg_tag_type
  168. (void *, const char *, unsigned int, enum debug_type_kind);
  169. static bfd_boolean tg_typdef (void *, const char *);
  170. static bfd_boolean tg_tag (void *, const char *);
  171. static bfd_boolean tg_int_constant (void *, const char *, bfd_vma);
  172. static bfd_boolean tg_float_constant (void *, const char *, double);
  173. static bfd_boolean tg_typed_constant (void *, const char *, bfd_vma);
  174. static bfd_boolean tg_variable
  175. (void *, const char *, enum debug_var_kind, bfd_vma);
  176. static bfd_boolean tg_start_function (void *, const char *, bfd_boolean);
  177. static bfd_boolean tg_function_parameter
  178. (void *, const char *, enum debug_parm_kind, bfd_vma);
  179. static bfd_boolean tg_start_block (void *, bfd_vma);
  180. static bfd_boolean tg_end_block (void *, bfd_vma);
  181. static bfd_boolean tg_lineno (void *, const char *, unsigned long, bfd_vma);
  182. static const struct debug_write_fns pr_fns =
  183. {
  184. pr_start_compilation_unit,
  185. pr_start_source,
  186. pr_empty_type,
  187. pr_void_type,
  188. pr_int_type,
  189. pr_float_type,
  190. pr_complex_type,
  191. pr_bool_type,
  192. pr_enum_type,
  193. pr_pointer_type,
  194. pr_function_type,
  195. pr_reference_type,
  196. pr_range_type,
  197. pr_array_type,
  198. pr_set_type,
  199. pr_offset_type,
  200. pr_method_type,
  201. pr_const_type,
  202. pr_volatile_type,
  203. pr_start_struct_type,
  204. pr_struct_field,
  205. pr_end_struct_type,
  206. pr_start_class_type,
  207. pr_class_static_member,
  208. pr_class_baseclass,
  209. pr_class_start_method,
  210. pr_class_method_variant,
  211. pr_class_static_method_variant,
  212. pr_class_end_method,
  213. pr_end_class_type,
  214. pr_typedef_type,
  215. pr_tag_type,
  216. pr_typdef,
  217. pr_tag,
  218. pr_int_constant,
  219. pr_float_constant,
  220. pr_typed_constant,
  221. pr_variable,
  222. pr_start_function,
  223. pr_function_parameter,
  224. pr_start_block,
  225. pr_end_block,
  226. pr_end_function,
  227. pr_lineno
  228. };
  229. static const struct debug_write_fns tg_fns =
  230. {
  231. tg_start_compilation_unit,
  232. tg_start_source,
  233. pr_empty_type, /* Same, push_type. */
  234. pr_void_type, /* Same, push_type. */
  235. pr_int_type, /* Same, push_type. */
  236. pr_float_type, /* Same, push_type. */
  237. pr_complex_type, /* Same, push_type. */
  238. pr_bool_type, /* Same, push_type. */
  239. tg_enum_type,
  240. pr_pointer_type, /* Same, changes to pointer. */
  241. pr_function_type, /* Same, push_type. */
  242. pr_reference_type, /* Same, changes to reference. */
  243. pr_range_type, /* FIXME: What's that?. */
  244. pr_array_type, /* Same, push_type. */
  245. pr_set_type, /* FIXME: What's that?. */
  246. pr_offset_type, /* FIXME: What's that?. */
  247. pr_method_type, /* Same. */
  248. pr_const_type, /* Same, changes to const. */
  249. pr_volatile_type, /* Same, changes to volatile. */
  250. tg_start_struct_type,
  251. tg_struct_field,
  252. tg_end_struct_type,
  253. tg_start_class_type,
  254. tg_class_static_member,
  255. tg_class_baseclass,
  256. pr_class_start_method, /* Same, remembers that's a method. */
  257. tg_class_method_variant,
  258. tg_class_static_method_variant,
  259. pr_class_end_method, /* Same, forgets that's a method. */
  260. tg_end_class_type,
  261. pr_typedef_type, /* Same, just push type. */
  262. tg_tag_type,
  263. tg_typdef,
  264. tg_tag,
  265. tg_int_constant, /* Untested. */
  266. tg_float_constant, /* Untested. */
  267. tg_typed_constant, /* Untested. */
  268. tg_variable,
  269. tg_start_function,
  270. tg_function_parameter,
  271. tg_start_block,
  272. tg_end_block,
  273. pr_end_function, /* Same, does nothing. */
  274. tg_lineno
  275. };
  276. /* Print out the generic debugging information recorded in dhandle. */
  277. bfd_boolean
  278. print_debugging_info (FILE *f, void *dhandle, bfd *abfd, asymbol **syms,
  279. char * (*demangler) (struct bfd *, const char *, int),
  280. bfd_boolean as_tags)
  281. {
  282. struct pr_handle info;
  283. info.f = f;
  284. info.indent = 0;
  285. info.stack = NULL;
  286. info.parameter = 0;
  287. info.filename = NULL;
  288. info.abfd = abfd;
  289. info.syms = syms;
  290. info.demangler = demangler;
  291. if (as_tags)
  292. {
  293. fputs ("!_TAG_FILE_FORMAT\t2\t/extended format/\n", f);
  294. fputs ("!_TAG_FILE_SORTED\t0\t/0=unsorted, 1=sorted/\n", f);
  295. fputs ("!_TAG_PROGRAM_AUTHOR\tIan Lance Taylor, Salvador E. Tropea and others\t//\n", f);
  296. fputs ("!_TAG_PROGRAM_NAME\tobjdump\t/From GNU binutils/\n", f);
  297. }
  298. return as_tags ? debug_write (dhandle, &tg_fns, (void *) & info)
  299. : debug_write (dhandle, &pr_fns, (void *) & info);
  300. }
  301. /* Indent to the current indentation level. */
  302. static void
  303. indent (struct pr_handle *info)
  304. {
  305. unsigned int i;
  306. for (i = 0; i < info->indent; i++)
  307. putc (' ', info->f);
  308. }
  309. /* Push a type on the type stack. */
  310. static bfd_boolean
  311. push_type (struct pr_handle *info, const char *type)
  312. {
  313. struct pr_stack *n;
  314. if (type == NULL)
  315. return FALSE;
  316. n = (struct pr_stack *) xmalloc (sizeof *n);
  317. memset (n, 0, sizeof *n);
  318. n->type = xstrdup (type);
  319. n->visibility = DEBUG_VISIBILITY_IGNORE;
  320. n->method = NULL;
  321. n->next = info->stack;
  322. info->stack = n;
  323. return TRUE;
  324. }
  325. /* Prepend a string onto the type on the top of the type stack. */
  326. static bfd_boolean
  327. prepend_type (struct pr_handle *info, const char *s)
  328. {
  329. char *n;
  330. assert (info->stack != NULL);
  331. n = (char *) xmalloc (strlen (s) + strlen (info->stack->type) + 1);
  332. sprintf (n, "%s%s", s, info->stack->type);
  333. free (info->stack->type);
  334. info->stack->type = n;
  335. return TRUE;
  336. }
  337. /* Append a string to the type on the top of the type stack. */
  338. static bfd_boolean
  339. append_type (struct pr_handle *info, const char *s)
  340. {
  341. unsigned int len;
  342. if (s == NULL)
  343. return FALSE;
  344. assert (info->stack != NULL);
  345. len = strlen (info->stack->type);
  346. info->stack->type = (char *) xrealloc (info->stack->type,
  347. len + strlen (s) + 1);
  348. strcpy (info->stack->type + len, s);
  349. return TRUE;
  350. }
  351. /* Append a string to the parents on the top of the type stack. */
  352. static bfd_boolean
  353. append_parent (struct pr_handle *info, const char *s)
  354. {
  355. unsigned int len;
  356. if (s == NULL)
  357. return FALSE;
  358. assert (info->stack != NULL);
  359. len = info->stack->parents ? strlen (info->stack->parents) : 0;
  360. info->stack->parents = (char *) xrealloc (info->stack->parents,
  361. len + strlen (s) + 1);
  362. strcpy (info->stack->parents + len, s);
  363. return TRUE;
  364. }
  365. /* We use an underscore to indicate where the name should go in a type
  366. string. This function substitutes a string for the underscore. If
  367. there is no underscore, the name follows the type. */
  368. static bfd_boolean
  369. substitute_type (struct pr_handle *info, const char *s)
  370. {
  371. char *u;
  372. assert (info->stack != NULL);
  373. u = strchr (info->stack->type, '|');
  374. if (u != NULL)
  375. {
  376. char *n;
  377. n = (char *) xmalloc (strlen (info->stack->type) + strlen (s));
  378. memcpy (n, info->stack->type, u - info->stack->type);
  379. strcpy (n + (u - info->stack->type), s);
  380. strcat (n, u + 1);
  381. free (info->stack->type);
  382. info->stack->type = n;
  383. return TRUE;
  384. }
  385. if (strchr (s, '|') != NULL
  386. && (strchr (info->stack->type, '{') != NULL
  387. || strchr (info->stack->type, '(') != NULL))
  388. {
  389. if (! prepend_type (info, "(")
  390. || ! append_type (info, ")"))
  391. return FALSE;
  392. }
  393. if (*s == '\0')
  394. return TRUE;
  395. return (append_type (info, " ")
  396. && append_type (info, s));
  397. }
  398. /* Indent the type at the top of the stack by appending spaces. */
  399. static bfd_boolean
  400. indent_type (struct pr_handle *info)
  401. {
  402. unsigned int i;
  403. for (i = 0; i < info->indent; i++)
  404. {
  405. if (! append_type (info, " "))
  406. return FALSE;
  407. }
  408. return TRUE;
  409. }
  410. /* Pop a type from the type stack. */
  411. static char *
  412. pop_type (struct pr_handle *info)
  413. {
  414. struct pr_stack *o;
  415. char *ret;
  416. assert (info->stack != NULL);
  417. o = info->stack;
  418. info->stack = o->next;
  419. ret = o->type;
  420. free (o);
  421. return ret;
  422. }
  423. /* Print a VMA value into a string. */
  424. static void
  425. print_vma (bfd_vma vma, char *buf, bfd_boolean unsignedp, bfd_boolean hexp)
  426. {
  427. if (sizeof (vma) <= sizeof (unsigned long))
  428. {
  429. if (hexp)
  430. sprintf (buf, "0x%lx", (unsigned long) vma);
  431. else if (unsignedp)
  432. sprintf (buf, "%lu", (unsigned long) vma);
  433. else
  434. sprintf (buf, "%ld", (long) vma);
  435. }
  436. #if BFD_HOST_64BIT_LONG_LONG
  437. else if (sizeof (vma) <= sizeof (unsigned long long))
  438. {
  439. #ifndef __MSVCRT__
  440. if (hexp)
  441. sprintf (buf, "0x%llx", (unsigned long long) vma);
  442. else if (unsignedp)
  443. sprintf (buf, "%llu", (unsigned long long) vma);
  444. else
  445. sprintf (buf, "%lld", (long long) vma);
  446. #else
  447. if (hexp)
  448. sprintf (buf, "0x%I64x", (unsigned long long) vma);
  449. else if (unsignedp)
  450. sprintf (buf, "%I64u", (unsigned long long) vma);
  451. else
  452. sprintf (buf, "%I64d", (long long) vma);
  453. #endif
  454. }
  455. #endif
  456. else
  457. {
  458. buf[0] = '0';
  459. buf[1] = 'x';
  460. sprintf_vma (buf + 2, vma);
  461. }
  462. }
  463. /* Start a new compilation unit. */
  464. static bfd_boolean
  465. pr_start_compilation_unit (void *p, const char *filename)
  466. {
  467. struct pr_handle *info = (struct pr_handle *) p;
  468. assert (info->indent == 0);
  469. fprintf (info->f, "%s:\n", filename);
  470. return TRUE;
  471. }
  472. /* Start a source file within a compilation unit. */
  473. static bfd_boolean
  474. pr_start_source (void *p, const char *filename)
  475. {
  476. struct pr_handle *info = (struct pr_handle *) p;
  477. assert (info->indent == 0);
  478. fprintf (info->f, " %s:\n", filename);
  479. return TRUE;
  480. }
  481. /* Push an empty type onto the type stack. */
  482. static bfd_boolean
  483. pr_empty_type (void *p)
  484. {
  485. struct pr_handle *info = (struct pr_handle *) p;
  486. return push_type (info, "<undefined>");
  487. }
  488. /* Push a void type onto the type stack. */
  489. static bfd_boolean
  490. pr_void_type (void *p)
  491. {
  492. struct pr_handle *info = (struct pr_handle *) p;
  493. return push_type (info, "void");
  494. }
  495. /* Push an integer type onto the type stack. */
  496. static bfd_boolean
  497. pr_int_type (void *p, unsigned int size, bfd_boolean unsignedp)
  498. {
  499. struct pr_handle *info = (struct pr_handle *) p;
  500. char ab[40];
  501. sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
  502. return push_type (info, ab);
  503. }
  504. /* Push a floating type onto the type stack. */
  505. static bfd_boolean
  506. pr_float_type (void *p, unsigned int size)
  507. {
  508. struct pr_handle *info = (struct pr_handle *) p;
  509. char ab[40];
  510. if (size == 4)
  511. return push_type (info, "float");
  512. else if (size == 8)
  513. return push_type (info, "double");
  514. sprintf (ab, "float%d", size * 8);
  515. return push_type (info, ab);
  516. }
  517. /* Push a complex type onto the type stack. */
  518. static bfd_boolean
  519. pr_complex_type (void *p, unsigned int size)
  520. {
  521. struct pr_handle *info = (struct pr_handle *) p;
  522. if (! pr_float_type (p, size))
  523. return FALSE;
  524. return prepend_type (info, "complex ");
  525. }
  526. /* Push a bfd_boolean type onto the type stack. */
  527. static bfd_boolean
  528. pr_bool_type (void *p, unsigned int size)
  529. {
  530. struct pr_handle *info = (struct pr_handle *) p;
  531. char ab[40];
  532. sprintf (ab, "bool%d", size * 8);
  533. return push_type (info, ab);
  534. }
  535. /* Push an enum type onto the type stack. */
  536. static bfd_boolean
  537. pr_enum_type (void *p, const char *tag, const char **names,
  538. bfd_signed_vma *values)
  539. {
  540. struct pr_handle *info = (struct pr_handle *) p;
  541. unsigned int i;
  542. bfd_signed_vma val;
  543. if (! push_type (info, "enum "))
  544. return FALSE;
  545. if (tag != NULL)
  546. {
  547. if (! append_type (info, tag)
  548. || ! append_type (info, " "))
  549. return FALSE;
  550. }
  551. if (! append_type (info, "{ "))
  552. return FALSE;
  553. if (names == NULL)
  554. {
  555. if (! append_type (info, "/* undefined */"))
  556. return FALSE;
  557. }
  558. else
  559. {
  560. val = 0;
  561. for (i = 0; names[i] != NULL; i++)
  562. {
  563. if (i > 0)
  564. {
  565. if (! append_type (info, ", "))
  566. return FALSE;
  567. }
  568. if (! append_type (info, names[i]))
  569. return FALSE;
  570. if (values[i] != val)
  571. {
  572. char ab[22];
  573. print_vma (values[i], ab, FALSE, FALSE);
  574. if (! append_type (info, " = ")
  575. || ! append_type (info, ab))
  576. return FALSE;
  577. val = values[i];
  578. }
  579. ++val;
  580. }
  581. }
  582. return append_type (info, " }");
  583. }
  584. /* Turn the top type on the stack into a pointer. */
  585. static bfd_boolean
  586. pr_pointer_type (void *p)
  587. {
  588. struct pr_handle *info = (struct pr_handle *) p;
  589. char *s;
  590. assert (info->stack != NULL);
  591. s = strchr (info->stack->type, '|');
  592. if (s != NULL && s[1] == '[')
  593. return substitute_type (info, "(*|)");
  594. return substitute_type (info, "*|");
  595. }
  596. /* Turn the top type on the stack into a function returning that type. */
  597. static bfd_boolean
  598. pr_function_type (void *p, int argcount, bfd_boolean varargs)
  599. {
  600. struct pr_handle *info = (struct pr_handle *) p;
  601. char **arg_types;
  602. unsigned int len;
  603. char *s;
  604. assert (info->stack != NULL);
  605. len = 10;
  606. if (argcount <= 0)
  607. {
  608. arg_types = NULL;
  609. len += 15;
  610. }
  611. else
  612. {
  613. int i;
  614. arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
  615. for (i = argcount - 1; i >= 0; i--)
  616. {
  617. if (! substitute_type (info, ""))
  618. {
  619. free (arg_types);
  620. return FALSE;
  621. }
  622. arg_types[i] = pop_type (info);
  623. if (arg_types[i] == NULL)
  624. {
  625. free (arg_types);
  626. return FALSE;
  627. }
  628. len += strlen (arg_types[i]) + 2;
  629. }
  630. if (varargs)
  631. len += 5;
  632. }
  633. /* Now the return type is on the top of the stack. */
  634. s = (char *) xmalloc (len);
  635. LITSTRCPY (s, "(|) (");
  636. if (argcount < 0)
  637. strcat (s, "/* unknown */");
  638. else
  639. {
  640. int i;
  641. for (i = 0; i < argcount; i++)
  642. {
  643. if (i > 0)
  644. strcat (s, ", ");
  645. strcat (s, arg_types[i]);
  646. }
  647. if (varargs)
  648. {
  649. if (i > 0)
  650. strcat (s, ", ");
  651. strcat (s, "...");
  652. }
  653. if (argcount > 0)
  654. free (arg_types);
  655. }
  656. strcat (s, ")");
  657. if (! substitute_type (info, s))
  658. return FALSE;
  659. free (s);
  660. return TRUE;
  661. }
  662. /* Turn the top type on the stack into a reference to that type. */
  663. static bfd_boolean
  664. pr_reference_type (void *p)
  665. {
  666. struct pr_handle *info = (struct pr_handle *) p;
  667. assert (info->stack != NULL);
  668. return substitute_type (info, "&|");
  669. }
  670. /* Make a range type. */
  671. static bfd_boolean
  672. pr_range_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper)
  673. {
  674. struct pr_handle *info = (struct pr_handle *) p;
  675. char abl[22], abu[22];
  676. assert (info->stack != NULL);
  677. if (! substitute_type (info, ""))
  678. return FALSE;
  679. print_vma (lower, abl, FALSE, FALSE);
  680. print_vma (upper, abu, FALSE, FALSE);
  681. return (prepend_type (info, "range (")
  682. && append_type (info, "):")
  683. && append_type (info, abl)
  684. && append_type (info, ":")
  685. && append_type (info, abu));
  686. }
  687. /* Make an array type. */
  688. static bfd_boolean
  689. pr_array_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper,
  690. bfd_boolean stringp)
  691. {
  692. struct pr_handle *info = (struct pr_handle *) p;
  693. char *range_type;
  694. char abl[22], abu[22], ab[50];
  695. range_type = pop_type (info);
  696. if (range_type == NULL)
  697. return FALSE;
  698. if (lower == 0)
  699. {
  700. if (upper == -1)
  701. sprintf (ab, "|[]");
  702. else
  703. {
  704. print_vma (upper + 1, abu, FALSE, FALSE);
  705. sprintf (ab, "|[%s]", abu);
  706. }
  707. }
  708. else
  709. {
  710. print_vma (lower, abl, FALSE, FALSE);
  711. print_vma (upper, abu, FALSE, FALSE);
  712. sprintf (ab, "|[%s:%s]", abl, abu);
  713. }
  714. if (! substitute_type (info, ab))
  715. return FALSE;
  716. if (strcmp (range_type, "int") != 0)
  717. {
  718. if (! append_type (info, ":")
  719. || ! append_type (info, range_type))
  720. return FALSE;
  721. }
  722. if (stringp)
  723. {
  724. if (! append_type (info, " /* string */"))
  725. return FALSE;
  726. }
  727. return TRUE;
  728. }
  729. /* Make a set type. */
  730. static bfd_boolean
  731. pr_set_type (void *p, bfd_boolean bitstringp)
  732. {
  733. struct pr_handle *info = (struct pr_handle *) p;
  734. if (! substitute_type (info, ""))
  735. return FALSE;
  736. if (! prepend_type (info, "set { ")
  737. || ! append_type (info, " }"))
  738. return FALSE;
  739. if (bitstringp)
  740. {
  741. if (! append_type (info, "/* bitstring */"))
  742. return FALSE;
  743. }
  744. return TRUE;
  745. }
  746. /* Make an offset type. */
  747. static bfd_boolean
  748. pr_offset_type (void *p)
  749. {
  750. struct pr_handle *info = (struct pr_handle *) p;
  751. char *t;
  752. if (! substitute_type (info, ""))
  753. return FALSE;
  754. t = pop_type (info);
  755. if (t == NULL)
  756. return FALSE;
  757. return (substitute_type (info, "")
  758. && prepend_type (info, " ")
  759. && prepend_type (info, t)
  760. && append_type (info, "::|"));
  761. }
  762. /* Make a method type. */
  763. static bfd_boolean
  764. pr_method_type (void *p, bfd_boolean domain, int argcount, bfd_boolean varargs)
  765. {
  766. struct pr_handle *info = (struct pr_handle *) p;
  767. unsigned int len;
  768. char *domain_type;
  769. char **arg_types;
  770. char *s;
  771. len = 10;
  772. if (! domain)
  773. domain_type = NULL;
  774. else
  775. {
  776. if (! substitute_type (info, ""))
  777. return FALSE;
  778. domain_type = pop_type (info);
  779. if (domain_type == NULL)
  780. return FALSE;
  781. if (CONST_STRNEQ (domain_type, "class ")
  782. && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
  783. domain_type += sizeof "class " - 1;
  784. else if (CONST_STRNEQ (domain_type, "union class ")
  785. && (strchr (domain_type + sizeof "union class " - 1, ' ')
  786. == NULL))
  787. domain_type += sizeof "union class " - 1;
  788. len += strlen (domain_type);
  789. }
  790. if (argcount <= 0)
  791. {
  792. arg_types = NULL;
  793. len += 15;
  794. }
  795. else
  796. {
  797. int i;
  798. arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
  799. for (i = argcount - 1; i >= 0; i--)
  800. {
  801. if (! substitute_type (info, ""))
  802. {
  803. free (arg_types);
  804. return FALSE;
  805. }
  806. arg_types[i] = pop_type (info);
  807. if (arg_types[i] == NULL)
  808. {
  809. free (arg_types);
  810. return FALSE;
  811. }
  812. len += strlen (arg_types[i]) + 2;
  813. }
  814. if (varargs)
  815. len += 5;
  816. }
  817. /* Now the return type is on the top of the stack. */
  818. s = (char *) xmalloc (len);
  819. if (! domain)
  820. *s = '\0';
  821. else
  822. strcpy (s, domain_type);
  823. strcat (s, "::| (");
  824. if (argcount < 0)
  825. strcat (s, "/* unknown */");
  826. else
  827. {
  828. int i;
  829. for (i = 0; i < argcount; i++)
  830. {
  831. if (i > 0)
  832. strcat (s, ", ");
  833. strcat (s, arg_types[i]);
  834. }
  835. if (varargs)
  836. {
  837. if (i > 0)
  838. strcat (s, ", ");
  839. strcat (s, "...");
  840. }
  841. if (argcount > 0)
  842. free (arg_types);
  843. }
  844. strcat (s, ")");
  845. if (! substitute_type (info, s))
  846. return FALSE;
  847. free (s);
  848. return TRUE;
  849. }
  850. /* Make a const qualified type. */
  851. static bfd_boolean
  852. pr_const_type (void *p)
  853. {
  854. struct pr_handle *info = (struct pr_handle *) p;
  855. return substitute_type (info, "const |");
  856. }
  857. /* Make a volatile qualified type. */
  858. static bfd_boolean
  859. pr_volatile_type (void *p)
  860. {
  861. struct pr_handle *info = (struct pr_handle *) p;
  862. return substitute_type (info, "volatile |");
  863. }
  864. /* Start accumulating a struct type. */
  865. static bfd_boolean
  866. pr_start_struct_type (void *p, const char *tag, unsigned int id,
  867. bfd_boolean structp, unsigned int size)
  868. {
  869. struct pr_handle *info = (struct pr_handle *) p;
  870. info->indent += 2;
  871. if (! push_type (info, structp ? "struct " : "union "))
  872. return FALSE;
  873. if (tag != NULL)
  874. {
  875. if (! append_type (info, tag))
  876. return FALSE;
  877. }
  878. else
  879. {
  880. char idbuf[20];
  881. sprintf (idbuf, "%%anon%u", id);
  882. if (! append_type (info, idbuf))
  883. return FALSE;
  884. }
  885. if (! append_type (info, " {"))
  886. return FALSE;
  887. if (size != 0 || tag != NULL)
  888. {
  889. char ab[30];
  890. if (! append_type (info, " /*"))
  891. return FALSE;
  892. if (size != 0)
  893. {
  894. sprintf (ab, " size %u", size);
  895. if (! append_type (info, ab))
  896. return FALSE;
  897. }
  898. if (tag != NULL)
  899. {
  900. sprintf (ab, " id %u", id);
  901. if (! append_type (info, ab))
  902. return FALSE;
  903. }
  904. if (! append_type (info, " */"))
  905. return FALSE;
  906. }
  907. if (! append_type (info, "\n"))
  908. return FALSE;
  909. info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
  910. return indent_type (info);
  911. }
  912. /* Output the visibility of a field in a struct. */
  913. static bfd_boolean
  914. pr_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
  915. {
  916. const char *s = NULL;
  917. char *t;
  918. unsigned int len;
  919. assert (info->stack != NULL);
  920. if (info->stack->visibility == visibility)
  921. return TRUE;
  922. switch (visibility)
  923. {
  924. case DEBUG_VISIBILITY_PUBLIC:
  925. s = "public";
  926. break;
  927. case DEBUG_VISIBILITY_PRIVATE:
  928. s = "private";
  929. break;
  930. case DEBUG_VISIBILITY_PROTECTED:
  931. s = "protected";
  932. break;
  933. case DEBUG_VISIBILITY_IGNORE:
  934. s = "/* ignore */";
  935. break;
  936. default:
  937. abort ();
  938. return FALSE;
  939. }
  940. /* Trim off a trailing space in the struct string, to make the
  941. output look a bit better, then stick on the visibility string. */
  942. t = info->stack->type;
  943. len = strlen (t);
  944. assert (t[len - 1] == ' ');
  945. t[len - 1] = '\0';
  946. if (! append_type (info, s)
  947. || ! append_type (info, ":\n")
  948. || ! indent_type (info))
  949. return FALSE;
  950. info->stack->visibility = visibility;
  951. return TRUE;
  952. }
  953. /* Add a field to a struct type. */
  954. static bfd_boolean
  955. pr_struct_field (void *p, const char *name, bfd_vma bitpos, bfd_vma bitsize,
  956. enum debug_visibility visibility)
  957. {
  958. struct pr_handle *info = (struct pr_handle *) p;
  959. char ab[22];
  960. char *t;
  961. if (! substitute_type (info, name))
  962. return FALSE;
  963. if (! append_type (info, "; /* "))
  964. return FALSE;
  965. if (bitsize != 0)
  966. {
  967. print_vma (bitsize, ab, TRUE, FALSE);
  968. if (! append_type (info, "bitsize ")
  969. || ! append_type (info, ab)
  970. || ! append_type (info, ", "))
  971. return FALSE;
  972. }
  973. print_vma (bitpos, ab, TRUE, FALSE);
  974. if (! append_type (info, "bitpos ")
  975. || ! append_type (info, ab)
  976. || ! append_type (info, " */\n")
  977. || ! indent_type (info))
  978. return FALSE;
  979. t = pop_type (info);
  980. if (t == NULL)
  981. return FALSE;
  982. if (! pr_fix_visibility (info, visibility))
  983. return FALSE;
  984. return append_type (info, t);
  985. }
  986. /* Finish a struct type. */
  987. static bfd_boolean
  988. pr_end_struct_type (void *p)
  989. {
  990. struct pr_handle *info = (struct pr_handle *) p;
  991. char *s;
  992. assert (info->stack != NULL);
  993. assert (info->indent >= 2);
  994. info->indent -= 2;
  995. /* Change the trailing indentation to have a close brace. */
  996. s = info->stack->type + strlen (info->stack->type) - 2;
  997. assert (s[0] == ' ' && s[1] == ' ' && s[2] == '\0');
  998. *s++ = '}';
  999. *s = '\0';
  1000. return TRUE;
  1001. }
  1002. /* Start a class type. */
  1003. static bfd_boolean
  1004. pr_start_class_type (void *p, const char *tag, unsigned int id,
  1005. bfd_boolean structp, unsigned int size,
  1006. bfd_boolean vptr, bfd_boolean ownvptr)
  1007. {
  1008. struct pr_handle *info = (struct pr_handle *) p;
  1009. char *tv = NULL;
  1010. info->indent += 2;
  1011. if (vptr && ! ownvptr)
  1012. {
  1013. tv = pop_type (info);
  1014. if (tv == NULL)
  1015. return FALSE;
  1016. }
  1017. if (! push_type (info, structp ? "class " : "union class "))
  1018. return FALSE;
  1019. if (tag != NULL)
  1020. {
  1021. if (! append_type (info, tag))
  1022. return FALSE;
  1023. }
  1024. else
  1025. {
  1026. char idbuf[20];
  1027. sprintf (idbuf, "%%anon%u", id);
  1028. if (! append_type (info, idbuf))
  1029. return FALSE;
  1030. }
  1031. if (! append_type (info, " {"))
  1032. return FALSE;
  1033. if (size != 0 || vptr || ownvptr || tag != NULL)
  1034. {
  1035. if (! append_type (info, " /*"))
  1036. return FALSE;
  1037. if (size != 0)
  1038. {
  1039. char ab[20];
  1040. sprintf (ab, "%u", size);
  1041. if (! append_type (info, " size ")
  1042. || ! append_type (info, ab))
  1043. return FALSE;
  1044. }
  1045. if (vptr)
  1046. {
  1047. if (! append_type (info, " vtable "))
  1048. return FALSE;
  1049. if (ownvptr)
  1050. {
  1051. if (! append_type (info, "self "))
  1052. return FALSE;
  1053. }
  1054. else
  1055. {
  1056. if (! append_type (info, tv)
  1057. || ! append_type (info, " "))
  1058. return FALSE;
  1059. }
  1060. }
  1061. if (tag != NULL)
  1062. {
  1063. char ab[30];
  1064. sprintf (ab, " id %u", id);
  1065. if (! append_type (info, ab))
  1066. return FALSE;
  1067. }
  1068. if (! append_type (info, " */"))
  1069. return FALSE;
  1070. }
  1071. info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
  1072. return (append_type (info, "\n")
  1073. && indent_type (info));
  1074. }
  1075. /* Add a static member to a class. */
  1076. static bfd_boolean
  1077. pr_class_static_member (void *p, const char *name, const char *physname,
  1078. enum debug_visibility visibility)
  1079. {
  1080. struct pr_handle *info = (struct pr_handle *) p;
  1081. char *t;
  1082. if (! substitute_type (info, name))
  1083. return FALSE;
  1084. if (! prepend_type (info, "static ")
  1085. || ! append_type (info, "; /* ")
  1086. || ! append_type (info, physname)
  1087. || ! append_type (info, " */\n")
  1088. || ! indent_type (info))
  1089. return FALSE;
  1090. t = pop_type (info);
  1091. if (t == NULL)
  1092. return FALSE;
  1093. if (! pr_fix_visibility (info, visibility))
  1094. return FALSE;
  1095. return append_type (info, t);
  1096. }
  1097. /* Add a base class to a class. */
  1098. static bfd_boolean
  1099. pr_class_baseclass (void *p, bfd_vma bitpos, bfd_boolean is_virtual,
  1100. enum debug_visibility visibility)
  1101. {
  1102. struct pr_handle *info = (struct pr_handle *) p;
  1103. char *t;
  1104. const char *prefix;
  1105. char ab[22];
  1106. char *s, *l, *n;
  1107. assert (info->stack != NULL && info->stack->next != NULL);
  1108. if (! substitute_type (info, ""))
  1109. return FALSE;
  1110. t = pop_type (info);
  1111. if (t == NULL)
  1112. return FALSE;
  1113. if (CONST_STRNEQ (t, "class "))
  1114. t += sizeof "class " - 1;
  1115. /* Push it back on to take advantage of the prepend_type and
  1116. append_type routines. */
  1117. if (! push_type (info, t))
  1118. return FALSE;
  1119. if (is_virtual)
  1120. {
  1121. if (! prepend_type (info, "virtual "))
  1122. return FALSE;
  1123. }
  1124. switch (visibility)
  1125. {
  1126. case DEBUG_VISIBILITY_PUBLIC:
  1127. prefix = "public ";
  1128. break;
  1129. case DEBUG_VISIBILITY_PROTECTED:
  1130. prefix = "protected ";
  1131. break;
  1132. case DEBUG_VISIBILITY_PRIVATE:
  1133. prefix = "private ";
  1134. break;
  1135. default:
  1136. prefix = "/* unknown visibility */ ";
  1137. break;
  1138. }
  1139. if (! prepend_type (info, prefix))
  1140. return FALSE;
  1141. if (bitpos != 0)
  1142. {
  1143. print_vma (bitpos, ab, TRUE, FALSE);
  1144. if (! append_type (info, " /* bitpos ")
  1145. || ! append_type (info, ab)
  1146. || ! append_type (info, " */"))
  1147. return FALSE;
  1148. }
  1149. /* Now the top of the stack is something like "public A / * bitpos
  1150. 10 * /". The next element on the stack is something like "class
  1151. xx { / * size 8 * /\n...". We want to substitute the top of the
  1152. stack in before the {. */
  1153. s = strchr (info->stack->next->type, '{');
  1154. assert (s != NULL);
  1155. --s;
  1156. /* If there is already a ':', then we already have a baseclass, and
  1157. we must append this one after a comma. */
  1158. for (l = info->stack->next->type; l != s; l++)
  1159. if (*l == ':')
  1160. break;
  1161. if (! prepend_type (info, l == s ? " : " : ", "))
  1162. return FALSE;
  1163. t = pop_type (info);
  1164. if (t == NULL)
  1165. return FALSE;
  1166. n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
  1167. memcpy (n, info->stack->type, s - info->stack->type);
  1168. strcpy (n + (s - info->stack->type), t);
  1169. strcat (n, s);
  1170. free (info->stack->type);
  1171. info->stack->type = n;
  1172. free (t);
  1173. return TRUE;
  1174. }
  1175. /* Start adding a method to a class. */
  1176. static bfd_boolean
  1177. pr_class_start_method (void *p, const char *name)
  1178. {
  1179. struct pr_handle *info = (struct pr_handle *) p;
  1180. assert (info->stack != NULL);
  1181. info->stack->method = name;
  1182. return TRUE;
  1183. }
  1184. /* Add a variant to a method. */
  1185. static bfd_boolean
  1186. pr_class_method_variant (void *p, const char *physname,
  1187. enum debug_visibility visibility,
  1188. bfd_boolean constp, bfd_boolean volatilep,
  1189. bfd_vma voffset, bfd_boolean context)
  1190. {
  1191. struct pr_handle *info = (struct pr_handle *) p;
  1192. char *method_type;
  1193. char *context_type;
  1194. assert (info->stack != NULL);
  1195. assert (info->stack->next != NULL);
  1196. /* Put the const and volatile qualifiers on the type. */
  1197. if (volatilep)
  1198. {
  1199. if (! append_type (info, " volatile"))
  1200. return FALSE;
  1201. }
  1202. if (constp)
  1203. {
  1204. if (! append_type (info, " const"))
  1205. return FALSE;
  1206. }
  1207. /* Stick the name of the method into its type. */
  1208. if (! substitute_type (info,
  1209. (context
  1210. ? info->stack->next->next->method
  1211. : info->stack->next->method)))
  1212. return FALSE;
  1213. /* Get the type. */
  1214. method_type = pop_type (info);
  1215. if (method_type == NULL)
  1216. return FALSE;
  1217. /* Pull off the context type if there is one. */
  1218. if (! context)
  1219. context_type = NULL;
  1220. else
  1221. {
  1222. context_type = pop_type (info);
  1223. if (context_type == NULL)
  1224. return FALSE;
  1225. }
  1226. /* Now the top of the stack is the class. */
  1227. if (! pr_fix_visibility (info, visibility))
  1228. return FALSE;
  1229. if (! append_type (info, method_type)
  1230. || ! append_type (info, " /* ")
  1231. || ! append_type (info, physname)
  1232. || ! append_type (info, " "))
  1233. return FALSE;
  1234. if (context || voffset != 0)
  1235. {
  1236. char ab[22];
  1237. if (context)
  1238. {
  1239. if (! append_type (info, "context ")
  1240. || ! append_type (info, context_type)
  1241. || ! append_type (info, " "))
  1242. return FALSE;
  1243. }
  1244. print_vma (voffset, ab, TRUE, FALSE);
  1245. if (! append_type (info, "voffset ")
  1246. || ! append_type (info, ab))
  1247. return FALSE;
  1248. }
  1249. return (append_type (info, " */;\n")
  1250. && indent_type (info));
  1251. }
  1252. /* Add a static variant to a method. */
  1253. static bfd_boolean
  1254. pr_class_static_method_variant (void *p, const char *physname,
  1255. enum debug_visibility visibility,
  1256. bfd_boolean constp, bfd_boolean volatilep)
  1257. {
  1258. struct pr_handle *info = (struct pr_handle *) p;
  1259. char *method_type;
  1260. assert (info->stack != NULL);
  1261. assert (info->stack->next != NULL);
  1262. assert (info->stack->next->method != NULL);
  1263. /* Put the const and volatile qualifiers on the type. */
  1264. if (volatilep)
  1265. {
  1266. if (! append_type (info, " volatile"))
  1267. return FALSE;
  1268. }
  1269. if (constp)
  1270. {
  1271. if (! append_type (info, " const"))
  1272. return FALSE;
  1273. }
  1274. /* Mark it as static. */
  1275. if (! prepend_type (info, "static "))
  1276. return FALSE;
  1277. /* Stick the name of the method into its type. */
  1278. if (! substitute_type (info, info->stack->next->method))
  1279. return FALSE;
  1280. /* Get the type. */
  1281. method_type = pop_type (info);
  1282. if (method_type == NULL)
  1283. return FALSE;
  1284. /* Now the top of the stack is the class. */
  1285. if (! pr_fix_visibility (info, visibility))
  1286. return FALSE;
  1287. return (append_type (info, method_type)
  1288. && append_type (info, " /* ")
  1289. && append_type (info, physname)
  1290. && append_type (info, " */;\n")
  1291. && indent_type (info));
  1292. }
  1293. /* Finish up a method. */
  1294. static bfd_boolean
  1295. pr_class_end_method (void *p)
  1296. {
  1297. struct pr_handle *info = (struct pr_handle *) p;
  1298. info->stack->method = NULL;
  1299. return TRUE;
  1300. }
  1301. /* Finish up a class. */
  1302. static bfd_boolean
  1303. pr_end_class_type (void *p)
  1304. {
  1305. return pr_end_struct_type (p);
  1306. }
  1307. /* Push a type on the stack using a typedef name. */
  1308. static bfd_boolean
  1309. pr_typedef_type (void *p, const char *name)
  1310. {
  1311. struct pr_handle *info = (struct pr_handle *) p;
  1312. return push_type (info, name);
  1313. }
  1314. /* Push a type on the stack using a tag name. */
  1315. static bfd_boolean
  1316. pr_tag_type (void *p, const char *name, unsigned int id,
  1317. enum debug_type_kind kind)
  1318. {
  1319. struct pr_handle *info = (struct pr_handle *) p;
  1320. const char *t, *tag;
  1321. char idbuf[22];
  1322. switch (kind)
  1323. {
  1324. case DEBUG_KIND_STRUCT:
  1325. t = "struct ";
  1326. break;
  1327. case DEBUG_KIND_UNION:
  1328. t = "union ";
  1329. break;
  1330. case DEBUG_KIND_ENUM:
  1331. t = "enum ";
  1332. break;
  1333. case DEBUG_KIND_CLASS:
  1334. t = "class ";
  1335. break;
  1336. case DEBUG_KIND_UNION_CLASS:
  1337. t = "union class ";
  1338. break;
  1339. default:
  1340. abort ();
  1341. return FALSE;
  1342. }
  1343. if (! push_type (info, t))
  1344. return FALSE;
  1345. if (name != NULL)
  1346. tag = name;
  1347. else
  1348. {
  1349. sprintf (idbuf, "%%anon%u", id);
  1350. tag = idbuf;
  1351. }
  1352. if (! append_type (info, tag))
  1353. return FALSE;
  1354. if (name != NULL && kind != DEBUG_KIND_ENUM)
  1355. {
  1356. sprintf (idbuf, " /* id %u */", id);
  1357. if (! append_type (info, idbuf))
  1358. return FALSE;
  1359. }
  1360. return TRUE;
  1361. }
  1362. /* Output a typedef. */
  1363. static bfd_boolean
  1364. pr_typdef (void *p, const char *name)
  1365. {
  1366. struct pr_handle *info = (struct pr_handle *) p;
  1367. char *s;
  1368. if (! substitute_type (info, name))
  1369. return FALSE;
  1370. s = pop_type (info);
  1371. if (s == NULL)
  1372. return FALSE;
  1373. indent (info);
  1374. fprintf (info->f, "typedef %s;\n", s);
  1375. free (s);
  1376. return TRUE;
  1377. }
  1378. /* Output a tag. The tag should already be in the string on the
  1379. stack, so all we have to do here is print it out. */
  1380. static bfd_boolean
  1381. pr_tag (void *p, const char *name ATTRIBUTE_UNUSED)
  1382. {
  1383. struct pr_handle *info = (struct pr_handle *) p;
  1384. char *t;
  1385. t = pop_type (info);
  1386. if (t == NULL)
  1387. return FALSE;
  1388. indent (info);
  1389. fprintf (info->f, "%s;\n", t);
  1390. free (t);
  1391. return TRUE;
  1392. }
  1393. /* Output an integer constant. */
  1394. static bfd_boolean
  1395. pr_int_constant (void *p, const char *name, bfd_vma val)
  1396. {
  1397. struct pr_handle *info = (struct pr_handle *) p;
  1398. char ab[22];
  1399. indent (info);
  1400. print_vma (val, ab, FALSE, FALSE);
  1401. fprintf (info->f, "const int %s = %s;\n", name, ab);
  1402. return TRUE;
  1403. }
  1404. /* Output a floating point constant. */
  1405. static bfd_boolean
  1406. pr_float_constant (void *p, const char *name, double val)
  1407. {
  1408. struct pr_handle *info = (struct pr_handle *) p;
  1409. indent (info);
  1410. fprintf (info->f, "const double %s = %g;\n", name, val);
  1411. return TRUE;
  1412. }
  1413. /* Output a typed constant. */
  1414. static bfd_boolean
  1415. pr_typed_constant (void *p, const char *name, bfd_vma val)
  1416. {
  1417. struct pr_handle *info = (struct pr_handle *) p;
  1418. char *t;
  1419. char ab[22];
  1420. t = pop_type (info);
  1421. if (t == NULL)
  1422. return FALSE;
  1423. indent (info);
  1424. print_vma (val, ab, FALSE, FALSE);
  1425. fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
  1426. free (t);
  1427. return TRUE;
  1428. }
  1429. /* Output a variable. */
  1430. static bfd_boolean
  1431. pr_variable (void *p, const char *name, enum debug_var_kind kind,
  1432. bfd_vma val)
  1433. {
  1434. struct pr_handle *info = (struct pr_handle *) p;
  1435. char *t;
  1436. char ab[22];
  1437. if (! substitute_type (info, name))
  1438. return FALSE;
  1439. t = pop_type (info);
  1440. if (t == NULL)
  1441. return FALSE;
  1442. indent (info);
  1443. switch (kind)
  1444. {
  1445. case DEBUG_STATIC:
  1446. case DEBUG_LOCAL_STATIC:
  1447. fprintf (info->f, "static ");
  1448. break;
  1449. case DEBUG_REGISTER:
  1450. fprintf (info->f, "register ");
  1451. break;
  1452. default:
  1453. break;
  1454. }
  1455. print_vma (val, ab, TRUE, TRUE);
  1456. fprintf (info->f, "%s /* %s */;\n", t, ab);
  1457. free (t);
  1458. return TRUE;
  1459. }
  1460. /* Start outputting a function. */
  1461. static bfd_boolean
  1462. pr_start_function (void *p, const char *name, bfd_boolean global)
  1463. {
  1464. struct pr_handle *info = (struct pr_handle *) p;
  1465. char *t;
  1466. if (! substitute_type (info, name))
  1467. return FALSE;
  1468. t = pop_type (info);
  1469. if (t == NULL)
  1470. return FALSE;
  1471. indent (info);
  1472. if (! global)
  1473. fprintf (info->f, "static ");
  1474. fprintf (info->f, "%s (", t);
  1475. info->parameter = 1;
  1476. return TRUE;
  1477. }
  1478. /* Output a function parameter. */
  1479. static bfd_boolean
  1480. pr_function_parameter (void *p, const char *name,
  1481. enum debug_parm_kind kind, bfd_vma val)
  1482. {
  1483. struct pr_handle *info = (struct pr_handle *) p;
  1484. char *t;
  1485. char ab[22];
  1486. if (kind == DEBUG_PARM_REFERENCE
  1487. || kind == DEBUG_PARM_REF_REG)
  1488. {
  1489. if (! pr_reference_type (p))
  1490. return FALSE;
  1491. }
  1492. if (! substitute_type (info, name))
  1493. return FALSE;
  1494. t = pop_type (info);
  1495. if (t == NULL)
  1496. return FALSE;
  1497. if (info->parameter != 1)
  1498. fprintf (info->f, ", ");
  1499. if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
  1500. fprintf (info->f, "register ");
  1501. print_vma (val, ab, TRUE, TRUE);
  1502. fprintf (info->f, "%s /* %s */", t, ab);
  1503. free (t);
  1504. ++info->parameter;
  1505. return TRUE;
  1506. }
  1507. /* Start writing out a block. */
  1508. static bfd_boolean
  1509. pr_start_block (void *p, bfd_vma addr)
  1510. {
  1511. struct pr_handle *info = (struct pr_handle *) p;
  1512. char ab[22];
  1513. if (info->parameter > 0)
  1514. {
  1515. fprintf (info->f, ")\n");
  1516. info->parameter = 0;
  1517. }
  1518. indent (info);
  1519. print_vma (addr, ab, TRUE, TRUE);
  1520. fprintf (info->f, "{ /* %s */\n", ab);
  1521. info->indent += 2;
  1522. return TRUE;
  1523. }
  1524. /* Write out line number information. */
  1525. static bfd_boolean
  1526. pr_lineno (void *p, const char *filename, unsigned long lineno, bfd_vma addr)
  1527. {
  1528. struct pr_handle *info = (struct pr_handle *) p;
  1529. char ab[22];
  1530. indent (info);
  1531. print_vma (addr, ab, TRUE, TRUE);
  1532. fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
  1533. return TRUE;
  1534. }
  1535. /* Finish writing out a block. */
  1536. static bfd_boolean
  1537. pr_end_block (void *p, bfd_vma addr)
  1538. {
  1539. struct pr_handle *info = (struct pr_handle *) p;
  1540. char ab[22];
  1541. info->indent -= 2;
  1542. indent (info);
  1543. print_vma (addr, ab, TRUE, TRUE);
  1544. fprintf (info->f, "} /* %s */\n", ab);
  1545. return TRUE;
  1546. }
  1547. /* Finish writing out a function. */
  1548. static bfd_boolean
  1549. pr_end_function (void *p ATTRIBUTE_UNUSED)
  1550. {
  1551. return TRUE;
  1552. }
  1553. /* Tags style generation functions start here. */
  1554. /* Variables for address to line translation. */
  1555. static bfd_vma pc;
  1556. static const char *filename;
  1557. static const char *functionname;
  1558. static unsigned int line;
  1559. static bfd_boolean found;
  1560. /* Look for an address in a section. This is called via
  1561. bfd_map_over_sections. */
  1562. static void
  1563. find_address_in_section (bfd *abfd, asection *section, void *data)
  1564. {
  1565. bfd_vma vma;
  1566. bfd_size_type size;
  1567. asymbol **syms = (asymbol **) data;
  1568. if (found)
  1569. return;
  1570. if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
  1571. return;
  1572. vma = bfd_get_section_vma (abfd, section);
  1573. if (pc < vma)
  1574. return;
  1575. size = bfd_get_section_size (section);
  1576. if (pc >= vma + size)
  1577. return;
  1578. found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
  1579. &filename, &functionname, &line);
  1580. }
  1581. static void
  1582. translate_addresses (bfd *abfd, char *addr_hex, FILE *f, asymbol **syms)
  1583. {
  1584. pc = bfd_scan_vma (addr_hex, NULL, 16);
  1585. found = FALSE;
  1586. bfd_map_over_sections (abfd, find_address_in_section, syms);
  1587. if (! found)
  1588. fprintf (f, "??");
  1589. else
  1590. fprintf (f, "%u", line);
  1591. }
  1592. /* Start a new compilation unit. */
  1593. static bfd_boolean
  1594. tg_start_compilation_unit (void * p, const char *fname ATTRIBUTE_UNUSED)
  1595. {
  1596. struct pr_handle *info = (struct pr_handle *) p;
  1597. free (info->filename);
  1598. /* Should it be relative? best way to do it here?. */
  1599. info->filename = strdup (fname);
  1600. return TRUE;
  1601. }
  1602. /* Start a source file within a compilation unit. */
  1603. static bfd_boolean
  1604. tg_start_source (void *p, const char *fname)
  1605. {
  1606. struct pr_handle *info = (struct pr_handle *) p;
  1607. free (info->filename);
  1608. /* Should it be relative? best way to do it here?. */
  1609. info->filename = strdup (fname);
  1610. return TRUE;
  1611. }
  1612. /* Push an enum type onto the type stack. */
  1613. static bfd_boolean
  1614. tg_enum_type (void *p, const char *tag, const char **names,
  1615. bfd_signed_vma *values)
  1616. {
  1617. struct pr_handle *info = (struct pr_handle *) p;
  1618. unsigned int i;
  1619. const char *name;
  1620. char ab[22];
  1621. if (! pr_enum_type (p, tag, names, values))
  1622. return FALSE;
  1623. name = tag ? tag : "unknown";
  1624. /* Generate an entry for the enum. */
  1625. if (tag)
  1626. fprintf (info->f, "%s\t%s\t0;\"\tkind:e\ttype:%s\n", tag,
  1627. info->filename, info->stack->type);
  1628. /* Generate entries for the values. */
  1629. if (names != NULL)
  1630. {
  1631. for (i = 0; names[i] != NULL; i++)
  1632. {
  1633. print_vma (values[i], ab, FALSE, FALSE);
  1634. fprintf (info->f, "%s\t%s\t0;\"\tkind:g\tenum:%s\tvalue:%s\n",
  1635. names[i], info->filename, name, ab);
  1636. }
  1637. }
  1638. return TRUE;
  1639. }
  1640. /* Start accumulating a struct type. */
  1641. static bfd_boolean
  1642. tg_start_struct_type (void *p, const char *tag, unsigned int id,
  1643. bfd_boolean structp,
  1644. unsigned int size ATTRIBUTE_UNUSED)
  1645. {
  1646. struct pr_handle *info = (struct pr_handle *) p;
  1647. const char *name;
  1648. char idbuf[20];
  1649. if (tag != NULL)
  1650. name = tag;
  1651. else
  1652. {
  1653. name = idbuf;
  1654. sprintf (idbuf, "%%anon%u", id);
  1655. }
  1656. if (! push_type (info, name))
  1657. return FALSE;
  1658. info->stack->flavor = structp ? "struct" : "union";
  1659. fprintf (info->f, "%s\t%s\t0;\"\tkind:%c\n", name, info->filename,
  1660. info->stack->flavor[0]);
  1661. info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
  1662. return indent_type (info);
  1663. }
  1664. /* Output the visibility of a field in a struct. */
  1665. static bfd_boolean
  1666. tg_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
  1667. {
  1668. assert (info->stack != NULL);
  1669. if (info->stack->visibility == visibility)
  1670. return TRUE;
  1671. assert (info->stack->visibility != DEBUG_VISIBILITY_IGNORE);
  1672. info->stack->visibility = visibility;
  1673. return TRUE;
  1674. }
  1675. /* Add a field to a struct type. */
  1676. static bfd_boolean
  1677. tg_struct_field (void *p, const char *name, bfd_vma bitpos ATTRIBUTE_UNUSED,
  1678. bfd_vma bitsize ATTRIBUTE_UNUSED,
  1679. enum debug_visibility visibility)
  1680. {
  1681. struct pr_handle *info = (struct pr_handle *) p;
  1682. char *t;
  1683. t = pop_type (info);
  1684. if (t == NULL)
  1685. return FALSE;
  1686. if (! tg_fix_visibility (info, visibility))
  1687. return FALSE;
  1688. /* It happens, a bug? */
  1689. if (! name[0])
  1690. return TRUE;
  1691. fprintf (info->f, "%s\t%s\t0;\"\tkind:m\ttype:%s\t%s:%s\taccess:%s\n",
  1692. name, info->filename, t, info->stack->flavor, info->stack->type,
  1693. visibility_name (visibility));
  1694. return TRUE;
  1695. }
  1696. /* Finish a struct type. */
  1697. static bfd_boolean
  1698. tg_end_struct_type (void *p ATTRIBUTE_UNUSED)
  1699. {
  1700. assert (((struct pr_handle *) p)->stack != NULL);
  1701. return TRUE;
  1702. }
  1703. /* Start a class type. */
  1704. static bfd_boolean
  1705. tg_start_class_type (void *p, const char *tag, unsigned int id,
  1706. bfd_boolean structp, unsigned int size,
  1707. bfd_boolean vptr, bfd_boolean ownvptr)
  1708. {
  1709. struct pr_handle *info = (struct pr_handle *) p;
  1710. char *tv = NULL;
  1711. const char *name;
  1712. info->indent += 2;
  1713. if (vptr && ! ownvptr)
  1714. {
  1715. tv = pop_type (info);
  1716. if (tv == NULL)
  1717. return FALSE;
  1718. }
  1719. if (tag != NULL)
  1720. name = tag;
  1721. else
  1722. {
  1723. char idbuf[20];
  1724. sprintf (idbuf, "%%anon%u", id);
  1725. name = idbuf;
  1726. }
  1727. if (! push_type (info, name))
  1728. return FALSE;
  1729. info->stack->flavor = structp ? "class" : "union class";
  1730. info->stack->parents = NULL;
  1731. info->stack->num_parents = 0;
  1732. if (size != 0 || vptr || ownvptr || tag != NULL)
  1733. {
  1734. if (vptr)
  1735. {
  1736. if (! append_type (info, " vtable "))
  1737. return FALSE;
  1738. if (ownvptr)
  1739. {
  1740. if (! append_type (info, "self "))
  1741. return FALSE;
  1742. }
  1743. else
  1744. {
  1745. if (! append_type (info, tv)
  1746. || ! append_type (info, " "))
  1747. return FALSE;
  1748. }
  1749. }
  1750. }
  1751. info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
  1752. return TRUE;
  1753. }
  1754. /* Add a static member to a class. */
  1755. static bfd_boolean
  1756. tg_class_static_member (void *p, const char *name,
  1757. const char *physname ATTRIBUTE_UNUSED,
  1758. enum debug_visibility visibility)
  1759. {
  1760. struct pr_handle *info = (struct pr_handle *) p;
  1761. char *t;
  1762. int len_var, len_class;
  1763. char *full_name;
  1764. len_var = strlen (name);
  1765. len_class = strlen (info->stack->next->type);
  1766. full_name = (char *) xmalloc (len_var + len_class + 3);
  1767. if (! full_name)
  1768. return FALSE;
  1769. sprintf (full_name, "%s::%s", info->stack->next->type, name);
  1770. if (! substitute_type (info, full_name))
  1771. {
  1772. free (full_name);
  1773. return FALSE;
  1774. }
  1775. if (! prepend_type (info, "static "))
  1776. {
  1777. free (full_name);
  1778. return FALSE;
  1779. }
  1780. t = pop_type (info);
  1781. if (t == NULL)
  1782. {
  1783. free (full_name);
  1784. return FALSE;
  1785. }
  1786. if (! tg_fix_visibility (info, visibility))
  1787. {
  1788. free (t);
  1789. free (full_name);
  1790. return FALSE;
  1791. }
  1792. fprintf (info->f, "%s\t%s\t0;\"\tkind:x\ttype:%s\tclass:%s\taccess:%s\n",
  1793. name, info->filename, t, info->stack->type,
  1794. visibility_name (visibility));
  1795. free (t);
  1796. free (full_name);
  1797. return TRUE;
  1798. }
  1799. /* Add a base class to a class. */
  1800. static bfd_boolean
  1801. tg_class_baseclass (void *p, bfd_vma bitpos ATTRIBUTE_UNUSED,
  1802. bfd_boolean is_virtual, enum debug_visibility visibility)
  1803. {
  1804. struct pr_handle *info = (struct pr_handle *) p;
  1805. char *t;
  1806. const char *prefix;
  1807. assert (info->stack != NULL && info->stack->next != NULL);
  1808. t = pop_type (info);
  1809. if (t == NULL)
  1810. return FALSE;
  1811. if (CONST_STRNEQ (t, "class "))
  1812. t += sizeof "class " - 1;
  1813. /* Push it back on to take advantage of the prepend_type and
  1814. append_type routines. */
  1815. if (! push_type (info, t))
  1816. return FALSE;
  1817. if (is_virtual)
  1818. {
  1819. if (! prepend_type (info, "virtual "))
  1820. return FALSE;
  1821. }
  1822. switch (visibility)
  1823. {
  1824. case DEBUG_VISIBILITY_PUBLIC:
  1825. prefix = "public ";
  1826. break;
  1827. case DEBUG_VISIBILITY_PROTECTED:
  1828. prefix = "protected ";
  1829. break;
  1830. case DEBUG_VISIBILITY_PRIVATE:
  1831. prefix = "private ";
  1832. break;
  1833. default:
  1834. prefix = "/* unknown visibility */ ";
  1835. break;
  1836. }
  1837. if (! prepend_type (info, prefix))
  1838. return FALSE;
  1839. t = pop_type (info);
  1840. if (t == NULL)
  1841. return FALSE;
  1842. if (info->stack->num_parents && ! append_parent (info, ", "))
  1843. return FALSE;
  1844. if (! append_parent (info, t))
  1845. return FALSE;
  1846. info->stack->num_parents++;
  1847. free (t);
  1848. return TRUE;
  1849. }
  1850. /* Add a variant to a method. */
  1851. static bfd_boolean
  1852. tg_class_method_variant (void *p, const char *physname ATTRIBUTE_UNUSED,
  1853. enum debug_visibility visibility,
  1854. bfd_boolean constp, bfd_boolean volatilep,
  1855. bfd_vma voffset ATTRIBUTE_UNUSED,
  1856. bfd_boolean context)
  1857. {
  1858. struct pr_handle *info = (struct pr_handle *) p;
  1859. char *method_type;
  1860. char *context_type;
  1861. char *method_name;
  1862. assert (info->stack != NULL);
  1863. assert (info->stack->next != NULL);
  1864. /* Put the const and volatile qualifiers on the type. */
  1865. if (volatilep)
  1866. {
  1867. if (! append_type (info, " volatile"))
  1868. return FALSE;
  1869. }
  1870. if (constp)
  1871. {
  1872. if (! append_type (info, " const"))
  1873. return FALSE;
  1874. }
  1875. method_name = strdup (context ? info->stack->next->next->method
  1876. : info->stack->next->method);
  1877. /* Stick the name of the method into its type. */
  1878. if (! substitute_type (info, method_name))
  1879. {
  1880. free (method_name);
  1881. return FALSE;
  1882. }
  1883. /* Get the type. */
  1884. method_type = pop_type (info);
  1885. if (method_type == NULL)
  1886. {
  1887. free (method_name);
  1888. return FALSE;
  1889. }
  1890. /* Pull off the context type if there is one. */
  1891. if (! context)
  1892. context_type = NULL;
  1893. else
  1894. {
  1895. context_type = pop_type (info);
  1896. if (context_type == NULL)
  1897. {
  1898. free (method_type);
  1899. free (method_name);
  1900. return FALSE;
  1901. }
  1902. }
  1903. /* Now the top of the stack is the class. */
  1904. if (! tg_fix_visibility (info, visibility))
  1905. {
  1906. free (method_type);
  1907. free (method_name);
  1908. free (context_type);
  1909. return FALSE;
  1910. }
  1911. fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\n",
  1912. method_name, info->filename, method_type, info->stack->type);
  1913. free (method_type);
  1914. free (method_name);
  1915. free (context_type);
  1916. return TRUE;
  1917. }
  1918. /* Add a static variant to a method. */
  1919. static bfd_boolean
  1920. tg_class_static_method_variant (void *p,
  1921. const char *physname ATTRIBUTE_UNUSED,
  1922. enum debug_visibility visibility,
  1923. bfd_boolean constp, bfd_boolean volatilep)
  1924. {
  1925. struct pr_handle *info = (struct pr_handle *) p;
  1926. char *method_type;
  1927. char *method_name;
  1928. assert (info->stack != NULL);
  1929. assert (info->stack->next != NULL);
  1930. assert (info->stack->next->method != NULL);
  1931. /* Put the const and volatile qualifiers on the type. */
  1932. if (volatilep)
  1933. {
  1934. if (! append_type (info, " volatile"))
  1935. return FALSE;
  1936. }
  1937. if (constp)
  1938. {
  1939. if (! append_type (info, " const"))
  1940. return FALSE;
  1941. }
  1942. /* Mark it as static. */
  1943. if (! prepend_type (info, "static "))
  1944. return FALSE;
  1945. method_name = strdup (info->stack->next->method);
  1946. /* Stick the name of the method into its type. */
  1947. if (! substitute_type (info, info->stack->next->method))
  1948. {
  1949. free (method_name);
  1950. return FALSE;
  1951. }
  1952. /* Get the type. */
  1953. method_type = pop_type (info);
  1954. if (method_type == NULL)
  1955. {
  1956. free (method_name);
  1957. return FALSE;
  1958. }
  1959. /* Now the top of the stack is the class. */
  1960. if (! tg_fix_visibility (info, visibility))
  1961. {
  1962. free (method_type);
  1963. free (method_name);
  1964. return FALSE;
  1965. }
  1966. fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\taccess:%s\n",
  1967. method_name, info->filename, method_type, info->stack->type,
  1968. visibility_name (visibility));
  1969. free (method_type);
  1970. free (method_name);
  1971. return TRUE;
  1972. }
  1973. /* Finish up a class. */
  1974. static bfd_boolean
  1975. tg_end_class_type (void *p)
  1976. {
  1977. struct pr_handle *info = (struct pr_handle *) p;
  1978. fprintf (info->f, "%s\t%s\t0;\"\tkind:c\ttype:%s", info->stack->type,
  1979. info->filename, info->stack->flavor);
  1980. if (info->stack->num_parents)
  1981. {
  1982. fprintf (info->f, "\tinherits:%s", info->stack->parents);
  1983. free (info->stack->parents);
  1984. }
  1985. fputc ('\n', info->f);
  1986. return tg_end_struct_type (p);
  1987. }
  1988. /* Push a type on the stack using a tag name. */
  1989. static bfd_boolean
  1990. tg_tag_type (void *p, const char *name, unsigned int id,
  1991. enum debug_type_kind kind)
  1992. {
  1993. struct pr_handle *info = (struct pr_handle *) p;
  1994. const char *t, *tag;
  1995. char idbuf[20];
  1996. switch (kind)
  1997. {
  1998. case DEBUG_KIND_STRUCT:
  1999. t = "struct ";
  2000. break;
  2001. case DEBUG_KIND_UNION:
  2002. t = "union ";
  2003. break;
  2004. case DEBUG_KIND_ENUM:
  2005. t = "enum ";
  2006. break;
  2007. case DEBUG_KIND_CLASS:
  2008. t = "class ";
  2009. break;
  2010. case DEBUG_KIND_UNION_CLASS:
  2011. t = "union class ";
  2012. break;
  2013. default:
  2014. abort ();
  2015. return FALSE;
  2016. }
  2017. if (! push_type (info, t))
  2018. return FALSE;
  2019. if (name != NULL)
  2020. tag = name;
  2021. else
  2022. {
  2023. sprintf (idbuf, "%%anon%u", id);
  2024. tag = idbuf;
  2025. }
  2026. if (! append_type (info, tag))
  2027. return FALSE;
  2028. return TRUE;
  2029. }
  2030. /* Output a typedef. */
  2031. static bfd_boolean
  2032. tg_typdef (void *p, const char *name)
  2033. {
  2034. struct pr_handle *info = (struct pr_handle *) p;
  2035. char *s;
  2036. s = pop_type (info);
  2037. if (s == NULL)
  2038. return FALSE;
  2039. fprintf (info->f, "%s\t%s\t0;\"\tkind:t\ttype:%s\n", name,
  2040. info->filename, s);
  2041. free (s);
  2042. return TRUE;
  2043. }
  2044. /* Output a tag. The tag should already be in the string on the
  2045. stack, so all we have to do here is print it out. */
  2046. static bfd_boolean
  2047. tg_tag (void *p ATTRIBUTE_UNUSED, const char *name ATTRIBUTE_UNUSED)
  2048. {
  2049. struct pr_handle *info = (struct pr_handle *) p;
  2050. char *t;
  2051. t = pop_type (info);
  2052. if (t == NULL)
  2053. return FALSE;
  2054. free (t);
  2055. return TRUE;
  2056. }
  2057. /* Output an integer constant. */
  2058. static bfd_boolean
  2059. tg_int_constant (void *p, const char *name, bfd_vma val)
  2060. {
  2061. struct pr_handle *info = (struct pr_handle *) p;
  2062. char ab[22];
  2063. indent (info);
  2064. print_vma (val, ab, FALSE, FALSE);
  2065. fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const int\tvalue:%s\n",
  2066. name, info->filename, ab);
  2067. return TRUE;
  2068. }
  2069. /* Output a floating point constant. */
  2070. static bfd_boolean
  2071. tg_float_constant (void *p, const char *name, double val)
  2072. {
  2073. struct pr_handle *info = (struct pr_handle *) p;
  2074. indent (info);
  2075. fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const double\tvalue:%g\n",
  2076. name, info->filename, val);
  2077. return TRUE;
  2078. }
  2079. /* Output a typed constant. */
  2080. static bfd_boolean
  2081. tg_typed_constant (void *p, const char *name, bfd_vma val)
  2082. {
  2083. struct pr_handle *info = (struct pr_handle *) p;
  2084. char *t;
  2085. char ab[22];
  2086. t = pop_type (info);
  2087. if (t == NULL)
  2088. return FALSE;
  2089. indent (info);
  2090. print_vma (val, ab, FALSE, FALSE);
  2091. fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const %s\tvalue:%s\n",
  2092. name, info->filename, t, ab);
  2093. free (t);
  2094. return TRUE;
  2095. }
  2096. /* Output a variable. */
  2097. static bfd_boolean
  2098. tg_variable (void *p, const char *name, enum debug_var_kind kind,
  2099. bfd_vma val ATTRIBUTE_UNUSED)
  2100. {
  2101. struct pr_handle *info = (struct pr_handle *) p;
  2102. char *t, *dname, *from_class;
  2103. t = pop_type (info);
  2104. if (t == NULL)
  2105. return FALSE;
  2106. dname = NULL;
  2107. if (info->demangler)
  2108. dname = info->demangler (info->abfd, name, DMGL_ANSI | DMGL_PARAMS);
  2109. from_class = NULL;
  2110. if (dname != NULL)
  2111. {
  2112. char *sep;
  2113. sep = strstr (dname, "::");
  2114. if (sep)
  2115. {
  2116. *sep = 0;
  2117. name = sep + 2;
  2118. from_class = dname;
  2119. }
  2120. else
  2121. /* Obscure types as vts and type_info nodes. */
  2122. name = dname;
  2123. }
  2124. fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:%s", name, info->filename, t);
  2125. switch (kind)
  2126. {
  2127. case DEBUG_STATIC:
  2128. case DEBUG_LOCAL_STATIC:
  2129. fprintf (info->f, "\tfile:");
  2130. break;
  2131. case DEBUG_REGISTER:
  2132. fprintf (info->f, "\tregister:");
  2133. break;
  2134. default:
  2135. break;
  2136. }
  2137. if (from_class)
  2138. fprintf (info->f, "\tclass:%s", from_class);
  2139. if (dname)
  2140. free (dname);
  2141. fprintf (info->f, "\n");
  2142. free (t);
  2143. return TRUE;
  2144. }
  2145. /* Start outputting a function. */
  2146. static bfd_boolean
  2147. tg_start_function (void *p, const char *name, bfd_boolean global)
  2148. {
  2149. struct pr_handle *info = (struct pr_handle *) p;
  2150. char *dname;
  2151. if (! global)
  2152. info->stack->flavor = "static";
  2153. else
  2154. info->stack->flavor = NULL;
  2155. dname = NULL;
  2156. if (info->demangler)
  2157. dname = info->demangler (info->abfd, name, DMGL_ANSI | DMGL_PARAMS);
  2158. if (! substitute_type (info, dname ? dname : name))
  2159. return FALSE;
  2160. info->stack->method = NULL;
  2161. if (dname != NULL)
  2162. {
  2163. char *sep;
  2164. sep = strstr (dname, "::");
  2165. if (sep)
  2166. {
  2167. info->stack->method = dname;
  2168. *sep = 0;
  2169. name = sep + 2;
  2170. }
  2171. else
  2172. {
  2173. info->stack->method = "";
  2174. name = dname;
  2175. }
  2176. sep = strchr (name, '(');
  2177. if (sep)
  2178. *sep = 0;
  2179. /* Obscure functions as type_info function. */
  2180. }
  2181. info->stack->parents = strdup (name);
  2182. if (! info->stack->method && ! append_type (info, "("))
  2183. return FALSE;
  2184. info->parameter = 1;
  2185. return TRUE;
  2186. }
  2187. /* Output a function parameter. */
  2188. static bfd_boolean
  2189. tg_function_parameter (void *p, const char *name, enum debug_parm_kind kind,
  2190. bfd_vma val ATTRIBUTE_UNUSED)
  2191. {
  2192. struct pr_handle *info = (struct pr_handle *) p;
  2193. char *t;
  2194. if (kind == DEBUG_PARM_REFERENCE
  2195. || kind == DEBUG_PARM_REF_REG)
  2196. {
  2197. if (! pr_reference_type (p))
  2198. return FALSE;
  2199. }
  2200. if (! substitute_type (info, name))
  2201. return FALSE;
  2202. t = pop_type (info);
  2203. if (t == NULL)
  2204. return FALSE;
  2205. if (! info->stack->method)
  2206. {
  2207. if (info->parameter != 1 && ! append_type (info, ", "))
  2208. return FALSE;
  2209. if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
  2210. if (! append_type (info, "register "))
  2211. return FALSE;
  2212. if (! append_type (info, t))
  2213. return FALSE;
  2214. }
  2215. free (t);
  2216. ++info->parameter;
  2217. return TRUE;
  2218. }
  2219. /* Start writing out a block. */
  2220. static bfd_boolean
  2221. tg_start_block (void *p, bfd_vma addr)
  2222. {
  2223. struct pr_handle *info = (struct pr_handle *) p;
  2224. char ab[22], kind, *partof;
  2225. char *t;
  2226. bfd_boolean local;
  2227. if (info->parameter > 0)
  2228. {
  2229. info->parameter = 0;
  2230. /* Delayed name. */
  2231. fprintf (info->f, "%s\t%s\t", info->stack->parents, info->filename);
  2232. free (info->stack->parents);
  2233. print_vma (addr, ab, TRUE, TRUE);
  2234. translate_addresses (info->abfd, ab, info->f, info->syms);
  2235. local = info->stack->flavor != NULL;
  2236. if (info->stack->method && *info->stack->method)
  2237. {
  2238. kind = 'm';
  2239. partof = (char *) info->stack->method;
  2240. }
  2241. else
  2242. {
  2243. kind = 'f';
  2244. partof = NULL;
  2245. if (! info->stack->method && ! append_type (info, ")"))
  2246. return FALSE;
  2247. }
  2248. t = pop_type (info);
  2249. if (t == NULL)
  2250. return FALSE;
  2251. fprintf (info->f, ";\"\tkind:%c\ttype:%s", kind, t);
  2252. if (local)
  2253. fputs ("\tfile:", info->f);
  2254. if (partof)
  2255. {
  2256. fprintf (info->f, "\tclass:%s", partof);
  2257. free (partof);
  2258. }
  2259. fputc ('\n', info->f);
  2260. }
  2261. return TRUE;
  2262. }
  2263. /* Write out line number information. */
  2264. static bfd_boolean
  2265. tg_lineno (void *p ATTRIBUTE_UNUSED, const char *fname ATTRIBUTE_UNUSED,
  2266. unsigned long lineno ATTRIBUTE_UNUSED,
  2267. bfd_vma addr ATTRIBUTE_UNUSED)
  2268. {
  2269. return TRUE;
  2270. }
  2271. /* Finish writing out a block. */
  2272. static bfd_boolean
  2273. tg_end_block (void *p ATTRIBUTE_UNUSED, bfd_vma addr ATTRIBUTE_UNUSED)
  2274. {
  2275. return TRUE;
  2276. }
  2277. /* Convert the visibility value into a human readable name. */
  2278. static const char *
  2279. visibility_name (enum debug_visibility visibility)
  2280. {
  2281. const char *s;
  2282. switch (visibility)
  2283. {
  2284. case DEBUG_VISIBILITY_PUBLIC:
  2285. s = "public";
  2286. break;
  2287. case DEBUG_VISIBILITY_PRIVATE:
  2288. s = "private";
  2289. break;
  2290. case DEBUG_VISIBILITY_PROTECTED:
  2291. s = "protected";
  2292. break;
  2293. case DEBUG_VISIBILITY_IGNORE:
  2294. s = "/* ignore */";
  2295. break;
  2296. default:
  2297. abort ();
  2298. return FALSE;
  2299. }
  2300. return s;
  2301. }