l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
November 4: Social gathering
Next Installfest:
Latest News:
Oct. 24: LUGOD election season has begun!
Page last updated:
2004 Nov 25 03:13

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] Understanding a C hello world program
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] Understanding a C hello world program

On Wed, 24 Nov 2004, Peter Jay Salzman wrote:

> > At least in theory. data section is also executable in practice which
> > allows buffer overflow to be used to execute malicious codes.  But we
> > can't simply block out execution of the data section because I think
> > function pointers need to be executed outside the text area..???
> Is that true?  I'd think that function pointers necessarily need to point
> inside the text section.

Well let's see:

  ::writes some code::

  We're gonna execute a simple code in stack, data, and heap.
  If they execute OK, you'll see 'OK' at the end of this program.
  Stack: 1
  Data: 1
  Heap: 1

My computer can execute code on the stack, in the data section of the
program (as a global variable), and from the heap.  I think they're all in
the data section, actually, so I think I just did the same test three
times. =P

The code is below the message for your reference.

> YMC.  Where did you learn this from?  Have you taken a compiler class?  At
> some point in my life, I'd like to learn more about this.

I learned about custom sections from working with assemblers at work.  We
were using a custom CPU with a core modeled after a generally available
CPU.  So it had pretty much the same CPU instructions but also had extra
memory mapping areas that wasn't available in the general version of the
CPU.  Since memory maps have to go to specific areas of the memory, we
ended up creating special section names and telling the assembler to map
them to certain memory addresses.  We also did lots of other things with
custom sections that are too crazy to mention.  But in each case, it was
given that the data in each section stayed in one piece, but each section
could be shuffled around to fit the hardware constraints.

Just from reading that assembler's manual and looking at the motivations
behind memory maps, it seemed like general enough idea that it was likely
to be implemented in other assemblers as well.

We did some minor things with sections and assemblers in the microcomputer
class at UC Davis, EEC172.  They used to use the MIPS-based processor but
now they use another one, I think a Motorolla processor.  It was an
excellent course and I highly recommend it to anyone interested in that


Mark K. Kim
AIM: markus kimius
Homepage: http://www.cbreak.org/
Xanga: http://www.xanga.com/vindaci
Friendster: http://www.friendster.com/user.php?uid=13046
PGP key fingerprint: 7324 BACA 53AD E504 A76E  5167 6822 94F0 F298 5DCE
PGP key available on the homepage


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

/* Create a FUNCTION type */
typedef int (*FUNCTION)(int i);

/* Create a simple function */
int fn_in_text(int i)
   return i+1;

/* Above function uses 9 bytes on my system -- Modify as necessary */

/* Create some memory in the data section */
char mem_in_data[FUNCTION_SIZE];

/* Main program */
int main()
   /* Create some stack data */
   char mem_on_stack[9];

   /* Make some function pointers */
   FUNCTION fn_on_stack = (FUNCTION)mem_on_stack;
   FUNCTION fn_in_data = (FUNCTION)mem_in_data;
   FUNCTION fn_in_heap = (FUNCTION)malloc(9);

   /* Copy the function in the text area to data area */
   memmove(fn_on_stack, fn_in_text, 9);
   memmove(fn_in_data, fn_in_text, 9);
   memmove(fn_in_heap, fn_in_text, 9);

   /* Print some informational message */
   printf("We'll execute a simple code in stack, data, and heap.\n");
   printf("You should see 'OK' at the end of this program.\n");

   /* Execute them */
   printf("Stack: %d\n", (*fn_on_stack)(0));
   printf("Data: %d\n", (*fn_in_data)(0));
   printf("Heap: %d\n", (*fn_in_heap)(0));
   printf("OK\n");    /* If you don't see this, it's not OK */

   return 0;

vox-tech mailing list

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.