1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the Free
10 // Software Foundation; either version 2 of the License, or (at your option)
13 // This program is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc.,
20 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 //=================================================================
29 // Memory Layout Tool map data structure manipulation class
31 //=================================================================
32 //#####DESCRIPTIONBEGIN####
34 // Author(s): John Dallaway
36 // Date: 1998/07/29 $RcsDate$ {or whatever}
37 // Version: 0.00+ $RcsVersion$ {or whatever}
38 // Purpose: Provides functions to create and destroy memory regions
39 // and sections within the memory map.
40 // Description: Each function manipulates data structures representing
41 // memory regions, memory sections and the view of memory
42 // sections as presented to the user. The section view
43 // structure organises the sections by region and
44 // will contain two instances of each relocated section
46 // Provides: create_memory_region()
47 // delete_memory_region()
48 // edit_memory_region()
49 // create_memory_section()
50 // delete_memory_section()
51 // edit_memory_section()
52 // delete_all_memory_sections()
57 // Known bugs: <UPDATE_ME_AT_RELEASE_TIME>
58 // WARNING: Do not modify data structures other than by using the
60 // Usage: #include "memmap.h"
62 // status = set_map_size (0x8000);
64 //####DESCRIPTIONEND####
66 #pragma warning (disable:4514) /* unreferenced inline function */
67 #pragma warning (disable:4710) /* function not inlined */
69 // Includes other headers for precompiled compilation
80 //////////////////////////////////////////////////////////////////////
81 // Construction/Destruction
82 //////////////////////////////////////////////////////////////////////
86 map_modified_flag = true;
87 map_size = (mem_address) 0;
95 mem_section::mem_section()
100 mem_section::~mem_section()
105 ///////////////////////////////////////////////////////////////////////
106 // get_memory_region() retrieves the parameters of a memory region
108 bool mem_map::get_memory_region (std::string region_name, mem_address * region_address, mem_address * region_size, mem_type * region_type, std::string * note)
110 for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end (); ++region)
111 if (region->name == region_name)
113 *region_address = region->address;
114 *region_size = region->size;
115 *region_type = region->type;
116 *note = region->note;
124 ///////////////////////////////////////////////////////////////////////
125 // create_memory_region() inserts a new item into the memory region list
126 // in order of memory address
128 int mem_map::create_memory_region (std::string new_region_name, mem_address new_region_address, mem_address new_region_size, mem_type new_region_type, std::string note)
130 const mem_address new_region_end = new_region_address + new_region_size; // the byte after the new region end
132 // check that the new region name is specified
134 if (new_region_name == "")
135 return ERR_MEMMAP_REGION_NONAME; // the new region name must be specified
137 // check that the new region lies within the memory map
139 if (new_region_end > map_size)
140 return ERR_MEMMAP_REGION_MAPSIZE; // the new region does not lie within the memory map
142 // check that the region end address hasn't exceeded the storage size
144 if (new_region_end < new_region_address)
145 return ERR_MEMMAP_REGION_MAPSIZE; // the new region does not lie within the memory map
147 // initialise the insertion point for the new region
149 list <mem_region>::iterator insertion_point = region_list.end ();
151 // check that the new region does not overlap existing regions and does not already exist
153 for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end (); ++region)
155 const mem_address region_end = region->address + region->size; // the byte after the region end
157 if ((new_region_address >= region->address) && (new_region_address < region_end))
159 error_info = region->name;
160 return ERR_MEMMAP_REGION_INTERSECT; // the start of the new region is within an existing region
163 if ((new_region_end > region->address) && (new_region_end <= region_end))
165 error_info = region->name;
166 return ERR_MEMMAP_REGION_INTERSECT; // the end of the new region is within an existing region
169 if ((new_region_address < region->address) && (new_region_end > region_end))
171 error_info = region->name;
172 return ERR_MEMMAP_REGION_INTERSECT; // an existing region lies within the new region
175 if (region->name == new_region_name)
176 return ERR_MEMMAP_REGION_NAMEINUSE; // the new region name is not unique
178 if ((insertion_point == region_list.end ()) && (region->address > new_region_address))
179 insertion_point = region; // insert the new region here
182 // add the new region to the region list
184 list <mem_region>::iterator new_region = region_list.insert (insertion_point);
185 new_region->name = new_region_name;
186 new_region->address = new_region_address;
187 new_region->size = new_region_size;
188 new_region->type = new_region_type;
189 new_region->note = note;
191 // initialise the section list for the new region
193 calc_section_list (new_region);
195 map_modified_flag = true;
200 ///////////////////////////////////////////////////////////////////////
201 // edit_memory_region() edits an item in the memory region list
203 int mem_map::edit_memory_region (std::string old_region_name, std::string new_region_name, mem_address new_region_address, mem_address new_region_size, mem_type new_region_type, std::string note)
205 list <mem_region>::iterator edit_region = find_memory_region (old_region_name);
206 if (edit_region == NULL)
207 return ERR_MEMMAP_REGION_NOTFOUND; // the region to be modified does not exist
209 // check that the new region name is specified
211 if (new_region_name == "")
212 return ERR_MEMMAP_REGION_NONAME; // the new region name must be specified
214 // check that the region end address hasn't exceeded the storage size
216 if (new_region_address + new_region_size < new_region_address)
217 return ERR_MEMMAP_REGION_MAPSIZE; // the new region does not lie within the memory map
219 // check region name change
221 if ((old_region_name != new_region_name) &&
222 (find_memory_region (new_region_name) != NULL))
223 return ERR_MEMMAP_REGION_NAMEINUSE; // new region name is not unique
225 // check region address/size change wrt other regions
227 const mem_address new_region_end = new_region_address + new_region_size;
228 if ((new_region_address != edit_region->address) ||
229 (new_region_size != edit_region->size))
231 for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end (); ++region)
232 if (region != edit_region)
234 const mem_address region_end = region->address + region->size; // the byte after the region end
236 if ((new_region_address >= region->address) && (new_region_address < region_end))
238 error_info = region->name;
239 return ERR_MEMMAP_REGION_INTERSECT; // the start of the modified region is within another region
242 if ((new_region_end > region->address) && (new_region_end <= region_end))
244 error_info = region->name;
245 return ERR_MEMMAP_REGION_INTERSECT; // the end of the modified region is within an existing region
248 if ((new_region_address < region->address) && (new_region_end > region_end))
250 error_info = region->name;
251 return ERR_MEMMAP_REGION_INTERSECT; // another region lies within the modified region
256 // check region size change wrt sections within region (if any)
258 for (list <mem_section_view>::iterator section_view = edit_region->section_view_list.begin (); section_view != edit_region->section_view_list.end (); ++section_view)
259 if (section_view->section != NULL)
261 if ((section_view->section_location == final_location) || (section_view->section_location == fixed_location))
262 if (section_view->section->final_location->anchor == absolute)
263 if (section_view->section->final_location->address + section_view->section->size - edit_region->address > new_region_size)
264 return ERR_MEMMAP_REGION_SIZE; // region is now too small
266 if (section_view->section_location == initial_location)
267 if (section_view->section->initial_location->anchor == absolute)
268 if (section_view->section->initial_location->address + section_view->section->size - edit_region->address > new_region_size)
269 return ERR_MEMMAP_REGION_SIZE; // region is now too small
272 // check region read-only change FIXME
274 // move sections within the region having absolute anchors
276 for (section_view = edit_region->section_view_list.begin (); section_view != edit_region->section_view_list.end (); ++section_view)
277 if (section_view->section != NULL)
279 if ((section_view->section_location == final_location) || (section_view->section_location == fixed_location))
280 if (section_view->section->final_location->anchor == absolute)
281 section_view->section->final_location->address += (new_region_address - edit_region->address);
283 if ((section_view->section_location == initial_location) || (section_view->section_location == fixed_location))
284 if (section_view->section->initial_location->anchor == absolute)
285 section_view->section->initial_location->address += (new_region_address - edit_region->address);
288 // deleteZ(the region and recreate it to make sure the region list is ordered correctly)
290 region_list.erase (edit_region);
291 if (create_memory_region (new_region_name, new_region_address, new_region_size, new_region_type, note))
292 return ERR_MEMMAP_ALLOC;
294 map_modified_flag = true;
299 //////////////////////////////////////////////////////////////////
300 // delete_memory_region() removes an existing item from the memory
303 bool mem_map::delete_memory_region (std::string name)
305 // make sure that there are no used sections in this region before deleting it
307 for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end (); ++region)
309 if ((region->name == name) && (region->section_view_list.size () == 1) && (region->section_view_list.front ().section == NULL))
311 region_list.erase (region);
312 map_modified_flag = true;
320 ///////////////////////////////////////////////////////////////////
321 // set_map_size() sets the maximum permitted address for the end
322 // of any memory region
324 bool mem_map::set_map_size (mem_address new_map_size)
326 // check that the new size is sufficient for all previously defined memory regions
328 for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end (); ++region)
330 if (region->address + region->size > new_map_size)
331 return false; // the new map size is too small
336 map_size = new_map_size;
342 ////////////////////////////////////////////////////////////////////
343 // edit_memory_section() edits an item to the memory section map
345 int mem_map::edit_memory_section (std::string old_section_name, std::string new_section_name, mem_address section_size, mem_address section_alignment, mem_anchor initial_section_anchor, std::string initial_anchor_section_name, mem_address initial_anchor_address, mem_anchor final_section_anchor, std::string final_anchor_section_name, mem_address final_anchor_address, bool relocates, bool anchor_to_initial_location, bool linker_defined, std::string note)
347 // do all the parameter validation
349 if (new_section_name == "") // the new section name must be specified
350 return ERR_MEMMAP_SECTION_NONAME;
352 if ((new_section_name != old_section_name) &&
353 (find_memory_section (new_section_name) != NULL))
354 return ERR_MEMMAP_SECTION_NAMEINUSE; // the new section name is not unique
356 list <mem_section>::iterator section = find_memory_section (old_section_name);
358 return ERR_MEMMAP_SECTION_NOTFOUND; // the specified old section name could not be found
360 // check that the LMA (if absolute) is within a memory region
362 list <mem_region>::iterator region;
363 if (initial_section_anchor == absolute)
365 region = find_region_by_address (initial_anchor_address);
367 return ERR_MEMMAP_SECTION_LMA_NOTINREGION; // section LMA is not within a memory region
368 if ((section_size > 0) && (initial_anchor_address + section_size > region->address + region->size))
369 return ERR_MEMMAP_SECTION_LMA_NOTINREGION; // end of section is not within the memory region
370 if (relocates && (region->type == read_write))
371 return ERR_MEMMAP_SECTION_LMA_READWRITE; // section LMA must be in a read-only memory region
374 // check that the VMA (if absolute) is within a memory region
376 if (final_section_anchor == absolute)
378 region = find_region_by_address (final_anchor_address);
380 return ERR_MEMMAP_SECTION_VMA_NOTINREGION; // section VMA is not within a memory region
381 if ((section_size > 0) && (final_anchor_address + section_size > region->address + region->size))
382 return ERR_MEMMAP_SECTION_VMA_NOTINREGION; // end of section is not within the memory region
383 if (relocates && (region->type == read_only))
384 return ERR_MEMMAP_SECTION_VMA_READONLY; // section VMA must be in a read/write memory region
387 // check relative location information as appropriate
389 if (relocates) // only check the initial parent section if the section relocates
391 if (initial_section_anchor == relative)
393 list <mem_section>::iterator parent_section = find_memory_section (initial_anchor_section_name);
394 if (parent_section == section_list.end ())
395 return ERR_MEMMAP_SECTION_LMA_ANCHORNOTFOUND; // initial anchor name not found
397 if ((parent_section->initial_location->following_section != section) && (parent_section->initial_location->following_section != NULL))
398 return ERR_MEMMAP_SECTION_LMA_ANCHORNOTAVAIL; // initial anchor specified has changed and is unavailable
400 if ((parent_section->size == 0) && (! parent_section->linker_defined))
401 return ERR_MEMMAP_SECTION_LMA_ANCHORNOTAVAIL; // initial anchor specified expands to fit available space
403 if (find_region_by_section (parent_section, initial_location)->type == read_write)
404 return ERR_MEMMAP_SECTION_LMA_READWRITE; // initial anchor must be in a read-only memory region
408 if (final_section_anchor == relative)
410 list <mem_section>::iterator parent_section = find_memory_section (final_anchor_section_name);
411 if (parent_section == NULL)
412 return ERR_MEMMAP_SECTION_VMA_ANCHORNOTFOUND; // final anchor name not found
414 if ((parent_section->size == 0) && (! parent_section->linker_defined))
415 return ERR_MEMMAP_SECTION_VMA_ANCHORNOTAVAIL; // final anchor specified expands to fit available space
417 if ((!relocates) && anchor_to_initial_location) // final anchor to initial location of parent section
419 if ((parent_section->initial_location->following_section != section) && (parent_section->initial_location->following_section != NULL))
420 return ERR_MEMMAP_SECTION_VMA_ANCHORNOTAVAIL; // final anchor specified has changed and is unavailable
424 if ((parent_section->final_location->following_section != section) && (parent_section->final_location->following_section != NULL))
425 return ERR_MEMMAP_SECTION_VMA_ANCHORNOTAVAIL; // final anchor specified has changed and is unavailable
428 if (relocates && (find_region_by_section (parent_section, final_location)->type == read_only))
429 return ERR_MEMMAP_SECTION_VMA_READONLY; // final anchor of relocating section must be in a read/write memory region
432 // check for a non-relocating section changing to relocating where the final
433 // location moves from a read_only region to a read_write region and there
434 // is a following non-relocating section
436 if (relocates && (! section->relocates) &&
437 (find_region_by_section (section, fixed_location)->type == read_only) &&
438 (section->final_location->following_section != NULL) &&
439 (! section->final_location->following_section->relocates))
441 return ERR_MEMMAP_SECTION_ILLEGAL_RELOCATION;
444 // FIXME check for overlap of absolute sections
446 // modify the initial section location data
448 if (section->initial_location->anchor == relative) // initial section anchor was relative
449 find_preceding_section (section, true)->initial_location->following_section = NULL;
451 if (initial_section_anchor == absolute) // initial location now absolute
452 section->initial_location->address = initial_anchor_address;
453 else // initial location now relative
455 list <mem_section>::iterator initial_parent = find_memory_section (initial_anchor_section_name);
456 if (relocates || (! initial_parent->relocates))
457 initial_parent->initial_location->following_section = section;
460 // modify the final section location data
462 if (section->final_location->anchor == relative) // final section anchor was relative
463 find_preceding_section (section, false)->final_location->following_section = NULL;
465 if (final_section_anchor == absolute) // final location now absolute
466 section->final_location->address = final_anchor_address;
467 else // final location now relative
469 list <mem_section>::iterator final_parent = find_memory_section (final_anchor_section_name);
470 final_parent->final_location->following_section = section;
473 // handle relocation changes
475 if (relocates && (! section->relocates)) // section was non-relocating but now relocates
477 if (find_region_by_section (section, fixed_location)->type == read_only) // the section was in a read_only region
478 section->final_location->following_section = NULL; // there is now no section following the final location
480 section->initial_location->following_section = NULL; // there is now no section following the initial location
483 else if ((! relocates) && section->relocates) // section was relocating but is now non-relocating
485 // determine the type of memory region in which the section now resides
488 if ((final_section_anchor == relative) && anchor_to_initial_location)
489 type = find_region_by_section (find_memory_section (final_anchor_section_name), initial_location)->type;
490 else if (final_section_anchor == relative) // anchored to final location of preceding section
491 type = find_region_by_section (find_memory_section (final_anchor_section_name), final_location)->type;
492 else // final_section_anchor must be absolute
493 type = find_region_by_address (final_anchor_address)->type;
495 if (type == read_only) // the section is now in a read-only memory region
497 if ((section->initial_location->following_section != NULL) && ! section->initial_location->following_section->relocates)
498 section->final_location->following_section = section->initial_location->following_section;
500 section->final_location->following_section = NULL;
502 else // the section is now in a read-write memory region
504 if ((section->final_location->following_section != NULL) && ! section->final_location->following_section->relocates)
505 section->initial_location->following_section = section->final_location->following_section;
507 section->initial_location->following_section = NULL;
511 // modify the remaining section data
513 section->name = new_section_name;
514 section->size = section_size;
515 section->alignment = section_alignment;
516 section->relocates = relocates;
517 section->note = note;
518 section->linker_defined = linker_defined;
519 section->initial_location->anchor = initial_section_anchor;
520 section->final_location->anchor = final_section_anchor;
522 // recalculate section lists for all regions
524 calc_section_lists ();
526 map_modified_flag = true;
531 ////////////////////////////////////////////////////////////////////
532 // create_memory_section() adds a new item to the memory section map
533 // either a section name (for relative locations) or an anchor address
534 // (for absolute locations) must be specified
536 int mem_map::create_memory_section (std::string section_name, mem_address section_size, mem_address section_alignment, mem_anchor initial_section_anchor, std::string initial_anchor_section_name, mem_address initial_anchor_address, mem_anchor final_section_anchor, std::string final_anchor_section_name, mem_address final_anchor_address, bool relocates, bool anchor_to_initial_location, bool linker_defined, std::string note)
538 list <mem_region>::iterator region;
540 // check that the new section name is specified
542 if (section_name == "")
543 return ERR_MEMMAP_SECTION_NONAME; // the new section name must be specified
545 // check that the new section name is unique
547 if (find_memory_section (section_name) != NULL)
548 return ERR_MEMMAP_SECTION_NAMEINUSE; // the new section name is not unique
550 // check that the LMA (if absolute) is within a memory region
552 if (initial_section_anchor == absolute)
554 region = find_region_by_address (initial_anchor_address);
556 return ERR_MEMMAP_SECTION_LMA_NOTINREGION; // section LMA is not within a memory region
557 if ((section_size > 0) && (initial_anchor_address + section_size > region->address + region->size))
558 return ERR_MEMMAP_SECTION_LMA_NOTINREGION; // end of section is not within the memory region
559 if (relocates && (region->type == read_write))
560 return ERR_MEMMAP_SECTION_LMA_READWRITE; // section LMA must be in a read-only memory region
563 // check that the VMA (if absolute) is within a memory region
565 if (final_section_anchor == absolute)
567 region = find_region_by_address (final_anchor_address);
569 return ERR_MEMMAP_SECTION_VMA_NOTINREGION; // section VMA is not within a memory region
570 if ((section_size > 0) && (final_anchor_address + section_size > region->address + region->size))
571 return ERR_MEMMAP_SECTION_VMA_NOTINREGION; // end of section is not within the memory region
572 if (relocates && (region->type == read_only))
573 return ERR_MEMMAP_SECTION_VMA_READONLY; // section VMA must be in a read/write memory region
576 // FIXME check for overlap of absolute sections
578 // check that specified parent(s) (for relative anchors) are available
580 if (relocates) // only check the initial parent section if the section relocates
582 if (initial_section_anchor == relative)
584 list <mem_section>::iterator parent_section = find_memory_section (initial_anchor_section_name);
585 if (parent_section == section_list.end ())
586 return ERR_MEMMAP_SECTION_LMA_ANCHORNOTFOUND; // initial anchor name not found
588 if (parent_section->initial_location->following_section != NULL)
589 return ERR_MEMMAP_SECTION_LMA_ANCHORNOTAVAIL; // initial anchor specified is unavailable
591 if ((parent_section->size == 0) && (! parent_section->linker_defined))
592 return ERR_MEMMAP_SECTION_LMA_ANCHORNOTAVAIL; // initial anchor specified expands to fit available space
594 if (find_region_by_section (parent_section, initial_location)->type == read_write)
595 return ERR_MEMMAP_SECTION_LMA_READWRITE; // initial anchor must be in a read-only memory region
599 if (final_section_anchor == relative)
601 list <mem_section>::iterator parent_section = find_memory_section (final_anchor_section_name);
602 if (parent_section == NULL)
603 return ERR_MEMMAP_SECTION_VMA_ANCHORNOTFOUND; // final anchor name not found
605 if ((parent_section->size == 0) && (! parent_section->linker_defined))
606 return ERR_MEMMAP_SECTION_VMA_ANCHORNOTAVAIL; // final anchor specified expands to fit available space
608 if ((!relocates) && anchor_to_initial_location) // final anchor to initial location of parent section
610 if (parent_section->initial_location->following_section != NULL)
611 return ERR_MEMMAP_SECTION_VMA_ANCHORNOTAVAIL; // final anchor specified is unavailable
615 if (parent_section->final_location->following_section != NULL)
616 return ERR_MEMMAP_SECTION_VMA_ANCHORNOTAVAIL; // final anchor specified is unavailable
619 if (relocates && (find_region_by_section (parent_section, final_location)->type == read_only))
620 return ERR_MEMMAP_SECTION_VMA_READONLY; // final anchor of relocating section must be in a read/write memory region
623 // add the new section to the section map
625 mem_section new_mem_section;
626 list <mem_section>::iterator new_section = section_list.insert (section_list.begin (), new_mem_section);
627 new_section->name = section_name;
628 new_section->size = section_size;
629 new_section->alignment = section_alignment;
630 new_section->relocates = relocates;
631 new_section->note = note;
632 new_section->linker_defined = linker_defined;
633 new_section->initial_location = new mem_location;
634 new_section->final_location = new mem_location;
635 new_section->initial_location->following_section = NULL; // initialize struct
636 new_section->final_location->following_section = NULL; // initialize struct
637 new_section->initial_location->anchor = initial_section_anchor;
638 new_section->final_location->anchor = final_section_anchor;
640 if ((initial_section_anchor == relative) &&
641 (!relocates) && (find_memory_section (initial_anchor_section_name)->relocates))
643 // a non-relocating relative section anchored to a relocating section
645 if (anchor_to_initial_location) // new section is anchored to the initial location of a relocating section
647 list <mem_section>::iterator anchor_section = find_memory_section (initial_anchor_section_name);
648 new_section->initial_location->following_section = anchor_section->initial_location->following_section;
649 anchor_section->initial_location->following_section = new_section;
651 else // new section is anchored to the final location of a relocating section
653 list <mem_section>::iterator anchor_section = find_memory_section (initial_anchor_section_name);
654 new_section->final_location->following_section = anchor_section->final_location->following_section;
655 anchor_section->final_location->following_section = new_section;
660 // copy initial location data
662 if (initial_section_anchor == relative) // new section follows the named anchor section
664 list <mem_section>::iterator anchor_section = find_memory_section (initial_anchor_section_name);
665 new_section->initial_location->following_section = anchor_section->initial_location->following_section; // move anchor of the following section
666 anchor_section->initial_location->following_section = new_section; // anchor the new section
668 else // new section has an absolute anchor
669 new_section->initial_location->address = initial_anchor_address;
671 // copy final location data
673 if (final_section_anchor == relative) // new section follows the named anchor section
675 list <mem_section>::iterator anchor_section = find_memory_section (final_anchor_section_name);
676 new_section->final_location->following_section = anchor_section->final_location->following_section; // move anchor of the following section
677 anchor_section->final_location->following_section = new_section; // anchor the new section
679 else // new section has an absolute anchor
680 new_section->final_location->address = final_anchor_address;
683 // recalculate section lists for all regions
685 calc_section_lists ();
687 map_modified_flag = true;
692 ////////////////////////////////////////////////////////////////////////
693 // calc_section_lists() updates the lists of memory sections for all
696 bool mem_map::calc_section_lists ()
698 for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end(); ++region)
699 calc_section_list (region);
705 ////////////////////////////////////////////////////////////////////////
706 // calc_section_list() updates the list of memory sections which reside
707 // in the specified memory region. It is called whenever the section
710 bool mem_map::calc_section_list (list <mem_region>::iterator region)
712 // clear the old list (if any)
714 //TRACE (_T("Calculating section list for region '%s'\n"), CString (region->name.c_str()));
715 region->section_view_list.clear ();
717 // add the initial and final locations of each absolute section as necessary
719 for (list <mem_section>::iterator section = section_list.begin (); section != section_list.end (); ++section)
721 if (section->relocates) // the section is relocated and must be added to the view twice
723 add_absolute_section_to_list (region, section, initial_location);
724 add_absolute_section_to_list (region, section, final_location);
726 else // the section is not relocated and must be added to the view once only
727 add_absolute_section_to_list (region, section, fixed_location);
730 // add unused sections to section view list where appropriate
732 list <mem_section_view>::iterator previous_section_view = region->section_view_list.begin ();
734 if (previous_section_view == region->section_view_list.end ()) // no used sections in this region
736 // add a single unused section to the section view list
738 mem_section_view new_section_view;
739 new_section_view.section = NULL; // an unused section
740 region->section_view_list.push_back (new_section_view); // add to the section list for this region
742 else // there are used sections in this region
744 list <mem_section_view>::iterator second_section_view = region->section_view_list.begin ();
745 ++second_section_view;
747 // add unused sections between used sections where they do not meet in either initial or final locations
749 for (list <mem_section_view>::iterator section_view = second_section_view; section_view != region->section_view_list.end (); ++section_view)
751 if (! (absolute_sections_meet (previous_section_view->section, section_view->section)))
753 list <mem_section_view>::iterator new_section_view = region->section_view_list.insert (section_view); // add an unused section
754 new_section_view->section = NULL;
757 previous_section_view = section_view;
760 // add an unused section to end of region if the last section does not reach the end of the region in initial or final locations
762 if (! at_end_of_region (region->section_view_list.back().section, region))
764 mem_section_view new_section_view;
765 new_section_view.section = NULL; // an unused section
766 region->section_view_list.push_back (new_section_view); // add an unused section
769 // add an unused section to start of region if the first section does not start at the start of the region in initial or final locations
771 if (! at_start_of_region (region->section_view_list.front().section, region))
773 mem_section_view new_section_view;
774 new_section_view.section = NULL; // an unused section
775 region->section_view_list.push_front (new_section_view); // add an unused section
779 // add the initial and final locations of the each relative section as necessary
781 for (list <mem_section_view>::iterator section_view = region->section_view_list.begin (); section_view != region->section_view_list.end (); ++section_view)
782 if (section_view->section != NULL) // if section is used
784 list <mem_section>::iterator section = section_view->section;
786 TRACE (_T("Calculating relative sections for section view '%s' %s\n"), CString (section->name.c_str ()),
787 section_view->section_location == final_location ? _T("(final)") :
788 section_view->section_location == initial_location ? _T("(initial)") : _T("(fixed)"));
791 if (section_view->section_location == final_location)
793 if (section->final_location->anchor == absolute)
794 add_relative_sections_to_list (region, section_view, final_location);
797 else if (section_view->section_location == initial_location)
799 if (section->initial_location->anchor == absolute)
800 add_relative_sections_to_list (region, section_view, initial_location);
803 else // section_view->section_location == fixed_location
805 if (section->initial_location->anchor == absolute)
806 add_relative_sections_to_list (region, section_view, initial_location);
807 if (section->final_location->anchor == absolute)
808 add_relative_sections_to_list (region, section_view, final_location);
812 // remove unused sections where user-defined section of unknown size will be placed
814 section_view = region->section_view_list.begin ();
815 while (section_view != region->section_view_list.end ())
817 bool expanding_section = false;
818 if ((section_view->section != NULL) &&
819 (section_view->section->size == 0) &&
820 (! section_view->section->linker_defined))
821 expanding_section = true;
825 if (expanding_section && (section_view != region->section_view_list.end ()) && (section_view->section == NULL))
826 section_view = region->section_view_list.erase (section_view);
832 /////////////////////////////////////////////////////////////////////
833 // add_relative_sections_to_list() inserts the sections defined relative
834 // to the specified section list item to the section list for the
835 // specified region in the appropriate order
837 bool mem_map::add_relative_sections_to_list (list <mem_region>::iterator region, list <mem_section_view>::iterator section_view, section_location_type location_type)
839 // insert following relative sections of type 'location_type' in region_view.section_view_list
841 list <mem_section>::iterator new_section = section_view->section;
842 mem_location * new_section_location = (location_type == initial_location ? new_section->initial_location : new_section->final_location);
843 list <mem_section_view>::iterator insertion_point = section_view;
845 bool no_relocation = true;
847 while (new_section_location->following_section != NULL)
849 // add the new section to the section view list
851 mem_section_view new_section_view;
852 new_section_view.section = new_section_location->following_section;
853 const bool section_relocates = new_section->relocates;
854 new_section = new_section_view.section;
855 new_section_view.section_location = (new_section->relocates ? location_type : fixed_location);
856 if ((new_section_view.section_location == fixed_location) && (location_type == final_location) && (! section_view->section->relocates) && (! section_relocates) && no_relocation)
858 // section already added to the view so add nothing but
859 // increment insertion point for following sections
860 // TRACE (_T("Skipping section %s %s location (relative) preceding %s\n"), CString (new_section_location->following_section->name.c_str()), location_type == initial_location ? _T("initial") : _T("final"), ((insertion_point != region->section_view_list.end ()) && (insertion_point->section != NULL)) ? CString (insertion_point->section->name.c_str()) : _T("(null)"));
865 // TRACE (_T("Inserting section %s %s location (relative) preceding %s\n"), CString (new_section_location->following_section->name.c_str()), location_type == initial_location ? _T("initial") : _T("final"), ((insertion_point != region->section_view_list.end ()) && (insertion_point->section != NULL)) ? CString (insertion_point->section->name.c_str()) : _T("(null)"));
866 region->section_view_list.insert (insertion_point, new_section_view);
867 no_relocation = no_relocation && ! new_section_view.section->relocates;
869 new_section_location = (location_type == initial_location ? new_section->initial_location : new_section->final_location);
875 /////////////////////////////////////////////////////////////////////
876 // add_absolute_section_to_list() inserts the specified section to the
877 // specified section list at the appropriate place if it has an
878 // absolute location and that location is within the specified memory
881 bool mem_map::add_absolute_section_to_list (list <mem_region>::iterator region, list <mem_section>::iterator additional_section, section_location_type location_type)
883 // get location of new section
884 mem_location * new_section_location = (location_type == initial_location ? additional_section->initial_location : additional_section->final_location);
886 if ((new_section_location->anchor == absolute) && (new_section_location->address >= region->address) && (new_section_location->address < region->address + region->size))
888 // the section lies in the region
890 // initialise the insertion point for the new section
891 list <mem_section_view>::iterator insertion_point = region->section_view_list.end ();
893 for (list <mem_section_view>::iterator section = region->section_view_list.begin (); section != region->section_view_list.end (); ++section)
895 // get location of section
896 mem_location * section_location = (section->section_location == initial_location ? section->section->initial_location : section->section->final_location);
898 // compare with location of new section
899 if ((new_section_location->anchor == absolute) && (section_location->address >= new_section_location->address))
901 // insert new section here if the current section has a higher address
902 insertion_point = section;
907 // add the new section to the section view list
909 // TRACE (_T("Inserting section %s %s location (absolute) preceding %s\n"), CString (additional_section->name.c_str()), location_type == initial_location ? _T("initial") : _T("final"), insertion_point != region->section_view_list.end () ? CString (insertion_point->section->name.c_str()) : _T("(end)"));
910 mem_section_view new_section_view;
911 new_section_view.section = additional_section;
912 new_section_view.section_location = location_type;
913 region->section_view_list.insert (insertion_point, new_section_view);
920 ////////////////////////////////////////////////////////////////////
921 // absolute_sections_meet() determines whether the specified
922 // absolute memory sections meet. It assumes that section2 comes
923 // after section1 in the memory map.
925 bool mem_map::absolute_sections_meet(list <mem_section>::iterator section1, list <mem_section>::iterator section2)
927 if (section1->size == 0) // size of section1 is unknown
930 // check if initial section locations meet
932 if ((section1->initial_location->anchor == absolute) &&
933 ((section2->initial_location->anchor == absolute) &&
934 section1->initial_location->address + section1->size == section2->initial_location->address))
937 // check if final section locations meet
939 if ((section1->final_location->anchor == absolute) &&
940 ((section2->final_location->anchor == absolute) &&
941 section1->final_location->address + section1->size == section2->final_location->address))
948 //////////////////////////////////////////////////////////////
949 // at_start_of_region() determines whether the specified section
950 // is located at the very start of the specified region
952 bool mem_map::at_start_of_region (list <mem_section>::iterator section, list <mem_region>::iterator region)
954 // check initial section location
956 if ((section->initial_location->anchor == absolute) &&
957 (section->initial_location->address == region->address))
960 // check final section location
962 if ((section->final_location->anchor == absolute) &&
963 (section->final_location->address == region->address))
969 //////////////////////////////////////////////////////////////
970 // at_end_of_region() determines whether the specified section
971 // is located at the very end of the specified region
973 bool mem_map::at_end_of_region (list <mem_section>::iterator section, list <mem_region>::iterator region)
975 if (section->size == 0) // size of section is unknown
978 // check initial section location
980 if ((section->initial_location->anchor == absolute) &&
981 section->initial_location->address + section->size == region->address + region->size)
984 // check final section location
986 if ((section->final_location->anchor == absolute) &&
987 section->final_location->address + section->size == region->address + region->size)
993 ////////////////////////////////////////////////////////////////////////
994 // find_preceding_section() finds the preceding section in the
995 // memory section list
997 list <mem_section>::iterator mem_map::find_preceding_section (list <mem_section>::iterator reference_section, bool initial_location)
999 for (list <mem_section>::iterator section = section_list.begin (); section != section_list.end (); ++section)
1001 if (reference_section == (reference_section->relocates && initial_location ? section->initial_location->following_section : section->final_location->following_section)) // if preceding section found
1002 return section; // return the section iterator
1004 return NULL; // section not found
1007 ////////////////////////////////////////////////////////////////////////
1008 // find_memory_section() finds an existing section in the
1009 // memory section list
1011 list <mem_section>::iterator mem_map::find_memory_section (std::string section_name)
1013 for (list <mem_section>::iterator section = section_list.begin (); section != section_list.end (); ++section)
1014 if (section->name == section_name) // if section found
1015 return section; // return the section iterator
1017 return NULL; // section not found
1021 ////////////////////////////////////////////////////////////////////////
1022 // find_memory_region() finds an existing region in the
1023 // memory region list
1025 list <mem_region>::iterator mem_map::find_memory_region (std::string region_name)
1027 for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end (); ++region)
1028 if (region->name == region_name) // if region found
1029 return region; // return the region iterator
1031 return NULL; // region not found
1035 ////////////////////////////////////////////////////////////////////////
1036 // delete_memory_section() removes an existing item from the
1037 // memory section map
1039 bool mem_map::delete_memory_section (std::string name)
1041 // make sure that the section exists
1043 list <mem_section>::iterator section = find_memory_section (name);
1044 if (section == NULL)
1045 return false; // there is no section with this name
1048 // make sure that there are no sections defined relative to this section before deleting it
1050 if (section->initial_location->following_section != NULL)
1053 if (section->final_location->following_section != NULL)
1057 // if section is absolute, copy the initial and final location information to
1058 // the following sections (if any)
1060 if ((section->initial_location->anchor == absolute) && (section->initial_location->following_section != NULL))
1062 section->initial_location->following_section->initial_location->anchor = absolute;
1063 section->initial_location->following_section->initial_location->address = section->initial_location->address;
1064 // FIXME adjust new address of following section for alignment here
1067 if ((section->final_location->anchor == absolute) && (section->final_location->following_section != NULL))
1069 section->final_location->following_section->final_location->anchor = absolute;
1070 section->final_location->following_section->final_location->address = section->final_location->address;
1071 // FIXME adjust new address of following section for alignment here
1074 // if section is relative, find the initial and final sections to which it is attached
1075 // and set their pointers to the sections following the one to be deleted (if any)
1077 list <mem_section>::iterator related_section;
1079 if (section->initial_location->anchor == relative)
1080 for (related_section = section_list.begin (); related_section != section_list.end (); ++related_section)
1081 if (related_section->initial_location->following_section == section)
1082 related_section->initial_location->following_section = section->initial_location->following_section;
1084 if (section->final_location->anchor == relative)
1085 for (related_section = section_list.begin (); related_section != section_list.end (); ++related_section)
1086 if (related_section->final_location->following_section == section)
1087 related_section->final_location->following_section = section->final_location->following_section;
1089 // delete the section
1091 delete section->initial_location;
1092 section->initial_location = NULL;
1094 delete section->final_location;
1095 section->final_location = NULL;
1097 section_list.erase (section);
1099 // recalculate section lists for all regions
1101 calc_section_lists ();
1103 map_modified_flag = true;
1108 ////////////////////////////////////////////////////////////////////////
1109 // delete_memory_sections() deletes all memory sections in preparation
1110 // for layout loading or application closure
1112 bool mem_map::delete_all_memory_sections ()
1114 // deleteZ(each section in turn)
1116 while (section_list.size () > 0)
1118 list <mem_section>::iterator section = section_list.begin ();
1119 delete section->initial_location;
1120 section->initial_location = NULL;
1122 delete section->final_location;
1123 section->final_location = NULL;
1125 section_list.erase (section);
1127 // section_list.clear ();
1129 // recalculate section view lists for all regions
1131 calc_section_lists ();
1133 map_modified_flag = true;
1138 ////////////////////////////////////////////////////////////////////////
1139 // export_sections() exports section-related info for regions of the
1140 // specified type to the linker script fragment and header file
1142 bool mem_map::export_sections (FILE * script_stream, FILE * header_stream, mem_type type)
1144 for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end(); ++region)
1145 if (region->type == type)
1147 for (list <mem_section_view>::iterator section_view = region->section_view_list.begin (); section_view != region->section_view_list.end (); ++section_view)
1149 if ((section_view->section != NULL) && (section_view->section_location != initial_location))
1151 if (section_view->section->linker_defined) // section is linker-defined
1153 // output section name and region name
1154 fprintf (script_stream, " SECTION_%s (%s, ",
1155 encode_section_name (section_view->section->name).c_str (), region->name.c_str ());
1158 if (section_view->section->final_location->anchor == absolute) // an absolute VMA
1159 fprintf (script_stream, "%#lx, ", section_view->section->final_location->address); // specify absolute address
1160 else // a relative VMA
1161 fprintf (script_stream, "ALIGN (%#lx), ", section_view->section->alignment); // specify alignment
1164 if (! section_view->section->relocates) // section does not relocate so LMA == VMA
1165 fprintf (script_stream, "LMA_EQ_VMA)");
1166 else if (section_view->section->initial_location->anchor == absolute) // an absolute LMA
1167 fprintf (script_stream, "AT (%#lx))", section_view->section->initial_location->address);
1168 else // a relative LMA
1170 list <mem_section>::iterator parent_section;
1171 for (parent_section = section_list.begin (); parent_section != section_list.end (); ++parent_section)
1172 if (parent_section->initial_location->following_section == section_view->section)
1175 if (parent_section->linker_defined) // parent section is linker-defined
1176 fprintf (script_stream, "FOLLOWING (.%s))", parent_section->name.c_str ());
1177 else // parent section is user-defined
1178 fprintf (script_stream, "AT (__%s + %#lx))", parent_section->name.c_str (), parent_section->size);
1181 else // section is user-defined
1183 // output section symbol
1184 if (section_view->section->final_location->anchor == absolute) // an absolute VMA
1185 fprintf (script_stream, " CYG_LABEL_DEFN(__%s) = %#lx;", section_view->section->name.c_str (), section_view->section->final_location->address);
1186 else // a relative VMA
1187 fprintf (script_stream, " CYG_LABEL_DEFN(__%s) = ALIGN (%#lx);", section_view->section->name.c_str (), section_view->section->alignment);
1189 // update current location pointer
1190 if (section_view->section->size != 0) // size is known
1191 fprintf (script_stream, " . = CYG_LABEL_DEFN(__%s) + %#lx;", section_view->section->name.c_str (), section_view->section->size);
1193 // output reference to symbol in header file
1194 fprintf (header_stream, "#ifndef __ASSEMBLER__\nextern char CYG_LABEL_NAME (__%s) [];\n#endif\n", section_view->section->name.c_str ());
1195 fprintf (header_stream, "#define CYGMEM_SECTION_%s (CYG_LABEL_NAME (__%s))\n", section_view->section->name.c_str (), section_view->section->name.c_str ());
1196 if (section_view->section->size == 0) // a section of unknown size
1198 mem_address section_end_address;
1200 ++section_view; // move to next section_view
1201 if (section_view == region->section_view_list.end ()) // section continues to end of region
1202 section_end_address = region->address + region->size;
1203 else // section continues to next section with an absolute location
1204 section_end_address = section_view->section->final_location->address;
1205 --section_view; // move back to previous section view
1207 fprintf (header_stream, "#define CYGMEM_SECTION_%s_SIZE (%#lx - (size_t) CYG_LABEL_NAME (__%s))\n", section_view->section->name.c_str (), section_end_address, section_view->section->name.c_str ());
1209 else // a section of known size
1210 fprintf (header_stream, "#define CYGMEM_SECTION_%s_SIZE (%#lx)\n", section_view->section->name.c_str (), section_view->section->size);
1213 // end of section description
1215 fprintf (script_stream, "\n"); // new line
1223 ////////////////////////////////////////////////////////////////////////
1224 // export_files() creates a fragment of linker script and a header file
1225 // describing the memory layout
1227 bool mem_map::export_files (const wxChar* script_name, const wxChar* header_name)
1229 FILE * script_stream;
1230 FILE * header_stream;
1231 list <mem_region>::iterator region;
1233 // do not export files if the memory layout is empty
1234 // assume that there are default LDI files available
1236 if (region_list.size () == 0)
1239 // open the script fragment file for writing
1241 script_stream = _tfopen (script_name, _T("wt"));
1242 if (script_stream == NULL)
1245 // open the header file for writing
1247 header_stream = _tfopen (header_name, _T("wt"));
1248 if (header_stream == NULL)
1250 fclose (script_stream);
1254 // output the linker script fragment header
1257 time (&export_time);
1258 struct tm * local = localtime (&export_time);
1259 fprintf (script_stream, "// eCos memory layout - %s\n%s\n\n", asctime (local), MLT_GENERATED_WARNING);
1260 fprintf (script_stream, "#include <cyg/infra/cyg_type.inc>\n\n");
1262 // output the header file header
1264 fprintf (header_stream, "// eCos memory layout - %s\n%s\n\n", asctime (local), MLT_GENERATED_WARNING);
1265 fprintf (header_stream, "#ifndef __ASSEMBLER__\n");
1266 fprintf (header_stream, "#include <cyg/infra/cyg_type.h>\n"); // for the CYG_LABEL_NAME macro definition
1267 fprintf (header_stream, "#include <stddef.h>\n\n"); // for size_t
1268 fprintf (header_stream, "#endif\n");
1270 // output the MEMORY block
1272 fprintf (script_stream, "MEMORY\n{\n"); // start of MEMORY block
1273 for (region = region_list.begin (); region != region_list.end(); ++region)
1275 fprintf (script_stream, " %s : ORIGIN = %#lx, LENGTH = %#lx\n", region->name.c_str(), region->address, region->size);
1276 fprintf (header_stream, "#define CYGMEM_REGION_%s (%#lx)\n", region->name.c_str(), region->address);
1277 fprintf (header_stream, "#define CYGMEM_REGION_%s_SIZE (%#lx)\n", region->name.c_str(), region->size);
1278 fprintf (header_stream, "#define CYGMEM_REGION_%s_ATTR (CYGMEM_REGION_ATTR_R%s)\n", region->name.c_str(), (read_write == region->type) ? " | CYGMEM_REGION_ATTR_W" : "");
1280 fprintf (script_stream, "}\n\n"); // end of MEMORY block
1282 // output the SECTIONS block
1284 fprintf (script_stream, "SECTIONS\n{\n"); // start of SECTIONS block
1285 fprintf (script_stream, " SECTIONS_BEGIN\n"); // SECTIONS block initial script macro call
1286 export_sections (script_stream, header_stream, read_only); // export sections in read-only regions first
1287 export_sections (script_stream, header_stream, read_write); // followed by sections in read-write regions
1288 fprintf (script_stream, " SECTIONS_END\n"); // SECTIONS block final script macro call
1289 fprintf (script_stream, "}\n"); // end of SECTIONS block
1293 fclose (script_stream);
1294 fclose (header_stream);
1300 ////////////////////////////////////////////////////////////////////////
1301 // import_linker_defined_sections() reads a the linker-defined section
1302 // names from the "SECTION_*" CPP macro definitions within the linker
1305 bool mem_map::import_linker_defined_sections (const wxChar* filename)
1307 // clear the linker-defined section name list
1309 linker_defined_section_list.clear ();
1311 // open the linker script file for reading
1314 stream = _tfopen (filename, _T("rt"));
1318 bool macro = false; // not reading a CPP macro definition initially
1319 char input_string [32];
1320 while (! feof (stream))
1324 if (fscanf (stream, "%8s", input_string) == EOF) // read the next 8 chars (not including whitespace)
1327 if (strcmp (input_string, "SECTION_") == 0) // an MLT section macro definition
1329 if (fscanf (stream, "%31[^(]", input_string) == EOF) // read the section name up to the '(' character
1332 std::string section_name = decode_section_name (input_string);
1333 if (find (linker_defined_section_list.begin (), linker_defined_section_list.end (), section_name) == linker_defined_section_list.end ()) // if section name is unique
1334 linker_defined_section_list.push_back (section_name);
1342 if (fscanf (stream, "%31s", input_string) == EOF)
1345 if (strcmp (input_string, "#define") == 0)
1346 macro = true; // macro starts with "#define"
1353 if (fclose (stream))
1359 ////////////////////////////////////////////////////////////////////////
1360 // encode_note() encodes newlines in note
1362 std::string mem_map::encode_note (std::string in)
1364 std::string out = "!"; // dummy first character to ensure output string length > 0
1366 for (unsigned int item = 0; item < in.size (); item++)
1367 if (in [item] == _TCHAR('\n')) // an LF character
1368 out += "\x07F"; // output substitution character 0x7F instead
1369 else if (in [item] != _TCHAR('\r')) // ignore the CR (present under Win32 only)
1370 out += in [item]; // copy other characters to output string unprocessed
1375 ////////////////////////////////////////////////////////////////////////
1376 // decode_note() decodes newlines in note
1378 std::string mem_map::decode_note (std::string in)
1382 for (unsigned int item = 1; item < in.size (); item++) // ignore dummy first character
1383 if (in [item] == _TCHAR('\x07F')) // the newline substitution character
1384 out += "\r\n"; // output CRLF instead
1391 ////////////////////////////////////////////////////////////////////////
1392 // encode_section_name() encodes period -> double underscore in section name
1394 std::string mem_map::encode_section_name (std::string in)
1398 for (unsigned int item = 0; item < in.size (); item++)
1399 if (in [item] == '.') // a period character
1400 out += "__"; // output a double underscore instead
1407 ////////////////////////////////////////////////////////////////////////
1408 // decode_section_name() decodes double underscore -> period in section name
1410 std::string mem_map::decode_section_name (std::string in)
1414 for (unsigned int item = 0; item < in.size (); item++)
1415 if ((item + 1 < in.size ()) && (in [item] == '_') && (in [item + 1] == '_')) // two consecutive underscore characters
1417 out += "."; // output a period instead
1418 item++; // skip the second underscore
1426 ////////////////////////////////////////////////////////////////////////
1427 // save_memory_layout() saves the memory layout to file for later use
1429 bool mem_map::save_memory_layout (const wxChar* filename)
1432 list <mem_region>::iterator region;
1434 // open the save file for writing
1436 stream = _tfopen (filename, _T("wt"));
1440 // write the save file format version number
1442 fprintf (stream, "version %u\n", (unsigned int) MLT_FILE_VERSION);
1444 // save the memory region data in address order
1446 for (region = region_list.begin (); region != region_list.end (); ++region)
1447 fprintf (stream, "region %s %lx %lx %d %s\n", region->name.c_str (),
1448 region->address, region->size, (region->type == read_only), encode_note (region->note).c_str ());
1450 // save the memory section data in VMA order
1452 for (region = region_list.begin (); region != region_list.end(); ++region)
1454 for (list <mem_section_view>::iterator section_view = region->section_view_list.begin (); section_view != region->section_view_list.end (); ++section_view)
1456 if ((section_view->section != NULL) && (section_view->section_location != initial_location))
1458 list <mem_section>::iterator section = section_view->section;
1459 fprintf (stream, "section %s %lx %lx %d %d %d %d %d %d",
1460 section->name.c_str (), section->size, section->alignment,
1461 section->relocates, section->linker_defined,
1462 section->final_location->anchor == absolute,
1463 section->final_location->following_section != NULL,
1464 section->initial_location->anchor == absolute,
1465 section->initial_location->following_section != NULL);
1467 if (section->final_location->anchor == absolute)
1468 fprintf (stream, " %lx", section->final_location->address);
1470 if (section->initial_location->anchor == absolute)
1471 fprintf (stream, " %lx", section->initial_location->address);
1473 if (section->final_location->following_section != NULL)
1474 fprintf (stream, " %s", section->final_location->following_section->name.c_str ());
1476 if (section->initial_location->following_section != NULL)
1477 fprintf (stream, " %s", section->initial_location->following_section->name.c_str ());
1479 fprintf (stream, " %s", encode_note (section->note).c_str ());
1481 // end of section description
1483 fprintf (stream, "\n"); // new line
1490 if (fclose (stream))
1493 map_modified_flag = false;
1498 ////////////////////////////////////////////////////////////////////////
1499 // load_memory_layout() loads a previously saved memory layout from file
1501 bool mem_map::load_memory_layout (const wxChar* filename)
1505 // open the save file for reading
1507 stream = _tfopen (filename, _T("rt"));
1511 // read the file version
1513 unsigned int file_version;
1514 if ((fscanf (stream, "%*s %u", &file_version) != 1) ||
1515 (file_version != MLT_FILE_VERSION))
1517 fclose (stream); // missing or incorrect file version
1521 new_memory_layout ();
1523 // read the new memory layout (first pass)
1525 while (! feof (stream))
1527 char record_type [32];
1528 if (fscanf (stream, "%31s", record_type) == EOF)
1531 if (strcmp (record_type, "section") == 0) // a section record
1533 if (! load_memory_section_1 (stream))
1536 else if (strcmp (record_type, "region") == 0) // a region record
1538 mem_address address, size;
1539 bool read_only_region;
1543 fscanf (stream, "%s %lx %lx %d %1023[^\n]", name, &address, &size, &read_only_region, note);
1545 if (create_memory_region (name, address, size, (read_only_region ? read_only : read_write), decode_note (note)))
1548 else // an unknown record type
1552 // quit if the end of the file was not reached (due to an error)
1554 if (! feof (stream))
1556 new_memory_layout ();
1561 // move the file pointer back to the beginning of the file
1563 fseek (stream, 0, SEEK_SET);
1565 while (! feof (stream)) // read the memory layout (second pass)
1567 char record_type [32];
1568 if (fscanf (stream, "%31s", record_type) == EOF)
1571 if ((strcmp (record_type, "section") == 0) && (! load_memory_section_2 (stream)))
1577 if (fclose (stream))
1579 new_memory_layout ();
1583 // recalculate section view lists for all regions
1585 calc_section_lists ();
1587 map_modified_flag = false;
1592 ////////////////////////////////////////////////////////////////////////
1593 // load_memory_section_1() loads a previously saved memory section from
1594 // file (first pass)
1596 bool mem_map::load_memory_section_1 (FILE * stream)
1598 char section_name [32];
1599 int relocates, linker_defined;
1600 int final_absolute, initial_absolute, final_following, initial_following;
1601 mem_section new_section;
1603 new_section.initial_location = new mem_location;
1604 new_section.initial_location->following_section = NULL;
1605 new_section.final_location = new mem_location;
1606 new_section.final_location->following_section = NULL;
1608 fscanf (stream,"%31s %lx %lx %d %d %d %d %d %d",
1609 section_name, &new_section.size, &new_section.alignment,
1610 &relocates, &linker_defined, &final_absolute, &final_following,
1611 &initial_absolute, &initial_following);
1613 new_section.name = section_name;
1614 new_section.relocates = (relocates != 0);
1615 new_section.linker_defined = (linker_defined != 0);
1617 new_section.final_location->anchor = (final_absolute ? absolute : relative);
1618 if (final_absolute) // final location is absolute
1619 fscanf (stream, "%lx", &new_section.final_location->address);
1621 new_section.initial_location->anchor = (initial_absolute ? absolute : relative);
1622 if (initial_absolute) // initial location is absolute
1623 fscanf (stream, "%lx", &new_section.initial_location->address);
1625 if (final_following)
1626 fscanf (stream, "%*s"); // skip the final following section field on first pass
1628 if (initial_following)
1629 fscanf (stream, "%*s"); // skip the initial following section field on first pass
1632 fscanf (stream, " %1023[^\n]", note);
1633 new_section.note = decode_note (note);
1635 // add the new section to the section map
1637 section_list.push_front (new_section);
1643 ////////////////////////////////////////////////////////////////////////
1644 // load_memory_section_2() loads a previously saved memory section from
1645 // file (second pass)
1647 bool mem_map::load_memory_section_2 (FILE * stream)
1649 char section_name [32];
1650 char following_section_name [32];
1651 int final_absolute, initial_absolute, final_following, initial_following;
1653 fscanf (stream,"%31s %*lx %*lx %*d %*d %d %d %d %d",
1654 section_name, &final_absolute, &final_following,
1655 &initial_absolute, &initial_following);
1657 if (final_absolute) // final location is absolute
1658 fscanf (stream, "%*lx"); // skip the final location
1660 if (initial_absolute) // initial location is absolute
1661 fscanf (stream, "%*lx"); // skip the initial location
1663 if (initial_following || final_following) // the section is a parent
1665 list <mem_section>::iterator section = find_memory_section (section_name);
1667 if (final_following)
1669 fscanf (stream, "%31s", following_section_name); // read the final following section name
1670 section->final_location->following_section =
1671 find_memory_section (following_section_name);
1674 if (initial_following)
1676 fscanf (stream, "%31s", following_section_name); // read the initial following section name
1677 section->initial_location->following_section =
1678 find_memory_section (following_section_name);
1682 fscanf (stream, "%*1023[^\n]"); // skip the note
1688 ////////////////////////////////////////////////////////////////////////
1689 // new_memory_layout() clears the memory layout
1691 bool mem_map::new_memory_layout ()
1693 delete_all_memory_sections ();
1694 // section_list.clear ();
1695 region_list.clear ();
1697 map_modified_flag = false; // no need to save an empty memory layout
1702 ////////////////////////////////////////////////////////////////////////
1703 // section_exists() determines if the specified section is defined
1705 bool mem_map::section_exists (std::string section_name)
1707 return (find_memory_section (section_name) != NULL);
1711 ////////////////////////////////////////////////////////////////////////
1712 // find_region_by_address() finds the region containing the specified
1715 list <mem_region>::iterator mem_map::find_region_by_address (mem_address address)
1717 for (list <mem_region>::iterator region = region_list.begin (); region !=region_list.end(); ++region)
1718 if ((address >= region->address) && (address < region->address + region->size))
1721 return NULL; // the specified address is not in a memory region
1725 ////////////////////////////////////////////////////////////////////////
1726 // find_region_by_section() finds the region containing the specified
1729 list <mem_region>::iterator mem_map::find_region_by_section (list <mem_section>::iterator section, section_location_type location_type)
1731 for (list <mem_region>::iterator region = region_list.begin (); region !=region_list.end(); ++region)
1732 for (list <mem_section_view>::iterator section_view = region->section_view_list.begin (); section_view != region->section_view_list.end (); ++section_view)
1733 if ((section_view->section != NULL) && (section_view->section == section) &&
1734 (section_view->section_location == (section_view->section->relocates ? location_type : fixed_location)))
1737 return NULL; // the specified section location type was not found (you probably searched for the fixed_location of a relocating section)