static void write_reloc_rec(struct RelocRec *r) { if (r->refseg != NO_SEG && (r->refseg & 1)) error (ERR_NONFATAL, "RDF format does not support segment base" " references"); r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */ membufwrite(header,&r->type,1); membufwrite(header,&r->segment,1); membufwrite(header,&r->offset,-4); membufwrite(header,&r->length,1); membufwrite(header,&r->refseg,-2); /* 9 bytes written */ }
void membufwrite(memorybuffer *b, void *data, int bytes) { int16 w; long l; if (b->next) { /* memory buffer full - use next buffer */ membufwrite(b->next,data,bytes); return; } if ((bytes < 0 && b->length - bytes > BUF_BLOCK_LEN) || (bytes > 0 && b->length + bytes > BUF_BLOCK_LEN)) { /* buffer full and no next allocated... allocate and initialise next * buffer */ b->next = newmembuf(); membufwrite(b->next,data,bytes); return; } switch(bytes) { case -4: /* convert to little-endian */ l = * (long *) data ; b->buffer[b->length++] = l & 0xFF; l >>= 8 ; b->buffer[b->length++] = l & 0xFF; l >>= 8 ; b->buffer[b->length++] = l & 0xFF; l >>= 8 ; b->buffer[b->length++] = l & 0xFF; break; case -2: w = * (int16 *) data ; b->buffer[b->length++] = w & 0xFF; w >>= 8 ; b->buffer[b->length++] = w & 0xFF; break; default: while(bytes--) { b->buffer[b->length++] = *(* (unsigned char **) &data); (* (unsigned char **) &data)++ ; } break; } }
void membufwrite(memorybuffer * const b, void *data, int bytes) { uint16_t w; int32_t l; char *c; if (b->next) { /* memory buffer full - use next buffer */ membufwrite(b->next, data, bytes); return; } if ((bytes < 0 && b->length - bytes > BUF_BLOCK_LEN) || (bytes > 0 && b->length + bytes > BUF_BLOCK_LEN)) { /* buffer full and no next allocated... allocate and initialise next * buffer */ b->next = newmembuf(); membufwrite(b->next, data, bytes); return; } switch (bytes) { case -4: /* convert to little-endian */ l = *(int32_t *)data; b->buffer[b->length++] = l & 0xFF; l >>= 8; b->buffer[b->length++] = l & 0xFF; l >>= 8; b->buffer[b->length++] = l & 0xFF; l >>= 8; b->buffer[b->length++] = l & 0xFF; break; case -2: w = *(uint16_t *) data; b->buffer[b->length++] = w & 0xFF; w >>= 8; b->buffer[b->length++] = w & 0xFF; break; default: c = data; while (bytes--) b->buffer[b->length++] = *c++; break; } }
int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r) { #ifndef STRICT_ERRORS int i; #endif membufwrite(h->buf, &r->type, 1); membufwrite(h->buf, &r->g.reclen, 1); switch (r->type) { case RDFREC_GENERIC: /* generic */ membufwrite(h->buf, &r->g.data, r->g.reclen); break; case RDFREC_RELOC: case RDFREC_SEGRELOC: membufwrite(h->buf, &r->r.segment, 1); membufwrite(h->buf, &r->r.offset, -4); membufwrite(h->buf, &r->r.length, 1); membufwrite(h->buf, &r->r.refseg, -2); /* 9 bytes written */ break; case RDFREC_IMPORT: /* import */ case RDFREC_FARIMPORT: membufwrite(h->buf, &r->i.flags, 1); membufwrite(h->buf, &r->i.segment, -2); membufwrite(h->buf, &r->i.label, strlen(r->i.label) + 1); break; case RDFREC_GLOBAL: /* export */ membufwrite(h->buf, &r->e.flags, 1); membufwrite(h->buf, &r->e.segment, 1); membufwrite(h->buf, &r->e.offset, -4); membufwrite(h->buf, &r->e.label, strlen(r->e.label) + 1); break; case RDFREC_DLL: /* DLL */ membufwrite(h->buf, &r->d.libname, strlen(r->d.libname) + 1); break; case RDFREC_BSS: /* BSS */ membufwrite(h->buf, &r->b.amount, -4); break; case RDFREC_MODNAME: /* Module name */ membufwrite(h->buf, &r->m.modname, strlen(r->m.modname) + 1); break; default: #ifdef STRICT_ERRORS return rdf_errno = RDF_ERR_RECTYPE; #else for (i = 0; i < r->g.reclen; i++) membufwrite(h->buf, r->g.data[i], 1); #endif } return 0; }
int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r) { switch (r->type) { case 1: membufwrite(h,&r->type,1); membufwrite(h,&r->r.segment,1); membufwrite(h,&r->r.offset,-4); membufwrite(h,&r->r.length,1); membufwrite(h,&r->r.refseg,-2); /* 9 bytes written */ break; case 2: /* import */ membufwrite(h,&r->type,1); membufwrite(h,&r->i.segment,-2); membufwrite(h,&r->i.label,strlen(r->i.label) + 1); break ; case 3: /* export */ membufwrite(h,&r->type,1); membufwrite(h,&r->e.segment,1); membufwrite(h,&r->e.offset,-4); membufwrite(h,&r->e.label,strlen(r->e.label) + 1); break ; case 4: /* DLL */ membufwrite(h,&r->type,1); membufwrite(h,&r->d.libname,strlen(r->d.libname) + 1); break ; case 5: /* BSS */ membufwrite(h,&r->type,1); membufwrite(h,&r->b.amount,-4); break ; default: return (rdf_errno = 2); } return 0; }
int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r) { #ifndef STRICT_ERRORS int i; #endif membufwrite(h->buf,&r->type,1); membufwrite(h->buf,&r->g.reclen,1); switch (r->type) { case 1: case 6: membufwrite(h->buf,&r->r.segment,1); membufwrite(h->buf,&r->r.offset,-4); membufwrite(h->buf,&r->r.length,1); membufwrite(h->buf,&r->r.refseg,-2); /* 9 bytes written */ break; case 2: /* import */ case 7: membufwrite(h->buf,&r->i.segment,-2); membufwrite(h->buf,&r->i.label,strlen(r->i.label) + 1); break ; case 3: /* export */ membufwrite(h->buf,&r->e.segment,1); membufwrite(h->buf,&r->e.offset,-4); membufwrite(h->buf,&r->e.label,strlen(r->e.label) + 1); break ; case 4: /* DLL */ membufwrite(h->buf,&r->d.libname,strlen(r->d.libname) + 1); break ; case 5: /* BSS */ membufwrite(h->buf,&r->b.amount,-4); break ; default: #ifdef STRICT_ERRORS return (rdf_errno = 8); #else for (i = 0; i < r->g.reclen; i++) membufwrite(h->buf, r->g.data[i], 1); #endif } return 0; }