PulkoMandy | 15f3f26 | 2014-06-28 11:45:08 +0200 | [diff] [blame] | 1 | #include "loader.h" |
kthacker | 6de6775 | 2006-04-17 15:02:26 +0000 | [diff] [blame] | 2 | #include "rel.h" |
| 3 | #include <stddef.h> |
| 4 | #include <malloc.h> |
| 5 | |
| 6 | extern void *progend; |
| 7 | |
| 8 | struct prg_hdr { |
| 9 | char *relocatedata; |
| 10 | char arch[8]; |
| 11 | char version[8]; |
| 12 | char initfunc[1]; |
| 13 | }; |
| 14 | |
| 15 | struct dsc_hdr { |
| 16 | char *relocatedata; |
| 17 | struct dsc dscdata; |
| 18 | }; |
| 19 | |
| 20 | unsigned char loader_arch_load(const char *name, char *arg) |
| 21 | { |
| 22 | char *loadaddr; |
| 23 | struct prg_hdr *prghdr; |
| 24 | int length; |
| 25 | |
| 26 | /* get length of file */ |
| 27 | length = get_file_length(name); |
| 28 | if (length==0) |
| 29 | return LOADER_ERR_OPEN; |
| 30 | |
| 31 | /* allocate memory */ |
| 32 | loadaddr = malloc(length); |
| 33 | if (loadaddr==NULL) |
| 34 | return LOADER_ERR_MEM; |
| 35 | |
| 36 | /* load the file */ |
| 37 | load_file(name,loadaddr); |
| 38 | |
| 39 | prghdr = (struct prg_hdr *)loadaddr; |
| 40 | |
| 41 | /* relocate it */ |
| 42 | relocate(prghdr->relocatedata,loadaddr); |
| 43 | |
| 44 | ((void (*)(char *))prghdr->initfunc)(arg); |
| 45 | |
| 46 | return LOADER_OK; |
| 47 | } |
| 48 | |
| 49 | struct dsc *loader_arch_load_dsc(const char *name) |
| 50 | { |
| 51 | char *loadaddr; |
| 52 | struct dsc_hdr *dschdr; |
| 53 | int length; |
| 54 | |
| 55 | /* get length of file */ |
| 56 | length = get_file_length(name); |
| 57 | if (length==0) |
| 58 | return NULL; |
| 59 | |
| 60 | /* allocate memory */ |
| 61 | loadaddr = malloc(length); |
| 62 | if (loadaddr==NULL) |
| 63 | return NULL; |
| 64 | |
| 65 | /* load the file */ |
| 66 | load_file(name, loadaddr); |
| 67 | |
| 68 | dschdr = (struct dsc_hdr *)loadaddr; |
| 69 | /* relocate it */ |
| 70 | relocate(dschdr->relocatedata, loadaddr); |
| 71 | |
| 72 | return &dschdr->dscdata; |
| 73 | } |
| 74 | |
| 75 | void loader_arch_free(void *loadaddr) |
| 76 | { |
| 77 | /* free module */ |
| 78 | /* we're given the start of 'arch' member of the prg_hdr, |
| 79 | calculate the real start address and then free the block */ |
| 80 | void *header = (void *)((char *)loadaddr - offsetof(struct prg_hdr,arch)); |
| 81 | free(header); |
| 82 | } |
| 83 | |
| 84 | void loader_arch_free_dsc(struct dsc *dscdata) |
| 85 | { |
| 86 | /* we're given the start of 'dsc' member of the dsc_hdr, |
| 87 | calculate the real start address and then free the block */ |
| 88 | void *header = (void *)((char *)dscdata - 2); |
| 89 | //offsetof(struct |
| 90 | //dsc_hdr,dscdata)); |
| 91 | free(header); |
| 92 | } |
| 93 | |