Fix stray character that had snuck in. Damned.
[nasm/avx512.git] / rdoff / rdlib.c
blob6eee5a9c273c4382cf9a6e6e5099c2d4e85680ae
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 * optional signature ('.sig')
11 * repeat
12 * null terminated module name (max 255 chars)
13 * RDOFF module
14 * until eof
15 * optional directory ('.dir')
19 * TODO
21 * No support exists yet for special modules. But we aren't using
22 * any special modules yet. They are only defined now so that their
23 * existance doesn't break older versions of the linker... presently
24 * anything whose name begins with '.' is ignored.
27 int rdl_error = 0;
29 char *rdl_errors[5] = {
30 "no error","could not open file", "invalid file structure",
31 "file contains modules of an unsupported RDOFF version",
32 "module not found"
34 int rdl_verify(const char * filename)
36 FILE * fp = fopen(filename, "rb");
37 char buf[257];
38 int i;
39 long length;
40 static char lastverified[256];
41 static int lastresult = -1;
43 if (lastresult != -1 && !strcmp(filename, lastverified))
44 return lastresult;
46 strcpy(lastverified, filename);
48 if (!fp)
49 return (rdl_error = lastresult = 1);
51 while (!feof(fp))
53 i = 0;
55 while (fread(buf + i,1,1,fp) == 1 && buf[i] && i < 257)
56 i++;
57 if (feof(fp)) break;
59 if (buf[0] == '.') {
61 * A special module, eg a signature block or a directory.
62 * Format of such a module is defined to be:
63 * six char type identifier
64 * long count bytes content
65 * content
66 * so we can handle it uniformaly with RDOFF2 modules.
68 fread(buf, 6, 1, fp);
69 buf[6] = 0;
70 /* Currently, nothing useful to do with signature block.. */
71 } else {
72 fread(buf, 6, 1, fp);
73 buf[6] = 0;
74 if (strncmp(buf, "RDOFF", 5)) {
75 return rdl_error = lastresult = 2;
76 } else if (buf[5] != '2') {
77 return rdl_error = lastresult = 3;
80 fread(&length, 4, 1, fp);
81 fseek(fp, length, SEEK_CUR); /* skip over the module */
83 fclose(fp);
84 return lastresult = 0; /* library in correct format */
87 int rdl_open (struct librarynode * lib, const char * name)
89 int i = rdl_verify(name);
90 if (i) return i;
92 lib->fp = NULL;
93 lib->name = strdup(name);
94 lib->referenced = 0;
95 lib->next = NULL;
96 return 0;
99 void rdl_close (struct librarynode * lib)
101 if (lib->fp)
102 fclose(lib->fp);
103 free(lib->name);
106 int rdl_searchlib (struct librarynode * lib,
107 const char * label, rdffile * f)
109 char buf[512];
110 int i, t;
111 void * hdr;
112 rdfheaderrec * r;
113 long l;
115 rdl_error = 0;
116 lib->referenced ++;
118 if (! lib->fp)
120 lib->fp = fopen(lib->name,"rb");
122 if (! lib->fp) {
123 rdl_error = 1;
124 return 0;
127 else
128 rewind(lib->fp);
130 while (! feof(lib->fp) )
133 * read the module name from the file, and prepend
134 * the library name and '.' to it.
136 strcpy(buf, lib->name);
138 i = strlen(lib->name);
139 buf[i++] = '.'; t = i;
140 while (fread(buf + i,1,1,lib->fp) == 1 && buf[i] && i < 512)
141 i++;
143 buf[i] = 0;
145 if (feof(lib->fp)) break;
146 if (!strcmp(buf + t, ".dir")) /* skip over directory */
148 fread (&l, 4, 1, lib->fp);
149 fseek (lib->fp, l, SEEK_CUR);
150 continue;
153 * open the RDOFF module
155 if ( rdfopenhere(f,lib->fp,&lib->referenced,buf) ) {
156 rdl_error = 16 * rdf_errno;
157 return 0;
160 * read in the header, and scan for exported symbols
162 hdr = malloc(f->header_len);
163 rdfloadseg(f,RDOFF_HEADER,hdr);
165 while ((r = rdfgetheaderrec(f)))
167 if (r->type != 3) /* not an export */
168 continue;
170 if (! strcmp(r->e.label, label) ) /* match! */
172 free(hdr); /* reset to 'just open' */
173 f->header_loc = NULL; /* state... */
174 f->header_fp = 0;
175 return 1;
179 /* find start of next module... */
180 i = f->eof_offset;
181 rdfclose(f);
182 fseek(lib->fp,i,SEEK_SET);
186 * close the file if nobody else is using it
188 lib->referenced --;
189 if (! lib->referenced)
191 fclose(lib->fp);
192 lib->fp = NULL;
194 return 0;
197 int rdl_openmodule (struct librarynode * lib, int moduleno, rdffile * f)
199 char buf[512];
200 int i, cmod, t;
201 long length;
203 lib->referenced++;
205 if (!lib->fp)
207 lib->fp = fopen(lib->name, "rb");
208 if (!lib->fp) {
209 lib->referenced--;
210 return (rdl_error = 1);
213 else
214 rewind(lib->fp);
216 cmod = -1;
217 while (!feof(lib->fp))
219 strcpy(buf, lib->name);
220 i = strlen(buf);
221 buf[i++] = '.'; t = i;
222 while (fread(buf + i,1,1,lib->fp) == 1 && buf[i] && i < 512)
223 i++;
224 buf[i] = 0;
225 if (feof(lib->fp)) break;
227 if (buf[t] != '.') /* special module - not counted in the numbering */
228 cmod++; /* of RDOFF modules - must be referred to by name */
230 if (cmod == moduleno) {
231 rdl_error = 16 *
232 rdfopenhere(f, lib->fp, &lib->referenced, buf);
233 lib->referenced--;
234 if (!lib->referenced) {
235 fclose(lib->fp);
236 lib->fp = NULL;
238 return rdl_error;
241 fread(buf, 6, 1, lib->fp);
242 buf[6] = 0;
243 if (buf[t] == '.') {
244 /* do nothing */
246 else if (strncmp(buf, "RDOFF", 5)) {
247 if (! --lib->referenced) {
248 fclose(lib->fp);
249 lib->fp = NULL;
251 return rdl_error = 2;
253 else if (buf[5] != '2') {
254 if (! --lib->referenced) {
255 fclose(lib->fp);
256 lib->fp = NULL;
258 return rdl_error = 3;
261 fread(&length, 4, 1, lib->fp);
262 fseek(lib->fp, length, SEEK_CUR); /* skip over the module */
264 if (! --lib->referenced) {
265 fclose(lib->fp);
266 lib->fp = NULL;
268 return rdl_error = 4; /* module not found */
271 void rdl_perror(const char *apname, const char *filename)
273 if (rdl_error >= 16)
274 rdfperror(apname, filename);
275 else
276 fprintf(stderr,"%s:%s:%s\n",apname,filename,rdl_errors[rdl_error]);