+/* rtos_qsymbol() processes and replies to all qSymbol packets from GDB.
+ *
+ * GDB sends a qSymbol:: packet (empty address, empty name) to notify
+ * that it can now answer qSymbol::hexcodedname queries, to look up symbols.
+ *
+ * If the qSymbol packet has no address that means GDB did not find the
+ * symbol, in which case auto-detect will move on to try the next RTOS.
+ *
+ * rtos_qsymbol() then calls the next_symbol() helper function, which
+ * iterates over symbol names for the current RTOS until it finds the
+ * symbol in the received GDB packet, and then returns the next entry
+ * in the list of symbols.
+ *
+ * If GDB replied about the last symbol for the RTOS and the RTOS was
+ * specified explicitly, then no further symbol lookup is done. When
+ * auto-detecting, the RTOS driver _detect() function must return success.
+ *
+ * The symbol is tried twice to handle the -flto case with gcc. The first
+ * attempt uses the symbol as-is, and the second attempt tries the symbol
+ * with ".lto_priv.0" appended to it. We only consider the first static
+ * symbol here from the -flto case. (Each subsequent static symbol with
+ * the same name is exported as .lto_priv.1, .lto_priv.2, etc.)
+ *
+ * rtos_qsymbol() returns 1 if an RTOS has been detected, or 0 otherwise.
+ */
+int rtos_qsymbol(struct connection *connection, char const *packet, int packet_size)
+{
+ int rtos_detected = 0;
+ uint64_t addr = 0;
+ size_t reply_len;
+ char reply[GDB_BUFFER_SIZE + 1], cur_sym[GDB_BUFFER_SIZE / 2 + 1] = ""; /* Extra byte for null-termination */
+ struct symbol_table_elem *next_sym = NULL;
+ struct target *target = get_target_from_connection(connection);
+ struct rtos *os = target->rtos;
+
+ reply_len = sprintf(reply, "OK");
+
+ if (!os)
+ goto done;
+
+ /* Decode any symbol name in the packet*/
+ size_t len = unhexify((uint8_t *)cur_sym, strchr(packet + 8, ':') + 1, strlen(strchr(packet + 8, ':') + 1));
+ cur_sym[len] = 0;
+
+ const char no_suffix[] = "";
+ const char lto_suffix[] = ".lto_priv.0";
+ const size_t lto_suffix_len = strlen(lto_suffix);
+
+ const char *cur_suffix;
+ const char *next_suffix;
+
+ /* Detect what suffix was used during the previous symbol lookup attempt, and
+ * speculatively determine the next suffix (only used for the unknown address case) */
+ if (len > lto_suffix_len && !strcmp(cur_sym + len - lto_suffix_len, lto_suffix)) {
+ /* Trim the suffix from cur_sym for comparison purposes below */
+ cur_sym[len - lto_suffix_len] = '\0';
+ cur_suffix = lto_suffix;
+ next_suffix = NULL;
+ } else {
+ cur_suffix = no_suffix;
+ next_suffix = lto_suffix;
+ }