On 2015-12-01 at  6:59 'Davide Libenzi' via Akaros wrote:
> Since I am currently using dummies in other code, can you check if
> something like this pleases the linker, without screwing up with
> section size constraints (multiple of object size):
> 
> #define INIT_SECTION(sect) char __attribute__((__section__( #sect )))
> __init_section_##sect[0]

My usual approach is to try something and see if the compiler/linker
yell at me, then do an objdump to see if it worked.  =)

> A better alternative would be weaks for __start_/__stop_. This
> generates no linker errors, even though foo is never defined:
> 
> extern int __attribute__((weak)) foo[];

I like this approach.

> int main(void)
> {
>     if (foo)
>         return 0;
> 
>     return 1;
> }
> 
> Look at what GCC (and linker missing relocation) does with rax/eax ☺
> 
> 
> 00000000004004ed <main>:
>   4004ed:       55                      push   %rbp
>   4004ee:       48 89 e5                mov    %rsp,%rbp
>   4004f1:       b8 00 00 00 00          mov    $0x0,%eax
>   4004f6:       48 85 c0                test   %rax,%rax
>   4004f9:       74 07                   je     400502 <main+0x15>
>   4004fb:       b8 00 00 00 00          mov    $0x0,%eax
>   400500:       eb 05                   jmp    400507 <main+0x1a>
>   400502:       b8 01 00 00 00          mov    $0x1,%eax
>   400507:       5d                      pop    %rbp
>   400508:       c3                      retq
>   400509:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)

That 0x0 gets overriden at link time, so it seems to work.

foo.c:

        extern int __attribute__((weak)) foo[];
        int main(void)
        {
                if (foo)
                        return 0;
                return 1;
        }

bar.c:
        int foo[5];

$ gcc -c foo.c -o foo.o
$ objdump -d foo.o

foo.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   b8 00 00 00 00          mov    $0x0,%eax
   9:   48 85 c0                test   %rax,%rax
   c:   74 07                   je     15 <main+0x15>
   e:   b8 00 00 00 00          mov    $0x0,%eax
  13:   eb 05                   jmp    1a <main+0x1a>
  15:   b8 01 00 00 00          mov    $0x1,%eax
  1a:   5d                      pop    %rbp
  1b:   c3                      retq   

Still has $0x0 for the instruction at byte 0x4.

$ objdump -x foo.o

foo.o:     file format elf64-x86-64
foo.o
architecture: i386:x86-64, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x0000000000000000

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         0000001c  0000000000000000  0000000000000000  00000040  2**0
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  1 .data         00000000  0000000000000000  0000000000000000  0000005c  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000000  0000000000000000  0000000000000000  0000005c  2**0
                  ALLOC
  3 .comment      0000002b  0000000000000000  0000000000000000  0000005c  2**0
                  CONTENTS, READONLY
  4 .note.GNU-stack 00000000  0000000000000000  0000000000000000  00000087  2**0
                  CONTENTS, READONLY
  5 .eh_frame     00000038  0000000000000000  0000000000000000  00000088  2**3
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 foo.c
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 l    d  .data  0000000000000000 .data
0000000000000000 l    d  .bss   0000000000000000 .bss
0000000000000000 l    d  .note.GNU-stack        0000000000000000 .note.GNU-stack
0000000000000000 l    d  .eh_frame      0000000000000000 .eh_frame
0000000000000000 l    d  .comment       0000000000000000 .comment
0000000000000000 g     F .text  000000000000001c main
0000000000000000  w      *UND*  0000000000000000 foo


RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE 
0000000000000005 R_X86_64_32       foo


RELOCATION RECORDS FOR [.eh_frame]:
OFFSET           TYPE              VALUE 
0000000000000020 R_X86_64_PC32     .text

It has a relocation record at offset 0x5, which is where the $0x0 is in
the instruction mov $0x0, %eax.

$ gcc -c bar.c -o bar.o
$ gcc -o foobar foo.o bar.o
$ objdump -d foobar
(edited to just show main)

0000000000400526 <main>:
  400526:       55                      push   %rbp
  400527:       48 89 e5                mov    %rsp,%rbp
  40052a:       b8 50 10 60 00          mov    $0x601050,%eax
  40052f:       48 85 c0                test   %rax,%rax
  400532:       74 07                   je     40053b <main+0x15>
  400534:       b8 00 00 00 00          mov    $0x0,%eax
  400539:       eb 05                   jmp    400540 <main+0x1a>
  40053b:       b8 01 00 00 00          mov    $0x1,%eax
  400540:       5d                      pop    %rbp
  400541:       c3                      retq 

Barret

-- 
You received this message because you are subscribed to the Google Groups 
"Akaros" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to