prdbg.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839
  1. /* prdbg.c -- Print out generic debugging information.
  2. Copyright (C) 1995-2015 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[10];
  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[10];
  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[10];
  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[20];
  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[20], abu[20];
  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[20], abu[20], 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[20];
  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[20];
  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[20];
  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[20];
  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[20];
  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[20];
  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[20];
  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[20];
  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[20];
  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[20];
  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[20];
  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[20];
  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[20];
  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[20];
  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[20], 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. }