NASM 0.98.08
[nasm/avx512.git] / rdoff / rdlib.c
blobed78a0052e85a0b4531607e65a7713f17523fdc0
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "rdoff.h"
6 #include "rdlib.h"
8 /*
9 * format of rdoff library files:
10 * repeat
11 * null terminated module name (max 255 chars)
12 * RDOFF module
13 * until eof
17 * TODO
19 * No support exists yet for special modules. But we aren't using
20 * any special modules yet. They are only defined now so that their
21 * existance doesn't break older versions of the linker... presently
22 * anything whose name begins with '.' is ignored.
25 int rdl_error = 0;
27 char *rdl_errors[5] = {
28 "no error","could not open file", "invalid file structure",
29 "file contains modules of an unsupported RDOFF version",
30 "module not found"
32 int rdl_verify(const char * filename)
34 FILE * fp = fopen(filename, "rb");
35 char buf[257];
36 int i;
37 long length;
38 static char lastverified[256];
39 static int lastresult = -1;
41 if (lastresult != -1 && !strcmp(filename, lastverified))
42 return lastresult;
44 strcpy(lastverified, filename);
46 if (!fp)
47 return (rdl_error = lastresult = 1);
49 while (!feof(fp))
51 i = 0;
53 while (fread(buf + i,1,1,fp) == 1 && buf[i] && i < 257)
54 i++;
55 if (feof(fp)) break;
57 fread(buf, 6, 1, fp);
58 buf[6] = 0;
59 if (buf[0] == '.') {
61 * a special module, eg a directory.
62 * Format of such a module is defined to be:
63 * six char type identifier (which we've already read)
64 * long count bytes content
65 * content
66 * so we can handle it uniformaly with RDOFF2 modules...
67 * do nothing here. :-)
70 else if (strncmp(buf, "RDOFF", 5)) {
71 return rdl_error = lastresult = 2;
73 else if (buf[5] != '2') {
74 return rdl_error = lastresult = 3;
77 fread(&length, 4, 1, fp);
78 fseek(fp, length, SEEK_CUR); /* skip over the module */
80 fclose(fp);
81 return lastresult = 0; /* library in correct format */
84 int rdl_open (struct librarynode * lib, const char * name)
86 int i = rdl_verify(name);
87 if (i) return i;
89 lib->fp = NULL;
90 lib->name = strdup(name);
91 lib->referenced = 0;
92 lib->next = NULL;
93 return 0;
96 void rdl_close (struct librarynode * lib)
98 if (lib->fp)
99 fclose(lib->fp);
100 free(lib->name);
103 int rdl_searchlib (struct librarynode * lib,
104 const char * label, rdffile * f)
106 char buf[512];
107 int i, t;
108 void * hdr;
109 rdfheaderrec * r;
110 long l;
112 rdl_error = 0;
113 lib->referenced ++;
115 if (! lib->fp)
117 lib->fp = fopen(lib->name,"rb");
119 if (! lib->fp) {
120 rdl_error = 1;
121 return 0;
124 else
125 rewind(lib->fp);
127 while (! feof(lib->fp) )
130 * read the module name from the file, and prepend
131 * the library name and '.' to it.
133 strcpy(buf, lib->name);
135 i = strlen(lib->name);
136 buf[i++] = '.'; t = i;
137 while (fread(buf + i,1,1,lib->fp) == 1 && buf[i] && i < 512)
138 i++;
140 buf[i] = 0;
142 if (feof(lib->fp)) break;
143 if (!strcmp(buf + t, ".dir")) /* skip over directory */
145 fread (&l, 4, 1, lib->fp);
146 fseek (lib->fp, l, SEEK_CUR);
147 continue;
150 * open the RDOFF module
152 if ( rdfopenhere(f,lib->fp,&lib->referenced,buf) ) {
153 rdl_error = 16 * rdf_errno;
154 return 0;
157 * read in the header, and scan for exported symbols
159 hdr = malloc(f->header_len);
160 rdfloadseg(f,RDOFF_HEADER,hdr);
162 while ((r = rdfgetheaderrec(f)))
164 if (r->type != 3) /* not an export */
165 continue;
167 if (! strcmp(r->e.label, label) ) /* match! */
169 free(hdr); /* reset to 'just open' */
170 f->header_loc = NULL; /* state... */
171 f->header_fp = 0;
172 return 1;
176 /* find start of next module... */
177 i = f->eof_offset;
178 rdfclose(f);
179 fseek(lib->fp,i,SEEK_SET);
183 * close the file if nobody else is using it
185 lib->referenced --;
186 if (! lib->referenced)
188 fclose(lib->fp);
189 lib->fp = NULL;
191 return 0;
194 int rdl_openmodule (struct librarynode * lib, int moduleno, rdffile * f)
196 char buf[512];
197 int i, cmod, t;
198 long length;
200 lib->referenced++;
202 if (!lib->fp)
204 lib->fp = fopen(lib->name, "rb");
205 if (!lib->fp) {
206 lib->referenced--;
207 return (rdl_error = 1);
210 else
211 rewind(lib->fp);
213 cmod = -1;
214 while (!feof(lib->fp))
216 strcpy(buf, lib->name);
217 i = strlen(buf);
218 buf[i++] = '.'; t = i;
219 while (fread(buf + i,1,1,lib->fp) == 1 && buf[i] && i < 512)
220 i++;
221 buf[i] = 0;
222 if (feof(lib->fp)) break;
224 if (buf[t] != '.') /* special module - not counted in the numbering */
225 cmod++; /* of RDOFF modules - must be referred to by name */
227 if (cmod == moduleno) {
228 rdl_error = 16 *
229 rdfopenhere(f, lib->fp, &lib->referenced, buf);
230 lib->referenced--;
231 if (!lib->referenced) {
232 fclose(lib->fp);
233 lib->fp = NULL;
235 return rdl_error;
238 fread(buf, 6, 1, lib->fp);
239 buf[6] = 0;
240 if (buf[t] == '.') {
241 /* do nothing */
243 else if (strncmp(buf, "RDOFF", 5)) {
244 if (! --lib->referenced) {
245 fclose(lib->fp);
246 lib->fp = NULL;
248 return rdl_error = 2;
250 else if (buf[5] != '2') {
251 if (! --lib->referenced) {
252 fclose(lib->fp);
253 lib->fp = NULL;
255 return rdl_error = 3;
258 fread(&length, 4, 1, lib->fp);
259 fseek(lib->fp, length, SEEK_CUR); /* skip over the module */
261 if (! --lib->referenced) {
262 fclose(lib->fp);
263 lib->fp = NULL;
265 return rdl_error = 4; /* module not found */
268 void rdl_perror(const char *apname, const char *filename)
270 if (rdl_error >= 16)
271 rdfperror(apname, filename);
272 else
273 fprintf(stderr,"%s:%s:%s\n",apname,filename,rdl_errors[rdl_error]);