aboutsummaryrefslogtreecommitdiff
path: root/gas/codeview.c
blob: 2d96d4467fcf204a137002f5c074c32e60bc9634 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* codeview.c - CodeView debug support
   Copyright (C) 2022-2023 Free Software Foundation, Inc.

   This file is part of GAS, the GNU Assembler.

   GAS 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.

   GAS 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 GAS; see the file COPYING.  If not, write to the Free
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */

#include "as.h"
#include "codeview.h"
#include "subsegs.h"
#include "filenames.h"
#include "md5.h"

#if defined (TE_PE) && defined (O_secrel)

#define NUM_MD5_BYTES       	16

#define FILE_ENTRY_PADDING	2
#define FILE_ENTRY_LENGTH	(sizeof (struct file_checksum) + NUM_MD5_BYTES \
				 + FILE_ENTRY_PADDING)

struct line
{
  struct line *next;
  unsigned int lineno;
  addressT frag_offset;
};

struct line_file
{
  struct line_file *next;
  unsigned int fileno;
  struct line *lines_head, *lines_tail;
  unsigned int num_lines;
};

struct line_block
{
  struct line_block *next;
  segT seg;
  unsigned int subseg;
  fragS *frag;
  symbolS *sym;
  struct line_file *files_head, *files_tail;
};

struct source_file
{
  struct source_file *next;
  unsigned int num;
  char *filename;
  uint32_t string_pos;
  uint8_t md5[NUM_MD5_BYTES];
};

static struct line_block *blocks_head = NULL, *blocks_tail = NULL;
static struct source_file *files_head = NULL, *files_tail = NULL;
static unsigned int num_source_files = 0;

/* Return the size of the current fragment (taken from dwarf2dbg.c).  */
static offsetT
get_frag_fix (fragS *frag, segT seg)
{
  frchainS *fr;

  if (frag->fr_next)
    return frag->fr_fix;

  for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
    if (fr->frch_last == frag)
      return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;

  abort ();
}

/* Emit a .secrel32 relocation.  */
static void
emit_secrel32_reloc (symbolS *sym)
{
  expressionS exp;

  memset (&exp, 0, sizeof (exp));
  exp.X_op = O_secrel;
  exp.X_add_symbol = sym;
  exp.X_add_number = 0;
  emit_expr (&exp, sizeof (uint32_t));
}

/* Emit a .secidx relocation.  */
static void
emit_secidx_reloc (symbolS *sym)
{
  expressionS exp;

  memset (&exp, 0, sizeof (exp));
  exp.X_op = O_secidx;
  exp.X_add_symbol = sym;
  exp.X_add_number = 0;
  emit_expr (&exp, sizeof (uint16_t));
}

/* Write the DEBUG_S_STRINGTABLE subsection.  */
static void
write_string_table (void)
{
  uint32_t len;
  unsigned int padding;
  char *ptr, *start;

  len = 1;

  for (struct source_file *sf = files_head; sf; sf = sf->next)
    {
      len += strlen (sf->filename) + 1;
    }

  if (len % 4)
    padding = 4 - (len % 4);
  else
    padding = 0;

  ptr = frag_more (sizeof (uint32_t) + sizeof (uint32_t) + len + padding);

  bfd_putl32 (DEBUG_S_STRINGTABLE, ptr);
  ptr += sizeof (uint32_t);
  bfd_putl32 (len, ptr);
  ptr += sizeof (uint32_t);

  start = ptr;

  *ptr = 0;
  ptr++;

  for (struct source_file *sf = files_head; sf; sf = sf->next)
    {
      size_t fn_len = strlen (sf->filename);

      sf->string_pos = ptr - start;

      memcpy(ptr, sf->filename, fn_len + 1);
      ptr += fn_len + 1;
    }

  memset (ptr, 0, padding);
}

/* Write the DEBUG_S_FILECHKSMS subsection.  */
static void
write_checksums (void)
{
  uint32_t len;
  char *ptr;

  len = FILE_ENTRY_LENGTH * num_source_files;

  ptr = frag_more (sizeof (uint32_t) + sizeof (uint32_t) + len);

  bfd_putl32 (DEBUG_S_FILECHKSMS, ptr);
  ptr += sizeof (uint32_t);
  bfd_putl32 (len, ptr);
  ptr += sizeof (uint32_t);

  for (struct source_file *sf = files_head; sf; sf = sf->next)
    {
      struct file_checksum fc;

      fc.file_id = sf->string_pos;
      fc.checksum_length = NUM_MD5_BYTES;
      fc.checksum_type = CHKSUM_TYPE_MD5;

      memcpy (ptr, &fc, sizeof (struct file_checksum));
      ptr += sizeof (struct file_checksum);

      memcpy (ptr, sf->md5, NUM_MD5_BYTES);
      ptr += NUM_MD5_BYTES;

      memset (ptr, 0, FILE_ENTRY_PADDING);
      ptr += FILE_ENTRY_PADDING;
    }
}

