0
#include<stdio.h>

volatile int data __attribute__((section(".interface")) = 5;
int* dataPtr = (int *) (0x12345678);

int main(void){
    printf("%p\n",&dataPtr);
    printf("%p\n",dataPtr);
    printf("%d\n",*(int *)dataPtr);
    printf("%p\n",&data);
    return 0;
}

Line printf("%d\n",*(int *)dataPtr); gives segmentation fault. Pasting relevant snippets from the objdump -x of the application: snippet of the memory map

architecture: i386:x86-64, flags 0x00000150: 
HAS_SYMS, DYNAMIC, D_PAGED
start address 0x0000000000001060
.
.
22 .dynamic      000001f0  0000000000003dc8  0000000000003dc8  00002dc8  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 23 .got          00000048  0000000000003fb8  0000000000003fb8  00002fb8  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 24 .data         00000018  0000000000004000  0000000000004000  00003000  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 25 .bss          00000008  0000000000004018  0000000000004018  00003018  2**0
                  ALLOC
 26 .comment      0000002a  0000000000000000  0000000000000000  0000367c  2**0
                  CONTENTS, READONLY
 27 .ipl1_interface 00000004  0000000012345678  0000000012345678  00003678  2**2
                  CONTENTS, ALLOC, LOAD, DATA
.
.
SYMBOL TABLE:
.
.
0000000000004010 g     O .data 0000000000000008              dataPtr
0000000000001238 g     F .fini 0000000000000000              .hidden _fini
0000000000000000       F *UND* 0000000000000000              printf@@GLIBC_2.2.5
0000000000000000       F *UND* 0000000000000000              __libc_start_main@@GLIBC_2.2.5
0000000000004000 g       .data 0000000000000000              __data_start
0000000000000000  w      *UND* 0000000000000000              __gmon_start__
0000000000004008 g     O .data 0000000000000000              .hidden __dso_handle
0000000000002000 g     O .rodata 0000000000000004              _IO_stdin_used
0000000012345678 g     O .ipl1_interface 0000000000000004              data

In GDB this works fine:p *(int *)dataPtr;. The data symbol is in the symbol table and its address is 0x12345678. Can you provide an insight into what might be going on?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Umar
  • 1
  • 3
    `0x12345678` is an invalid address. What makes you think it is a valid address? – Jabberwocky Nov 04 '21 at 08:32
  • Please post the output of your program when executing. Please post the output of `p *(int *)dataPtr;` from your gdb session. Does the program segfault inside gdb session? Sections with a leading dot `.` are "meant" to be for implementation, prefer no dot for your custom sections. How did you add `.ipl1_interface` section? How have you compiled your program? And, I think, read about [ASLR](https://en.wikipedia.org/wiki/Address_space_layout_randomization) and https://stackoverflow.com/questions/56347064/enable-aslr-in-gdb – KamilCuk Nov 04 '21 at 08:32
  • 2
    BTW: don't post pictures of text. The snippet of the memory map is text, so post that as text. – Jabberwocky Nov 04 '21 at 08:33
  • 3
    I get `error: expected ')' before '=' token 3 | volatile int data __attribute__((section(".interface")) = 5;` when trying to compile it. Typo? Should it be `volatile int data __attribute__((section(".interface"))) = 5;` like [this](https://godbolt.org/z/W5GnMe7En) instead? – Ted Lyngmo Nov 04 '21 at 08:34
  • 1
    You can link something to put it at address 0x12345678, but to actually access it, there must be accessible, readable memory at that address. This is unlikely to be the case, unless you are working on firmware on a microcontroller where RAM or some such is mapped to a range of addresses including 0x12345678. From what you have pasted above, with the tag, 'linux', this does not appear to be the case. In general, if you are working in an operating system, you do not specify literal address values in that way. – Basya Nov 04 '21 at 08:44
  • Yeah I am aware of that. I wanted to write a unit test of firmware code on Linux system. thanks @KamilCuk in point towards the right direction. It indeed was due to ASLR being turned off in GDB, when I turn it on I get an error saying can not access the memory, with it turned off this was the output `(gdb) p *(int *) dataPtr $1 = 5`. Thanks @Ted for pointing out the typo. I provided a linker script while compiling the above code defining a section ipl1_interface. The intent was to assign a variable of the firmware code the intended address without change in the code. – Umar Nov 04 '21 at 08:53
  • 3
    @Umar You can [edit](https://stackoverflow.com/posts/69836197/edit) your question to fix typos and add additional information. That's usually better than providing that info in comments. – Ted Lyngmo Nov 04 '21 at 08:58

0 Answers0