Changeset 58648 in vbox for trunk/src/VBox/ValidationKit/bootsectors
- Timestamp:
- Nov 10, 2015 3:01:59 PM (9 years ago)
- Location:
- trunk/src/VBox/ValidationKit/bootsectors/bs3kit
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk
r58632 r58648 40 40 VBoxBs3Linker_TEMPLATE = VBoxBldProg 41 41 VBoxBs3Linker_SOURCES = $(VBOX_PATH_BS3KIT_SRC)/VBoxBs3Linker.cpp 42 43 # 64-bit relocation conversion tool 44 BLDPROGS += VBoxBs3ObjConverter 45 VBoxBs3ObjConverter_TEMPLATE = VBoxBldProg 46 VBoxBs3ObjConverter_SOURCES = $(VBOX_PATH_BS3KIT_SRC)/VBoxBs3ObjConverter.cpp 42 47 43 48 # Dummy CP "linker" tool. -
trunk/src/VBox/ValidationKit/bootsectors/bs3kit/VBoxBs3ObjConverter.cpp
r58637 r58648 36 36 #include <iprt/assert.h> 37 37 38 #include <iprt/formats/elf64.h> 39 38 40 39 41 /********************************************************************************************************************************* … … 60 62 if (!pFile) 61 63 fprintf(stderr, "error: Failed to open '%s' for %s: %s (%d)\n", 62 fWrite ? "writing" : "reading", strerror(errno), errno);64 pszFile, fWrite ? "writing" : "reading", strerror(errno), errno); 63 65 return pFile; 64 66 } 65 67 66 68 67 static int readfile(const char *pszFile, void **ppvFile, size_t *pcbFile) 68 { 69 70 } 71 72 73 static int writefile(const char *pszFile, void *pvFile, size_t cbFile) 74 { 75 69 /** 70 * Read the given file into memory. 71 * 72 * @returns true on success, false on failure. 73 * @param pszFile The file to read. 74 * @param ppvFile Where to return the memory. 75 * @param pcbFile Where to return the size. 76 */ 77 static bool readfile(const char *pszFile, void **ppvFile, size_t *pcbFile) 78 { 79 FILE *pFile = openfile(pszFile, false); 80 if (pFile) 81 { 82 /* 83 * Figure the size. 84 */ 85 if (fseek(pFile, SEEK_END, 0) == 0) 86 { 87 long cbFile = ftell(pFile); 88 if (cbFile > 0) 89 { 90 if (fseek(pFile, SEEK_SET, 0) == 0) 91 { 92 /* 93 * Allocate and read content. 94 */ 95 void *pvFile = malloc((size_t)cbFile); 96 if (pvFile) 97 { 98 if (fread(pvFile, cbFile, 1, pFile) == 1) 99 { 100 *ppvFile = pvFile; 101 *pcbFile = (size_t)cbFile; 102 fclose(pFile); 103 return true; 104 } 105 free(pvFile); 106 fprintf(stderr, "error: fread failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno); 107 } 108 else 109 fprintf(stderr, "error: failed to allocate %ld bytes of memory for '%s'\n", cbFile, pszFile); 110 } 111 else 112 fprintf(stderr, "error: fseek #2 failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno); 113 } 114 else 115 fprintf(stderr, "error: ftell failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno); 116 } 117 else 118 fprintf(stderr, "error: fseek #1 failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno); 119 fclose(pFile); 120 } 121 return false; 122 } 123 124 125 /** 126 * Write the given file into memory. 127 * 128 * @returns true on success, false on failure. 129 * @param pszFile The file to write. 130 * @param pvFile Where to return the memory. 131 * @param cbFile Where to return the size. 132 */ 133 static bool writefile(const char *pszFile, void const *pvFile, size_t cbFile) 134 { 135 remove(pszFile); 136 137 int rc = -1; 138 FILE *pFile = openfile(pszFile, true); 139 if (pFile) 140 { 141 if (fwrite(pvFile, cbFile, 1, pFile) == 1) 142 { 143 fclose(pFile); 144 return true; 145 } 146 fprintf(stderr, "error: fwrite failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno); 147 fclose(pFile); 148 } 149 return false; 150 } 151 152 153 /** 154 * Reports an error and returns false. 155 * 156 * @returns false 157 * @param pszFile The filename. 158 * @param pszFormat The message format string. 159 * @param ... Format arguments. 160 */ 161 static bool error(const char *pszFile, const char *pszFormat, ...) 162 { 163 fprintf(stderr, "error: %s: ", pszFile); 164 va_list va; 165 va_start(va, pszFormat); 166 vfprintf(stderr, pszFormat, va); 167 va_end(va); 168 return false; 169 } 170 171 172 static bool convertelf(const char *pszFile, uint8_t *pbFile, size_t cbFile) 173 { 174 /* 175 * Validate the header. 176 */ 177 Elf64_Ehdr const *pEhdr = (Elf64_Ehdr const *)pbFile; 178 if ( pEhdr->e_ident[EI_CLASS] != ELFCLASS64 179 || pEhdr->e_ident[EI_DATA] != ELFDATA2LSB 180 || pEhdr->e_ehsize != sizeof(Elf64_Ehdr) 181 || pEhdr->e_shentsize != sizeof(Elf64_Shdr) 182 || pEhdr->e_version != EV_CURRENT ) 183 return error(pszFile, "Unsupported ELF config\n"); 184 if (pEhdr->e_type != ET_REL) 185 return error(pszFile, "Expected relocatable ELF file (e_type=%d)\n", pEhdr->e_type); 186 if (pEhdr->e_machine != EM_X86_64) 187 return error(pszFile, "Expected relocatable ELF file (e_type=%d)\n", pEhdr->e_machine); 188 189 #if 0 190 if ( pEhdr->e_phoff < pEhdr->e_ehsize 191 && !(pEhdr->e_phoff && pEhdr->e_phnum) 192 && pEhdr->e_phnum) 193 { 194 Log(("RTLdrELF: %s: The program headers overlap with the ELF header! e_phoff=" FMT_ELF_OFF "\n", 195 pszLogName, pEhdr->e_phoff)); 196 return VERR_BAD_EXE_FORMAT; 197 } 198 if ( pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize > cbRawImage 199 || pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize < pEhdr->e_phoff) 200 { 201 Log(("RTLdrELF: %s: The program headers extends beyond the file! e_phoff=" FMT_ELF_OFF " e_phnum=" FMT_ELF_HALF "\n", 202 pszLogName, pEhdr->e_phoff, pEhdr->e_phnum)); 203 return VERR_BAD_EXE_FORMAT; 204 } 205 206 207 if ( pEhdr->e_shoff < pEhdr->e_ehsize 208 && !(pEhdr->e_shoff && pEhdr->e_shnum)) 209 { 210 Log(("RTLdrELF: %s: The section headers overlap with the ELF header! e_shoff=" FMT_ELF_OFF "\n", 211 pszLogName, pEhdr->e_shoff)); 212 return VERR_BAD_EXE_FORMAT; 213 } 214 if ( pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize > cbRawImage 215 || pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize < pEhdr->e_shoff) 216 { 217 Log(("RTLdrELF: %s: The section headers extends beyond the file! e_shoff=" FMT_ELF_OFF " e_shnum=" FMT_ELF_HALF "\n", 218 pszLogName, pEhdr->e_shoff, pEhdr->e_shnum)); 219 return VERR_BAD_EXE_FORMAT; 220 } 221 222 if (pEhdr->e_shstrndx == 0 || pEhdr->e_shstrndx > pEhdr->e_shnum) 223 { 224 Log(("RTLdrELF: %s: The section headers string table is out of bounds! e_shstrndx=" FMT_ELF_HALF " e_shnum=" FMT_ELF_HALF "\n", 225 pszLogName, pEhdr->e_shstrndx, pEhdr->e_shnum)); 226 return VERR_BAD_EXE_FORMAT; 227 } 228 #endif 229 230 return true; 231 } 232 233 234 /** 235 * Does the convertion using convertelf and convertcoff. 236 * 237 * @returns exit code (0 on success, non-zero on failure) 238 * @param pszFile The file to convert. 239 */ 240 static int convertit(const char *pszFile) 241 { 242 void *pvFile; 243 size_t cbFile; 244 if (readfile(pszFile, &pvFile, &cbFile)) 245 { 246 bool fRc = false; 247 uint8_t *pbFile = (uint8_t *)pvFile; 248 if ( cbFile > sizeof(Elf64_Ehdr) 249 && pbFile[0] == ELFMAG0 250 && pbFile[1] == ELFMAG1 251 && pbFile[2] == ELFMAG2 252 && pbFile[3] == ELFMAG3) 253 fRc = convertelf(pszFile, pbFile, cbFile); 254 else 255 fprintf(stderr, "error: Don't recognize format of '%s'\n", pszFile); 256 if (fRc) 257 fRc = writefile(pszFile, pvFile, cbFile); 258 free(pvFile); 259 if (fRc) 260 return 0; 261 } 262 return 1; 76 263 } 77 264 … … 94 281 pszOpt--; 95 282 if (!strcmp(pszOpt, "--verbose")) 96 pszOpt = 'v';283 pszOpt = "v"; 97 284 else if (!strcmp(pszOpt, "--version")) 98 285 pszOpt = "V"; … … 102 289 { 103 290 fprintf(stderr, "syntax errro: Unknown options '%s'\n", pszOpt); 104 free(paInputs);105 291 return 2; 106 292 } … … 133 319 * File to convert. Do the job right away. 134 320 */ 321 rcExit = convertit(argv[i]); 322 if (rcExit != 0) 323 break; 135 324 } 136 325 } -
trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-docs.c
r58612 r58648 41 41 * in separate files and compiled/assembled into libraries, so the linker will 42 42 * only include exactly what is needed. The current linker is the OpenWatcom 43 * one, wlink, that we're already using when building the BIOSes. It's possible 44 * that we might be able to convince the binutils ELF linker to do the job 45 * eventually, 43 * one, wlink, that we're already using when building the BIOSes. If it wasn't 44 * for the segment/selector fixups in 16-bit code (mostly), maybe we could 45 * convince the ELF linker from GNU binutils to do the job too (with help from 46 * the ). 46 47 * 47 48 *
Note:
See TracChangeset
for help on using the changeset viewer.