This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix profiling on hppa (Fix structure alignment in gmon)


libc-alpha,

==================================
Fixing Structure Alignment in gmon
==================================

hppa requires gmon/gmon.c to be changed in order for profiling to work
correctly. A description of the required changes and their impact follows:

If an arch requires n-bit loads to be n-bit aligned, and sizeof(unsigned short)
!= sizeof(unsigned long) then 'p->froms' can be *unaligned* depending on
TEXT_START and etext values, passed in from csu/gmon-start.c (__gmon_start__).

An explanation of the change required on line 117 of gmon/gmon.c:

gmon/gmon.c:

114   p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
115   p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
116   p->textsize = p->highpc - p->lowpc;
117   p->kcountsize = ROUNDUP(p->textsize / HISTFRACTION, sizeof(*p->froms));
...
135   cp = calloc (p->kcountsize + p->fromssize + p->tossize, 1);
...
143   p->tos = (struct tostruct *)cp;
144   cp += p->tossize;
145   p->kcount = (HISTCOUNTER *)cp;
146   cp += p->kcountsize;
147   p->froms = (ARCINDEX *)cp;

When allocating 'cp' on 135, upon which we will store 'tos', 'kcount'
and 'froms', we must be very careful about the start of each of these
elements, since their alignment is important.

We begin by determining their individual types and sizes:

struct tostruct - 3 long sized value, alignment is 4-byte.
HISTCOUNTER - unsigned short, alignment is 2-byte.
ARCINDEX - unsigned long, alignment is 4-byte.

Some quick math shows that while 'p->highpc' and 'p->lowpc' are aligned
at 4-bytes, and their difference is a multiple of 4-bytes, when you
divide 'p->textsize' by HISTFRACTION you are not guaranteed a value 
that is a multiple of 4-bytes. Keep in mind that 'p->textsize' determines 
the start of 'p->froms'.

The order of elements in the memory at 'cp' is as follows

        |-- tos --|-- kcount --|-- froms --|

It might happen that 'p->froms' is unaligned due to 'p->kcount'. To
prevent this we round 'p->kcountsize' to 'sizeof(*p->froms)' on 117,
adding 0 or 2 bytes of empty space at the end of 'p->kcount'.

I've tested this on hppa, and i386 with no adverse effects. It's required 
for hppa to function properly.

c.

2003-11-15  Randolph Chung  <tausq@debian.org>

	* gmon/gmon.c (__monstartup): Round kcountsize to multiples of
	the froms[] array so the array is properly aligned.

--- libc-orig/gmon/gmon.c	2003-09-04 10:20:46.000000000 -0400
+++ libc/gmon/gmon.c	2003-12-15 00:38:40.000000000 -0500
@@ -114,7 +114,7 @@
   p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
   p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
   p->textsize = p->highpc - p->lowpc;
-  p->kcountsize = p->textsize / HISTFRACTION;
+  p->kcountsize = ROUNDUP(p->textsize / HISTFRACTION, sizeof(*p->froms));
   p->hashfraction = HASHFRACTION;
   p->log_hashfraction = -1;
   /* The following test must be kept in sync with the corresponding


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]