0

This experiment is on the 32 bit Linux.

I want to do a transformation on the asm level, and I am trying to implement my transformation before the function main is called.

Currently I am trying to program a new entry point, implement my transformation code, and hope this new entry point can successfully call main

Basically the default entry point of gcc generated assembly code is main, which I give an example as follow:

c code:

int main()
{
    return 0;
}

I use this command to generate asm code:

gcc -masm=intel -fno-asynchronous-unwind-tables -S main.c

and this is what I got:

    .file   "main.c"
    .intel_syntax noprefix
    .text
    .globl  main
    .type   main, @function
main:
    push    ebp
    mov     ebp, esp
    mov     eax, 0
    pop     ebp
    ret
    .size   main, .-main
    .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
    .section        .note.GNU-stack,"",@progbits

Could anyone tell me how to implement a new entry point(probably a function similiar like _start) and call main at the end of this new entry point?

Thank you!

lllllllllllll
  • 8,519
  • 9
  • 45
  • 80
  • 1
    Related: http://stackoverflow.com/questions/8116648/ – Nemo Dec 23 '13 at 07:05
  • 1
    How would your new entry point be called? Otherwise consider rewriting the `crt*.o` startup. Study GCC 4.8.2 source code! – Basile Starynkevitch Dec 23 '13 at 09:32
  • 1
    When you link your code, you can decide the entry point using `-e` switch... For ex: `ld -o output_file_name obj_file.o -lc -dynamic-linker /lib/ld-linux-so.2 -e _start` – Sam Dec 23 '13 at 09:42
  • @SAM Hi, ASM, I did a modiifcation of the code, but it just crashed at the end of start function...I have no idea about what is going on... – lllllllllllll Dec 23 '13 at 16:11
  • @SAM I post my problem [here](http://stackoverflow.com/questions/20747265/why-my-code-will-crash-at-the-end-of-the-start-function), could you please give some help..? Thank you! – lllllllllllll Dec 23 '13 at 16:21
  • @computereasy Hi, sorry for the late reply... You already got the answer there... :) – Sam Dec 24 '13 at 12:25

1 Answers1

2

I doubt you should replace _start() because it's very platform- and libc-specific. Either you write all code in assembler and so you don't need libc-specific initialization, or you should copy all _start() activity including things you aren't aware. The latter looks simply bogus.

If you agree not to replace start() but use a mechanism to run some code before main(), declare a function with __attribute__((constructor)). This is documented GCC extension and it's actively used e.g. for static object initializing in C++. Such function can't get arguments or return a real value, nor shall it override control flow in another way. I can't catch what you mean for "transformation" so it can contradict to your intention; if so, you would have explained this more detailedly.

Netch
  • 4,171
  • 1
  • 19
  • 31
  • Thank you for your answer, now I know it should not be a good idea to implement a __start() like function.. but I think __attribute__((constructor)) should be added in the c/c++ code level, not in assemble level.. – lllllllllllll Dec 23 '13 at 05:43
  • @computereasy In GCC, all features specifies on c/c++ code level are producing assembler output and the latter is compiled into object code. This is principal GCC mode, so it's not important where you implement this attribute, it will work anyway. – Netch Dec 23 '13 at 06:15