@@ -140,8 +140,25 @@ struct range {
140140 to = other.clamp (to);
141141 }
142142
143+ bool intersects (const range& other) const {
144+ return !(other.from >= to || other.to < from);
145+ }
146+
143147};
144148
149+ static void __noreturn fail(int code, string msg) {
150+ throw command_failure (code, std::move (msg));
151+ }
152+
153+ static void __noreturn fail(int code, const char *format, ...) {
154+ va_list args;
155+ va_start (args, format);
156+ static char error_msg[512 ];
157+ vsnprintf (error_msg, sizeof (error_msg), format, args);
158+ va_end (args);
159+ fail (code, string (error_msg));
160+ }
161+
145162// ranges should not overlap
146163template <typename T> struct range_map {
147164 struct mapping {
@@ -150,19 +167,20 @@ template <typename T> struct range_map {
150167 const uint32_t max_offset;
151168 };
152169
153- void check_overlap (uint32_t p) {
154- auto f = m.lower_bound (p);
155- if (f != m.end ()) {
156- assert (p >= f->first );
157- assert (p < f->second .first );
158- }
159- }
160-
161170 void insert (const range& r, T t) {
162171 if (r.to != r.from ) {
163172 assert (r.to > r.from );
164- check_overlap (r.from );
165- check_overlap (r.to );
173+ // check we don't overlap any existing map entries
174+
175+ auto f = m.lower_bound (r.from ); // first element that starts after r.from
176+ if (f != m.begin ()) f--; // back up, to catch element that starts on or before r.from
177+ for (; f != m.end () && f->first <= r.from ; f++) {
178+ range r2 (f->first , f->second .first );
179+ if (r2.intersects (r)) {
180+ fail (ERROR_FORMAT, " Found overlapping memory ranges 0x%08x->0x%08x and 0x%08x->%08x\n " ,
181+ r.from , r.to , r2.from , r2.to );
182+ }
183+ }
166184 m.insert (std::make_pair (r.from , std::make_pair (r.to , t)));
167185 }
168186 }
@@ -695,10 +713,6 @@ struct memory_access {
695713 }
696714};
697715
698- static void __noreturn fail(int code, string msg) {
699- throw command_failure (code, msg);
700- }
701-
702716uint32_t bootrom_func_lookup (memory_access& access, uint16_t tag) {
703717 auto magic = access.read_int (BOOTROM_MAGIC_ADDR);
704718 magic &= 0xffffff ; // ignore bootrom version
@@ -891,16 +905,6 @@ static void read_and_check_elf32_header(FILE *in, elf32_header& eh_out) {
891905 }
892906}
893907
894- static char error_msg[512 ];
895-
896- static void __noreturn fail(int code, const char *format, ...) {
897- va_list args;
898- va_start (args, format);
899- vsnprintf (error_msg, sizeof (error_msg), format, args);
900- va_end (args);
901- fail (code, string (error_msg));
902- }
903-
904908static void __noreturn fail_read_error() {
905909 fail (ERROR_READ_FAILED, " Failed to read input file" );
906910}
0 commit comments