[Lldb-commits] [lldb] 5014830 - ObjectFile: introduce a COFF object file plugin

2023-05-07 Thread Saleem Abdulrasool via lldb-commits

Author: Saleem Abdulrasool
Date: 2023-05-07T12:40:44-07:00
New Revision: 5014830ede78e55e2c638948ca00cea045eac9ce

URL: 
https://github.com/llvm/llvm-project/commit/5014830ede78e55e2c638948ca00cea045eac9ce
DIFF: 
https://github.com/llvm/llvm-project/commit/5014830ede78e55e2c638948ca00cea045eac9ce.diff

LOG: ObjectFile: introduce a COFF object file plugin

Windows uses COFF as an object file format and PE/COFF as an executable
file format. They are subtly different and certain elements of a COFF
file may not be present in an executable. Introduce a new plugin to add
support for the COFF object file format which is required to support
loading of modules built with -gmodules. This is motivated by Swift
which serialises debugging information into a PCM which is a COFF object
file.

Differential Revision: https://reviews.llvm.org/D149987
Reviewed By: bulbazord

Added: 
lldb/source/Plugins/ObjectFile/COFF/CMakeLists.txt
lldb/source/Plugins/ObjectFile/COFF/ObjectFileCOFF.cpp
lldb/source/Plugins/ObjectFile/COFF/ObjectFileCOFF.h
lldb/test/Shell/ObjectFile/COFF/basic.yaml

Modified: 
lldb/source/Plugins/ObjectFile/CMakeLists.txt

Removed: 




diff  --git a/lldb/source/Plugins/ObjectFile/CMakeLists.txt 
b/lldb/source/Plugins/ObjectFile/CMakeLists.txt
index 851b680dc3c35..773241c8944c8 100644
--- a/lldb/source/Plugins/ObjectFile/CMakeLists.txt
+++ b/lldb/source/Plugins/ObjectFile/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_subdirectory(Breakpad)
+add_subdirectory(COFF)
 add_subdirectory(ELF)
 add_subdirectory(JSON)
 add_subdirectory(Mach-O)

diff  --git a/lldb/source/Plugins/ObjectFile/COFF/CMakeLists.txt 
b/lldb/source/Plugins/ObjectFile/COFF/CMakeLists.txt
new file mode 100644
index 0..e65b7ea501218
--- /dev/null
+++ b/lldb/source/Plugins/ObjectFile/COFF/CMakeLists.txt
@@ -0,0 +1,13 @@
+add_lldb_library(lldbPluginObjectFileCOFF PLUGIN
+  ObjectFileCOFF.cpp
+
+  LINK_LIBS
+lldbCore
+lldbHost
+lldbSymbol
+lldbTarget
+
+  LINK_COMPONENTS
+BinaryFormat
+Object
+Support)

