Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 1 | //===-- SBSection.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/API/SBSection.h" |
| 11 | #include "lldb/API/SBStream.h" |
Greg Clayton | 5c5a38e | 2012-06-27 22:22:28 +0000 | [diff] [blame^] | 12 | #include "lldb/API/SBTarget.h" |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 13 | #include "lldb/Core/DataBuffer.h" |
| 14 | #include "lldb/Core/DataExtractor.h" |
| 15 | #include "lldb/Core/Log.h" |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Module.h" |
Greg Clayton | 15ef51e | 2011-09-24 05:04:40 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Section.h" |
| 18 | #include "lldb/Core/StreamString.h" |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 19 | |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | |
| 25 | SBSection::SBSection () : |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 26 | m_opaque_wp () |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 27 | { |
| 28 | } |
| 29 | |
| 30 | SBSection::SBSection (const SBSection &rhs) : |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 31 | m_opaque_wp (rhs.m_opaque_wp) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 32 | { |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | |
| 36 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 37 | SBSection::SBSection (const lldb::SectionSP §ion_sp) : |
| 38 | m_opaque_wp () // Don't init with section_sp otherwise this will throw if section_sp doesn't contain a valid Section * |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 39 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 40 | if (section_sp) |
| 41 | m_opaque_wp = section_sp; |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | const SBSection & |
| 45 | SBSection::operator = (const SBSection &rhs) |
| 46 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 47 | m_opaque_wp = rhs.m_opaque_wp; |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 48 | return *this; |
| 49 | } |
| 50 | |
| 51 | SBSection::~SBSection () |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | bool |
| 56 | SBSection::IsValid () const |
| 57 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 58 | SectionSP section_sp (GetSP()); |
| 59 | return section_sp && section_sp->GetModule().get() != NULL; |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Greg Clayton | 980c750 | 2011-09-24 01:37:21 +0000 | [diff] [blame] | 62 | const char * |
| 63 | SBSection::GetName () |
| 64 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 65 | SectionSP section_sp (GetSP()); |
| 66 | if (section_sp) |
| 67 | return section_sp->GetName().GetCString(); |
Greg Clayton | 980c750 | 2011-09-24 01:37:21 +0000 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 72 | lldb::SBSection |
| 73 | SBSection::FindSubSection (const char *sect_name) |
| 74 | { |
| 75 | lldb::SBSection sb_section; |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 76 | if (sect_name) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 77 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 78 | SectionSP section_sp (GetSP()); |
| 79 | if (section_sp) |
| 80 | { |
| 81 | ConstString const_sect_name(sect_name); |
| 82 | sb_section.SetSP(section_sp->GetChildren ().FindSectionByName(const_sect_name)); |
| 83 | } |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 84 | } |
| 85 | return sb_section; |
| 86 | } |
| 87 | |
| 88 | size_t |
| 89 | SBSection::GetNumSubSections () |
| 90 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 91 | SectionSP section_sp (GetSP()); |
| 92 | if (section_sp) |
| 93 | return section_sp->GetChildren ().GetSize(); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | lldb::SBSection |
| 98 | SBSection::GetSubSectionAtIndex (size_t idx) |
| 99 | { |
| 100 | lldb::SBSection sb_section; |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 101 | SectionSP section_sp (GetSP()); |
| 102 | if (section_sp) |
| 103 | sb_section.SetSP (section_sp->GetChildren ().GetSectionAtIndex(idx)); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 104 | return sb_section; |
| 105 | } |
| 106 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 107 | lldb::SectionSP |
| 108 | SBSection::GetSP() const |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 109 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 110 | return m_opaque_wp.lock(); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 114 | SBSection::SetSP(const lldb::SectionSP §ion_sp) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 115 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 116 | m_opaque_wp = section_sp; |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 119 | lldb::addr_t |
| 120 | SBSection::GetFileAddress () |
| 121 | { |
| 122 | lldb::addr_t file_addr = LLDB_INVALID_ADDRESS; |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 123 | SectionSP section_sp (GetSP()); |
| 124 | if (section_sp) |
| 125 | return section_sp->GetFileAddress(); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 126 | return file_addr; |
| 127 | } |
| 128 | |
| 129 | lldb::addr_t |
Greg Clayton | 5c5a38e | 2012-06-27 22:22:28 +0000 | [diff] [blame^] | 130 | SBSection::GetLoadAddress (lldb::SBTarget &sb_target) |
| 131 | { |
| 132 | TargetSP target_sp(sb_target.GetSP()); |
| 133 | if (target_sp) |
| 134 | { |
| 135 | SectionSP section_sp (GetSP()); |
| 136 | if (section_sp) |
| 137 | return section_sp->GetLoadBaseAddress(target_sp.get()); |
| 138 | } |
| 139 | return LLDB_INVALID_ADDRESS; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | lldb::addr_t |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 146 | SBSection::GetByteSize () |
| 147 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 148 | SectionSP section_sp (GetSP()); |
| 149 | if (section_sp) |
| 150 | return section_sp->GetByteSize(); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | uint64_t |
| 155 | SBSection::GetFileOffset () |
| 156 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 157 | SectionSP section_sp (GetSP()); |
| 158 | if (section_sp) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 159 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 160 | ModuleSP module_sp (section_sp->GetModule()); |
| 161 | if (module_sp) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 162 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 163 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 164 | if (objfile) |
| 165 | return objfile->GetOffset() + section_sp->GetFileOffset(); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 168 | return UINT64_MAX; |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | uint64_t |
| 172 | SBSection::GetFileByteSize () |
| 173 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 174 | SectionSP section_sp (GetSP()); |
| 175 | if (section_sp) |
| 176 | return section_sp->GetFileSize(); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | SBData |
Greg Clayton | 15ef51e | 2011-09-24 05:04:40 +0000 | [diff] [blame] | 181 | SBSection::GetSectionData () |
| 182 | { |
| 183 | return GetSectionData (0, UINT64_MAX); |
| 184 | } |
| 185 | |
| 186 | SBData |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 187 | SBSection::GetSectionData (uint64_t offset, uint64_t size) |
| 188 | { |
| 189 | SBData sb_data; |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 190 | SectionSP section_sp (GetSP()); |
| 191 | if (section_sp) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 192 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 193 | const uint64_t sect_file_size = section_sp->GetFileSize(); |
| 194 | if (sect_file_size > 0) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 195 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 196 | ModuleSP module_sp (section_sp->GetModule()); |
| 197 | if (module_sp) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 198 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 199 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 200 | if (objfile) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 201 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 202 | const uint64_t sect_file_offset = objfile->GetOffset() + section_sp->GetFileOffset(); |
| 203 | const uint64_t file_offset = sect_file_offset + offset; |
| 204 | uint64_t file_size = size; |
| 205 | if (file_size == UINT64_MAX) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 206 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 207 | file_size = section_sp->GetByteSize(); |
| 208 | if (file_size > offset) |
| 209 | file_size -= offset; |
| 210 | else |
| 211 | file_size = 0; |
| 212 | } |
| 213 | DataBufferSP data_buffer_sp (objfile->GetFileSpec().ReadFileContents (file_offset, file_size)); |
| 214 | if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) |
| 215 | { |
| 216 | DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp, |
| 217 | objfile->GetByteOrder(), |
| 218 | objfile->GetAddressByteSize())); |
| 219 | |
| 220 | sb_data.SetOpaque (data_extractor_sp); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | return sb_data; |
| 227 | } |
| 228 | |
| 229 | SectionType |
| 230 | SBSection::GetSectionType () |
| 231 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 232 | SectionSP section_sp (GetSP()); |
| 233 | if (section_sp.get()) |
| 234 | return section_sp->GetType(); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 235 | return eSectionTypeInvalid; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | bool |
| 240 | SBSection::operator == (const SBSection &rhs) |
| 241 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 242 | SectionSP lhs_section_sp (GetSP()); |
| 243 | SectionSP rhs_section_sp (rhs.GetSP()); |
| 244 | if (lhs_section_sp && rhs_section_sp) |
| 245 | return lhs_section_sp == rhs_section_sp; |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | |
| 249 | bool |
| 250 | SBSection::operator != (const SBSection &rhs) |
| 251 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 252 | SectionSP lhs_section_sp (GetSP()); |
| 253 | SectionSP rhs_section_sp (rhs.GetSP()); |
| 254 | return lhs_section_sp != rhs_section_sp; |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | bool |
| 258 | SBSection::GetDescription (SBStream &description) |
| 259 | { |
Greg Clayton | 96154be | 2011-11-13 06:57:31 +0000 | [diff] [blame] | 260 | Stream &strm = description.ref(); |
| 261 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 262 | SectionSP section_sp (GetSP()); |
| 263 | if (section_sp) |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 264 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 265 | const addr_t file_addr = section_sp->GetFileAddress(); |
| 266 | strm.Printf ("[0x%16.16llx-0x%16.16llx) ", file_addr, file_addr + section_sp->GetByteSize()); |
| 267 | section_sp->DumpName(&strm); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 268 | } |
| 269 | else |
| 270 | { |
Greg Clayton | 96154be | 2011-11-13 06:57:31 +0000 | [diff] [blame] | 271 | strm.PutCString ("No value"); |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return true; |
| 275 | } |
| 276 | |