home *** CD-ROM | disk | FTP | other *** search
/ Dream 57 / Amiga_Dream_57.iso / Amiga / Programmation / c / Compilateur / Gcc / Edition / newgccstart.lha / readme < prev    next >
Encoding:
Text File  |  1994-03-14  |  9.4 KB  |  276 lines

  1.  
  2. ***** A new startup code for gcc *****
  3.  
  4. Why a new startup code?
  5.  
  6. A few months ago I decided to install GCC on my amiga to write and compile some funny
  7. progs (perhaps you've tested my program TolleUhr) - and found out very soon:
  8. It doesn't feel at home very much there - it uses it's own shell, it's own
  9. way of handling filenames and a lot of stuff like that.
  10.  
  11. I tried to recompile my little workbench-clock 'TolleUhr' - and what do you think -
  12. it worked. (Ehrm) Except two little new features: If you started it from WB
  13. you got a little funny extra cli-window (perhaps there's a way to shut this down - I
  14. don't know - the documentation that comes with GCC doesn't tell you everything you
  15. want to know) and to run this 20k thingy you needed a new 150k shared library called
  16. ixemul.library just to process the WBStartup message.
  17.  
  18. Later I tried out some other libraries for gcc (even gerlib, you know it ;-) ),
  19. but none of them did exactly what I wanted them to do. So I decided to write my own.
  20. To write a new libc first of all you need a new startup code - here it is.
  21.  
  22. So please let me know if you think this is worth writing a new libc - it's a hell of
  23. a lot of work.
  24. Apart from this you get a new startup code for writing amiga specific programs
  25. with some features you always need - as auto-opening libraries for example.
  26.  
  27. The legal stuff:
  28.  
  29. I don't know what to do with this yet, so I think I should make some restrictions
  30. just to be on the save side:
  31.  
  32. Every part of the sourcecode is (C) by me. You can do modifications to it to fit
  33. your own needs and compile it to get a bug-free version of the objectfiles and
  34. link-libraries. I denote them freeware. You can do everything you want with them -
  35. I make no restrictions. You can link them together with your own objects
  36. to get a program with the legal status you want. But you are not allowed to
  37. redistribute a modified version of the sourcecode - you are only allowed
  38. to redistribute it in totally unmodified form. Perhaps this may change later.
  39. And of course there is no warranty at all, not even the warranty that it works.
  40.  
  41. Usage:
  42.  
  43. You can use one of the following lines:
  44.  
  45. gcc -nostdlib crt0.o your_program.c libm.a libamy.a libstubs.a
  46.  
  47. gcc -nostdlib -fbaserel -msmall-code bcrt0.o your_program.c blibm.a libamy.a blibstubs.a
  48.  
  49. gcc -nostdlib -fbaserel -msmall-code rcrt0.o your_program.c blibm.a libamy.a blibstubs.a
  50.  
  51. depanding on the code model you want to get.
  52.  
  53. Important note: 
  54. (b)libstubs.a must be the last file to get linked in because it contains the
  55. library base pointers. Otherwise you will very probably get un unresolved symbol
  56. error. The startup code modules (b/r)crt0.o must be the first or your code
  57. will crash ;-).
  58.  
  59. What you get - features:
  60.  
  61. startup modules: 
  62.  
  63. crt0.o  large data model startup code (contains geta4 stub function)
  64. bcrt0.o small data model (a4 relative) startup code (contains geta4 function)
  65. rcrt0.o resident startup code
  66.  
  67. * The startup code modules first of all try to open all referenced libraries
  68.   with the library version number taken from the global variable
  69.  
  70.     ULONG __oslibversion;
  71.  
  72.   you can set this variable yourself if you like. Otherwise the default versionnumber
  73.   33 gets linked in (from libstubs.a).
  74.  
  75.   If you want to use a main function instead of main you must set this to at least 36.
  76.   (gets linked in automagically if you use the main function and don't set your own
  77.    variable).
  78.  
  79. * After that the function 
  80.   
  81.     int _autoinit(void);
  82.  
  83.   gets called (maybe this is subject to change). This function should return a value
  84.   of 0 if all went well. Otherwise it should deallocate all ressources before returning.
  85.   If you don't set this function a stubs routine that does nothing gets called.
  86.  
  87. * Your program gets called by jumping into
  88.  
  89.     int _main(char *commandline);
  90.  
  91.   (if you like). The workbench startup message stands in the global variable
  92.  
  93.     extern struct WBStartup *_WBenchMsg;
  94.   
  95.   You can exit your program either by calling
  96.  
  97.     extern void _exit(int returncode);
  98.  
  99.   or by simply returning from _main.
  100.  
  101. * The function
  102.   
  103.     void _autoexit(void);
  104.  
  105.   gets called if _autoinit was done successfully. (maybe this is subject to change, too).
  106.  
  107. * You also get an
  108.  
  109.     void geta4(void);
  110.  
  111.   function if this is possible. This isn't possible for resident programs 
  112.   (as far as I know) and very useless for large data model.
  113.  
  114. A little ANSI support:
  115.  
  116. libm.a  normal clib (large data model)
  117. blibm.a base-relative clib (small data model)
  118.  
  119. These libraries (link-libraries, not the shared ones you know of the 
  120. libs: directory) contain a _main() function calling your 
  121.  
  122.     int main(int argc,char *argv[]);
  123.  
  124. function. If you start your program using WB you get an argc of 0 and the 
  125. pointer to the WBStartup message as argv. It's not possible to get 
  126. an argc of 0 by starting your program from the shell (as you already know).
  127.  
  128. There are also the following ANSI functions:
  129.  
  130.   void exit(int returncode);
  131.   void *malloc(size_t size);
  132.   void free(void *ptr);
  133.   void *realloc(void *ptr,size_t size);
  134.   void *calloc(size_t nmemb,size_t size);
  135.   size_t strlen(const char *s);
  136.   FILE *fopen(const char *filename,const char *mode);
  137.   int fclose(FILE *stream);
  138.   int fgetc(FILE *stream);
  139.   int fputc(int c,FILE *stream);
  140.   int ungetc(int c,FILE *stream);
  141.  
  142. Most of the ANSI-stuff contains only little work, but the printf-style functions
  143. are a very huge hurdle. I would like to hear about any incompatibilities of these
  144. functions to the ANSI-standard.
  145.  
  146. Last you get two functions you know from amiga.lib
  147.  
  148.   void sprintf(STRPTR buffer,STRPTR fmt,...);
  149.   void BeginIO(struct IORequest *iorequest);
  150.  
  151. The only reason that they are there is that I needed them myself.
  152.  
  153. The stubs:
  154.  
  155. libstubs.a  Normal
  156. blibstubs.a Base-relative
  157.  
  158. These libraries mainly contain the library base pointers and library names.
  159. (Of course there are a some other things as well). It's very easy to add 
  160. new libraries to the supported list (i.e. your favourite PD library):
  161. Just add it to the library.list file and rerun the whole make process for the stubs.
  162. It's only about 1 hour on my plain 68000 so I think everybody can do this
  163. (at least everybody who has enough memory to run gcc :-) ).
  164.  
  165. Theory of operation:
  166.  
  167. I think you want to know how it works: here is the answer.
  168.  
  169. The linkage process:
  170. Every object file contains some references to other objects or some elements
  171. referenced by other objects (or both). You may think of them as the two ends 
  172. of an arrow. For example the startup code contains a reference to _main and
  173. the function _exit which can be referenced by other objects.
  174.  
  175.     +---------+
  176.     | Startup |
  177.     |         |
  178.     |   _main |-> Startup code calls _main
  179.     |         |
  180.     |   _exit |<- You can call _exit if you want
  181.     |         |
  182.     +---------+
  183.  
  184. A link-library is nothing else than a large heap of this object files.
  185.  
  186.     +---------+
  187.     |   fopen |<-
  188.     +---------+
  189.     |  fclose |<-
  190.     +---------+
  191.          .
  192.          .
  193.          .
  194.     +---------+
  195.     |  malloc |<-
  196.     +---------+
  197.  
  198. All the linker has to do is taking all the objects and looking for unresolved
  199. references (the -> arrows) in the link-libraries. If the objects that have to
  200. be linked in have unresolved references too, they must be looked after too.
  201.  
  202. The linker normally scans the libraries in the order you give them. Therefore
  203. if a reference could be resolved by multiple libraries there is no conflict
  204. between them.
  205.  
  206. What does this mean for opening the shared libraries?
  207.  
  208. Every time you call a function of one of the shared libraries the glue code
  209. in amiga.lib (or the assembler inlines of gcc) make up a reference to 
  210. the library base pointer. The libstubs.a library contains these base 
  211. pointers together with one pointer to each library name (which are also in this
  212. library):
  213.  
  214.     +---------+
  215.     | label 1 |<-
  216.     +---------+
  217.     | dosbase |<-
  218.     |         |
  219.     | pointer |->....
  220.     +---------+     .
  221.          .          .
  222.          .          .
  223.          .          .
  224.     +---------+     .
  225.     | gfxbase |<-   .
  226.     |         |     .
  227.     | pointer |->.. .
  228.     +---------+   . .
  229.     | label 2 |<- . .
  230.     +---------+   . .
  231.     | gfxname |<-.. .
  232.     +---------+     .
  233.     | dosname |<-....
  234.     +---------+
  235.          .
  236.          .
  237.          .
  238.  
  239. All that is to do now is putting two labels at both ends of this table.
  240. The startup code now contains references to both of these labels before and after
  241. the library base pointers. If they follow immediately in your resulting executable
  242. there is no base pointer between them (ha!). This is evaluated run-time.
  243. (I know it's a lousy trick to depend on the order the objects get linked together,
  244.  but it seems to work :-P. Perhaps someone in the FSF can tell me if this always
  245.  works with the gnu ld.)
  246.  
  247. I used this trick many times - for example the standard file I/O is only initialized
  248. if it's used and so on.
  249.  
  250. A note on library bases: the startup code itself (and the clib, of course) also
  251. need DOSBase and UtilityBase sometimes. It would be of no good if you were allowed
  252. to open these libraries yourself after they got already used. Therefore I 
  253. decided to modify my code to use a private _DOSBase and _UtilityBase. So you
  254. are allowed to open them yourself (or let them be opened automatically).
  255. Don't be alarmed if some system monitor tells you your program opened
  256. dos.library twice. This is fully normal (and legal and costs you nothing) 
  257. most startup codes do this.
  258.  
  259. That's all.
  260.  
  261. As stated above a few times I expect lots of email - you can even flame me if you want
  262. (but don't expect an answer then :-} )
  263.  
  264. My email address:
  265.  
  266.   fleischr@izfm.uni-stuttgart.de
  267.  
  268. I'm reachable by snail mail too (but I don't reply on it):
  269.  
  270.   Matthias Fleischer
  271.   Adlerstraâ–€e 30
  272.   73760 Ostfildern
  273.   Federal Republic of Germany
  274.  
  275.   Phone: 0711/3430286
  276.