📄 memmap.cpp
字号:
fclose (script_stream); return false; } // output the linker script fragment header time_t export_time; time (&export_time); struct tm * local = localtime (&export_time); fprintf (script_stream, "// eCos memory layout - %s\n%s\n\n", asctime (local), MLT_GENERATED_WARNING); // output the header file header fprintf (header_stream, "// eCos memory layout - %s\n%s\n\n", asctime (local), MLT_GENERATED_WARNING); fprintf (header_stream, "#include <cyg/infra/cyg_type.h>\n"); // for the CYG_LABEL_NAME macro definition fprintf (header_stream, "#include <stddef.h>\n\n"); // for size_t // output the MEMORY block fprintf (script_stream, "MEMORY\n{\n"); // start of MEMORY block for (region = region_list.begin (); region != region_list.end(); ++region) { fprintf (script_stream, " %s : ORIGIN = %#lx, LENGTH = %#lx\n", region->name.c_str(), region->address, region->size); fprintf (header_stream, "#define CYGMEM_REGION_%s (%#lx)\n", region->name.c_str(), region->address); fprintf (header_stream, "#define CYGMEM_REGION_%s_SIZE (%#lx)\n", region->name.c_str(), region->size); 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" : ""); } fprintf (script_stream, "}\n\n"); // end of MEMORY block // output the SECTIONS block fprintf (script_stream, "SECTIONS\n{\n"); // start of SECTIONS block fprintf (script_stream, " SECTIONS_BEGIN\n"); // SECTIONS block initial script macro call export_sections (script_stream, header_stream, read_only); // export sections in read-only regions first export_sections (script_stream, header_stream, read_write); // followed by sections in read-write regions fprintf (script_stream, " SECTIONS_END\n"); // SECTIONS block final script macro call fprintf (script_stream, "}\n"); // end of SECTIONS block // close the files fclose (script_stream); fclose (header_stream); return true;}////////////////////////////////////////////////////////////////////////// import_linker_defined_sections() reads a the linker-defined section// names from the "SECTION_*" CPP macro definitions within the linker// scriptbool mem_map::import_linker_defined_sections (LPCTSTR filename){ // clear the linker-defined section name list linker_defined_section_list.clear (); // open the linker script file for reading FILE * stream; stream = _tfopen (filename, _T("rt")); if (stream == NULL) return false; bool macro = false; // not reading a CPP macro definition initially char input_string [32]; while (! feof (stream)) { if (macro) { if (fscanf (stream, "%8s", input_string) == EOF) // read the next 8 chars (not including whitespace) break; if (strcmp (input_string, "SECTION_") == 0) // an MLT section macro definition { if (fscanf (stream, "%31[^(]", input_string) == EOF) // read the section name up to the '(' character break; string section_name = decode_section_name (input_string); if (find (linker_defined_section_list.begin (), linker_defined_section_list.end (), section_name) == linker_defined_section_list.end ()) // if section name is unique linker_defined_section_list.push_back (section_name); } macro = false; } else { if (fscanf (stream, "%31s", input_string) == EOF) break; if (strcmp (input_string, "#define") == 0) macro = true; // macro starts with "#define" } } // close the file if (fclose (stream)) return false; return true;}////////////////////////////////////////////////////////////////////////// encode_note() encodes newlines in notestring mem_map::encode_note (string in){ string out = "!"; // dummy first character to ensure output string length > 0 for (unsigned int item = 0; item < in.size (); item++) if (in [item] == _TCHAR('\n')) // an LF character out += "\x07F"; // output substitution character 0x7F instead else if (in [item] != _TCHAR('\r')) // ignore the CR (present under Win32 only) out += in [item]; // copy other characters to output string unprocessed return out;}////////////////////////////////////////////////////////////////////////// decode_note() decodes newlines in notestring mem_map::decode_note (string in){ string out; for (unsigned int item = 1; item < in.size (); item++) // ignore dummy first character if (in [item] == _TCHAR('\x07F')) // the newline substitution character out += "\r\n"; // output CRLF instead else out += in [item]; return out;}////////////////////////////////////////////////////////////////////////// encode_section_name() encodes period -> double underscore in section namestring mem_map::encode_section_name (string in){ string out; for (unsigned int item = 0; item < in.size (); item++) if (in [item] == '.') // a period character out += "__"; // output a double underscore instead else out += in [item]; return out;}////////////////////////////////////////////////////////////////////////// decode_section_name() decodes double underscore -> period in section namestring mem_map::decode_section_name (string in){ string out; for (unsigned int item = 0; item < in.size (); item++) if ((item + 1 < in.size ()) && (in [item] == '_') && (in [item + 1] == '_')) // two consecutive underscore characters { out += "."; // output a period instead item++; // skip the second underscore } else out += in [item]; return out;}////////////////////////////////////////////////////////////////////////// save_memory_layout() saves the memory layout to file for later usebool mem_map::save_memory_layout (LPCTSTR filename){ FILE * stream; list <mem_region>::iterator region; // open the save file for writing stream = _tfopen (filename, _T("wt")); if (stream == NULL) return false; // write the save file format version number fprintf (stream, "version %u\n", (unsigned int) MLT_FILE_VERSION); // save the memory region data in address order for (region = region_list.begin (); region != region_list.end (); ++region) fprintf (stream, "region %s %lx %lx %d %s\n", region->name.c_str (), region->address, region->size, (region->type == read_only), encode_note (region->note).c_str ()); // save the memory section data in VMA order for (region = region_list.begin (); region != region_list.end(); ++region) { for (list <mem_section_view>::iterator section_view = region->section_view_list.begin (); section_view != region->section_view_list.end (); ++section_view) { if ((section_view->section != NULL) && (section_view->section_location != initial_location)) { list <mem_section>::iterator section = section_view->section; fprintf (stream, "section %s %lx %lx %d %d %d %d %d %d", section->name.c_str (), section->size, section->alignment, section->relocates, section->linker_defined, section->final_location->anchor == absolute, section->final_location->following_section != NULL, section->initial_location->anchor == absolute, section->initial_location->following_section != NULL); if (section->final_location->anchor == absolute) fprintf (stream, " %lx", section->final_location->address); if (section->initial_location->anchor == absolute) fprintf (stream, " %lx", section->initial_location->address); if (section->final_location->following_section != NULL) fprintf (stream, " %s", section->final_location->following_section->name.c_str ()); if (section->initial_location->following_section != NULL) fprintf (stream, " %s", section->initial_location->following_section->name.c_str ()); fprintf (stream, " %s", encode_note (section->note).c_str ()); // end of section description fprintf (stream, "\n"); // new line } } } // close the file if (fclose (stream)) return false; map_modified_flag = false; return true;}////////////////////////////////////////////////////////////////////////// load_memory_layout() loads a previously saved memory layout from filebool mem_map::load_memory_layout (LPCTSTR filename){ FILE * stream; // open the save file for reading stream = _tfopen (filename, _T("rt")); if (stream == NULL) return false; // read the file version unsigned int file_version; if ((fscanf (stream, "%*s %u", &file_version) != 1) || (file_version != MLT_FILE_VERSION)) { fclose (stream); // missing or incorrect file version return false; } new_memory_layout (); // read the new memory layout (first pass) while (! feof (stream)) { char record_type [32]; if (fscanf (stream, "%31s", record_type) == EOF) break; if (strcmp (record_type, "section") == 0) // a section record { if (! load_memory_section_1 (stream)) break; } else if (strcmp (record_type, "region") == 0) // a region record { mem_address address, size; bool read_only_region; char name [32]; char note [1024]; fscanf (stream, "%s %lx %lx %d %1023[^\n]", name, &address, &size, &read_only_region, note); if (create_memory_region (name, address, size, (read_only_region ? read_only : read_write), decode_note (note))) break; } else // an unknown record type break; } // quit if the end of the file was not reached (due to an error) if (! feof (stream)) { new_me
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -