public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: mainland@apeiron.net
To: gcc-gnats@gcc.gnu.org
Subject: target/5474: GCC 3+ generates bad stack offset on ARM with -fpic and -msingle-pic-base
Date: Wed, 23 Jan 2002 22:36:00 -0000	[thread overview]
Message-ID: <200201240629.g0O6TTW06482@anaximander.apeiron.net> (raw)


>Number:         5474
>Category:       target
>Synopsis:       GCC 3+ generates bad stack offset on ARM with -fpic and -msingle-pic-base
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Wed Jan 23 22:36:01 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Geoffrey Mainland
>Release:        3.0.2
>Organization:
>Environment:
System: FreeBSD anaximander.apeiron.net 4.5-PRERELEASE FreeBSD 4.5-PRERELEASE #1: Sat Jan 5 18:42:31 PST 2002 toor@anaximander.apeiron.net:/.amd_mnt/anaximander/home01/freebsd-src/obj/.amd_mnt/anaximander/home01/freebsd-src/src/sys/APEIRON i386

host: i386-unknown-freebsd4.5
build: i386-unknown-freebsd4.5
target: arm-unknown-elf
configured with: ../../tools/gcc/configure --prefix=/home/mainland/work/handspring/arm-tools/ --target=arm-elf --enable-languages=c++ --with-gnu-as --with-gnu-ld --with-newlib --with-dwarf2 --with-cross-host --enable-multilib
>Description:
Compiling with -fpic and -msingle-pic-base can produce incorrect code. If a function uses global data the static base register will not be saved on the stack, but offsets to variables stored on the stack are still calculated as if the static base register *had* been saved on the stack.

>How-To-Repeat:

Code fragment:

static const char	const_string[] = "";
char				string[10];

void	foo(int arg);
int		bar(int* arg);

void    
foo(int arg)
{
	int	local;
	
	string[0] = const_string[0];
	bar(&local);
}

int
bar(int* arg)
{
	return *arg;
}


With -fpic only:

arm-elf-gcc -g -fpic -c test.c -o test.o

test.o:     file format elf32-littlearm

Disassembly of section .text:

00000000 <foo>:
   0:	e1a0c00d 	mov	ip, sp
   4:	e92ddc00 	stmdb	sp!, {sl, fp, ip, lr, pc}
   8:	e24cb004 	sub	fp, ip, #4	; 0x4
   c:	e24dd008 	sub	sp, sp, #8	; 0x8
  10:	e59fa028 	ldr	sl, [pc, #28]	; 40 <foo+0x40>
  14:	e08fa00a 	add	sl, pc, sl
  18:	e50b0014 	str	r0, [fp, -#20]
  1c:	e59f3020 	ldr	r3, [pc, #20]	; 44 <foo+0x44>
  20:	e79a3003 	ldr	r3, [sl, r3]
  24:	e5d32000 	ldrb	r2, [r3]
  28:	e59f3018 	ldr	r3, [pc, #18]	; 48 <foo+0x48>
  2c:	e79a3003 	ldr	r3, [sl, r3]
  30:	e5c32000 	strb	r2, [r3]
  34:	e24b0018 	sub	r0, fp, #24	; 0x18
  38:	ebfffffe 	bl	0 <foo>
  3c:	e91bac00 	ldmdb	fp, {sl, fp, sp, pc}
  40:	00000024 	andeq	r0, r0, r4, lsr #32
	...

0000004c <bar>:
  4c:	e1a0c00d 	mov	ip, sp
  50:	e92dd800 	stmdb	sp!, {fp, ip, lr, pc}
  54:	e24cb004 	sub	fp, ip, #4	; 0x4
  58:	e24dd004 	sub	sp, sp, #4	; 0x4
  5c:	e50b0010 	str	r0, [fp, -#16]
  60:	e51b3010 	ldr	r3, [fp, -#16]
  64:	e5933000 	ldr	r3, [r3]
  68:	e1a00003 	mov	r0, r3
  6c:	e91ba800 	ldmdb	fp, {fp, sp, pc}
Disassembly of section .data:
Disassembly of section .rodata:

00000000 <const_string>:
   0:	00000000 	andeq	r0, r0, r0


With -fpic and -msingle-pic-base:

arm-elf-gcc -g -fpic -msingle-pic-base -c test.c -o test.o

test.o:     file format elf32-littlearm

Disassembly of section .text:

00000000 <foo>:
   0:	e1a0c00d 	mov	ip, sp
   4:	e92dd800 	stmdb	sp!, {fp, ip, lr, pc}
   8:	e24cb004 	sub	fp, ip, #4	; 0x4
   c:	e24dd008 	sub	sp, sp, #8	; 0x8
  10:	e50b0014 	str	r0, [fp, -#20]
  14:	e59f301c 	ldr	r3, [pc, #1c]	; 38 <foo+0x38>
  18:	e79a3003 	ldr	r3, [sl, r3]
  1c:	e5d32000 	ldrb	r2, [r3]
  20:	e59f3014 	ldr	r3, [pc, #14]	; 3c <foo+0x3c>
  24:	e79a3003 	ldr	r3, [sl, r3]
  28:	e5c32000 	strb	r2, [r3]
  2c:	e24b0018 	sub	r0, fp, #24	; 0x18
  30:	ebfffffe 	bl	0 <foo>
  34:	e91ba800 	ldmdb	fp, {fp, sp, pc}
	...

00000040 <bar>:
  40:	e1a0c00d 	mov	ip, sp
  44:	e92dd800 	stmdb	sp!, {fp, ip, lr, pc}
  48:	e24cb004 	sub	fp, ip, #4	; 0x4
  4c:	e24dd004 	sub	sp, sp, #4	; 0x4
  50:	e50b0010 	str	r0, [fp, -#16]
  54:	e51b3010 	ldr	r3, [fp, -#16]
  58:	e5933000 	ldr	r3, [r3]
  5c:	e1a00003 	mov	r0, r3
  60:	e91ba800 	ldmdb	fp, {fp, sp, pc}
Disassembly of section .data:
Disassembly of section .rodata:

00000000 <const_string>:
   0:	00000000 	andeq	r0, r0, r0

>Fix:

--- gcc/config/arm/arm.h.orig	Thu May 24 14:03:58 2001
+++ gcc/config/arm/arm.h	Wed Jan 23 19:37:42 2002
@@ -1681,7 +1681,9 @@
 	      && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])		\
 	    saved_hard_reg = 1, offset += 4;				\
 	  /* PIC register is a fixed reg, so call_used_regs set.  */	\
-	  if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])	\
+	  if (flag_pic							\
+	  && ! TARGET_SINGLE_PIC_BASE 					\
+	  && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])			\
 	    saved_hard_reg = 1, offset += 4;				\
           for (regno = FIRST_ARM_FP_REGNUM;				\
 	       regno <= LAST_ARM_FP_REGNUM; regno++)			\
>Release-Note:
>Audit-Trail:
>Unformatted:


             reply	other threads:[~2002-01-24  6:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-23 22:36 mainland [this message]
2002-01-25  4:14 pb
2002-04-23  8:02 rearnsha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200201240629.g0O6TTW06482@anaximander.apeiron.net \
    --to=mainland@apeiron.net \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).