/* Write the DEBUG_S_LINES subsection.  */
static void
write_lines_info (void)
{
  while (blocks_head)
    {
      struct line_block *lb;
      struct line_file *lf;
      uint32_t len;
      uint32_t off;
      char *ptr;

      lb = blocks_head;

      bfd_putl32 (DEBUG_S_LINES, frag_more (sizeof (uint32_t)));

      len = sizeof (struct cv_lines_header);

      for (lf = lb->files_head; lf; lf = lf->next)
	{
	  len += sizeof (struct cv_lines_block);
	  len += sizeof (struct cv_line) * lf->num_lines;
	}

      bfd_putl32 (len, frag_more (sizeof (uint32_t)));

      /* Write the header (struct cv_lines_header).  We can't use a struct
	 for this as we're also emitting relocations.  */

      emit_secrel32_reloc (lb->sym);
      emit_secidx_reloc (lb->sym);

      ptr = frag_more (len - sizeof (uint32_t) - sizeof (uint16_t));

      /* Flags */
      bfd_putl16 (0, ptr);
      ptr += sizeof (uint16_t);

      off = lb->files_head->lines_head->frag_offset;

      /* Length of region */
      bfd_putl32 (get_frag_fix (lb->frag, lb->seg) - off, ptr);
      ptr += sizeof (uint32_t);

      while (lb->files_head)
	{
	  struct cv_lines_block *block = (struct cv_lines_block *) ptr;

	  lf = lb->files_head;

	  bfd_putl32(lf->fileno * FILE_ENTRY_LENGTH, &block->file_id);
	  bfd_putl32(lf->num_lines, &block->num_lines);
	  bfd_putl32(sizeof (struct cv_lines_block)
		     + (sizeof (struct cv_line) * lf->num_lines),
		     &block->length);

	  ptr += sizeof (struct cv_lines_block);

	  while (lf->lines_head)
	    {
	      struct line *l;
	      struct cv_line *l2 = (struct cv_line *) ptr;

	      l = lf->lines_head;

	      /* Only the bottom 24 bits of line_no actually encode the
		 line number.  The top bit is a flag meaning "is
		 a statement".  */

	      bfd_putl32 (l->frag_offset - off, &l2->offset);
	      bfd_putl32 (0x80000000 | (l->lineno & 0xffffff),
			  &l2->line_no);

	      lf->lines_head = l->next;

	      free(l);

	      ptr += sizeof (struct cv_line);
	    }

	  lb->files_head = lf->next;
	  free (lf);
	}

      blocks_head = lb->next;

      free (lb);
    }
}

/* Return the CodeView constant for the selected architecture.  */
static uint16_t
target_processor (void)
{
  if (stdoutput->arch_info->arch != bfd_arch_i386)
    return 0;

  if (stdoutput->arch_info->mach & bfd_mach_x86_64)
    return CV_CFL_X64;
  else
    return CV_CFL_80386;
}

/* Write the CodeView symbols, describing the object name and
   assembler version.  */
static void
write_symbols_info (void)
{
  static const char assembler[] = "GNU AS " VERSION;

  char *path = lrealpath (out_file_name);
  char *path2 = remap_debug_filename (path);
  size_t path_len, padding;
  uint32_t len;
  struct OBJNAMESYM objname;
  struct COMPILESYM3 compile3;
  char *ptr;

  free (path);
  path = path2;

  path_len = strlen (path);

  len = sizeof (struct OBJNAMESYM) + path_len + 1;
  len += sizeof (struct COMPILESYM3) + sizeof (assembler);

  if (len % 4)
    padding = 4 - (len % 4);
  else
    padding = 0;

  len += padding;

  ptr = frag_more (sizeof (uint32_t) + sizeof (uint32_t) + len);

  bfd_putl32 (DEBUG_S_SYMBOLS, ptr);
  ptr += sizeof (uint32_t);
  bfd_putl32 (len, ptr);
  ptr += sizeof (uint32_t);

  /* Write S_OBJNAME entry.  */

  bfd_putl16 (sizeof (struct OBJNAMESYM) - sizeof (uint16_t) + path_len + 1,
	      &objname.length);
  bfd_putl16 (S_OBJNAME, &objname.type);
  bfd_putl32 (0, &objname.signature);

  memcpy (ptr, &objname, sizeof (struct OBJNAMESYM));
  ptr += sizeof (struct OBJNAMESYM);
  memcpy (ptr, path, path_len + 1);
  ptr += path_len + 1;

  free (path);

  /* Write S_COMPILE3 entry.  */

  bfd_putl16 (sizeof (struct COMPILESYM3) - sizeof (uint16_t)
	      + sizeof (assembler) + padding, &compile3.length);
  bfd_putl16 (S_COMPILE3, &compile3.type);
  bfd_putl32 (CV_CFL_MASM, &compile3.flags);
  bfd_putl16 (target_processor (), &compile3.machine);
  bfd_putl16 (0, &compile3.frontend_major);
  bfd_putl16 (0, &compile3.frontend_minor);
  bfd_putl16 (0, &compile3.frontend_build);
  bfd_putl16 (0, &compile3.frontend_qfe);
  bfd_putl16 (0, &compile3.backend_major);
  bfd_putl16 (0, &compile3.backend_minor);
  bfd_putl16 (0, &compile3.backend_build);
  bfd_putl16 (0, &compile3.backend_qfe);

  memcpy (ptr, &compile3, sizeof (struct COMPILESYM3));
  ptr += sizeof (struct COMPILESYM3);
  memcpy (ptr, assembler, sizeof (assembler));
  ptr += sizeof (assembler);

  memset (ptr, 0, padding);
}

/* Processing of the file has finished, emit the .debug$S section.  */
void
codeview_finish (void)
{
  segT seg;

  if (!blocks_head)
    return;

  seg = subseg_new (".debug$S", 0);

  bfd_set_section_flags (seg, SEC_READONLY | SEC_NEVER_LOAD);

  bfd_putl32 (CV_SIGNATURE_C13, frag_more (sizeof (uint32_t)));

  write_string_table ();
  write_checksums ();
  write_lines_info ();
  write_symbols_info ();
}

/* Assign a new index number for the given file, or return the existing
   one if already assigned.  */
static unsigned int
get_fileno (const char *file)
{
  struct source_file *sf;
  char *path = lrealpath (file);
  char *path2 = remap_debug_filename (path);
  size_t path_len;
  FILE *f;

  free (path);
  path = path2;

  path_len = strlen (path);

  for (sf = files_head; sf; sf = sf->next)
    {
      if (path_len == strlen (sf->filename)
	  && !filename_ncmp (sf->filename, path, path_len))
	{
	  free (path);
	  return sf->num;
	}
    }

  sf = xmalloc (sizeof (struct source_file));

  sf->next = NULL;
  sf->num = num_source_files;
  sf->filename = path;

  f = fopen (file, "r");
  if (!f)
    as_fatal (_("could not open %s for reading"), file);

  if (md5_stream (f, sf->md5))
    {
      fclose(f);
      as_fatal (_("md5_stream failed"));
    }

  fclose(f);

  if (!files_head)
    files_head = sf;
  else
    files_tail->next = sf;

  files_tail = sf;

  num_source_files++;

  return num_source_files - 1;
}

/* Called for each new line in asm file.  */
void
codeview_generate_asm_lineno (void)
{
  const char *file;
  unsigned int filenr;
  unsigned int lineno;
  struct line *l;
  symbolS *sym = NULL;
  struct line_block *lb;
  struct line_file *lf;

  file = as_where (&lineno);

  filenr = get_fileno (file);

  if (!blocks_tail || blocks_tail->frag != frag_now)
    {
      static int label_num = 0;
      char name[32];

      sprintf (name, ".Loc.%u", label_num);
      label_num++;
      sym = symbol_new (name, now_seg, frag_now, frag_now_fix ());

      lb = xmalloc (sizeof (struct line_block));
      lb->next = NULL;
      lb->seg = now_seg;
      lb->subseg = now_subseg;
      lb->frag = frag_now;
      lb->sym = sym;
      lb->files_head = lb->files_tail = NULL;

      if (!blocks_head)
	blocks_head = lb;
      else
	blocks_tail->next = lb;

      blocks_tail = lb;
    }
  else
    {
      lb = blocks_tail;
    }

  if (!lb->files_tail || lb->files_tail->fileno != filenr)
    {
      lf = xmalloc (sizeof (struct line_file));
      lf->next = NULL;
      lf->fileno = filenr;
      lf->lines_head = lf->lines_tail = NULL;
      lf->num_lines = 0;

      if (!lb->files_head)
	lb->files_head = lf;
      else
	lb->files_tail->next = lf;

      lb->files_tail = lf;
    }
  else
    {
      lf = lb->files_tail;
    }

  l = xmalloc (sizeof (struct line));
  l->next = NULL;
  l->lineno = lineno;
  l->frag_offset = frag_now_fix ();

  if (!lf->lines_head)
    lf->lines_head = l;
  else
    lf->lines_tail->next = l;

  lf->lines_tail = l;
  lf->num_lines++;
}

#else

void
codeview_finish (void)
{
}

void
codeview_generate_asm_lineno (void)
{
}

#endif /* TE_PE && O_secrel */