diff options
| author | mingodad <mingodad@gmail.com> | 2013-01-11 00:04:38 +0000 |
|---|---|---|
| committer | mingodad <mingodad@gmail.com> | 2013-01-11 00:04:38 +0000 |
| commit | 59e18aee0e509a3ca75dbe6f909e18c1d17893d1 (patch) | |
| tree | 1ee20b231505273a1b1ba678b67c6c9e5812a414 /tccelf.c | |
| parent | 0a8c7d143eb0dcae15db9b61feb7b93228645d5f (diff) | |
| download | tinycc-59e18aee0e509a3ca75dbe6f909e18c1d17893d1.tar.gz tinycc-59e18aee0e509a3ca75dbe6f909e18c1d17893d1.tar.bz2 | |
Added what I call virtual io to tinycc this way we can make a monolitic executable or library that contains all needed to compile programs, truly tinycc portable.
Tested under linux exec the "mk-it" shell script and you'll end up with a portable tinycc executable that doesn't depend on anything else.
Diffstat (limited to 'tccelf.c')
| -rw-r--r-- | tccelf.c | 43 |
1 files changed, 21 insertions, 22 deletions
@@ -2326,13 +2326,13 @@ LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename) return ret; } -static void *load_data(int fd, unsigned long file_offset, unsigned long size) +static void *load_data(vio_fd fd, unsigned long file_offset, unsigned long size) { void *data; data = tcc_malloc(size); - lseek(fd, file_offset, SEEK_SET); - read(fd, data, size); + vio_lseek(fd, file_offset, SEEK_SET); + vio_read(fd, data, size); return data; } @@ -2346,7 +2346,7 @@ typedef struct SectionMergeInfo { /* load an object file and merge it with current files */ /* XXX: handle correctly stab (debug) info */ ST_FUNC int tcc_load_object_file(TCCState *s1, - int fd, unsigned long file_offset) + vio_fd fd, unsigned long file_offset) { ElfW(Ehdr) ehdr; ElfW(Shdr) *shdr, *sh; @@ -2364,7 +2364,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, stab_index = stabstr_index = 0; - if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) + if (vio_read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) goto fail1; if (ehdr.e_ident[0] != ELFMAG0 || ehdr.e_ident[1] != ELFMAG1 || @@ -2491,9 +2491,9 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, size = sh->sh_size; if (sh->sh_type != SHT_NOBITS) { unsigned char *ptr; - lseek(fd, file_offset + sh->sh_offset, SEEK_SET); + vio_lseek(fd, file_offset + sh->sh_offset, SEEK_SET); ptr = section_ptr_add(s, size); - read(fd, ptr, size); + vio_read(fd, ptr, size); } else { s->data_offset += size; } @@ -2649,7 +2649,7 @@ static int get_be32(const uint8_t *b) } /* load only the objects which resolve undefined symbols */ -static int tcc_load_alacarte(TCCState *s1, int fd, int size) +static int tcc_load_alacarte(TCCState *s1, vio_fd fd, int size) { int i, bound, nsyms, sym_index, off, ret; uint8_t *data; @@ -2658,7 +2658,7 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size) ElfW(Sym) *sym; data = tcc_malloc(size); - if (read(fd, data, size) != size) + if (vio_read(fd, data, size) != size) goto fail; nsyms = get_be32(data); ar_index = data + 4; @@ -2676,7 +2676,7 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size) printf("%5d\t%s\t%08x\n", i, p, sym->st_shndx); #endif ++bound; - lseek(fd, off, SEEK_SET); + vio_lseek(fd, off, SEEK_SET); if(tcc_load_object_file(s1, fd, off) < 0) { fail: ret = -1; @@ -2693,7 +2693,7 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size) } /* load a '.a' file */ -ST_FUNC int tcc_load_archive(TCCState *s1, int fd) +ST_FUNC int tcc_load_archive(TCCState *s1, vio_fd fd) { ArchiveHeader hdr; char ar_size[11]; @@ -2703,10 +2703,10 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) unsigned long file_offset; /* skip magic which was already checked */ - read(fd, magic, sizeof(magic)); + vio_read(fd, magic, sizeof(magic)); for(;;) { - len = read(fd, &hdr, sizeof(hdr)); + len = vio_read(fd, &hdr, sizeof(hdr)); if (len == 0) break; if (len != sizeof(hdr)) { @@ -2723,7 +2723,7 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) } ar_name[i + 1] = '\0'; // printf("name='%s' size=%d %s\n", ar_name, size, ar_size); - file_offset = lseek(fd, 0, SEEK_CUR); + file_offset = vio_lseek(fd, 0, SEEK_CUR); /* align to even */ size = (size + 1) & ~1; if (!strcmp(ar_name, "/")) { @@ -2739,7 +2739,7 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) if (tcc_load_object_file(s1, fd, file_offset) < 0) return -1; } - lseek(fd, file_offset + size, SEEK_SET); + vio_lseek(fd, file_offset + size, SEEK_SET); } return 0; } @@ -2748,7 +2748,7 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL is referenced by the user (so it should be added as DT_NEEDED in the generated ELF file) */ -ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) +ST_FUNC int tcc_load_dll(TCCState *s1, vio_fd fd, const char *filename, int level) { ElfW(Ehdr) ehdr; ElfW(Shdr) *shdr, *sh, *sh1; @@ -2759,7 +2759,7 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) const char *name, *soname; DLLReference *dllref; - read(fd, &ehdr, sizeof(ehdr)); + vio_read(fd, &ehdr, sizeof(ehdr)); /* test CPU specific stuff */ if (ehdr.e_ident[5] != ELFDATA2LSB || @@ -2986,14 +2986,13 @@ static int ld_next(TCCState *s1, char *name, int name_size) /* * Extract the file name from the library name * - * /!\ No test on filename capacity, be careful */ -static void libname_to_filename(TCCState *s1, const char libname[], char filename[]) +static void libname_to_filename(TCCState *s1, const char libname[], char filename[], size_t size) { if (!s1->static_link) { - sprintf(filename, "lib%s.so", libname); + snprintf(filename, size, "lib%s.so", libname); } else { - sprintf(filename, "lib%s.a", libname); + snprintf(filename, size, "lib%s.a", libname); } } @@ -3044,7 +3043,7 @@ static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed) goto lib_parse_error; } strcpy(libname, &filename[1]); - libname_to_filename(s1, libname, filename); + libname_to_filename(s1, libname, filename, sizeof(libname)); } else if (t != LD_TOK_NAME) { tcc_error_noabort("filename expected"); ret = -1; |
