Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local labels in GNU assembler; gdb printing backtrace as though labels are functions

Two pieces of example code; first some C++ code calling into assembly:

/* test1.cc */
#include <stdio.h>

extern "C" void blah();
extern "C" void stuff() {
  printf( "This is a test\n" );
}

int main( int argc, char *argv[] ) {
  blah();

  return 0;
}

... then the assembly:

.file "test2.s"
.text
.globl blah, stuff
.type blah,@function
.type stuff,@function
.align 16

blah:
  /* normal function preamble */
  pushl %ebp
  movl %esp, %ebp

label1:
  call stuff

  leave
  retn

Built with:

as -g --32 test2.s -o test2.o
clang++ -m32 -g test1.cc -c
clang++ -m32 -g test*.o -o test

Run it under gdb, set a breakpoint on stuff(), then look at the backtrace:

gdb test
(gdb) break stuff
(gdb) run
(gdb) back
     #0  stuff () at test1.cc:5
---> #1  0x08048458 in label1 () at test2.s:12
---> #2  0xffffc998 in ?? ()
     #3  0x0804843e in main (argc=1, argv=0xffffca44) at test1.cc:9

After sifting through [edit an older copy of] the GNU assembler documentation, I tried labels prefixed with L & postfixed with $ to see if it would prevent the labels from being exported, but it didn't work.

If I make the labels numeric the backtrace looks normal, but I'm not overly fond of the notion of using numeric labels.

Could someone point me in the right direction, please?

like image 777
Brian Vandenberg Avatar asked Sep 26 '14 17:09

Brian Vandenberg


People also ask

What is the difference between macro labels and local labels?

However, an ordinary label whose scope is the whole function cannot be used: if the macro can be expanded several times in one function, the label is multiply defined in that function. A local label avoids this problem. For example:

What is the purpose of the local label feature?

The local label feature is useful for complex macros. If a macro contains nested loops, a goto can be useful for breaking out of them. However, an ordinary label whose scope is the whole function cannot be used: if the macro can be expanded several times in one function, the label is multiply defined in that function.

How do you declare a local label in a GCC statement?

Statement Exprs, Up: C Extensions [ Contents ] [ Index] GCC allows you to declare local labels in any nested block scope. A local label is just like an ordinary label, but you can only reference it (with a goto statement, or by taking its address) within the block in which it is declared.

What is the use of labels in Assembly?

Labels A labelcan be placed at the beginning of a statement. During assembly, the label is assigned the current value of the active location counter and serves as an instruction operand. There are two types of lables: symbolicand numeric. Symbolic Labels A symboliclabel consists of an identifier(or symbol) followed by a colon (:) (ASCII 0x3A).


1 Answers

I found an answer in the GNU assembler manual; quoting from that doc:

A local symbol is any symbol beginning with certain local label prefixes. By default, the local label prefix is ‘.L’ for ELF systems or ‘L’ for traditional a.out systems, but each target may have its own set of local label prefixes.

Sure enough, as soon as I put .L on there, it worked.

.L labels don't appear as symbols in the object file.

like image 89
Brian Vandenberg Avatar answered Sep 18 '22 19:09

Brian Vandenberg