public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Unomitted frame pointers
@ 2004-12-10 15:44 Thomas R. Truscott
  2004-12-10 16:05 ` jlh
  0 siblings, 1 reply; 30+ messages in thread
From: Thomas R. Truscott @ 2004-12-10 15:44 UTC (permalink / raw)
  To: gcc

Off-topic, but the hello.c program does:

	write(2, "Hello World!\n\0", 16);

The string length is 15, not 16.
It would be nice if gcc issued warnings for compile-time constant
string/length pairs where the length is too large.

I do that in my local copy of gcc, and it routinely finds
coding mistakes for memcpy().
Perhaps this could somehow be an "attribute" that gcc can check.

(Hmm, the attribute thing is too limited, what would be really
nice is a way to write code that does general compile-time argument
checking.  Then one could do more thorough checks such as is
done for printf/scanf.  And the same code could be invoked
at run time ... oops, way off topic.)

Tom Truscott

^ permalink raw reply	[flat|nested] 30+ messages in thread
* Re: Unomitted frame pointers
@ 2004-12-11 16:29 Sam Lauber
  2004-12-12  1:33 ` Ian Lance Taylor
  0 siblings, 1 reply; 30+ messages in thread
From: Sam Lauber @ 2004-12-11 16:29 UTC (permalink / raw)
  To: Andreas Schwab, gcc

How can the linker combine them? The _program_ is way outside GCC's control. The assembler and linker don't modify the generated assembly. Otherwise, BFD and binutils would become very twisted up, and thus make the assembler world on GCC diffrent than it is now.

----- Original Message -----
From: "Andreas Schwab" <schwab@suse.de>
To: "Sam Lauber" <sam124@operamail.com>
Subject: Re: Unomitted frame pointers
Date: Sat, 11 Dec 2004 16:44:54 +0100

> 
> "Sam Lauber" <sam124@operamail.com> writes:
> 
> > We would end up with two copies of the string. #defines are
> > preprocessed, so it would expand to the second thing.
> 
> And the compiler and the linker combine them again.
> 
> > The _safe_ way to do it is
> >
> > char str = "Hello World!\n";
> > write(2, str, strlen(str)-1);
> 
> Why would you want to exclude the trailing newline?
> 
> Andreas.
> 
> --
> Andreas Schwab, SuSE Labs, schwab@suse.de
> SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
> Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
> "And now for something completely different."

-- 
_____________________________________________________________
Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.

Powered by Outblaze

^ permalink raw reply	[flat|nested] 30+ messages in thread
* Re: Unomitted frame pointers
@ 2004-12-11 16:26 Sam Lauber
  2004-12-11 16:42 ` Nathan Sidwell
  2004-12-12  0:09 ` Peter Barada
  0 siblings, 2 replies; 30+ messages in thread
From: Sam Lauber @ 2004-12-11 16:26 UTC (permalink / raw)
  To: Nathan Sidwell, gcc

Do you know about macro expansion? CPP would make it seperate strings. So there would be (simplified);

.LC0:
.string "Hello World!\n"
.LC1:
.string "Hello World!\n"
main:
; push LC0 into strlen
push #.LC0, (%eax)
call strlen
; push LC1 into write
push #.LC1, (%ebx)
...

It automatically happens, and I don't know of any way to avoid cpp from macro-expanding it to that. Though inlining would reduce it to the C code

int main(void)
{
       char str1 = "Hello World!\n";
       char str2 = "Hello World!\n";
       int str_len = 0;
       while (*str1++ != "\0")
              str_len++;
       write(2, str2, str_len);
}

embedding strlen inside main. Or even more optimization

int main(void)
{
        write(2, "Hello World!\n", 13);
}

which is back where we started.

Samuel Lauber

----- Original Message -----
From: "Nathan Sidwell" <nathan@codesourcery.com>
To: "Sam Lauber" <sam124@operamail.com>
Subject: Re: Unomitted frame pointers
Date: Sat, 11 Dec 2004 15:40:38 +0000

> 
> Sam Lauber wrote:
> > We would end up with two copies of the string. #defines are 
> > preprocessed, so it would expand to the second thing. The _safe_ 
> > way to do it is
> Why do you think you'd get two copies of the string?
> 
> >
> > char str = "Hello World!\n";
> type mismatch error
> 
> > write(2, str, strlen(str)-1);
> off by one error
> 
> nathan
> -- Nathan Sidwell    ::   http://www.codesourcery.com   ::     
> CodeSourcery LLC
> nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk

-- 
_____________________________________________________________
Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.

Powered by Outblaze

^ permalink raw reply	[flat|nested] 30+ messages in thread
* Re: Unomitted frame pointers
@ 2004-12-11 16:15 Sam Lauber
  2004-12-11 17:00 ` Eric Botcazou
  2004-12-11 18:10 ` Robert Dewar
  0 siblings, 2 replies; 30+ messages in thread
