prdbg.c 61 KB

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