comparison tools/objlink.c @ 2554:aabfa00eafd9

Improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 27 Feb 2022 16:37:14 +0200
parents 5c714feeb3a7
children 6249aa494e83
comparison
equal deleted inserted replaced
2553:5c714feeb3a7 2554:aabfa00eafd9
217 } 217 }
218 218
219 219
220 /* Memory block handling 220 /* Memory block handling
221 */ 221 */
222 BOOL dmReserveMemBlock(int startAddr, int endAddr, const char *blockName, int blockType) 222 int dmReserveMemBlock(int startAddr, int endAddr, const char *blockName, int blockType)
223 { 223 {
224 if (startAddr > endAddr) 224 if (startAddr > endAddr)
225 { 225 {
226 dmErrorMsg("ERROR! Block '%s' has startAddr=$%.4x > endAddr=$%.4x!\n", 226 return dmError(DMERR_INVALID_ARGS,
227 "ERROR! Block '%s' has startAddr=$%.4x > endAddr=$%.4x!\n",
227 blockName, startAddr, endAddr); 228 blockName, startAddr, endAddr);
228 return FALSE;
229 } 229 }
230 230
231 if (nmemBlocks < SET_MAX_FILENAMES) 231 if (nmemBlocks < SET_MAX_FILENAMES)
232 { 232 {
233 memBlocks[nmemBlocks].start = startAddr; 233 memBlocks[nmemBlocks].start = startAddr;
234 memBlocks[nmemBlocks].end = endAddr; 234 memBlocks[nmemBlocks].end = endAddr;
235 memBlocks[nmemBlocks].name = dm_strdup(blockName); 235 memBlocks[nmemBlocks].name = dm_strdup(blockName);
236 memBlocks[nmemBlocks].type = blockType; 236 memBlocks[nmemBlocks].type = blockType;
237 nmemBlocks++; 237 nmemBlocks++;
238 return TRUE; 238 return DMERR_OK;
239 } 239 }
240 else 240 else
241 { 241 {
242 dmErrorMsg("Maximum number of memBlock definitions (%d) exceeded!\n", 242 return dmError(DMERR_BOUNDS,
243 "Maximum number of memBlock definitions (%d) exceeded!\n",
243 SET_MAX_FILENAMES); 244 SET_MAX_FILENAMES);
244 return FALSE;
245 } 245 }
246 } 246 }
247 247
248 248
249 int dmCompareMemBlock(const void *cva, const void *cvb) 249 int dmCompareMemBlock(const void *cva, const void *cvb)
411 // Allocate memory block 411 // Allocate memory block
412 sectLen = sectEnd - sectStart + 1; 412 sectLen = sectEnd - sectStart + 1;
413 dmMsg(1, "Reserve $%.4x - $%.4x ($%x, %d bytes) as '%s'\n", 413 dmMsg(1, "Reserve $%.4x - $%.4x ($%x, %d bytes) as '%s'\n",
414 sectStart, sectEnd, sectLen, sectLen, sectName); 414 sectStart, sectEnd, sectLen, sectLen, sectName);
415 415
416 if (!dmReserveMemBlock(sectStart, sectEnd, sectName, MTYPE_RES)) 416 if (dmReserveMemBlock(sectStart, sectEnd, sectName, MTYPE_RES) != DMERR_OK)
417 return FALSE; 417 return FALSE;
418 } 418 }
419 break; 419 break;
420 420
421 case 16: 421 case 16:
606 } 606 }
607 607
608 dmPrint(1, " .. OK\n"); 608 dmPrint(1, " .. OK\n");
609 609
610 // Add to list of blocks 610 // Add to list of blocks
611 if (!dmReserveMemBlock(loadAddr, endAddr, filename, MTYPE_RES)) 611 if ((res = dmReserveMemBlock(loadAddr, endAddr, filename, MTYPE_RES)) != DMERR_OK)
612 res = DMERR_MALLOC; 612 goto out;
613 613
614 out: 614 out:
615 if (fh != NULL) 615 if (fh != NULL)
616 fclose(fh); 616 fclose(fh);
617 617
663 } 663 }
664 664
665 dmPrint(1, " .. OK\n"); 665 dmPrint(1, " .. OK\n");
666 666
667 // Add info to list 667 // Add info to list
668 if (!dmReserveMemBlock(destAddr, endAddr, filename, MTYPE_RES)) 668 if ((res = dmReserveMemBlock(destAddr, endAddr, filename, MTYPE_RES)) != DMERR_OK)
669 res = DMERR_MALLOC; 669 goto out;
670 670
671 out: 671 out:
672 if (fh != NULL) 672 if (fh != NULL)
673 fclose(fh); 673 fclose(fh);
674 674
682 int blockSize; 682 int blockSize;
683 683
684 blockSize = (blockEnd - blockStart + 1); 684 blockSize = (blockEnd - blockStart + 1);
685 685
686 // Create label name from filename 686 // Create label name from filename
687 tmpStr = dm_strdup(blockName); 687 if ((tmpStr = dm_strdup(blockName)) == NULL)
688 if (tmpStr == NULL) 688 {
689 { 689 return dmError(DMERR_MALLOC,
690 dmErrorMsg("Could not allocate memory for string '%s'!\n", 690 "Could not allocate memory for string '%s'!\n",
691 blockName); 691 blockName);
692 return -1;
693 } 692 }
694 693
695 if ((t = strrchr(tmpStr, '/'))) 694 if ((t = strrchr(tmpStr, '/')))
696 s = (t + 1); 695 s = (t + 1);
697 else 696 else
727 fprintf(dfile, "%s = $%.4x\n", s, blockStart); 726 fprintf(dfile, "%s = $%.4x\n", s, blockStart);
728 break; 727 break;
729 } 728 }
730 729
731 dmFree(tmpStr); 730 dmFree(tmpStr);
732 return 0; 731 return DMERR_OK;
733 } 732 }
734 733
735 734
736 /* Print out an ASCII presentation of memory map 735 /* Print out an ASCII presentation of memory map
737 */ 736 */
817 */ 816 */
818 int main(int argc, char *argv[]) 817 int main(int argc, char *argv[])
819 { 818 {
820 FILE *outFile = NULL; 819 FILE *outFile = NULL;
821 BOOL hasOverlaps; 820 BOOL hasOverlaps;
822 int startAddr, endAddr, dataSize, totalSize; 821 int res = DMERR_OK, startAddr, endAddr, dataSize, totalSize;
823 822
824 dmInitProg("objlink", "Simple file-linker", "0.82", NULL, NULL); 823 dmInitProg("objlink", "Simple file-linker", "0.82", NULL, NULL);
825 824
826 // Parse arguments 825 // Parse arguments
827 if (!dmArgsProcess(argc, argv, optList, optListN, 826 if (!dmArgsProcess(argc, argv, optList, optListN,
896 895
897 // Add memory model blocks 896 // Add memory model blocks
898 dmMsg(1, "Applying memory model restrictions...\n"); 897 dmMsg(1, "Applying memory model restrictions...\n");
899 for (int i = 0; i < memModel->nmemBlocks; i++) 898 for (int i = 0; i < memModel->nmemBlocks; i++)
900 { 899 {
901 if (!dmReserveMemBlock( 900 if ((res = dmReserveMemBlock(
902 memModel->memBlocks[i].start, 901 memModel->memBlocks[i].start,
903 memModel->memBlocks[i].end, 902 memModel->memBlocks[i].end,
904 memModel->memBlocks[i].name, 903 memModel->memBlocks[i].name,
905 memModel->memBlocks[i].type)) 904 memModel->memBlocks[i].type)) != DMERR_OK)
906 goto out; 905 goto out;
907 } 906 }
908 907
909 // Sort the blocks 908 // Sort the blocks
910 qsort(memBlocks, nmemBlocks, sizeof(DMMemBlock), dmCompareMemBlock); 909 qsort(memBlocks, nmemBlocks, sizeof(DMMemBlock), dmCompareMemBlock);
964 if (optLinkFileName) 963 if (optLinkFileName)
965 { 964 {
966 dmMsg(1, "Writing linkfile to '%s'\n", optLinkFileName); 965 dmMsg(1, "Writing linkfile to '%s'\n", optLinkFileName);
967 if ((outFile = fopen(optLinkFileName, "wb")) == NULL) 966 if ((outFile = fopen(optLinkFileName, "wb")) == NULL)
968 { 967 {
969 int err = dmGetErrno(); 968 res = dmGetErrno();
970 dmErrorMsg("Error creating file '%s' #%d: %s.\n", 969 dmErrorMsg("Error creating file '%s': %s.\n",
971 optLinkFileName, err, dmErrorStr(err)); 970 optLinkFileName, dmErrorStr(res));
972 goto out; 971 goto out;
973 } 972 }
974 973
975 switch (optLinkFileFormat) 974 switch (optLinkFileFormat)
976 { 975 {
1015 outFile = stdout; 1014 outFile = stdout;
1016 dmPrint(1, "...\n"); 1015 dmPrint(1, "...\n");
1017 } 1016 }
1018 else if ((outFile = fopen(optDestName, "wb")) == NULL) 1017 else if ((outFile = fopen(optDestName, "wb")) == NULL)
1019 { 1018 {
1020 int err = dmGetErrno(); 1019 res = dmGetErrno();
1021 dmErrorMsg("Error creating output file '%s' #%d: %s.\n", 1020 dmErrorMsg("Error creating output file '%s': %s.\n",
1022 optDestName, err, dmErrorStr(err)); 1021 optDestName, dmErrorStr(res));
1023 goto out; 1022 goto out;
1024 } 1023 }
1025 else 1024 else
1026 dmPrint(1, "to '%s'\n", optDestName); 1025 dmPrint(1, "to '%s'\n", optDestName);
1027 1026
1043 } 1042 }
1044 1043
1045 // Save the data 1044 // Save the data
1046 if (fwrite(&memory[startAddr], dataSize, 1, outFile) < 1) 1045 if (fwrite(&memory[startAddr], dataSize, 1, outFile) < 1)
1047 { 1046 {
1048 int err = dmGetErrno(); 1047 res = dmGetErrno();
1049 dmErrorMsg("Error writing to file #%d: %s.\n", 1048 dmErrorMsg(
1050 err, dmErrorStr(err)); 1049 "Error writing to file: %s.\n",
1050 dmErrorStr(res));
1051 goto out; 1051 goto out;
1052 } 1052 }
1053 1053
1054 // Describe the memory layout 1054 // Describe the memory layout
1055 if (optDescribe) 1055 if (optDescribe)
1057 1057
1058 out: 1058 out:
1059 if (outFile != NULL && outFile != stdout) 1059 if (outFile != NULL && outFile != stdout)
1060 fclose(outFile); 1060 fclose(outFile);
1061 1061
1062 return 0; 1062 return res;
1063 } 1063 }