From: Sam Lauber @ 2004-12-11 16:15 UTC (permalink / raw)
  To: Eric Botcazou, gcc

Then why does -fomit-frame-pointer work on x86 at all? Then that option to omit a frame pointer shall be omitted ;). At least on x86. As for ABI, I simply don't understand this kind of stuff, because there is not enough information and documentation, and I don't have the time to check out the GCC source tree every time I want to look at something. And I _still_ don't recall any warning. I ran strings on GCC, and the only thing I saw was in the builtin specs saying "-pg and -fomit-frame-pointer are incompatible". You must be using 4.0 or 3.3. Why does it have to dynamically align the frame pointer? And _I'm_ not using an old 8086, I've got a Pentium III! I find your comment a bit insulting. And what's the valid reason this time?

Samuel Lauber

----- Original Message -----
From: "Eric Botcazou" <ebotcazou@libertysurf.fr>
To: "Sam Lauber" <sam124@operamail.com>
Subject: Re: Unomitted frame pointers
Date: Sat, 11 Dec 2004 09:08:31 +0100

> 
> > What messages?
> 
> I already posted the same advice once.
> 
> >  I HAD to call my function main, because it was main.
> 
> You don't understand.  Don't do your experiment on a function called 'main',
> it is very special in C.  The bottom line is that it must dynamically align
> the stack pointer on x86 because of an antiquated ABI, thus preventing
> -fomit-frame-pointer to be effective.
> 
> And keep in mind that GCC has been working on x86 for more than 15 years, so
> it can't do as dumb things as you think it does.  There is almost always a
> valid reason.
> 
> --
> Eric Botcazou

-- 
_____________________________________________________________
Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.

Powered by Outblaze

^ permalink raw reply	[flat|nested] 30+ messages in thread
* RE: Unomitted frame pointers
@ 2004-12-11  2:44 Sam Lauber
  2004-12-11 15:40 ` Nathan Sidwell
  2004-12-11 15:45 ` Andreas Schwab
  0 siblings, 2 replies; 30+ messages in thread
From: Sam Lauber @ 2004-12-11  2:44 UTC (permalink / raw)
  To: Dave Korn, gcc

We would end up with two copies of the string. #defines are preprocessed, so it would expand to the second thing. The _safe_ way to do it is

char str = "Hello World!\n";
write(2, str, strlen(str)-1);

Then we can be sure that it refers to the same physical address in RAM. "Two copies" is when the strings are positioned at diffrent non-overlapping addresses. Though optimization and inlining might mess it up.

Samuel Lauber

----- Original Message -----
From: "Dave Korn" <dave.korn@artimi.com>
To: "'jlh'" <jlh@gmx.ch>, "'Thomas R. Truscott'" <trt@cs.duke.edu>
Subject: RE: Unomitted frame pointers
Date: Fri, 10 Dec 2004 16:23:59 -0000

> 
> > -----Original Message-----
> > From: gcc-owner On Behalf Of jlh
> > Sent: 10 December 2004 16:05
> > To: Thomas R. Truscott
> 
> > > Off-topic, but the hello.c program does:
> > > 	write(2, "Hello World!\n\0", 16);
> > > The string length is 15, not 16.
> >
> > 15 is what gets allocated for storing the string (counting
> > the additional \0 that always gets appened to string constants).
> > But neither the explicit nor the implicit \0 should be printed
> > to the console, so the call should definitely read:
> >
> >      write(2, "Hello World!\n", 13);
> >
> > jlh
> 
> 
>    Except of course on targets where '\n' expands to a two char CR-LF
> combination.
> 
>    So really the safe way to code this is
> 
> #define HELLOSTRING "Hello World!\n"
> 
>        write (2, HELLOSTRING, strlen(HELLOSTRING)-1);
> 
> and let the compiler statically compute the string length for you at
> compiletime.  The #define is used to make sure we don't end up with two copies
> of the string that get out of sync, as would be bound to happen 
> sooner or later
> if we wrote
> 
>        write (2, "Hello World!\n", strlen("Hello World!\n")-1);
> 
>      cheers,
>        DaveK
> --
> Can't think of a witty .sigline today....

-- 
_____________________________________________________________
Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.

Powered by Outblaze

^ permalink raw reply	[flat|nested] 30+ messages in thread
* Re: Unomitted frame pointers
@ 2004-12-11  2:37 Sam Lauber
  0 siblings, 0 replies; 30+ messages in thread
From: Sam Lauber @ 2004-12-11  2:37 UTC (permalink / raw)
  To: Eric Botcazou, gcc

I HAD to call my function main, it was the main routine. Compiling it with -W -Wall only gave me "implict declaration of function `write'.

Samuel Lauber

----- Original Message -----
From: "Eric Botcazou" <ebotcazou@libertysurf.fr>
To: "Sam Lauber" <sam124@operamail.com>
Subject: Re: Unomitted frame pointers
Date: Fri, 10 Dec 2004 08:19:17 +0100

> 
> > Clearly some one (or some code) isn't doing its job! The original C code is
> > attached. I thought -fomit-frame-pointer is supposed to OMIT the frame
> > pointer!
> 
> You didn't read all the messages.  Do not call your function 'main'.
> 
> --
> Eric Botcazou

-- 
_____________________________________________________________
Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.

Powered by Outblaze

^ permalink raw reply	[flat|nested] 30+ messages in thread
* Unomitted frame pointers
@ 2004-12-10  5:26 Sam Lauber
  2004-12-10  7:22 ` Eric Botcazou
  2004-12-10  9:08 ` Richard Guenther
  0 siblings, 2 replies; 30+ messages in thread
From: Sam Lauber @ 2004-12-10  5:26 UTC (permalink / raw)
  To: gcc

[-- Attachment #1: Type: text/plain, Size: 2006 bytes --]

Going back to useless assembly, a low-level Hello World with -fno-omit-frame-pointer:

        .file   "hello.c"
        .section        .rodata
.LC0:
        .string "Hello World!\n"
        .string ""
        .text
.globl main
        .type   main, @function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $24, %esp
        andl    $-16, %esp
        movl    $0, %eax
        addl    $15, %eax
        addl    $15, %eax
        shrl    $4, %eax
        sall    $4, %eax
        subl    %eax, %esp
        movl    $16, 8(%esp)
        movl    $.LC0, 4(%esp)
        movl    $2, (%esp)
        call    write
        movl    $0, %eax
        leave
        ret
        .size   main, .-main
        .section        .note.GNU-stack,"",@progbits
        .ident  "GCC: (GNU) 3.4.3"

And the same C code with -fomit-frame-pointer:

        .file   "hello.c"
        .section        .rodata
.LC0:
        .string "Hello World!\n"
        .string ""
        .text
.globl main
        .type   main, @function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $24, %esp
        andl    $-16, %esp
        movl    $0, %eax
        addl    $15, %eax
        addl    $15, %eax
        shrl    $4, %eax
        sall    $4, %eax
        subl    %eax, %esp
        movl    $16, 8(%esp)
        movl    $.LC0, 4(%esp)
        movl    $2, (%esp)
        call    write
        movl    $0, %eax
        leave
        ret
        .size   main, .-main
        .section        .note.GNU-stack,"",@progbits
        .ident  "GCC: (GNU) 3.4.3"

Clearly some one (or some code) isn't doing its job! The original C code is attached. I thought -fomit-frame-pointer is supposed to OMIT the frame pointer!

Samuel Lauber
-- 
_____________________________________________________________
Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.

Powered by Outblaze

[-- Attachment #2: hello.c --]
[-- Type: application/octet-stream, Size: 65 bytes --]

int main(void)
{
	write(2, "Hello World!\n\0", 16);
	return 0;
}

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2004-12-13 23:36 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-10 15:44 Unomitted frame pointers Thomas R. Truscott
2004-12-10 16:05 ` jlh
2004-12-10 16:25   ` Dave Korn
2004-12-10 16:30     ` Nathan Sidwell
2004-12-10 16:41       ` Dave Korn
2004-12-10 17:54         ` Nathan Sidwell
2004-12-10 18:09           ` [OT] " Dave Korn
2004-12-10 16:33     ` Andreas Schwab
2004-12-10 16:40       ` Dave Korn
2004-12-10 16:55       ` Thomas R. Truscott
2004-12-10 17:26         ` Paul Brook
2004-12-11 11:53           ` Kai Henningsen
  -- strict thread matches above, loose matches on Subject: below --
2004-12-11 16:29 Sam Lauber
2004-12-12  1:33 ` Ian Lance Taylor
2004-12-11 16:26 Sam Lauber
2004-12-11 16:42 ` Nathan Sidwell
2004-12-12  0:09 ` Peter Barada
2004-12-11 16:15 Sam Lauber
2004-12-11 17:00 ` Eric Botcazou
2004-12-11 18:10 ` Robert Dewar
2004-12-13  6:16   ` Ranjit Mathew
2004-12-13 14:22     ` Robert Dewar
2004-12-13 23:36     ` Ben Elliston
2004-12-11  2:44 Sam Lauber
2004-12-11 15:40 ` Nathan Sidwell
2004-12-11 15:45 ` Andreas Schwab
2004-12-11  2:37 Sam Lauber
2004-12-10  5:26 Sam Lauber
2004-12-10  7:22 ` Eric Botcazou
2004-12-10  9:08 ` Richard Guenther

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).