Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Pass a value through different memory sections

+0
−0

Lets get a bit closer to the metal than usual! The task is to pass a constant value between the different data memory sections of a typical computer.

For this challenge you need a system and language which allows you to allocate variables in all of the following sections (using ELF notation):

  • .rodata or .text
  • .data
  • .bss
  • .stack
  • .heap

Start by allocate an integer constant value in a read-only section (.rodata or .text etc).

Print the integer value and its address using the syntax value <space> address <new line>. The value should be printed with decimal notation and the address with hex notation.

Any common hex prefix/suffix is allowed, or could be left out: ABCD, 0xABCD, ABCDh and $ABCD are all valid outputs for the hex part.

Once printed, copy the value to any of the other memory sections above - in any order, then print it again, with the same syntax. Repeat until the value has passed through all memory sections.

Example in non-golfed standard C (gcc), using the value 5:

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
  static const int a=5;    
  printf("%d %p\n",a,&a); // .rodata
  
  static int b; b=a;
  printf("%d %p\n",b,&b); // .bss
  
  int c=b;
  printf("%d %p\n",c,&c); // .stack

  static int d=1;
  d=c;
  printf("%d %p\n",d,&d); // .data
  
  int* e = malloc(sizeof *e);
  *e=d;
  printf("%d %p\n",*e,e); // .heap
}

Try it online!

Output on Linux x86_64 (gcc/glibc):

5 0x402018
5 0x404034
5 0x7ffe4c9635e4
5 0x40402c
5 0x100de70

Output on Windows 86_64 (gcc/mingw/ms crt):

5 0000000000404008
5 0000000000407030
5 000000000022FE44
5 0000000000403010
5 0000000000327D90

Because of memory mapping, ASLR and similar, no solution is expected to have the same addresses. Yet one should be able to reason that large address differences generally means different sections.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads