objfile_flags flags = OBJF_USERLOADED;
symfile_add_flags add_flags = 0;
char *name = NULL;
+ bool stop_processing_options = false;
+ int idx;
+ char *arg;
if (from_tty)
add_flags |= SYMFILE_VERBOSE;
gdb_argv built_argv (args);
- for (char *arg : built_argv)
+ for (arg = built_argv[0], idx = 0; arg != NULL; arg = built_argv[++idx])
{
- if (strcmp (arg, "-readnow") == 0)
- flags |= OBJF_READNOW;
- else if (*arg == '-')
- error (_("unknown option `%s'"), arg);
- else
+ if (stop_processing_options || *arg != '-')
{
- symbol_file_add_main_1 (arg, add_flags, flags);
- name = arg;
+ if (name == NULL)
+ name = arg;
+ else
+ error (_("Unrecognized argument \"%s\""), arg);
}
+ else if (strcmp (arg, "-readnow") == 0)
+ flags |= OBJF_READNOW;
+ else if (strcmp (arg, "--") == 0)
+ stop_processing_options = true;
+ else
+ error (_("Unrecognized argument \"%s\""), arg);
}
if (name == NULL)
error (_("no symbol file name was specified"));
+
+ symbol_file_add_main_1 (name, add_flags, flags);
}
}
char *arg;
int argcnt = 0;
int sec_num = 0;
- int expecting_sec_name = 0;
- int expecting_sec_addr = 0;
struct objfile *objf;
objfile_flags flags = OBJF_USERLOADED | OBJF_SHARED;
symfile_add_flags add_flags = 0;
};
struct section_addr_info *section_addrs;
- std::vector<sect_opt> sect_opts;
+ std::vector<sect_opt> sect_opts = { { ".text", NULL } };
+ bool stop_processing_options = false;
struct cleanup *my_cleanups = make_cleanup (null_cleanup, NULL);
dont_repeat ();
if (args == NULL)
error (_("add-symbol-file takes a file name and an address"));
+ bool seen_addr = false;
gdb_argv argv (args);
for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt])
{
- /* Process the argument. */
- if (argcnt == 0)
- {
- /* The first argument is the file name. */
- filename.reset (tilde_expand (arg));
- }
- else if (argcnt == 1)
- {
- /* The second argument is always the text address at which
- to load the program. */
- sect_opt sect = { ".text", arg };
- sect_opts.push_back (sect);
- }
- else
+ if (stop_processing_options || *arg != '-')
{
- /* It's an option (starting with '-') or it's an argument
- to an option. */
- if (expecting_sec_name)
+ if (filename == NULL)
{
- sect_opt sect = { arg, NULL };
- sect_opts.push_back (sect);
- expecting_sec_name = 0;
+ /* First non-option argument is always the filename. */
+ filename.reset (tilde_expand (arg));
}
- else if (expecting_sec_addr)
+ else if (!seen_addr)
{
- sect_opts.back ().value = arg;
- expecting_sec_addr = 0;
- }
- else if (strcmp (arg, "-readnow") == 0)
- flags |= OBJF_READNOW;
- else if (strcmp (arg, "-s") == 0)
- {
- expecting_sec_name = 1;
- expecting_sec_addr = 1;
+ /* The second non-option argument is always the text
+ address at which to load the program. */
+ sect_opts[0].value = arg;
+ seen_addr = true;
}
else
error (_("Unrecognized argument \"%s\""), arg);
}
+ else if (strcmp (arg, "-readnow") == 0)
+ flags |= OBJF_READNOW;
+ else if (strcmp (arg, "-s") == 0)
+ {
+ if (argv[argcnt + 1] == NULL)
+ error (_("Missing section name after \"-s\""));
+ else if (argv[argcnt + 2] == NULL)
+ error (_("Missing section address after \"-s\""));
+
+ sect_opt sect = { argv[argcnt + 1], argv[argcnt + 2] };
+
+ sect_opts.push_back (sect);
+ argcnt += 2;
+ }
+ else if (strcmp (arg, "--") == 0)
+ stop_processing_options = true;
+ else
+ error (_("Unrecognized argument \"%s\""), arg);
}
+ if (filename == NULL)
+ error (_("You must provide a filename to be loaded."));
+
/* This command takes at least two arguments. The first one is a
filename, and the second is the address where this file has been
loaded. Abort now if this address hasn't been provided by the
user. */
- if (sect_opts.empty ())
+ if (!seen_addr)
error (_("The address where %s has been loaded is missing"),
filename.get ());
- if (expecting_sec_name)
- error (_("Missing section name after \"-s\""));
- else if (expecting_sec_addr)
- error (_("Missing section address after \"-s\""));
-
/* Print the prompt for the query below. And save the arguments into
a sect_addr_info structure to be passed around to other
functions. We have to split this up into separate print
"add-symbol-file: unknown option $word"
}
+# Check that we can pass parameters using any position in the command
+# line.
+set test "add-symbol-file positionless -readnow"
+gdb_test_multiple "add-symbol-file -readnow $binfile 0x100 -s .bss 0x3" $test {
+ -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\.bss_addr = 0x3\r\n\\(y or n\\) " {
+ gdb_test "n" "Not confirmed\." $test
+ }
+}
+# When we use -s as the first argument, the section will be printed
+# first as well.
+set test "add-symbol-file positionless -s"
+gdb_test_multiple "add-symbol-file -s .bss 0x3 -readnow $binfile 0x100" $test {
+ -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\.bss_addr = 0x3\r\n\\(y or n\\) " {
+ gdb_test "n" "Not confirmed\." $test
+ }
+}
+set test "add-symbol-file positionless -s, no -readnow"
+gdb_test_multiple "add-symbol-file $binfile 0x100 -s .bss 0x3" $test {
+ -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\.bss_addr = 0x3\r\n\\(y or n\\) " {
+ gdb_test "n" "Not confirmed\." $test
+ }
+}
+# Check that passing "-s .text", no matter the position, always has
+# the same result.
+set test "add-symbol-file different -s .text, after file"
+gdb_test_multiple "add-symbol-file $binfile 0x100 -s .text 0x200" $test {
+ -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\.text_addr = 0x200\r\n\\(y or n\\) " {
+ gdb_test "n" "Not confirmed\." $test
+ }
+}
+set test "add-symbol-file different -s .text, before file"
+gdb_test_multiple "add-symbol-file -s .text 0x200 $binfile 0x100" $test {
+ -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\.text_addr = 0x200\r\n\\(y or n\\) " {
+ gdb_test "n" "Not confirmed\." $test
+ }
+}
+# Test that passing "--" disables option processing.
+gdb_test "add-symbol-file -- $binfile 0x100 -s .bss 0x3" \
+ "Unrecognized argument \"-s\"" \
+ "add-symbol-file with -- disables option processing"
+set test "add-symbol-file with -- disables option processing, non-existent filename"
+gdb_test_multiple "add-symbol-file -- -non-existent-file 0x100" $test {
+ -re "add symbol table from file \"-non-existent-file\" at\r\n\t\.text_addr = 0x100\r\n\\(y or n\\) " {
+ gdb_test "y" "-non-existent-file: No such file or directory\." $test
+ }
+}
+# Test that passing the wrong number of arguments to '-s' leads to an
+# error.
+gdb_test "add-symbol-file $binfile -s" \
+ "Missing section name after \"-s\"" \
+ "add-symbol-file with -s without section name"
+gdb_test "add-symbol-file $binfile -s .bss" \
+ "Missing section address after \"-s\"" \
+ "add-symbol-file with -s without section address"
+# Test that '-s' accepts section names with '-'
+set test "add-symbol-file with -s using section name starting with dash"
+gdb_test_multiple "add-symbol-file -s -section-name 0x200 $binfile 0x100" $test {
+ -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\-section-name_addr = 0x200\r\n\\(y or n\\) " {
+ gdb_test "n" "Not confirmed\." $test
+ }
+}
+# Since we're here, might as well test the 'symbol-file' command and
+# if its arguments can also be passed at any position.
+gdb_test "symbol-file -readnow $binfile" \
+ "Reading symbols from ${binfile}\.\.\.expanding to full symbols\.\.\.done\." \
+ "symbol-file with -readnow first"
+clean_restart
+gdb_test "symbol-file $binfile -readnow" \
+ "Reading symbols from ${binfile}\.\.\.expanding to full symbols\.\.\.done\." \
+ "symbol-file with -readnow second"
+gdb_test "symbol-file -readnow" \
+ "no symbol file name was specified" \
+ "symbol-file without filename"
+gdb_test "symbol-file -- -non-existent-file" \
+ "-non-existent-file: No such file or directory\." \
+ "symbol-file with -- disables option processing"
+clean_restart
+gdb_test "symbol-file -readnow -- $binfile" \
+ "Reading symbols from ${binfile}\.\.\.expanding to full symbols\.\.\.done\." \
+ "symbol-file with -- and -readnow"
+gdb_test "symbol-file -- $binfile -readnow" \
+ "Unrecognized argument \"-readnow\"" \
+ "symbol-file with -- and -readnow, invalid option"
+
+clean_restart
+
gdb_test "add-symbol-file ${binfile} 0 -s" \
"Missing section name after .-s." \
"add-symbol-file bare -s"