🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #334

Re: Dynamic loading under MORE/bsd

#334
From: Ray Nickson
Date: Thu, 09 Apr 1992 21:48
65 lines
2311 bytes
In article <1992Apr9.104924.5834@cs.tu-berlin.de> net@cs.tu-berlin.de (Oliver Laumann) writes:

   I'm not sure if this solves your problem [...] 
   [Y]our program's `break' gets altered between the sbrk(0) and the
   call to the linker. [...]

You've identified a problem, but not mine.  I confess I was very lazy
in posting this fragment --- thank you for pointing it out.  The
_real_ code (which isn't mine) doesn't do the naughty things I did,
and still fails.  Here's a `corrected' tryld, which succeeds and fails
in the same way as the other.  I've added assert()s to check that the
break hasn't been moved and that the system calls don't fail.

#include <stdio.h>
#include <a.out.h>
#include <assert.h>
#include <sys/file.h>
#include <sys/types.h>

extern char *sbrk(int);

int 
main (int argc, char *argv[])
{
  char *codeptr, *codeptr1;
  char cmd[1024];
  int od, status, cc;
  struct exec header;
  int size;
  int (*funcp)();
  
  codeptr = sbrk(0);
  assert (codeptr != (char *)-1);
  
  sprintf (cmd, "ld -N -A %s -e %s -T %lx -o ext.o %s", 
           argv[0], argv[2], codeptr, argv[1]);
  status = system (cmd);
  assert (status == 0);
  
  od = open ("ext.o", O_RDONLY);
  assert (od != -1);
  
  cc = read (od, &header, sizeof(header));
  assert (cc == sizeof(header));
  
  size = header.a_text;
  funcp = (int(*)())(header.a_entry);
  
  codeptr1 = sbrk (size);
  assert (codeptr == codeptr1);

  status = lseek (od, N_TXTOFF(header), L_SET);
  assert (status != -1);
  
  cc = read (od, codeptr, size);
  assert (cc == size);
  
  close (od);

#ifdef HACKIT
  sleep (1);
#endif

  printf ("got %d\n", (*funcp)());
}
Technical Details
Message-ID: <1992Apr9.214839.11761@comp.vuw.ac.nz>
Path: arc1-ca.pugleaf.net!sparky!uunet!comp.vuw.ac.nz!nickson
References: <1992Apr9.015729.2278@comp.vuw.ac.nz> <1992Apr9.104924.5834@cs.tu-berlin.de>

Show Full Headers
Complete Article Headers
Newsgroups: comp.unix.programmer
Path: arc1-ca.pugleaf.net!sparky!uunet!comp.vuw.ac.nz!nickson
From: Ray.Nickson@comp.vuw.ac.nz (Ray Nickson)
Subject: Re: Dynamic loading under MORE/bsd
Nntp-Posting-Host: turakirae.comp.vuw.ac.nz
Message-ID: <1992Apr9.214839.11761@comp.vuw.ac.nz>
In-Reply-To: net@cs.tu-berlin.de's message of Thu, 9 Apr 1992 10:49:24 GMT
Organization: Victoria University of Wellington, Wellington, New Zealand
Sender: news@comp.vuw.ac.nz (News Admin)
References: <1992Apr9.015729.2278@comp.vuw.ac.nz> <1992Apr9.104924.5834@cs.tu-berlin.de>
Distribution: comp
Date: Thu, 9 Apr 1992 21:48:39 GMT