comparison tools/objlink.c @ 2564:2cf4e995b50c

Improve error checking.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 28 Feb 2022 10:18:38 +0200
parents 8dd81ec802f4
children d56a0e86067a
comparison
equal deleted inserted replaced
2563:8dd81ec802f4 2564:2cf4e995b50c
820 memPrintBox(fh, bsize, bcurr->start, bcurr->end, bdesc); 820 memPrintBox(fh, bsize, bcurr->start, bcurr->end, bdesc);
821 } 821 }
822 } 822 }
823 823
824 824
825 int dmAddOverlap(DMMemBlockOverlap *olp, const int index)
826 {
827 if (olp->noverlaps < SET_MAX_FILENAMES)
828 {
829 olp->overlaps[olp->noverlaps++] = index;
830 return DMERR_OK;
831 }
832 else
833 {
834 return dmError(DMERR_BOUNDS,
835 "Too many memory block overlaps (%d >= %d).\n",
836 olp->noverlaps, SET_MAX_FILENAMES);
837 }
838 }
839
825 /* 840 /*
826 * The main program 841 * The main program
827 */ 842 */
828 int main(int argc, char *argv[]) 843 int main(int argc, char *argv[])
829 { 844 {
853 dmMsg(1, "Using memory model #%d '%s', %d bytes.\n", 868 dmMsg(1, "Using memory model #%d '%s', %d bytes.\n",
854 optMemModel, memModel->name, memModel->size); 869 optMemModel, memModel->name, memModel->size);
855 870
856 if ((memory = (Uint8 *) dmMalloc(memModel->size + 32)) == NULL) 871 if ((memory = (Uint8 *) dmMalloc(memModel->size + 32)) == NULL)
857 { 872 {
858 dmErrorMsg("Could not allocate memory for memory model!\n"); 873 res = dmError(DMERR_MALLOC,
874 "Could not allocate memory for memory model!\n");
859 goto out; 875 goto out;
860 } 876 }
861 877
862 // Initialize memory 878 // Initialize memory
863 dmMsg(1, "Initializing memory with "); 879 dmMsg(1, "Initializing memory with ");
953 { 969 {
954 dmPrint(-1, "* '%s' and '%s' overlap ($%.4x-$%.4x vs $%.4x-$%.4x)\n", 970 dmPrint(-1, "* '%s' and '%s' overlap ($%.4x-$%.4x vs $%.4x-$%.4x)\n",
955 mb1->name, mb2->name, mb1->start, 971 mb1->name, mb2->name, mb1->start,
956 mb1->end, mb2->start, mb2->end); 972 mb1->end, mb2->start, mb2->end);
957 973
958 olp1->overlaps[olp1->noverlaps++] = bk2; 974 if ((res = dmAddOverlap(olp1, bk2)) != DMERR_OK ||
959 olp2->overlaps[olp2->noverlaps++] = bk1; 975 (res = dmAddOverlap(olp2, bk1)) != DMERR_OK)
976 goto out;
977
960 hasOverlaps = TRUE; 978 hasOverlaps = TRUE;
961 } 979 }
962 } 980 }
963 981
964 if (!optAllowOverlap && hasOverlaps) 982 if (!optAllowOverlap && hasOverlaps)
965 { 983 {
966 dmErrorMsg("Error occured, overlaps not allowed.\n"); 984 res = dmError(DMERR_BOUNDS,
985 "Error occured, overlaps not allowed.\n");
967 goto out; 986 goto out;
968 } 987 }
969 988
970 // Find out start and end-addresses 989 // Find out start and end-addresses
971 startAddr = memModel->size; 990 startAddr = memModel->size;
985 } 1004 }
986 } 1005 }
987 1006
988 if (startAddr >= memModel->size || endAddr < startAddr) 1007 if (startAddr >= memModel->size || endAddr < startAddr)
989 { 1008 {
990 dmErrorMsg("Invalid saveblock addresses (start=$%.4x, end=$%.4x)!\n", 1009 res = dmError(DMERR_BOUNDS,
1010 "Invalid saveblock addresses (start=$%.4x, end=$%.4x)!\n",
991 startAddr, endAddr); 1011 startAddr, endAddr);
992 goto out; 1012 goto out;
993 } 1013 }
994 1014
995 // Output linkfile 1015 // Output linkfile