home *** CD-ROM | disk | FTP | other *** search
- /*
- * Following coalesce functions added by Jeff Lydiatt. Having split
- * code, data, or BSS hunks in the same section seems to cause the
- * PD Linker Blink some problems. These functions save all data, BSS,
- * or directives like "PUBLIC" to be dumped at the end.
- */
-
- /* Changed to work under NORTHC by Steve Hawtin */
-
- #include "top.h"
-
- typedef struct save{
- struct save *next;
- char *value;
- } SAVE;
-
- static SAVE *phead; /* List of Directives like PUBLIC. */
- static SAVE *ptail;
- static SAVE *dhead; /* Data list */
- static SAVE *dtail;
- static SAVE *bhead; /* BSS list */
- static SAVE *btail;
- #ifndef NORTHC
- static char *dataname; /* pointer to current data section name. */
- static char *bssname; /* ditto for bss. */
- #endif
-
- static char *myalloc(size)
- unsigned int size;
- {
- register char *p;
- char *malloc();
-
- if ( (p = malloc(size)) == NULL ){
- fputs( "Out of Memory!\n", stderr);
- exit(EXIT_FAILURE);
- }
-
- return p;
- }
-
- #ifndef NORTHC
- void
- newBSS(name)
- char *name;
- {
- register char *p;
-
- if (bssname == NULL){
- p = myalloc(strlen(name)+1);
- strcpy(p,name);
- bssname = p;
- }
- }
-
- void
- newDATA(name)
- char *name;
- {
- register char *p;
-
- if (dataname == NULL){
- p = myalloc(strlen(name)+1);
- strcpy(p,name);
- dataname = p;
- }
- }
- #endif
-
- #define BSS 0
- #define DATA 1
-
- void
- store(m)
- int m;
- {
- register SAVE *s;
- register char *p;
-
- s = (SAVE *)myalloc(sizeof(struct save));
- p = myalloc(strlen(t_line)+1);
- strcpy(p,t_line);
- s->value = p;
-
- switch(m){
- case BSS:
- if (btail == NULL)
- bhead = s;
- else
- btail->next = s;
- btail = s;
- s->next = NULL;
- break;
- case DATA:
- if (dtail == NULL)
- dhead = s;
- else
- dtail->next = s;
- dtail = s;
- s->next = NULL;
- break;
- default:
- if (ptail == NULL)
- phead = s;
- else
- ptail->next = s;
- ptail = s;
- s->next = NULL;
- break;
- }
- }
-
- /*
- * Called after the last code section to dump all BSS and DATA
- * sections together. Follow that with any directives.
- */
-
- void
- dumpall()
- {
- void dump();
-
- if ( dhead ){
- fprintf(ofp, "\tSECTION\tCODATA,DATA\n");
- dump(dhead);
- dhead = dtail = NULL;
- #ifndef NORTHC
- free(dataname);
- dataname = NULL;
- #endif
- }
-
- if ( bhead ){
- fprintf(ofp, "\tSECTION\tCOBSS,BSS\n");
- dump(bhead);
- bhead = btail = NULL;
- #ifndef NORTHC
- free(bssname);
- bssname = NULL;
- #endif
- }
-
- if ( phead ){
- dump(phead);
- phead = ptail = NULL;
- }
-
- fprintf( ofp, "\tEND\n");
- }
-
- static void
- dump(head)
- SAVE *head;
- {
- register SAVE *p, *q;
-
- for ( p=head; p; p=q ){
- fputs( p->value, ofp);
- q = p->next;
- free(p->value);
- free(p);
- }
- }
-