This is the mail archive of the binutils@sourceware.org mailing list for the binutils 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]

Re: why ld return with error?


On 31/01/2011 08:28, ali hagigat wrote:
> Thank you for the reply. I think that GCC compiler collection has the
> definitions and C code of some standard libraries. It means that the
> code of printf exists inside the code of gcc and gcc does not need to
> go to /usr/bin on hard disk to find libc.a and then find the defintion
> and the body of printf.
> Is that right? or I am mistaken?
> If I am mistaken what will be the meaning of -fno-builtin then? I
> think the code of built-in functions exist inside the code segment of
> gcc when it is invoked.

  GCC's built-ins are an internal mechanism by which the compiler can "know"
enough about what goes on inside a function such as printf to be able to
optimise it - in some but not all cases - but ultimately they rely on the
underlying functionality of the system's C library.  They're kind of synthetic
inline wrappers around the real functions, that in some cases can be
simplified during expansion.  They don't actually have the functionality that
the C library supplies; you could picture the builtin printf as being
something roughly like this:

int builtin_printf (const char *fmt, ...) __attribute__ ((__always_inline__))
{
  if (__builtin_constant_p (fmt)
      && (strcmp (fmt, "%s") == 0 || strchr (fmt, "%") == NULL))
    {
      if (strcmp (fmt, "%s") == 0)
        {
          str = GET_FIRST_VARARG();
        }
      else
        {
          str = fmt;
        }
      /* If the string was "", printf does nothing.  */
      if (str[0] == '\0')
        return 0;
      /* If the string has length of 1, call putchar.  */
      if (str[1] == '\0)
        {
          builtin_putchar (fmt[0]);
          return 1;
        }
      /* If the string was "string\n", call puts("string").  */
              . . .
      /* Other optimisations etc. */
              . . .
    }
  /* Can't optimise it if we don't know the format string at compile
     time, so pass it through to the . */
  return __builtin_apply (printf, __builtin_apply_args(), GET_VARARGS_SIZE());
}

  When the compiler comes to inline this into a function that is calling
printf, it will end up either optimising the whole thing away into a call to
the C lib's underlying printf function, or translate it into a call with
modified args to builtin_putchar or builtin_puts, which each may also make
further transformations or just optimise away into a libcall likewise.  So in
the end all the actual functionality still has to come from the C library anyway.

    cheers,
      DaveK


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