diff  --git a/lldb/source/Plugins/ObjectFile/COFF/ObjectFileCOFF.cpp 
b/lldb/source/Plugins/ObjectFile/COFF/ObjectFileCOFF.cpp
new file mode 100644
index 0..03c454bf3efab
--- /dev/null
+++ b/lldb/source/Plugins/ObjectFile/COFF/ObjectFileCOFF.cpp
@@ -0,0 +1,311 @@
+//===-- ObjectFileCOFF.cpp 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ObjectFileCOFF.h"
+
+#include "lldb/Core/Module.h"
+#include "lldb/Core/ModuleSpec.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Utility/LLDBLog.h"
+
+#include "llvm/Support/Error.h"
+#include "llvm/Support/FormatAdapters.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+using namespace llvm;
+using namespace llvm::object;
+
+static bool IsCOFFObjectFile(const DataBufferSP &data) {
+  return identify_magic(toStringRef(data->GetData())) ==
+ file_magic::coff_object;
+}
+
+LLDB_PLUGIN_DEFINE(ObjectFileCOFF)
+
+char ObjectFileCOFF::ID;
+
+ObjectFileCOFF::~ObjectFileCOFF() = default;
+
+void ObjectFileCOFF::Initialize() {
+  PluginManager::RegisterPlugin(GetPluginNameStatic(),
+GetPluginDescriptionStatic(), CreateInstance,
+CreateMemoryInstance, GetModuleSpecifications);
+}
+
+void ObjectFileCOFF::Terminate() {
+  PluginManager::UnregisterPlugin(CreateInstance);
+}
+
+lldb_private::ObjectFile *
+ObjectFileCOFF::CreateInstance(const ModuleSP &module_sp, DataBufferSP data_sp,
+   offset_t data_offset, const FileSpec *file,
+   offset_t file_offset, offset_t length) {
+  Log *log = GetLog(LLDBLog::Object);
+
+  if (!data_sp) {
+data_sp = MapFileData(*file, length, file_offset);
+if (!data_sp) {
+  LLDB_LOG(log,
+   "Failed to create ObjectFileCOFF instance: cannot read file 
{0}",
+   file->GetPath());
+  return nullptr;
+}
+data_offset = 0;
+  }
+
+  assert(data_sp && "must have mapped file at this point");
+
+  if (!IsCOFFObjectFile(data_sp))
+return nullptr;
+
+  if (data_sp->GetByteSize() < length) {
+data_sp = MapFileData(*file, length, file_offset);
+if (!data_sp) {
+  LLDB_LOG(log,
+   "Failed to create ObjectFileCOFF instance: cannot read file 
{0}",
+   file->GetPath());
+  return nullptr;
+}
+data_offset = 0;
+  }
+
+
+  MemoryBufferRef buffer{toStringRef(data_sp->GetData()),
+ file->GetFilename().GetStringRef()};
+
+  Expected> binary = createBinary(buffer);
+  if (!binary) {
+LLDB_LOG_

[Lldb-commits] [PATCH] D149987: ObjectFile: introduce a COFF object file plugin

2023-05-07 Thread Saleem Abdulrasool via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG5014830ede78: ObjectFile: introduce a COFF object file 
plugin (authored by compnerd).

Changed prior to commit:
  https://reviews.llvm.org/D149987?vs=520102&id=520207#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D149987/new/

https://reviews.llvm.org/D149987

Files:
  lldb/source/Plugins/ObjectFile/CMakeLists.txt
  lldb/source/Plugins/ObjectFile/COFF/CMakeLists.txt
  lldb/source/Plugins/ObjectFile/COFF/ObjectFileCOFF.cpp
  lldb/source/Plugins/ObjectFile/COFF/ObjectFileCOFF.h
  lldb/test/Shell/ObjectFile/COFF/basic.yaml

Index: lldb/test/Shell/ObjectFile/COFF/basic.yaml
===
--- /dev/null
+++ lldb/test/Shell/ObjectFile/COFF/basic.yaml
@@ -0,0 +1,234 @@
+# RUN: yaml2obj %s -o %t
+# RUN: lldb-test object-file %t | FileCheck %s
+
+# CHECK: Plugin name: COFF
+# CHECK: Architecture: x86_64-unknown-windows-msvc
+
+# CHECK: Executable: false
+# CHECK: Stripped: false
+# CHECK: Type: object file
+# CHECK: Strata: user
+
+# CHECK: Name: .text
+# CHECK: Type: code
+
+# CHECK: Name: .data
+# CHECK: Type: data
+
+# CHECK: Name: .bss
+# CHECK: Type: zero-fill
+
+# CHECK: Name: .rdata
+# CHECK: Type: data
+
+# CHECK: Name: .debug_abbrev
+# CHECK: Type: dwarf-abbrev
+
+# CHECK: Name: .debug_info
+# CHECK: Type: dwarf-info
+
+# CHECK: Name: .debug_str
+# CHECK: Type: dwarf-str
+
+# CHECK: Name: .debug_line
+# CHECK: Type: dwarf-line
+
+--- !COFF
+header:
+  Machine: IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:.text
+Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+Alignment:   4
+SectionData: ''
+  - Name:.data
+Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+Alignment:   4
+SectionData: ''
+  - Name:.bss
+Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+Alignment:   4
+SectionData: ''
+SizeOfRawData:   0
+  - Name:.rdata
+Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+Alignment:   1
+SectionData: 31343A34313A303700
+  - Name:.debug_abbrev
+Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+Alignment:   1
+SectionData: 011101250E1305030E10171B0E023400030E49133F193A0B3B0B021803010149130421004913370B0526004913062400030E3E0B0B0B072400030E0B0B3E0B00
+  - Name:.debug_info
+Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+Alignment:   1
+SectionData: 4F00040008010C0032003400023F00330001010903033F00044B0009000544000649000601074E00080700
+Relocations:
+  - VirtualAddress:  6
+SymbolName:  .debug_abbrev
+Type:IMAGE_REL_AMD64_SECREL
+  - VirtualAddress:  12
+SymbolName:  .debug_str
+Type:IMAGE_REL_AMD64_SECREL
+  - VirtualAddress:  18
+SymbolName:  .debug_str
+Type:IMAGE_REL_AMD64_SECREL
+  - VirtualAddress:  22
+SymbolName:  .debug_line
+Type:IMAGE_REL_AMD64_SECREL
+  - VirtualAddress:  26
+SymbolName:  .debug_str
+Type:IMAGE_REL_AMD64_SECREL
+  - VirtualAddress:  31
+SymbolName:  .debug_str
+Type:IMAGE_REL_AMD64_SECREL
+  - VirtualAddress:  43
+SymbolName:  timestamp
+Type:IMAGE_REL_AMD64_ADDR64
+  - VirtualAddress:  69
+SymbolName:  .debug_str
+Type:IMAGE_REL_AMD64_SECREL
+  - VirtualAddress:  76
+SymbolName:  .debug_str
+Type:IMAGE_REL_AMD64_SECREL
+  - Name:.debug_str
+Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+Alignment:   1
+SectionData: 4170706C6520636C616E672076657273696F6E2031342E302E332028636C616E672D313430332E302E32322E31342E3129002D002F7661722F656D7074790074696D657374616D700063686172005F5F41525241595F53495A455F545950455F5F00
+  - Name:.debug_line
+Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+Alignment:   1
+SectionData: 250004001F00010101FB0E0D0001010101000101003C737464696E3E00
+  - Name:.llvm_addrsig
+Characteristics: [ IMAGE_SCN_LNK_REMOVE ]
+Alignment:   1
+SectionData: ''
+symbols:
+  - Name:.text
+Value:   0
+SectionNumber:   1
+SimpleType:  IM

[Lldb-commits] [PATCH] D150078: [lldb] Prevent mutation of CommandAlias::GetOptionArguments

2023-05-07 Thread Dave Lee via Phabricator via lldb-commits
kastiglione created this revision.
kastiglione added reviewers: jingham, JDevlieghere, aprantl.
Herald added a subscriber: jeroen.dobbelaere.
Herald added a project: All.
kastiglione requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Fix a mutation of `CommandAlias::m_option_args_sp`, which resulted in cases 
where
aliases would fail to run on second, and subsequent times.

For example, an alias such as:

  command alias p1 p 1

When run the second time, the following error would be reported to the user:

  error: expression failed to parse:
  error: :1:1: expression is not assignable
  --  1
  ^   ~

To fix this, `CommandAlias::Desugar` now constructs options to a freshly 
constructed
vector, rather than by appending to the results of `GetOptionArguments`.

rdar://107770836


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150078

Files:
  lldb/source/Interpreter/CommandAlias.cpp
  lldb/test/API/commands/command/nested_alias/TestNestedAlias.py


Index: lldb/test/API/commands/command/nested_alias/TestNestedAlias.py
===
--- lldb/test/API/commands/command/nested_alias/TestNestedAlias.py
+++ lldb/test/API/commands/command/nested_alias/TestNestedAlias.py
@@ -101,3 +101,7 @@
 self.expect('command alias two expr -- 2')
 self.expect('command alias add_two two +')
 self.expect('add_two 3', patterns=[' = 5$'])
+# Check that aliases to aliases to raw input commands work the second
+# and subsequent times.
+self.expect('add_two 3', patterns=[' = 5$'])
+self.expect('add_two 3', patterns=[' = 5$'])
Index: lldb/source/Interpreter/CommandAlias.cpp
===
--- lldb/source/Interpreter/CommandAlias.cpp
+++ lldb/source/Interpreter/CommandAlias.cpp
@@ -8,6 +8,7 @@
 
 #include "lldb/Interpreter/CommandAlias.h"
 
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/ErrorHandling.h"
 
 #include "lldb/Interpreter/CommandInterpreter.h"
@@ -211,9 +212,9 @@
 // FIXME: This doesn't work if the original alias fills a slot in the
 // underlying alias, since this just appends the two lists.
 auto desugared = ((CommandAlias *)underlying.get())->Desugar();
-auto options = GetOptionArguments();
-options->insert(options->begin(), desugared.second->begin(),
-desugared.second->end());
+OptionArgVectorSP options = std::make_shared();
+llvm::append_range(*options, *desugared.second);
+llvm::append_range(*options, *GetOptionArguments());
 return {desugared.first, options};
   }
 


Index: lldb/test/API/commands/command/nested_alias/TestNestedAlias.py
===
--- lldb/test/API/commands/command/nested_alias/TestNestedAlias.py
+++ lldb/test/API/commands/command/nested_alias/TestNestedAlias.py
@@ -101,3 +101,7 @@
 self.expect('command alias two expr -- 2')
 self.expect('command alias add_two two +')
 self.expect('add_two 3', patterns=[' = 5$'])
+# Check that aliases to aliases to raw input commands work the second
+# and subsequent times.
+self.expect('add_two 3', patterns=[' = 5$'])
+self.expect('add_two 3', patterns=[' = 5$'])
Index: lldb/source/Interpreter/CommandAlias.cpp
===
--- lldb/source/Interpreter/CommandAlias.cpp
+++ lldb/source/Interpreter/CommandAlias.cpp
@@ -8,6 +8,7 @@
 
 #include "lldb/Interpreter/CommandAlias.h"
 
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/ErrorHandling.h"
 
 #include "lldb/Interpreter/CommandInterpreter.h"
@@ -211,9 +212,9 @@
 // FIXME: This doesn't work if the original alias fills a slot in the
 // underlying alias, since this just appends the two lists.
 auto desugared = ((CommandAlias *)underlying.get())->Desugar();
-auto options = GetOptionArguments();
-options->insert(options->begin(), desugared.second->begin(),
-desugared.second->end());
+OptionArgVectorSP options = std::make_shared();
+llvm::append_range(*options, *desugared.second);
+llvm::append_range(*options, *GetOptionArguments());
 return {desugared.first, options};
   }
 
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits