Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active January 1, 2024 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x-yuri/748b93ccdf776908ff13a9d2a6cac7ee to your computer and use it in GitHub Desktop.
Save x-yuri/748b93ccdf776908ff13a9d2a6cac7ee to your computer and use it in GitHub Desktop.
C + constansts (defines) + gdb

C + constansts (defines) + gdb

a.sh:

set -eu
docker run --rm -itv "$PWD:/app" -w /app alpine:3.19 sh -euc '
    apk add gcc musl-dev gdb
    gcc -g3 a.c
    exec sh
'
$ sh a.sh
/app # readelf -wi a.out
Contents of the .debug_info section:

...
  Compilation Unit @ offset 0x17d:
   Length:        0xca (32-bit)
   Version:       5
   Unit Type:     DW_UT_compile (1)
   Abbrev Offset: 0xfc
   Pointer Size:  8
 <0><189>: Abbrev Number: 3 (DW_TAG_compile_unit)
    <18a>   DW_AT_producer    : (indirect string, offset: 0xacf): GNU C17 13.2.1 20231014 -mtune=generic -march=x86-64 -g3
    <18e>   DW_AT_language    : 29	(C11)
    <18f>   DW_AT_name        : (indirect line string, offset: 0x70): a.c
    <193>   DW_AT_comp_dir    : (indirect line string, offset: 0x74): /app
    <197>   DW_AT_low_pc      : 0x1185
    <19f>   DW_AT_high_pc     : 0x29
    <1a7>   DW_AT_stmt_list   : 0xba
    <1ab>   DW_AT_macros      : 0
...
 <1><1ee>: Abbrev Number: 2 (DW_TAG_const_type)
    <1ef>   DW_AT_type        : <0x1de>
 <1><1f3>: Abbrev Number: 6 (DW_TAG_variable)
    <1f4>   DW_AT_name        : msg
    <1f8>   DW_AT_decl_file   : 1
    <1f9>   DW_AT_decl_line   : 3
    <1fa>   DW_AT_decl_column : 12
    <1fb>   DW_AT_type        : <0x1ee>
    <1ff>   DW_AT_external    : 1
    <1ff>   DW_AT_location    : 9 byte block: 3 0 20 0 0 0 0 0 0 	(DW_OP_addr: 2000)
...

/app # readelf -wm a.out
Contents of the .debug_macro section:

  Offset:                      0
  Version:                     5
  Offset size:                 4
  Offset into .debug_line:     0xba

 DW_MACRO_import - offset : 0x46
 DW_MACRO_start_file - lineno: 0 filenum: 1
 DW_MACRO_start_file - lineno: 0 filenum: 3
 DW_MACRO_import - offset : 0x980
 DW_MACRO_end_file
 DW_MACRO_start_file - lineno: 1 filenum: 2
 DW_MACRO_define_strp - lineno : 2 macro : _STDIO_H 
 DW_MACRO_start_file - lineno: 8 filenum: 4
 DW_MACRO_import - offset : 0x9a2
 DW_MACRO_end_file
 DW_MACRO_import - offset : 0x9ca
 DW_MACRO_start_file - lineno: 26 filenum: 5
 DW_MACRO_import - offset : 0x9f2
 DW_MACRO_end_file
 DW_MACRO_import - offset : 0xa63
 DW_MACRO_end_file
 DW_MACRO_define_strp - lineno : 2 macro : MSG "hello world (define)\n"
 DW_MACRO_end_file
...

/app # gdb ./a.out
(gdb) b main
Breakpoint 1 at 0x1189: file a.c, line 5.
(gdb) r
Starting program: /app/a.out 

Breakpoint 1, main () at a.c:5
5           puts(MSG);
(gdb) p MSG
$1 = "hello world (define)\n"
(gdb) p msg
$2 = "hello world (const)\n"
(gdb) call strcmp(MSG, msg)
$3 = 1
(gdb) call strcmp(msg, MSG)
$4 = -1
(gdb) call strncmp(MSG, msg, 11)
$5 = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment