egcs/libc interaction (resend)

Robert Krawitz rlk@torrent.com
Tue Oct 6 14:57:00 GMT 1998


[I never got any confirmation on this, and never received a copy back]

This seems to be an interaction between egcs 1.1, libc5 (5.4.46,
dynload 1.9.9, binutils 2.9.1.0.7, but it happened with other binutils
versions).  I don't have g++ 2.7.2 handy, but this problem didn't seem
to happen when we were using g++ (perhaps we just didn't see it).  It
also doesn't happen on libc6.

It looks like even though I've called fpclose on the appropriate FILE
pointer, the I/O code is still caching it, and getting into trouble
when the shared libraries go away.

.PHONY: test2

CFLAGS= -O -fPIC

test2:
	g++ $(CFLAGS) -c test1.C
	g++ $(CFLAGS) -c test2.C
	g++ $(CFLAGS) -c test3.C
	g++ $(CFLAGS) -shared test1.o -o test1.so
	g++ $(CFLAGS) -shared test3.o -o test3.so
	g++ $(CFLAGS) -v -Wl,--verbose -o test2 test2.o test1.so -ldl


% make
g++ -O -fPIC -c test1.C
g++ -O -fPIC -c test2.C
g++ -O -fPIC -c test3.C
g++ -O -fPIC -shared test1.o -o test1.so
g++ -O -fPIC -shared test3.o -o test3.so
g++ -O -fPIC -v -Wl,--verbose -o test2 test2.o test1.so -ldl
Reading specs from /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/specs
gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
 /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/collect2 -m elf_i386 -dynamic-linker /lib/ld-linux.so.1 -o test2 /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/crtbegin.o -L/usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57 -L/usr/i686-pc-linux-gnulibc1/lib --verbose test2.o test1.so -ldl -lstdc++ -lm -lgcc -lc -lgcc /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/crtend.o /usr/lib/crtn.o
GNU ld version 2.9.1 (with BFD 2.9.1.0.7)
  Supported emulations:
   elf_i386
   i386linux
   i386coff
using internal linker script:
==================================================
OUTPUT_FORMAT("elf32-i386", "elf32-i386",
              "elf32-i386")
OUTPUT_ARCH(i386)
ENTRY(_start)
SEARCH_DIR(/lib); SEARCH_DIR(/usr/lib); SEARCH_DIR(/usr/local/lib); SEARCH_DIR(/usr/i586-unknown-linux-gnulibc1/lib);
/* Do we need any of these for elf?
   __DYNAMIC = 0;    */
SECTIONS
{
  /* Read-only sections, merged into text segment: */
  . = 0x08048000 + SIZEOF_HEADERS;
  .interp     : { *(.interp)    }
  .hash          : { *(.hash)           }
  .dynsym        : { *(.dynsym)         }
  .dynstr        : { *(.dynstr)         }
  .gnu.version   : { *(.gnu.version)    }
  .gnu.version_d   : { *(.gnu.version_d)        }
  .gnu.version_r   : { *(.gnu.version_r)        }
  .rel.text      :
    { *(.rel.text) *(.rel.gnu.linkonce.t*) }
  .rela.text     :
    { *(.rela.text) *(.rela.gnu.linkonce.t*) }
  .rel.data      :
    { *(.rel.data) *(.rel.gnu.linkonce.d*) }
  .rela.data     :
    { *(.rela.data) *(.rela.gnu.linkonce.d*) }
  .rel.rodata    :
    { *(.rel.rodata) *(.rel.gnu.linkonce.r*) }
  .rela.rodata   :
    { *(.rela.rodata) *(.rela.gnu.linkonce.r*) }
  .rel.got       : { *(.rel.got)                }
  .rela.got      : { *(.rela.got)               }
  .rel.ctors     : { *(.rel.ctors)      }
  .rela.ctors    : { *(.rela.ctors)     }
  .rel.dtors     : { *(.rel.dtors)      }
  .rela.dtors    : { *(.rela.dtors)     }
  .rel.init      : { *(.rel.init)       }
  .rela.init     : { *(.rela.init)      }
  .rel.fini      : { *(.rel.fini)       }
  .rela.fini     : { *(.rela.fini)      }
  .rel.bss       : { *(.rel.bss)                }
  .rela.bss      : { *(.rela.bss)               }
  .rel.plt       : { *(.rel.plt)                }
  .rela.plt      : { *(.rela.plt)               }
  .init          : { *(.init)   } =0x9090
  .plt      : { *(.plt) }
  .text      :
  {
    *(.text)
    *(.stub)
    /* .gnu.warning sections are handled specially by elf32.em.  */
    *(.gnu.warning)
    *(.gnu.linkonce.t*)
  } =0x9090
  _etext = .;
  PROVIDE (etext = .);
  .fini      : { *(.fini)    } =0x9090
  .rodata    : { *(.rodata) *(.gnu.linkonce.r*) }
  .rodata1   : { *(.rodata1) }
  /* Adjust the address for the data segment.  We want to adjust up to
     the same address within the page on the next page up.  */
  . = ALIGN(0x1000) + (. & (0x1000 - 1));
  .data    :
  {
    *(.data)
    *(.gnu.linkonce.d*)
    CONSTRUCTORS
  }
  .data1   : { *(.data1) }
  .ctors         :
  {
    *(.ctors)
  }
  .dtors         :
  {
    *(.dtors)
  }
  .got           : { *(.got.plt) *(.got) }
  .dynamic       : { *(.dynamic) }
  /* We want the small data sections together, so single-instruction offsets
     can access them all, and initialized data all before uninitialized, so
     we can shorten the on-disk segment size.  */
  .sdata     : { *(.sdata) }
  _edata  =  .;
  PROVIDE (edata = .);
  __bss_start = .;
  .sbss      : { *(.sbss) *(.scommon) }
  .bss       :
  {
   *(.dynbss)
   *(.bss)
   *(COMMON)
  }
  . = ALIGN(32 / 8);
  _end = . ;
  PROVIDE (end = .);
  /* Stabs debugging sections.  */
  .stab 0 : { *(.stab) }
  .stabstr 0 : { *(.stabstr) }
  .stab.excl 0 : { *(.stab.excl) }
  .stab.exclstr 0 : { *(.stab.exclstr) }
  .stab.index 0 : { *(.stab.index) }
  .stab.indexstr 0 : { *(.stab.indexstr) }
  .comment 0 : { *(.comment) }
  /* DWARF debug sections.
     Symbols in the DWARF debugging sections are relative to the beginning
     of the section so we begin them at 0.  */
  /* DWARF 1 */
  .debug          0 : { *(.debug) }
  .line           0 : { *(.line) }
  /* GNU DWARF 1 extensions */
  .debug_srcinfo  0 : { *(.debug_srcinfo) }
  .debug_sfnames  0 : { *(.debug_sfnames) }
  /* DWARF 1.1 and DWARF 2 */
  .debug_aranges  0 : { *(.debug_aranges) }
  .debug_pubnames 0 : { *(.debug_pubnames) }
  /* DWARF 2 */
  .debug_info     0 : { *(.debug_info) }
  .debug_abbrev   0 : { *(.debug_abbrev) }
  .debug_line     0 : { *(.debug_line) }
  .debug_frame    0 : { *(.debug_frame) }
  .debug_str      0 : { *(.debug_str) }
  .debug_loc      0 : { *(.debug_loc) }
  .debug_macinfo  0 : { *(.debug_macinfo) }
  /* SGI/MIPS DWARF 2 extensions */
  .debug_weaknames 0 : { *(.debug_weaknames) }
  .debug_funcnames 0 : { *(.debug_funcnames) }
  .debug_typenames 0 : { *(.debug_typenames) }
  .debug_varnames  0 : { *(.debug_varnames) }
  /* These must appear regardless of  .  */
}


==================================================
attempt to open /usr/lib/crt1.o succeeded
/usr/lib/crt1.o
attempt to open /usr/lib/crti.o succeeded
/usr/lib/crti.o
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/crtbegin.o succeeded
/usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/crtbegin.o
attempt to open test2.o succeeded
test2.o
attempt to open test1.so succeeded
test1.so
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libdl.so failed
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libdl.a failed
attempt to open /usr/i686-pc-linux-gnulibc1/lib/libdl.so failed
attempt to open /usr/i686-pc-linux-gnulibc1/lib/libdl.a failed
attempt to open /opt/gnu/bin/../lib/libdl.so failed
attempt to open /opt/gnu/bin/../lib/libdl.a failed
attempt to open /lib/libdl.so succeeded
-ldl (/lib/libdl.so)
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libstdc++.so failed
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libstdc++.a failed
attempt to open /usr/i686-pc-linux-gnulibc1/lib/libstdc++.so failed
attempt to open /usr/i686-pc-linux-gnulibc1/lib/libstdc++.a failed
attempt to open /opt/gnu/bin/../lib/libstdc++.so failed
attempt to open /opt/gnu/bin/../lib/libstdc++.a succeeded
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libm.so failed
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libm.a failed
attempt to open /usr/i686-pc-linux-gnulibc1/lib/libm.so failed
attempt to open /usr/i686-pc-linux-gnulibc1/lib/libm.a failed
attempt to open /opt/gnu/bin/../lib/libm.so succeeded
-lm (/opt/gnu/bin/../lib/libm.so)
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libgcc.so failed
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libgcc.a succeeded
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libc.so failed
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libc.a failed
attempt to open /usr/i686-pc-linux-gnulibc1/lib/libc.so failed
attempt to open /usr/i686-pc-linux-gnulibc1/lib/libc.a failed
attempt to open /opt/gnu/bin/../lib/libc.so succeeded
-lc (/opt/gnu/bin/../lib/libc.so)
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libgcc.so failed
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/libgcc.a succeeded
attempt to open /usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/crtend.o succeeded
/usr/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.57/crtend.o
attempt to open /usr/lib/crtn.o succeeded
/usr/lib/crtn.o


test.h:

class base
{
 public:
  base();
  virtual ~base();
  virtual void doFile();
  static base *allocate(const char *name);
  virtual int registerBaseClass();
 protected:
  void doRegistration(const char *className, base * (*allocatefcn)(void));
 private:
  struct registry
  {
    char *typename_;
    base *(*allocatefcn_)(void);
    struct registry *next_;
  };
  static struct registry *sTheRegistry_;
};

void loadDerived();
void unloadDerived();


test1.C:

#include <iostream.h>
#include <dlfcn.h>
#include <unistd.h>
#include <string.h>

#include "test.h"

struct base::registry *base::sTheRegistry_ = 0;

base::base()
{
  cerr << "Construct base at " << (void *) this << endl;
}

base::~base()
{
  cerr << "Destroy base at " << (void *) this << endl;
}

void
base::doFile()
{
  cerr << "base::doFile" << endl;
}

static base *
allocateBase()
{
  return new base;
}

static base sTheBase;

int
base::registerBaseClass()
{
  doRegistration("base", &allocateBase);
  return 0;
}

static int regDummy = sTheBase.registerBaseClass();

base *
base::allocate(const char *name)
{
  registry *t = sTheRegistry_;
  while (t)
    {
      if (!strcmp(name, t->typename_))
	return (t->allocatefcn_)();
      t = t->next_;
    }
  return 0;
}

void
base::doRegistration(const char *className, base * (*allocatefcn)())
{
  registry *tmp = new registry;
  tmp->typename_ = new char[strlen(className) + 1];
  strcpy(tmp->typename_, className);
  tmp->allocatefcn_ = allocatefcn;
  tmp->next_ = base::sTheRegistry_;
  base::sTheRegistry_ = tmp;
}

void *derivedHandle;

void
loadDerived()
{
  char buffer[2048];
  if (!getcwd(buffer, 2047))
    {
      cerr << "Unable to get cwd" << endl;
      exit(1);
    }
  strcat(buffer, "/test3.so");
  if ((derivedHandle = dlopen(buffer, RTLD_NOW | RTLD_GLOBAL)) != 0)
    return;
  cerr << dlerror() << endl;
  exit(2);
}

void
unloadDerived()
{
  char buffer[2048];
  if (!getcwd(buffer, 2047))
    {
      cerr << "Unable to get cwd" << endl;
      exit(1);
    }
  strcat(buffer, "/test3.so");
  if (dlclose(derivedHandle) == 0)
    return;
  cerr << dlerror() << endl;
  exit(2);
}


test3.C:

#include <iostream.h>
#include "test.h"
#include <stdio.h>

class derived : public base
{
 public:
  derived();
  virtual ~derived();
  virtual void doFile();
  virtual int registerDerivedClass();
};

derived::derived()
{
  cerr << "Construct derived at " << (void *) this << endl;
}

derived::~derived()
{
  cerr << "Destroy derived at " << (void *) this << endl;
}

void
derived::doFile()
{
  FILE *out_fp = fopen("/tmp/broken", "w");
  if (!out_fp)
    {
      cerr << "Unable to open /tmp/broken" << endl;
      return;
    }
  fprintf(out_fp, "Here I am\n");
  fclose(out_fp);
}

static base *
allocateDerived()
{
  return new derived;
}

static derived sTheDerived;

int
derived::registerDerivedClass()
{
  doRegistration("derived", &allocateDerived);
  return 0;
}

static int regDummy = sTheDerived.registerDerivedClass();


test2.C:

#include <string.h>
#include <iostream.h>
#include "test.h"

int debug = 0;

int
main(int, char **)
{
  base *theBase = base::allocate("base");
  theBase->doFile();
  delete theBase;
  loadDerived();
  base *theDerived = base::allocate("derived");
  theDerived->doFile();
  delete theDerived;
  unloadDerived();

  return 0;
}

-- 
Robert Krawitz <rlk@torrent.com>		     Founding Engineer
Torrent Systems Inc.				      617-354-8484x104



More information about the Gcc-bugs mailing list