int main(int argc, char **argv) { int a = 1; int format = TT_ANY; mFILE *ofp = mstdout(); char *name = NULL, *oname = NULL; int output_conf = 0; read_sections(READ_BASES); while (a < argc) { if (strcasecmp(argv[a], "-abi") == 0) format = TT_ABI; else if (strcasecmp(argv[a], "-alf") == 0) format = TT_ALF; else if (strcasecmp(argv[a], "-scf") == 0) format = TT_SCF; else if (strcasecmp(argv[a], "-pln") == 0) format = TT_PLN; else if (strcasecmp(argv[a], "-name") == 0) { if (a == argc) usage(); name = argv[++a]; } else if (strcasecmp(argv[a], "-output") == 0) { if (a == argc) usage(); ofp = mfopen(oname = argv[++a], "wb"); if (NULL == ofp) { perror(argv[a]); return 1; } } else if (strcasecmp(argv[a], "-conf") == 0) { output_conf = 1; } else if (argv[a][0] == '-') usage(); else break; a++; } if (a+1 != argc) usage(); if (!name) name = oname; if (!name) name = argv[a]; return convert(argv[a], format, ofp, name, output_conf); }
bool Vfile::read(string filename) { // open the file ifstream v(filename.c_str()); if (!v) { cout << "Couldn't open file " << filename << " for reading" << endl; return false; } // read the static portions read_ok = true; read_header(v); read_sections(v); // check if global bodies were found and datatype is double if (bodies.GroupID==-1) { cout << "==============================" << endl; read_ok = false; return read_ok; } assert(bodies.Type==6); read_dynamics(v); v.close(); return read_ok; }
int main(int argc, char *argv[]) { if (argc != 3) fail("usage: unself in.self out.elf"); self = mmap_file(argv[1]); read_header(); read_sections(); if (key_ver != 0x8000) self_decrypt(); out = fopen(argv[2], "w+"); write_elf(); check_elf(); fclose(out); return 0; }
/* * Read the ALF format sequence from FILE *fp into a Read structure. * All printing characters (as defined by ANSII C `isprint') * are accepted, but `N's are translated to `-'s. In this respect we * are adhering (more or less) to the CSET_DEFAULT uncertainty code set. * * Returns: * Read * - Success, the Read structure read. * NULLRead - Failure. */ Read *fread_alf(FILE *fp) { Read *read = NULLRead; int i; int numPoints; int sections = read_sections(0); uint_4 data_size; uint_4 dataO; uint_4 header_size=396; /* size of the header of the processed data section */ uint_2 actBaseDataSize; /* actual number of bytes of data of information containing the base and basePos information */ int num_points; /* keeps track of the actual number of points, rather than the early guess of numPoints */ off_t indexO; /* File offset where the index is */ uint_4 baseO; /* File offset where the bases are stored */ /* * RMD lots of changes below here until end of data reading section * Some are cosmetic. * getIndexEntry calls in front of where they were needed, and made * There is a substantive change to the inner loop of the sequence * reading section. This now uses fscanf - much less rigid than the * previous scheme. Note that it reads bp as a float. This is because * it is a float in multiple trace data files! (bizarre Pharmacia * programming!). */ /************************************************************* * Read the various file offsets *************************************************************/ /* indexO is the offset of the index. * Or I could look for the first label, starting 'ALF' * if I used 512 then none of the entries are on long * word boundaries */ indexO = 522; /* offset in file of first base of sequence */ if (! (getIndexEntryLW(fp,indexO,BaseEntryLabel,12,&baseO)) ) goto bail_out; /* actual size of region containing this data */ if (! (getIndexEntryW(fp,indexO,BaseEntryLabel,10,&actBaseDataSize)) ) goto bail_out; /* Look for Processed data first. If we fail to find it, then look for * the Raw data (same format). */ /* offset in file to start of processed data segment - there * is then a header of size header_size (currently 396) */ if (! (getIndexEntryLW(fp,indexO,DataEntryLabel,12,&dataO)) ) { if (! (getIndexEntryLW(fp,indexO,RawDataEntryLabel,12,&dataO)) ) goto bail_out; /* actual size of region containing this data */ if (! (getIndexEntryLW(fp,indexO,RawDataEntryLabel,10,&data_size)) ) goto bail_out; } else { /* actual size of region containing this data */ if (! (getIndexEntryLW(fp,indexO,DataEntryLabel,10,&data_size)) ) goto bail_out; } /* Because each trace value is stored in a 2 byte * integer, thus to store A C G T information * it takes 8 bytes. So subtract off the header and * divide by 8 */ numPoints = (int)((data_size - header_size)/ 8); /* Allocate the sequence */ if (NULLRead == (read = read_allocate(numPoints, BASELIMIT))) goto bail_out; /************************************************************* * Read the bases information *************************************************************/ if (sections & READ_BASES) { /* new locals introduced by LFW and/or RMD for the ALF */ int numBases; /* number of nucleotides read in */ float bp; char ch; if (!(fseek(fp, (off_t)baseO, 0) == 0)) goto bail_out; for (numBases = 0; (unsigned)ftell(fp) < baseO+(unsigned short)actBaseDataSize && numBases<BASELIMIT;) { char line[200]; fgets(line, (int)sizeof(line), fp); sscanf(line, "%c %*d %f", &ch, &bp); /* we convert ch to Staden format here */ switch (ch) { case 'A': case 'C': case 'G': case 'T': break; default: ch = '-'; /* if (isupper(ch)) ch = '-'; else ch = '\0'; */ } if (ch) { read->base[numBases] = ch; read->prob_A[numBases] = 0; read->prob_C[numBases] = 0; read->prob_G[numBases] = 0; read->prob_T[numBases] = 0; read->basePos[numBases] = bp; ++numBases; } } read->base[numBases] = 0; read->NBases = numBases; } /************************************************************* * Read the trace information *************************************************************/ if (sections & READ_SAMPLES) { /* * Traces are stored as 2 byte integers in records in the order of * A C G T A C G T ... */ if (fseek(fp, (off_t)(dataO+header_size), 0) != 0) goto bail_out; num_points = 0; for (i=0; i < read->NPoints; i++) { if (!le_read_int_2(fp, &(read->traceA[i]))) goto bail_out; if (read->maxTraceVal < read->traceA[i]) read->maxTraceVal = read->traceA[i]; if (!le_read_int_2(fp, &(read->traceC[i]))) goto bail_out; if (read->maxTraceVal < read->traceC[i]) read->maxTraceVal = read->traceC[i]; if (!le_read_int_2(fp, &(read->traceG[i]))) goto bail_out; if (read->maxTraceVal < read->traceG[i]) read->maxTraceVal = read->traceG[i]; if (!le_read_int_2(fp, &(read->traceT[i]))) goto bail_out; if (read->maxTraceVal < read->traceT[i]) read->maxTraceVal = read->traceT[i]; if (read->traceA[i]==0 && read->traceT[i]==0 && read->traceC[i]==0 && read->traceG[i]==0 && i > (numPoints-64)) break; num_points++; } } /* SUCCESS */ read->format = TT_ALF; return(read); /* FAILURE */ bail_out: if (read) read_deallocate(read); return NULLRead; }
int main(int argc, char **argv) { Read *r = NULL; char *directory = NULL; char *ident, *value; int ident_len, value_len; int i, j, found; int *found_args = NULL; char **FileList = NULL; char trace_filename[FILENAME_MAX]=""; int num_traces, trace_iter, files_read = 0; char *str; if(argc != 2) usage(); directory = argv[1]; // Get a list of all chromatogram files in the directory num_traces = GetFileList(&FileList, directory); if(num_traces == 0){ fprintf(stderr,"* Path %d yielded 0 files...exiting\n", num_traces); exit(2); } /* step through all the sequences */ for (trace_iter = 0; trace_iter < num_traces; trace_iter++){ if(r){ read_deallocate(r); } //Get the file name from the iterator. sprintf(trace_filename, "%s/%s",directory, FileList[trace_iter]); /* Read the file */ read_sections(READ_COMMENTS); if (NULL == (r = read_reading(trace_filename, TT_ANY))) { continue; // don't worry about it } files_read++; if (!r->info) return 1; for(str = strtok(r->info,"\n"); str != NULL; str = strtok(NULL,"\n")){ char *name; char *value; int items = 0; char *start,*end; name = str; value = strchr(name,'='); *value = '\0'; // skip over the '=' value++; if(!strcmp(name,"RUND")){ start = value; end = strstr(value," - "); *end = '\0'; // terminate the start string end += 3; // skip over the " - " fprintf(stdout,"%s.RUND=start=%s,end=%s\n", FileList[trace_iter], start,end); }else if(! strcmp(name,"DATE")){ start = value; end = strstr(value," to "); *end = '\0'; // terminate the start string end += 4; // skip over the " to " fprintf(stdout,"%s.DATE=start=%s,end=%s\n", FileList[trace_iter],start,end); }else{ fprintf(stdout,"%s.%s=%s\n", FileList[trace_iter],name, value); } } } return (files_read == 0); // is zero, unless we read nothing. }
/* * fread_ztr * * Reads a ZTR file from 'fp'. This checks for the correct magic number and * major version number, but not minor version number. * * FIXME: Add automatic uncompression? * * Arguments: * fp A readable FILE pointer * * Returns: * Success: Pointer to a ztr_t structure (malloced) * Failure: NULL */ ztr_t *fread_ztr(FILE *fp) { ztr_t *ztr; ztr_chunk_t *chunk; int sections = read_sections(0); /* Allocate */ if (NULL == (ztr = new_ztr())) return NULL; /* Read the header */ if (-1 == ztr_read_header(fp, &ztr->header)) return NULL; /* Check magic number and version */ if (memcmp(ztr->header.magic, ZTR_MAGIC, 8) != 0) return NULL; if (ztr->header.version_major != ZTR_VERSION_MAJOR) return NULL; /* Load chunks */ while ((chunk = ztr_read_chunk_hdr(fp))) { /* char str[5]; fprintf(stderr, "Read chunk %.4s %08x length %d\n", ZTR_BE2STR(chunk->type, str), chunk->type, chunk->dlength); */ switch(chunk->type) { case ZTR_TYPE_HEADER: /* End of file */ return ztr; case ZTR_TYPE_SAMP: case ZTR_TYPE_SMP4: if (! (sections & READ_SAMPLES)) { fseek(fp, chunk->dlength, SEEK_CUR); xfree(chunk); continue; } break; case ZTR_TYPE_BASE: case ZTR_TYPE_BPOS: case ZTR_TYPE_CNF4: case ZTR_TYPE_CNF1: case ZTR_TYPE_CSID: if (! (sections & READ_BASES)) { fseek(fp, chunk->dlength, SEEK_CUR); xfree(chunk); continue; } break; case ZTR_TYPE_TEXT: if (! (sections & READ_COMMENTS)) { fseek(fp, chunk->dlength, SEEK_CUR); xfree(chunk); continue; } break; case ZTR_TYPE_CLIP: case ZTR_TYPE_FLWO: case ZTR_TYPE_FLWC: break; /* default: fprintf(stderr, "Unknown chunk type '%s': skipping\n", ZTR_BE2STR(chunk->type,str)); fseek(fp, chunk->dlength, SEEK_CUR); xfree(chunk); continue; */ } chunk->ztr_owns = 1; chunk->data = (char *)xmalloc(chunk->dlength); if (chunk->dlength != fread(chunk->data, 1, chunk->dlength, fp)) { delete_ztr(ztr); return NULL; } ztr->nchunks++; ztr->chunk = (ztr_chunk_t *)xrealloc(ztr->chunk, ztr->nchunks * sizeof(ztr_chunk_t)); memcpy(&ztr->chunk[ztr->nchunks-1], chunk, sizeof(*chunk)); xfree(chunk); } return ztr; }
/* * Read the SCF format sequence from FILE *fp into a 'scf' structure. * A NULL result indicates failure. */ Scf *fread_scf(FILE *fp) { Scf *scf; Header h; int err; float scf_version; int sections = read_sections(0); /* Read header */ if (read_scf_header(fp, &h) == -1) { return NULL; } /* Allocate memory */ if (NULL == (scf = scf_allocate(h.samples, h.sample_size, h.bases, h.comments_size, h.private_size))) return NULL; /* fake things for older style SCF -- SD */ if (h.sample_size != 1 && h.sample_size != 2) h.sample_size = 1; scf_version = scf_version_str2float(h.version); memcpy(&scf->header, &h, sizeof(Header)); if (sections & READ_SAMPLES) { /* Read samples */ if (fseek(fp, (off_t)h.samples_offset, 0 /* SEEK_SET */) != 0) { scf_deallocate(scf); return NULL; } if ( 2.9 > scf_version ) { if (h.sample_size == 1) { err= read_scf_samples1(fp, scf->samples.samples1, h.samples); } else { err= read_scf_samples2(fp, scf->samples.samples2, h.samples); } } else { if (h.sample_size == 1) { err= read_scf_samples31(fp, scf->samples.samples1, h.samples); } else { err= read_scf_samples32(fp, scf->samples.samples2, h.samples); } } if (-1 == err) { scf_deallocate(scf); return NULL; } } if (sections & READ_BASES) { /* Read bases */ if (fseek(fp, (off_t)h.bases_offset, 0 /* SEEK_SET */) != 0) { scf_deallocate(scf); return NULL; } if ( 2.9 > scf_version ) { if (-1 == read_scf_bases(fp, scf->bases, h.bases)) { scf_deallocate(scf); return NULL; } } else { if (-1 == read_scf_bases3(fp, scf->bases, h.bases)) { scf_deallocate(scf); return NULL; } } } if (sections & READ_COMMENTS) { /* Read comments */ if (scf->comments) { if (fseek(fp,(off_t)(h.comments_offset), 0) != 0 || -1 == read_scf_comment(fp, scf->comments, h.comments_size)) { /* * Was: "scf_deallocate(scf); return NULL;". * We now simply clear the comments and gracefully continue. */ fprintf(stderr, "Warning: SCF file had invalid comment field\n"); xfree(scf->comments); scf->comments = NULL; } else { scf->comments[h.comments_size] = '\0'; } } } /* Read private data */ if (h.private_size) { if (-1 == fseek(fp, (off_t)(h.private_offset), 0) || h.private_size != fread(scf->private_data, 1, h.private_size, fp)){ scf_deallocate(scf); return NULL; } } return scf; }
/* * Translates an Scf structure into a Read structure. * The Scf structure is left unchanged. * * Returns: * A pointer to an allocated Read structure upon success. * NULLRead upon failure. */ Read *scf2read(Scf *scf) { Read *read; register int i, i_end; TRACE max_val = 0; int sections = read_sections(0); int nsamples = 0; int nbases = 0; /* allocate */ if (sections & READ_SAMPLES) nsamples = scf->header.samples; if (sections & READ_BASES) nbases = scf->header.bases; read = read_allocate(nsamples, nbases); if (NULLRead == read) return NULLRead; if (sections & READ_SAMPLES) { /* copy the samples */ i_end = scf->header.samples; read->NPoints = i_end; if (scf->header.sample_size == 1) { for (i = 0; i < i_end; i++) { read->traceA[i] = scf->samples.samples1[i].sample_A; read->traceC[i] = scf->samples.samples1[i].sample_C; read->traceG[i] = scf->samples.samples1[i].sample_G; read->traceT[i] = scf->samples.samples1[i].sample_T; if (read->traceA[i] > max_val) max_val = read->traceA[i]; if (read->traceC[i] > max_val) max_val = read->traceC[i]; if (read->traceG[i] > max_val) max_val = read->traceG[i]; if (read->traceT[i] > max_val) max_val = read->traceT[i]; } } else { /* sample_size == 2 */ for (i = 0; i < i_end; i++) { read->traceA[i] = scf->samples.samples2[i].sample_A; read->traceC[i] = scf->samples.samples2[i].sample_C; read->traceG[i] = scf->samples.samples2[i].sample_G; read->traceT[i] = scf->samples.samples2[i].sample_T; if (read->traceA[i] > max_val) max_val = read->traceA[i]; if (read->traceC[i] > max_val) max_val = read->traceC[i]; if (read->traceG[i] > max_val) max_val = read->traceG[i]; if (read->traceT[i] > max_val) max_val = read->traceT[i]; } } read->maxTraceVal = max_val; } if (sections & READ_BASES) { /* copy the bases */ i_end = scf->header.bases; read->NBases = i_end; for (i = 0; i < i_end; i++) { read->basePos[i] = scf->bases[i].peak_index; read->prob_A[i] = scf->bases[i].prob_A; read->prob_C[i] = scf->bases[i].prob_C; read->prob_G[i] = scf->bases[i].prob_G; read->prob_T[i] = scf->bases[i].prob_T; read->base[i] = scf->bases[i].base; } read->base[i] = 0; } if (sections & READ_COMMENTS) { /* allocate and copy the comments */ if (scf->header.comments_size > 0 && scf->comments) { read->info = (char *)xmalloc(scf->header.comments_size+1); if (NULL == read->info) { read_deallocate(read); return NULLRead; } memcpy(read->info, scf->comments, scf->header.comments_size); read->info[scf->header.comments_size] = '\0'; } } /* other bits and pieces */ read->leftCutoff = scf->header.bases_left_clip; read->rightCutoff = read->NBases - scf->header.bases_right_clip + 1; read->format = TT_SCF; return read; }
/* * Allocate a new sequence, with the given sizes. * Returns: * "Read *" for success * "NULLRead" for failure */ Read *read_allocate(int num_points, int num_bases) { Read *seq = NULLRead; int sections = read_sections(0); /* Allocate the body of the sequence */ if ((seq = (Read *)xmalloc(sizeof(Read))) == NULL) return(NULLRead); seq->NPoints = num_points; seq->NBases = num_bases; /* * Initialise the body, all pointers are set to NULL so we can * happily call `read_deallocate()`. */ seq->leftCutoff = 0; seq->rightCutoff = 0; seq->maxTraceVal = 0; seq->baseline = 0; seq->traceC = NULL; seq->traceA = NULL; seq->traceG = NULL; seq->traceT = NULL; seq->base = NULL; seq->basePos = NULL; seq->info = NULL; seq->format = TT_ANY; seq->trace_name = NULL; seq->prob_A = NULL; seq->prob_C = NULL; seq->prob_G = NULL; seq->prob_T = NULL; seq->orig_trace_format = TT_ANY; seq->orig_trace = NULL; seq->orig_trace_free = NULL; seq->ident = NULL; /* Allocate space for the bases - 1 extra for the ->base field so * that we can treat it as a NULL terminated string. */ if (sections & READ_BASES && (((seq->base = (char *)xcalloc(num_bases+1,1)) == NULL) || ((seq->basePos = (uint_2 *)xcalloc(num_bases+1,2)) == NULL) || ((seq->prob_A = (char *)xcalloc(num_bases+1,1)) == NULL) || ((seq->prob_C = (char *)xcalloc(num_bases+1,1)) == NULL) || ((seq->prob_G = (char *)xcalloc(num_bases+1,1)) == NULL) || ((seq->prob_T = (char *)xcalloc(num_bases+1,1)) == NULL)) ) { read_deallocate(seq); return NULLRead; } if (sections & READ_SAMPLES && (((seq->traceC =(TRACE *)xcalloc(num_points+1, 2)) == NULL)|| ((seq->traceA =(TRACE *)xcalloc(num_points+1, 2)) == NULL)|| ((seq->traceG =(TRACE *)xcalloc(num_points+1, 2)) == NULL)|| ((seq->traceT =(TRACE *)xcalloc(num_points+1, 2)) == NULL)) ) { read_deallocate(seq); return NULLRead; } return seq; }
/* * --------------------------------------------------------------------------- * Loads confidence values from the trace file and averages them. * 'opos' is optional - if not known then set to NULL. * * Returns 0 for success * -1 for failure */ int get_read_conf(Exp_info *e, int length, int2 *opos, int1 *conf) { int ttype, i; FILE *fp; uint_1 *prob_A, *prob_C, *prob_G, *prob_T; char *seq; float scf_version; int nbases = 0; /* Sanity check */ if (!(exp_Nentries(e,EFLT_LT) && exp_Nentries(e,EFLT_LN))) return -1; /* Find and load trace file */ ttype = trace_type_str2int(exp_get_entry(e, EFLT_LT)); if (ttype != TT_SCF && ttype != TT_ZTR) return -1; /* * We only support direct reading accuracy values from SCF files. * Otherwise we have to take a slower approach. */ if (ttype != TT_SCF) { Read *r; int sec = read_sections(0); read_sections(READ_BASES); if (NULL == (r = read_reading(exp_get_entry(e,EFLT_LN), TT_ANYTR))) { read_sections(sec); return -1; } prob_A = (int1 *)xmalloc(r->NBases); prob_C = (int1 *)xmalloc(r->NBases); prob_G = (int1 *)xmalloc(r->NBases); prob_T = (int1 *)xmalloc(r->NBases); seq = (char *)xmalloc(r->NBases); memcpy(prob_A, r->prob_A, r->NBases); memcpy(prob_C, r->prob_C, r->NBases); memcpy(prob_G, r->prob_G, r->NBases); memcpy(prob_T, r->prob_T, r->NBases); memcpy(seq, r->base, r->NBases); nbases = r->NBases; read_deallocate(r); read_sections(sec); } else { Header h; /* For SCF files we read directly - the above code would also do. */ if (NULL == (fp = open_trace_file(exp_get_entry(e,EFLT_LN), NULL))) return -1; /* Read the SCF header */ if (-1 == read_scf_header(fp, &h)) return -1; scf_version = scf_version_str2float(h.version); nbases = h.bases; /* Alloc memory */ prob_A = (uint_1 *)xmalloc(h.bases * sizeof(*prob_A)); prob_C = (uint_1 *)xmalloc(h.bases * sizeof(*prob_A)); prob_G = (uint_1 *)xmalloc(h.bases * sizeof(*prob_A)); prob_T = (uint_1 *)xmalloc(h.bases * sizeof(*prob_A)); seq = (char *)xmalloc(h.bases * sizeof(*seq)); if (NULL == prob_A || NULL == prob_C || NULL == prob_G || NULL == prob_T || NULL == seq) return -1; /* Load base scores */ if (scf_version >= 3.0) { /* * Version 3 base format: * num_bases * 4byte peak index * num_bases * prob_A * num_bases * prob_C * num_bases * prob_G * num_bases * prob_T * num_bases * base * num_bases * spare (x3) */ fseek(fp, (off_t)h.bases_offset + 4 * h.bases, SEEK_SET); if (h.bases != fread(prob_A, 1, h.bases, fp)) return -1; if (h.bases != fread(prob_C, 1, h.bases, fp)) return -1; if (h.bases != fread(prob_G, 1, h.bases, fp)) return -1; if (h.bases != fread(prob_T, 1, h.bases, fp)) return -1; if (h.bases != fread(seq, 1, h.bases, fp)) return -1; } else { int i; uint_1 buf[12]; /* * Version 2 base format * num_bases * base_struct, where base_struct is 12 bytes: * 0-3 peak_index * 4-7 prob_A/C/G/T * 8 base * 9- spare */ fseek(fp, (off_t)h.bases_offset, SEEK_SET); for (i = 0; (unsigned)i < h.bases; i++) { if (1 != fread(buf, 12, 1, fp)) return -1; prob_A[i] = buf[4]; prob_C[i] = buf[5]; prob_G[i] = buf[6]; prob_T[i] = buf[7]; seq[i] = buf[8]; } } fclose(fp); } /* Determine confidence values */ if (opos) { for (i=0; i<length; i++) { if (opos[i] == 0) { /* Inserted base, change to 0% */ conf[i] = 0; } else { switch(seq[opos[i]-1]) { case 'a': case 'A': conf[i] = prob_A[opos[i]-1]; break; case 'c': case 'C': conf[i] = prob_C[opos[i]-1]; break; case 'g': case 'G': conf[i] = prob_G[opos[i]-1]; break; case 't': case 'T': conf[i] = prob_T[opos[i]-1]; break; default: conf[i] = 2; } } } } else { int mlength = MIN(length, nbases); for (i=0; i < mlength; i++) { switch(seq[i]) { case 'a': case 'A': conf[i] = prob_A[i]; break; case 'c': case 'C': conf[i] = prob_C[i]; break; case 'g': case 'G': conf[i] = prob_G[i]; break; case 't': case 'T': conf[i] = prob_T[i]; break; case 'n': case 'N': case '-': conf[i] = (prob_A[i] + prob_C[i] + prob_G[i] + prob_T[i]) / 4; break; default: conf[i] = 2; } } for (; i < length; i++) conf[i] = 2; } xfree(prob_A); xfree(prob_C); xfree(prob_G); xfree(prob_T); xfree(seq); return 0; }
void elf_mod_symload(int strtablen) { Elf_Ehdr ehdr; char *shstrtab; struct elf_section *head, *s; char *symbuf, *strbuf; /* * Seek to the text offset to start loading... */ if (lseek(modfd, 0, SEEK_SET) == -1) err(12, "lseek"); if (read_elf_header(modfd, &ehdr) < 0) return; shstrtab = read_shstring_table(modfd, &ehdr); read_sections(modfd, &ehdr, shstrtab, &head); for (s = head; s; s = s->next) { struct elf_section *p = s; if ((p->type == SHT_SYMTAB) || (p->type == SHT_DYNSYM)) { if (debug) fprintf(stderr, "loading `%s': addr = %p, " "size = %#lx\n", s->name, s->addr, (u_long)s->size); /* * Seek to the file offset to start loading it... */ if (lseek(modfd, p->offset, SEEK_SET) == -1) err(12, "lseek"); symbuf = malloc(p->size); if (symbuf == 0) err(13, "malloc"); if (read(modfd, symbuf, p->size) != p->size) err(14, "read"); loadsym(symbuf, p->size); free(symbuf); } } for (s = head; s; s = s->next) { struct elf_section *p = s; if ((p->type == SHT_STRTAB) && (strcmp(p->name, ".strtab") == 0 )) { if (debug) fprintf(stderr, "loading `%s': addr = %p, " "size = %#lx\n", s->name, s->addr, (u_long)s->size); /* * Seek to the file offset to start loading it... */ if (lseek(modfd, p->offset, SEEK_SET) == -1) err(12, "lseek"); strbuf = malloc(p->size); if (strbuf == 0) err(13, "malloc"); if (read(modfd, strbuf, p->size) != p->size) err(14, "read"); loadsym(strbuf, p->size); free(strbuf); } } free(shstrtab); free_sections(head); return; }
/* load a prelinked module; returns entry point */ void * elf_mod_load(int fd) { Elf_Ehdr ehdr; size_t zero_size = 0; size_t b; ssize_t n; char *shstrtab; struct elf_section *head, *s; char buf[10 * BUFSIZ]; void *addr = NULL; if (read_elf_header(fd, &ehdr) < 0) return NULL; shstrtab = read_shstring_table(fd, &ehdr); read_sections(fd, &ehdr, shstrtab, &head); for (s = head; s; s = s->next) { if (s->type != SHT_STRTAB && s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) { if (debug) fprintf(stderr, "loading `%s': addr = %p, " "size = %#lx\n", s->name, s->addr, (u_long)s->size); if (s->type == SHT_NOBITS) { /* skip some space */ zero_size += s->size; } else { if (addr != NULL) { /* * if there is a gap in the prelinked * module, transfer some empty space. */ zero_size += (char*)s->addr - (char*)addr; } if (zero_size) { loadspace(zero_size); zero_size = 0; } b = s->size; if (lseek(fd, s->offset, SEEK_SET) == -1) err(1, "lseek"); while (b) { n = read(fd, buf, MIN(b, sizeof(buf))); if (n == 0) errx(1, "unexpected EOF"); if (n < 0) err(1, "read"); loadbuf(buf, n); b -= n; } addr = (char*)s->addr + s->size; } } } if (zero_size) loadspace(zero_size); free_sections(head); free(shstrtab); return (void *)ehdr.e_entry; }
/* return size needed by the module */ int elf_mod_sizes(int fd, size_t *modsize, int *strtablen, struct lmc_resrv *resrvp, struct stat *sp) { Elf_Ehdr ehdr; ssize_t off = 0; size_t data_hole = 0; char *shstrtab, *strtab; struct elf_section *head, *s, *stab; if (read_elf_header(fd, &ehdr) < 0) return -1; shstrtab = read_shstring_table(fd, &ehdr); read_sections(fd, &ehdr, shstrtab, &head); for (s = head; s; s = s->next) { /* XXX impossible! */ if (s->type == SHT_STRTAB && s->type == SHT_SYMTAB && s->type == SHT_DYNSYM) continue; if (debug) fprintf(stderr, "%s: addr = %p size = %#lx align = %#lx\n", s->name, s->addr, (u_long)s->size, (u_long)s->align); /* * XXX try to get rid of the hole before the data * section that GNU-ld likes to put there */ if (strcmp(s->name, ".data") == 0 && s->addr > (void *)off) { data_offset = roundup(off, s->align); if (debug) fprintf(stderr, ".data section forced to " "offset %p (was %p)\n", (void *)data_offset, s->addr); /* later remove size of compressed hole from off */ data_hole = (ssize_t)s->addr - data_offset; } off = (ssize_t)s->addr + s->size; } off -= data_hole; /* XXX round to pagesize? */ *modsize = roundup(off, sysconf(_SC_PAGESIZE)); /* get string table length */ strtab = read_string_table(fd, head, strtablen); free(shstrtab); free(strtab); /* get symbol table sections */ get_symtab(&head); stab = head; resrvp->sym_symsize = 0; while (stab) { resrvp->sym_symsize += stab->size; stab = stab->next; } resrvp->sym_size = resrvp->sym_symsize + *strtablen; free_sections(head); return (0); }