7

Any ways to get the address of an instruction? I have tried to used to labeled the instruction and get the address of the label but seems that it only works in GCC compiler with the && operator. (Refer to: this article) What if other compilers?

Any thoughts of this question?

Matt
  • 22,721
  • 17
  • 71
  • 112
nigong
  • 1,727
  • 3
  • 19
  • 33
  • 4
    C++ is not required to be implemented with instructions. – Seth Carnegie Jul 17 '12 at 20:48
  • Hi Basile, I am currently doing some research on the instruction / data watch features. – nigong Jul 17 '12 at 20:51
  • 1
    Can we assume GCC with [explicit register variables](http://gcc.gnu.org/onlinedocs/gcc/Explicit-Reg-Vars.html)? In that case, I'd use `register char *program_counter asm("pc");`, assuming that the program counter register PC (for ARM, use the instruction pointer, IP, on x86...it's processor dependent). – Kevin Vermeer Jul 17 '12 at 20:53
  • Seth, sorry, why I used instruction instead of statement is that after the code compiled, it will has one or more instructions associated with the statement. But I want to refer to the first instruction of the statement. Thanks for clarifying me. – nigong Jul 17 '12 at 20:54
  • Hi all, also I thought of using function pointer to a function of which only contains the instruction you want to get the address. It is a solution, but I don't think it is a neat way. :) – nigong Jul 17 '12 at 20:55
  • @nigong you miss my point; there's no way this can be done in standard C because C has no concept of instructions or the address of statements. (Sorry I said C++ before, I forget which tag I'm in all the time.) – Seth Carnegie Jul 17 '12 at 20:56
  • I think he isn't looking for a generic, standard safe way.. maybe just a somewhat portable one? @nigong: what are the targeted compilers/platforms? – Karoly Horvath Jul 17 '12 at 21:00
  • @KarolyHorvath: Anything non-standard is inherently non-portable. – Ed S. Jul 17 '12 at 21:00
  • @Ed S.: you missed the word "somewhat" (eg: working with gcc and msvc) – Karoly Horvath Jul 17 '12 at 21:03
  • @KarolyHorvath My platform is VDSP from Analog Devices, but I am also looking at a generic way to do so. – nigong Jul 17 '12 at 21:05
  • @KarolyHorvath: No, it is inherently non-portable. Of course, in practice there are ways to get around this, but technically, if it's you're relying on something that's not specified in the standard then you risk encountering issues when targeting a different platform. That much I think we can agree upon. – Ed S. Jul 17 '12 at 21:30
  • 1
    @Ed S.: agree? that was obvious. – Karoly Horvath Jul 17 '12 at 21:30
  • @KarolyHorvath: Then what are you questioning exactly? I'm confused. – Ed S. Jul 17 '12 at 21:50
  • @Ed S.: yes, you seem to be confused. I've just asked him about the targeted platform, that's all. I guess he's rather looking for a practical solution than a dogmatic answer ("there's no generic way") which doesn't have much pragmatic value. welcome to the real world (just kidding, don't take it seriously). – Karoly Horvath Jul 17 '12 at 22:00
  • @KarolyHorvath: Oh, is that last bit kind of like saying "No offense, but you're an idiot"? I already said there are obviously ways to work around it, doesn't change the fact that non-standard code is inherently non-portable. – Ed S. Jul 17 '12 at 22:03
  • 1
    @Ed S.: I just don't understand what made you think I wasn't aware of that... but nevermind. – Karoly Horvath Jul 17 '12 at 22:06

4 Answers4

4

I'm not sure that the C or the C++ language standards speak of instruction; you could conceivably run a C program compiled to be executed by a large group of human slaves (but that would be unethical, even if standard conforming). You can run by yourself a simplistic tiny program with a pencil and paper, you could use a C interpreter, etc... etc...

An optimizing C or C++ compiler would also translate some C statements into several machine instructions located at various places (in particular, compilers do inline or clone functions or even basic blocks, even without any inline keyword in the source code).

On most system, you might hand code (e.g. in assembly) a routine which returns as a pointer value its return address (i.e. the address of the caller).

With GCC (and compatible compilers, perhaps Clang/LLVM), you might use __builtin_return_address and related builtins.

Gnu Libc on Linux offers you backtrace (but I have been diappointed by it, when I tried).

Notice that these tricks might not work when compiling with -fno-frame-pointer

You might also consider customizing GCC e.g. with plugins or MELT extensions (MELT is a domain specific language, implemented as a GPLv3 plugin to GCC, to extend GCC more easily than with plugins coded in C). Your customized GCC might insert calls to instrumentation functions at appropriate places.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Hi Basile, thanks for your answer. But I didn't mean to say the relation of instructions and statement. What I want to do is just get the address of a statement or instruction in C/C++. :) – nigong Jul 17 '12 at 20:59
  • 1
    @nigong He's basically saying that there may not even be instructions at all. Especially if it were being run by slaves rather than a CPU. – Mysticial Jul 17 '12 at 21:03
  • 1
    If 'instruction' and 'statement' were sufficiently similar concepts, this might be feasible. However, most statements generate multiple instructions, which may not even be consecutive (loop constructs, etc.) and may be interleaved (optimization, instruction scheduling) with instructions from other statements. – twalberg Jul 17 '12 at 21:13
2

Caution: Way out of the box here..

Can you have the linker generate a map file that contains the addresses ( relative or otherwise ) of the applications objects ( functions and data )? Your application could then simply parse the linker map file...

Chimera
  • 5,884
  • 7
  • 49
  • 81
0

The only standard C or C++ construct that is even close to the address of an instruction is a function pointer, which I suppose is not what you mean. However, you can accomplish most of what you might do with gcc label values using instead pointers to functions containing the necessary blocks of code.

There are no other options in C/C++.

Gene
  • 46,253
  • 4
  • 58
  • 96
0

The return address of a function is the location of the next instruction to be executed upon return from the current function. GCC exposes this through __builtin_return_address; while Visual C++ provides it via _ReturnAddress.

user2023370
  • 10,488
  • 6
  • 50
  • 83