aboutsummaryrefslogtreecommitdiff
path: root/gprofng/src/CompCom.cc
blob: 2b63b8f5c40ea3c7b49e4924fdc13073d3af248e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Copyright (C) 2021-2023 Free Software Foundation, Inc.
   Contributed by Oracle.

   This file is part of GNU Binutils.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */

#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <sys/param.h>

#include "demangle.h"
#include "gp-defs.h"
#include "StringBuilder.h"
#include "CompCom.h"
#include "Elf.h"
#include "util.h"
#include "i18n.h"
#include "comp_com.c"

CompComment::CompComment (Elf *_elf, int _compcom)
{
  elf = _elf;
  compcom = _compcom;
  elf_cls = elf->elf_getclass ();
}

CompComment::~CompComment () { }

int
CompComment::get_align (int64_t offset, int align)
{
  int val = (int) (offset % align);
  if (val)
    val = align - val;
  return val;
}

/*
 * Preprocesses the header structure, builds a table of messages with the line
 * numbers, PCoffsets, original index, and compmsg pointer for each message.
 * If the show_bits field is not in the message, this routine would fill it in
 * from the mapping from COMPMSG_ID
 */
int
CompComment::compcom_open (CheckSrcName check_src)
{
  if (check_src == NULL)
    return 0;
  Elf_Data *data = elf->elf_getdata (compcom);
  uint64_t b_offset = data->d_off;
  if (get_align (b_offset, 4))   // not align 4
    return 0;
  char *CommData = (char *) data->d_buf;
  uint64_t offset = b_offset;
  for (uint64_t e_offset = b_offset + data->d_size; offset < e_offset;)
    {
      offset += get_align (offset, (int) data->d_align);
      if (offset >= e_offset)
	return 0;
      compcomhdr *hdr = (compcomhdr *) (CommData + (offset - b_offset));
      int hdr_msgcount = elf->decode (hdr->msgcount);
      int hdr_srcname = elf->decode (hdr->srcname);
      int hdr_stringlen = elf->decode (hdr->stringlen);
      int hdr_paramcount = elf->decode (hdr->paramcount);
      size_t length = sizeof (compcomhdr) + hdr_msgcount * sizeof (compmsg) +
	      hdr_paramcount * sizeof (int32_t);
      if (offset + length + hdr_stringlen > e_offset || hdr_srcname < 0
	  || hdr_srcname >= hdr_stringlen)
	return 0;

      // check source file
      char *src_name = (char *) (((char*) hdr) + length + hdr_srcname);
      if (check_src (src_name))
	{
	  msgs = (compmsg *) (((char *) hdr) + sizeof (compcomhdr));
	  params = (int32_t *) ((char *) msgs + hdr_msgcount * sizeof (compmsg));
	  strs = (char *) ((char *) params + hdr_paramcount * sizeof (int32_t));

	  // initialize the I18N/L10N strings & set the visible table
	  ccm_vis_init ();
	  return hdr_msgcount;
	}
      offset += (length + hdr_stringlen);
    }
  return 0;
}

char *
CompComment::get_demangle_name (char *fname)
{
  if (*fname == '_')
    return cplus_demangle (fname, DMGL_PARAMS);
  return NULL;
}

/*
 * takes the message, and returns the I18N string for the message.
 */
char *
CompComment::compcom_format (int index, compmsg *msg, int &visible)
{
  compmsg *p = msgs + index;
  msg->instaddr = elf->decode (p->instaddr);
  msg->lineno = elf->decode (p->lineno);
  msg->msg_type = elf->decode (p->msg_type);
  msg->nparam = elf->decode (p->nparam);
  msg->param_index = elf->decode (p->param_index);

  int vindex = ccm_vis_index (msg->msg_type);
  char *mbuf;
  Ccm_Primtype_t prim_ty;
  visible = ccm_attrs[vindex].vis;
  if (ccm_attrs[vindex].msg == NULL)
    {
      /* Print CCM_UNKNOWN message */
      int uindex = ccm_vis_index (CCM_UNKNOWN);
      visible = ccm_attrs[uindex].vis;
      return dbe_sprintf (ccm_attrs[uindex].msg, vindex);
    }

  /*
   * Construct the output buffer based on the primitive types of the
   * message parameters.
   *
   * Parameter lists have to be handled carefully -- the 1 parameter
   * is built up of all the elements separated by ", ".
   *
   * Old way: Case by message format string.
   */
  int *ind = params + msg->param_index;
  int plist_idx = ccm_paramlist_index (msg->msg_type);
  if (plist_idx <= 0)
    {
      /* No parameter list to handle; 0 parameters case is handled */

      enum
      {
	MAX_COMPCOM_ARGS = 13
      };
      char *parms[MAX_COMPCOM_ARGS];
      if (msg->nparam >= MAX_COMPCOM_ARGS)
	{
	  fprintf (stderr,
		   GTXT ("Warning: improperly formatted compiler commentary message (%d parameters >= %d);\n  please report this bug against the compiler\n"),
		   msg->nparam, MAX_COMPCOM_ARGS);
	  return NULL;
	}
      for (int i = 0; i < MAX_COMPCOM_ARGS; i++)
	parms[i] = NULL; // initialize array
      int prm_cnt = ccm_num_params (msg->msg_type);
      if (prm_cnt != msg->nparam)
	{
	  fprintf (stderr,
		   GTXT ("Warning, improperly formatted compiler commentary message (parameter count mismatch = %d, param# = %d, msg_type = %x, `%s');\n  please report this bug against the compiler\n"),
		   prm_cnt, msg->nparam, msg->msg_type, ccm_attrs[vindex].msg);
	  return NULL;
	}
      for (int i = 0; i < msg->nparam; i++)
	{
	  /* Parameters in message-type numbered from '1' */
	  prim_ty = ccm_param_primtype (msg->msg_type, i + 1);
	  if (prim_ty == CCM_PRIMTYPE_INTEGER)
	    {
	      unsigned long v = elf->decode (ind[i]);
	      parms[i] = (char*) v;
	    }
	  else if (prim_ty == CCM_PRIMTYPE_STRING)
	    {
	      char *fname = strs + elf->decode (ind[i]);
	      char *demName = get_demangle_name (fname);
	      parms[i] = demName ? demName : dbe_strdup (fname);
	    }
	  else if (prim_ty == CCM_PRIMTYPE_HEXSTRING)
	    parms[i] = dbe_sprintf (elf_cls == ELFCLASS32 ? NTXT ("0x%08llx") : NTXT ("0x%016llx"),
				    (unsigned long long) msg->instaddr);
	  else
	    {
	      fprintf (stderr,
		       GTXT ("Warning, improperly formatted compiler commentary message (unexpected primitive type %d);\n  please report this bug against the compiler\n"),
		       prim_ty);
	      // Dummy code to avoid compiler's warning: static function ccm_param_hightype is not used
	      Ccm_Hitype_t hightype = CCM_HITYPE_NONE;
	      if (hightype != CCM_HITYPE_NONE)
		hightype = ccm_param_hightype (msg->msg_type, i + 1);
	      return NULL;
	    }
	}

      /*
       * Must make sure to pass _ALL_ params; may pass more because
       * the format won't access the 'extra' parameters if all the
       * rules for messages have been followed.
       */
      mbuf = dbe_sprintf (ccm_attrs[vindex].msg, parms[0], parms[1], parms[2],
			  parms[3], parms[4], parms[5], parms[6], parms[7],
			  parms[8], parms[9], parms[10], parms[11]);
      // Cleanup allocated memory.
      for (int i = 0; i < msg->nparam; i++)
	{
	  prim_ty = ccm_param_primtype (msg->msg_type, i + 1);
	  if (prim_ty == CCM_PRIMTYPE_STRING || prim_ty == CCM_PRIMTYPE_HEXSTRING)
	    free (parms[i]);
	}
    }
  else
    {
      /*
       * Parameter list messages never have 0 parameters; the
       * primitive type for the parameter list elements is always
       * the same.  And as of 22-Sep-2006, it was always
       * CCM_PRIMTYPE_STRING.
       *
       * Account for different bases of parameter indices and
       * 'nparam' count (1 and 0, respectively).
       */
      char *parms[3];
      if (plist_idx > (int) ((sizeof (parms) / sizeof (char*))))
	{
	  fprintf (stderr,
		   GTXT ("Warning: improperly formatted compiler commentary message (msg->nparam=%d plist_idx=%d);\n  please report this bug against the compiler\n"),
		   msg->nparam, plist_idx);
	  return NULL;
	}
      for (size_t i = 0; i < (sizeof (parms) / sizeof (char*)); i++)
	parms[i] = NULL; // initialize array

      StringBuilder sb;
      prim_ty = ccm_param_primtype (msg->msg_type, plist_idx);
      for (int i = plist_idx - 1; i < msg->nparam; i++)
	{
	  if (i != plist_idx - 1)
	    sb.append (GTXT (", "));
	  if (prim_ty == CCM_PRIMTYPE_INTEGER)
	    sb.append (elf->decode (ind[i]));
	  else if (prim_ty == CCM_PRIMTYPE_STRING)
	    {
	      char *fname = strs + elf->decode (ind[i]);
	      char *demName = get_demangle_name (fname);
	      if (demName)
		{
		  sb.append (demName);
		  delete demName;
		}
	      else
		sb.append (fname);
	    }
	  else if (prim_ty == CCM_PRIMTYPE_HEXSTRING)
	    sb.appendf (elf_cls == ELFCLASS32 ? NTXT ("0x%08llx") : NTXT ("0x%016llx"),
			(unsigned long long) msg->instaddr);
	}
      parms[plist_idx - 1] = sb.toString ();

      for (int i = 0; i < plist_idx - 1; i++)
	{
	  prim_ty = ccm_param_primtype (msg->msg_type, i + 1);
	  if (prim_ty == CCM_PRIMTYPE_INTEGER)
	    {
	      unsigned long v = elf->decode (ind[i]);
	      parms[i] = (char*) v;
	    }
	  else if (prim_ty == CCM_PRIMTYPE_STRING)
	    {
	      char *fname = strs + elf->decode (ind[i]);
	      char *demName = get_demangle_name (fname);
	      parms[i] = demName ? demName : dbe_strdup (fname);
	    }
	  else if (prim_ty == CCM_PRIMTYPE_HEXSTRING)
	    parms[i] = dbe_sprintf (elf_cls == ELFCLASS32 ? NTXT ("0x%08llx") : NTXT ("0x%016llx"),
				    (unsigned long long) msg->instaddr);
	  else
	    {
	      fprintf (stderr,
		       GTXT ("Warning, improperly formatted compiler commentary message (unexpected primitive type %d);\n  please report this bug against the compiler\n"),
		       prim_ty);
	      return NULL;
	    }
	}

      /*
       * We have reduced the parameter list to a single string (as
       * the printf format specifier requires), so only have
       * 'plist_idx' parameters.
       */
      mbuf = dbe_sprintf (ccm_attrs[vindex].msg, parms[0], parms[1], parms[2]);

      // Cleanup allocated memory.
      free (parms[plist_idx - 1]);
      for (int i = 0; i < plist_idx - 1; i++)
	{
	  prim_ty = ccm_param_primtype (msg->msg_type, i + 1);
	  if (prim_ty == CCM_PRIMTYPE_STRING)
	    free (parms[i]);
	}
    }
  return mbuf;
}